Debian Codegraph @

src/lib/libdwarf/dwarf_die_deliv.c

raw
1 /*
2 Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2022 David Anderson. All Rights Reserved.
4 Portions Copyright 2012 SN Systems Ltd. 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 #include <stdio.h> /* debugging */
33
34 #include <string.h> /* memcmp() memcpy() memset() strcmp() strlen() */
35 #include <stdlib.h> /* calloc() free() */
36
37 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
38 #include "stdafx.h"
39 #endif /* HAVE_STDAFX_H */
40
41 #ifdef HAVE_STDINT_H
42 #include <stdint.h> /* uintptr_t */
43 #endif /* HAVE_STDINT_H */
44
45 #include "dwarf.h"
46 #include "libdwarf.h"
47 #include "libdwarf_private.h"
48 #include "dwarf_base_types.h"
49 #include "dwarf_safe_strcpy.h"
50 #include "dwarf_opaque.h"
51 #include "dwarf_alloc.h"
52 #include "dwarf_error.h"
53 #include "dwarf_util.h"
54 #include "dwarf_str_offsets.h"
55 #include "dwarf_string.h"
56 #include "dwarf_die_deliv.h"
57
58 /* These are sanity checks, not 'rules'. */
59 #define MINIMUM_ADDRESS_SIZE 2
60 #define MAXIMUM_ADDRESS_SIZE 8
61
62 #if 0 /* dump rnglists context */
63 #include "dwarf_rnglists.h" /* for debugging declaration */
64 static void
65 dumprnglists_context(Dwarf_Rnglists_Context *rnglists,
66 Dwarf_Unsigned count)
67 {
68 Dwarf_Rnglists_Context prc = 0;
69 Dwarf_Unsigned i = 0;
70 printf("Rnglists_Context count: 0x%lu\n",(unsigned long)count);
71
72 for ( ; i < count; ++i) {
73 prc = rnglists[i];
74 printf("[%3lu] Rnglists_Context rc_index : %lu\n",
75 (unsigned long)i,
76 (unsigned long)prc->rc_index);
77 printf(" hed offset : %lu\n",
78 (unsigned long)prc->rc_header_offset);
79 printf(" rc_length : %lu\n",
80 (unsigned long)prc->rc_length);
81 printf(" Version: %u offset size: "
82 "%u address_size: %u \n",
83 prc->rc_version,prc->rc_offset_size,
84 prc->rc_address_size);
85 printf(" offset_entry count : %lu\n",
86 (unsigned long)prc->rc_offset_entry_count);
87 printf(" offsets offset_ : %lu\n",
88 (unsigned long)prc->rc_offsets_off_in_sect);
89 }
90 }
91 static void
92 dumpcu_context_list( Dwarf_CU_Context first)
93 {
94 Dwarf_CU_Context ctx = 0;
95 unsigned cc = 0;
96
97 for ( ctx = first; ctx ; ctx = ctx->cc_next, ++cc) {
98 printf("[%3u] CU_Context rnglists_base "
99 " : 0x%lx\n",cc,
100 (unsigned long)ctx->cc_rnglists_base);
101 printf(" rnglists_base_contr_size: 0x%lx\n",
102 (unsigned long)ctx->cc_rnglists_base_contr_size);
103 printf(" rnglists_base_present : 0x%lx\n",
104 (unsigned long)ctx->cc_rnglists_base_present);
105 printf(" rnglists_header_length_present: %lu\n",
106 (unsigned long)ctx->cc_rnglists_header_length_present);
107 printf(" ranges_base : 0x%lx\n",
108 (unsigned long)ctx->cc_ranges_base);
109 printf(" ranges_base present : 0x%lx\n",
110 (unsigned long)ctx->cc_ranges_base_present);
111 }
112 }
113 static void
114 dump_rnglists_data(const char *msg,
115 Dwarf_Debug dbg,Dwarf_CU_Context cucon)
116 {
117 printf("Debugging dump_rnglists_data %s\n",msg);
118 if (!dbg->de_debug_rnglists.dss_data) {
119 printf("No de_rnglists section\n");
120 }
121 if (!dbg->de_rnglists_context_count) {
122 printf("No de_rnglists contexts\n");
123 } else {
124 dumprnglists_context(dbg->de_rnglists_context,
125 dbg->de_rnglists_context_count);
126 }
127 if (dbg->de_info_reading.de_cu_context_list) {
128 if (cucon) {
129 dumpcu_context_list(cucon);
130 } else {
131 dumpcu_context_list(
132 dbg->de_info_reading.de_cu_context_list);
133 }
134 } else {
135 printf("No debug_info cu contexts\n");
136 }
137 if (dbg->de_types_reading.de_cu_context_list) {
138 if (cucon) {
139 dumpcu_context_list(cucon);
140 } else {
141 dumpcu_context_list(
142 dbg->de_types_reading.de_cu_context_list);
143 }
144 } else {
145 printf("No debug_types cu contexts\n");
146 }
147 fflush(stdout);
148
149 }
150 #endif
151
152 static void assign_correct_unit_type(Dwarf_CU_Context cu_context);
153 static int find_cu_die_base_fields(Dwarf_Debug dbg,
154 Dwarf_CU_Context cucon,
155 Dwarf_Die cudie,
156 Dwarf_Bool *bad_pc_form,
157 Dwarf_Error *error);
158
159 static int _dwarf_siblingof_internal(Dwarf_Debug dbg,
160 Dwarf_Die die,
161 Dwarf_CU_Context context,
162 Dwarf_Bool is_info,
163 Dwarf_Die * caller_ret_die, Dwarf_Error * error);
164
165 /* see cuandunit.txt for an overview of the
166 DWARF5 split dwarf sections and values
167 and the DWARF4 GNU cc version of a draft
168 version of DWARF5 (quite different from
169 the final DWARF5).
170 */
171
172 static struct Dwarf_Sig8_s dwarfsig8zero;
173
174 #if 0 /* dump_bytes */
175 static void
176 dump_bytes(char * msg,Dwarf_Small * start, long len)
177 {
178 Dwarf_Small *end = start + len;
179 Dwarf_Small *cur = start;
180
181 printf("%s ",msg);
182 for (; cur < end; cur++) {
183 printf("%02x ", *cur);
184 }
185 printf("\n");
186 }
187 #endif /*0*/
188
189 /* New October 2011. Enables client code to know if
190 it is a debug_info or debug_types context. */
191 Dwarf_Bool
192 dwarf_get_die_infotypes_flag(Dwarf_Die die)
193 {
194 return die->di_is_info;
195 }
196
197 /*
198 For a given Dwarf_Debug dbg, this function checks
199 if a CU that includes the given offset has been read
200 or not. If yes, it returns the Dwarf_CU_Context
201 for the CU. Otherwise it returns NULL. Being an
202 internal routine, it is assumed that a valid dbg
203 is passed.
204
205 **This is a sequential search. May be too slow.
206
207 If debug_info and debug_abbrev not loaded, this will
208 wind up returning NULL. So no need to load before calling
209 this.
210 */
211 static Dwarf_CU_Context
212 _dwarf_find_CU_Context(Dwarf_Debug dbg,
213 Dwarf_Off offset,
214 Dwarf_Bool is_info)
215 {
216 Dwarf_CU_Context cu_context = 0;
217 Dwarf_Debug_InfoTypes dis = is_info? &dbg->de_info_reading:
218 &dbg->de_types_reading;
219
220 if (offset >= dis->de_last_offset){
221 return NULL;
222 }
223 if (dis->de_cu_context != NULL &&
224 dis->de_cu_context->cc_next != NULL &&
225 dis->de_cu_context->cc_next->cc_debug_offset == offset) {
226 return dis->de_cu_context->cc_next;
227 }
228 if (dis->de_cu_context != NULL &&
229 dis->de_cu_context->cc_debug_offset <= offset) {
230 for (cu_context = dis->de_cu_context;
231 cu_context != NULL;
232 cu_context = cu_context->cc_next) {
233 if (offset >= cu_context->cc_debug_offset &&
234 offset < cu_context->cc_debug_offset +
235 cu_context->cc_length + cu_context->cc_length_size
236 + cu_context->cc_extension_size) {
237 return cu_context;
238 }
239 }
240 }
241 for (cu_context = dis->de_cu_context_list;
242 cu_context != NULL;
243 cu_context = cu_context->cc_next) {
244 if (offset >= cu_context->cc_debug_offset &&
245 offset < cu_context->cc_debug_offset +
246 cu_context->cc_length + cu_context->cc_length_size
247 + cu_context->cc_extension_size) {
248 return cu_context;
249 }
250 }
251 return NULL;
252 }
253
254 int
255 dwarf_get_debugfission_for_die(Dwarf_Die die,
256 struct Dwarf_Debug_Fission_Per_CU_s *fission_out,
257 Dwarf_Error *error)
258 {
259 Dwarf_CU_Context context = 0;
260 Dwarf_Debug dbg = 0;
261 struct Dwarf_Debug_Fission_Per_CU_s * percu = 0;
262
263 CHECK_DIE(die, DW_DLV_ERROR);
264 context = die->di_cu_context;
265 dbg = context->cc_dbg;
266 if (!_dwarf_file_has_debug_fission_index(dbg)) {
267 return DW_DLV_NO_ENTRY;
268 }
269
270 /* Logic should work for DW4 and DW5. */
271 if (context->cc_unit_type == DW_UT_type||
272 context->cc_unit_type == DW_UT_split_type ) {
273 if (!_dwarf_file_has_debug_fission_tu_index(dbg)) {
274 return DW_DLV_NO_ENTRY;
275 }
276 } else if (context->cc_unit_type == DW_UT_split_compile) {
277 if (!_dwarf_file_has_debug_fission_cu_index(dbg)) {
278 return DW_DLV_NO_ENTRY;
279 }
280 } else { /* Fall through*/ }
281 percu = &context->cc_dwp_offsets;
282 if (!percu->pcu_type) {
283 return DW_DLV_NO_ENTRY;
284 }
285 *fission_out = *percu;
286 return DW_DLV_OK;
287 }
288
289 static Dwarf_Bool
290 is_unknown_UT_value(int ut)
291 {
292 switch(ut) {
293 case DW_UT_compile:
294 case DW_UT_type:
295 case DW_UT_partial:
296 return FALSE;
297 case DW_UT_skeleton:
298 case DW_UT_split_compile:
299 case DW_UT_split_type:
300 return FALSE;
301 default:
302 break;
303 }
304 return TRUE;
305 }
306
307 /* ASSERT: whichone is a DW_SECT* macro value. */
308 Dwarf_Unsigned
309 _dwarf_get_dwp_extra_offset(struct Dwarf_Debug_Fission_Per_CU_s* dwp,
310 unsigned whichone, Dwarf_Unsigned * size)
311 {
312 Dwarf_Unsigned sectoff = 0;
313 if (!dwp->pcu_type) {
314 return 0;
315 }
316 sectoff = dwp->pcu_offset[whichone];
317 *size = dwp->pcu_size[whichone];
318 return sectoff;
319 }
320
321 /* _dwarf_get_fission_addition_die returns DW_DLV_OK etc.
322 */
323 int
324 _dwarf_get_fission_addition_die(Dwarf_Die die, int dw_sect_index,
325 Dwarf_Unsigned *offset,
326 Dwarf_Unsigned *size,
327 Dwarf_Error *error)
328 {
329 /* We do not yet know the DIE hash, so we cannot use it
330 to identify the offset. */
331 Dwarf_CU_Context context = 0;
332 Dwarf_Unsigned dwpadd = 0;
333 Dwarf_Unsigned dwpsize = 0;
334
335 CHECK_DIE(die, DW_DLV_ERROR);
336 context = die->di_cu_context;
337 dwpadd = _dwarf_get_dwp_extra_offset(
338 &context->cc_dwp_offsets,
339 dw_sect_index,&dwpsize);
340 *offset = dwpadd;
341 *size = dwpsize;
342 return DW_DLV_OK;
343 }
344
345 /* Not sure if this is the only way to be sure early on in
346 reading a compile unit. */
347 static int
348 section_name_ends_with_dwo(const char *name)
349 {
350 size_t lenstr = 0;
351 size_t dotpos = 0;
352 if (!name) {
353 return FALSE;
354 }
355 lenstr = strlen(name);
356 if (lenstr < 5) {
357 return FALSE;
358 }
359 dotpos = lenstr - 4;
360 if (strcmp(name+dotpos,".dwo")) {
361 return FALSE;
362 }
363 return TRUE;
364 }
365
366 void
367 _dwarf_create_address_size_dwarf_error(Dwarf_Debug dbg,
368 Dwarf_Error *error,
369 Dwarf_Unsigned addrsize,
370 int errcode,const char *errname)
371 {
372 dwarfstring m;
373 const char *bites = "bytes";
374 if (addrsize == 1) {
375 bites = "byte";
376 }
377
378 dwarfstring_constructor(&m);
379 dwarfstring_append(&m,(char *)errname);
380 dwarfstring_append_printf_u(&m,
381 ": Address size of %u ",
382 addrsize);
383 dwarfstring_append_printf_s(&m,
384 "%s is not supported. Corrupt DWARF.",
385 (char *)bites);
386 _dwarf_error_string(dbg,error,errcode,
387 dwarfstring_string(&m));
388 dwarfstring_destructor(&m);
389 }
390
391 /* New January 2017 */
392 static int
393 _dwarf_read_cu_version_and_abbrev_offset(Dwarf_Debug dbg,
394 Dwarf_Small *data,
395 Dwarf_Bool is_info,
396 unsigned offset_size, /* 4 or 8 */
397 Dwarf_CU_Context cu_context,
398 /* end_data used for sanity checking */
399 Dwarf_Small * end_data,
400 Dwarf_Unsigned * bytes_read_out,
401 Dwarf_Error * error)
402 {
403 Dwarf_Small * data_start = data;
404 Dwarf_Small * dataptr = data;
405 Dwarf_Ubyte unit_type = 0;
406 Dwarf_Ubyte addrsize = 0;
407 Dwarf_Unsigned abbrev_offset = 0;
408 Dwarf_Half version = 0;
409
410 READ_UNALIGNED_CK(dbg, version, Dwarf_Half,
411 dataptr,DWARF_HALF_SIZE,error,end_data);
412 dataptr += DWARF_HALF_SIZE;
413 if (version == DW_CU_VERSION5) {
414 Dwarf_Ubyte unit_typeb = 0;
415 Dwarf_Unsigned herelen = sizeof(unit_typeb) +
416 sizeof(addrsize) + offset_size;
417
418 if ((dataptr+herelen) > end_data) {
419 _dwarf_error_string(dbg, error,
420 DW_DLE_CU_UT_TYPE_ERROR,
421 "DW_DLE_UT_TYPE_ERROR: "
422 " Reading the unit type, address size, "
423 "and abbrev_offset of the DWARF5 header"
424 " will run off the end of the section. "
425 "Corrupt DWARF");
426 }
427 READ_UNALIGNED_CK(dbg, unit_typeb, Dwarf_Ubyte,
428 dataptr, sizeof(unit_typeb),error,end_data);
429 dataptr += sizeof(unit_typeb);
430
431 unit_type = unit_typeb;
432 /* We do not need is_info flag in DWARF5 */
433 if (is_unknown_UT_value(unit_type)) {
434 /* DWARF5 object file is corrupt. Invalid value */
435 dwarfstring m;
436 dwarfstring_constructor(&m);
437 dwarfstring_append_printf_u(&m,
438 "DW_DLE_CU_UT_TYPE_ERROR: we do not know "
439 " the CU header unit_type 0x%x",unit_type);
440 dwarfstring_append_printf_u(&m," (%u) so cannot"
441 "process this compilation_unit. A valid type ",
442 unit_type);
443 dwarfstring_append(&m,"would be DW_UT_compile"
444 ", for example");
445 _dwarf_error_string(dbg, error,
446 DW_DLE_CU_UT_TYPE_ERROR,
447 dwarfstring_string(&m));
448 dwarfstring_destructor(&m);
449 return DW_DLV_ERROR;
450 }
451 READ_UNALIGNED_CK(dbg, addrsize, unsigned char,
452 dataptr, sizeof(addrsize),error,end_data);
453 dataptr += sizeof(char);
454
455 READ_UNALIGNED_CK(dbg, abbrev_offset, Dwarf_Unsigned,
456 dataptr, offset_size,error,end_data);
457 dataptr += offset_size;
458
459 } else if (version == DW_CU_VERSION2 ||
460 version == DW_CU_VERSION3 ||
461 version == DW_CU_VERSION4) {
462 Dwarf_Unsigned herelen = sizeof(addrsize) + offset_size;
463
464 if ((dataptr+herelen) > end_data) {
465 _dwarf_error_string(dbg, error,
466 DW_DLE_CU_UT_TYPE_ERROR,
467 "DW_DLE_UT_TYPE_ERROR: "
468 " Reading the address size, "
469 "and abbrev_offset of the DWARF header"
470 " will run off the end of the section. "
471 "Corrupt DWARF");
472 }
473 /* DWARF2,3,4 */
474 READ_UNALIGNED_CK(dbg, abbrev_offset, Dwarf_Unsigned,
475 dataptr, offset_size,error,end_data);
476 dataptr += offset_size;
477
478 READ_UNALIGNED_CK(dbg, addrsize, Dwarf_Ubyte,
479 dataptr, sizeof(addrsize),error,end_data);
480 dataptr += sizeof(addrsize);
481
482 /* This is an initial approximation of unit_type.
483 For DW4 we will refine this after we
484 have built the CU header (by reading
485 CU_die)
486 */
487 unit_type = is_info?DW_UT_compile:DW_UT_type;
488 } else {
489 _dwarf_error(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
490 return DW_DLV_ERROR;
491 }
492 cu_context->cc_version_stamp = version;
493 cu_context->cc_unit_type = unit_type;
494 cu_context->cc_address_size = addrsize;
495 cu_context->cc_abbrev_offset = abbrev_offset;
496 if (!addrsize) {
497 _dwarf_error(dbg,error,DW_DLE_ADDRESS_SIZE_ZERO);
498 return DW_DLV_ERROR;
499 }
500 if (addrsize < MINIMUM_ADDRESS_SIZE ||
501 addrsize > MAXIMUM_ADDRESS_SIZE ) {
502 _dwarf_create_address_size_dwarf_error(dbg,error,addrsize,
503 DW_DLE_ADDRESS_SIZE_ERROR,
504 "DW_DLE_ADDRESS_SIZE_ERROR::");
505 return DW_DLV_ERROR;
506 }
507 if (addrsize > sizeof(Dwarf_Addr)) {
508 _dwarf_create_address_size_dwarf_error(dbg,error,addrsize,
509 DW_DLE_ADDRESS_SIZE_ERROR,
510 "DW_DLE_ADDRESS_SIZE_ERROR: not representable"
511 " in Dwarf_Addr field.");
512 return DW_DLV_ERROR;
513 }
514
515 /* We are ignoring this. Can get it from DWARF5. */
516 cu_context->cc_segment_selector_size = 0;
517 *bytes_read_out = (dataptr - data_start);
518 return DW_DLV_OK;
519 }
520
521 /* .debug_info[.dwo] .debug_types[.dwo]
522 the latter only DWARF4. */
523 static int
524 read_info_area_length_and_check(Dwarf_Debug dbg,
525 Dwarf_CU_Context cu_context,
526 Dwarf_Unsigned offset,
527 Dwarf_Byte_Ptr *cu_ptr_io,
528 Dwarf_Unsigned section_size,
529 Dwarf_Byte_Ptr section_end_ptr,
530 Dwarf_Unsigned *max_cu_global_offset_out,
531 Dwarf_Error *error)
532 {
533 Dwarf_Byte_Ptr cu_ptr = 0;
534 /* The following two will be either 0,4, or 8. */
535 Dwarf_Unsigned local_length_size = 0;
536 Dwarf_Unsigned local_extension_size = 0;
537
538 Dwarf_Unsigned max_cu_global_offset = 0;
539 Dwarf_Unsigned length = 0;
540
541 cu_ptr = *cu_ptr_io;
542 /* READ_AREA_LENGTH updates cu_ptr for consumed bytes */
543 READ_AREA_LENGTH_CK(dbg, length, Dwarf_Unsigned,
544 cu_ptr, local_length_size, local_extension_size,
545 error,section_size,section_end_ptr);
546 if (!length) {
547 return DW_DLV_NO_ENTRY;
548 }
549
550 /* ASSERT: The following is either 4 or 8. */
551 cu_context->cc_length_size = (Dwarf_Small)local_length_size;
552 /* ASSERT: The following is either 0 or 4. */
553 cu_context->cc_extension_size = (Dwarf_Small)local_extension_size;
554 cu_context->cc_length = length;
555
556 /* This is a bare minimum, not the real max offset.
557 A preliminary sanity check. */
558 max_cu_global_offset = offset + length +
559 local_extension_size + local_length_size;
560 if (length > section_size ||
561 (length+local_length_size + local_extension_size)>
562 section_size) {
563 _dwarf_error(dbg, error, DW_DLE_CU_LENGTH_ERROR);
564 return DW_DLV_ERROR;
565 }
566 if (max_cu_global_offset > section_size) {
567 _dwarf_error(dbg, error, DW_DLE_CU_LENGTH_ERROR);
568 return DW_DLV_ERROR;
569 }
570 *cu_ptr_io = cu_ptr;
571 *max_cu_global_offset_out = max_cu_global_offset;
572 return DW_DLV_OK;
573 }
574
575 /* In DWARF4 GNU dwp there is a problem.
576 We cannot read the CU die and it's
577 DW_AT_GNU_dwo_id until we know the
578 section offsets from the index files.
579 Hence we do not know how to search the
580 index files by key. So search by offset.
581
582 There is no such problem in DWARF5.
583
584 We have not yet corrected the unit_type so, for DWARF4,
585 we check for simpler unit types.
586 */
587
588 static int
589 fill_in_dwp_offsets_if_present(Dwarf_Debug dbg,
590 Dwarf_CU_Context cu_context,
591 Dwarf_Sig8 * signaturedata,
592 Dwarf_Off offset,
593 Dwarf_Error *error)
594 {
595 Dwarf_Half unit_type = cu_context->cc_unit_type;
596 const char * typename = 0;
597 Dwarf_Half ver = cu_context->cc_version_stamp;
598
599 if (unit_type == DW_UT_split_type ||
600 (ver == DW_CU_VERSION4 && unit_type == DW_UT_type)){
601 typename = "tu";
602 if (!_dwarf_file_has_debug_fission_tu_index(dbg) ){
603 /* nothing to do. */
604 return DW_DLV_OK;
605 }
606 } else if (unit_type == DW_UT_split_compile ||
607 (ver == DW_CU_VERSION4 &&
608 unit_type == DW_UT_compile)){
609 typename = "cu";
610 if (!_dwarf_file_has_debug_fission_cu_index(dbg) ){
611 /* nothing to do. */
612 return DW_DLV_OK;
613 }
614 } else {
615 /* nothing to do. */
616 return DW_DLV_OK;
617 }
618
619 if (cu_context->cc_signature_present) {
620 int resdf = 0;
621
622 resdf = dwarf_get_debugfission_for_key(dbg,
623 signaturedata,
624 typename,
625 &cu_context->cc_dwp_offsets,
626 error);
627 if (resdf == DW_DLV_ERROR) {
628 return resdf;
629 }
630 if (resdf == DW_DLV_NO_ENTRY) {
631 _dwarf_error_string(dbg, error,
632 DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH,
633 "DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH: "
634 " dwarf_get_debugfission_for_key returned"
635 " DW_DLV_NO_ENTRY, something is wrong");
636 return DW_DLV_ERROR;
637 }
638 } else {
639 int resdf = 0;
640
641 resdf = _dwarf_get_debugfission_for_offset(dbg,
642 offset,
643 typename,
644 &cu_context->cc_dwp_offsets,
645 error);
646 if (resdf == DW_DLV_ERROR) {
647 return resdf;
648 }
649 if (resdf == DW_DLV_NO_ENTRY) {
650 _dwarf_error_string(dbg, error,
651 DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH,
652 "DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH: "
653 " dwarf_get_debugfission_for_offset returned"
654 " DW_DLV_NO_ENTRY, something is wrong");
655 return DW_DLV_ERROR;
656 }
657 cu_context->cc_signature =
658 cu_context->cc_dwp_offsets.pcu_hash;
659 cu_context->cc_signature_present = TRUE;
660 }
661 return DW_DLV_OK;
662 }
663
664 /* If returning DW_DLV_OK this will
665 push the cudie pointer back up through
666 local_cudie_return if local_cudie_return
667 is non-null. */
668 static int
669 finish_cu_context_via_cudie_inner(
670 Dwarf_Debug dbg,
671 Dwarf_CU_Context cu_context,
672 Dwarf_Die *local_cudie_return,
673 Dwarf_Error *error)
674 {
675 /* DW4: Look for DW_AT_dwo_id and
676 DW_AT_low_pc and more.
677 if there is one pick up the hash
678 DW5: hash in skeleton CU die
679 Also pick up cc_str_offset_base and
680 any other base values. */
681 Dwarf_Die cudie = 0;
682 int resdwo = 0;
683
684 /* Must call the internal siblingof so
685 we do not depend on the dbg...de_cu_context
686 used by and for dwarf_cu_header_* calls.
687 Safe because we know the correct cu_context. */
688 resdwo = _dwarf_siblingof_internal(dbg,NULL,
689 cu_context,
690 cu_context->cc_is_info,
691 &cudie, error);
692 if (resdwo == DW_DLV_OK) {
693 Dwarf_Half cutag = 0;
694 Dwarf_Bool bad_pc_form = FALSE;
695 int resdwob = 0;
696 resdwob = find_cu_die_base_fields(dbg,
697 cu_context,
698 cudie, &bad_pc_form,
699 error);
700 if (resdwob == DW_DLV_NO_ENTRY) {
701 /* The CU die has no children or has
702 some other issue like DW_FORM_ref_addr
703 on a low or high pc attribute. (Metrowerks) */
704 if (local_cudie_return) {
705 *local_cudie_return = cudie;
706 } else {
707 dwarf_dealloc_die(cudie);
708 }
709 if (!bad_pc_form) {
710 cu_context->cc_cu_die_has_children = FALSE;
711 }
712 return DW_DLV_OK;
713 }
714 if (resdwob == DW_DLV_ERROR) {
715 /* Not applicable or an error */
716 dwarf_dealloc_die(cudie);
717 cudie = 0;
718 return resdwob;
719 }
720 resdwob = dwarf_tag(cudie,&cutag,error);
721 if (resdwob == DW_DLV_OK) {
722 cu_context->cc_cu_die_tag = cutag;
723 }
724 if (local_cudie_return) {
725 *local_cudie_return = cudie;
726 } else {
727 dwarf_dealloc_die(cudie);
728 }
729 return resdwob;
730 }
731 if (resdwo == DW_DLV_NO_ENTRY) {
732 /* no cudie. Empty CU. */
733 return DW_DLV_OK;
734 }
735 /* no cudie. DW_DLV_ERROR.*/
736 return resdwo;
737 }
738
739 static void
740 local_dealloc_cu_context(Dwarf_Debug dbg,
741 Dwarf_CU_Context context)
742 {
743 Dwarf_Hash_Table hash_table = 0;
744
745 if (!context) {
746 return;
747 }
748 hash_table = context->cc_abbrev_hash_table;
749 if (hash_table) {
750 _dwarf_free_abbrev_hash_table_contents(hash_table,
751 FALSE);
752 hash_table->tb_entries = 0;
753 free(hash_table);
754 context->cc_abbrev_hash_table = 0;
755 }
756 dwarf_dealloc(dbg, context, DW_DLA_CU_CONTEXT);
757 }
758
759 static void
760 report_local_unit_type_error(Dwarf_Debug dbg,
761 int unit_type,
762 const char *msg,
763 Dwarf_Error *error)
764 {
765 dwarfstring m;
766
767 if (!error) {
768 return;
769 }
770 dwarfstring_constructor(&m);
771 dwarfstring_append_printf_s(&m,
772 "DW_DLE_CU_UT_TYPE_VALUE: %s ",(char *)msg);
773 dwarfstring_append_printf_u(&m,
774 "the compilation unit unit_type is 0x%x,"
775 " which is unknown to libdwarf. Corrupt DWARF.",
776 unit_type);
777 _dwarf_error_string(dbg,error,DW_DLE_CU_UT_TYPE_VALUE,
778 dwarfstring_string(&m));
779 dwarfstring_destructor(&m);
780 }
781
782 /* This function is used to create a CU Context for
783 a compilation-unit that begins at offset in
784 .debug_info. The CU Context is attached to the
785 list of CU Contexts for this dbg. It is assumed
786 that the CU at offset has not been read before,
787 and so do not call this routine before making
788 sure of this with _dwarf_find_CU_Context().
789 Returns NULL on error. As always, being an
790 internal routine, assumes a good dbg.
791
792 The offset argument is global offset, the offset
793 in the section, irrespective of CUs.
794 The offset has the DWP Package File offset built in
795 as it comes from the actual section.
796
797 max_cu_local_offset is a local offset in this CU.
798 So zero of this field is immediately following the length
799 field of the CU header. so max_cu_local_offset is
800 identical to the CU length field.
801 max_cu_global_offset is the offset one-past the end
802 of this entire CU. */
803 static int
804 _dwarf_make_CU_Context(Dwarf_Debug dbg,
805 Dwarf_Off offset,Dwarf_Bool is_info,
806 Dwarf_CU_Context * context_out,Dwarf_Error * error)
807 {
808 Dwarf_CU_Context cu_context = 0;
809 Dwarf_Unsigned length = 0;
810 Dwarf_Unsigned typeoffset = 0;
811 Dwarf_Sig8 signaturedata;
812 Dwarf_Unsigned types_extra_len = 0;
813 Dwarf_Unsigned max_cu_local_offset = 0;
814 Dwarf_Unsigned max_cu_global_offset = 0;
815 Dwarf_Byte_Ptr cu_ptr = 0;
816 Dwarf_Byte_Ptr section_end_ptr = 0;
817 int local_length_size = 0;
818 Dwarf_Unsigned bytes_read = 0;
819 const char * secname = 0;
820 Dwarf_Debug_InfoTypes dis = 0;
821 struct Dwarf_Section_s * secdp = 0;
822 Dwarf_Unsigned section_size = 0;
823 Dwarf_Half unit_type = 0;
824 Dwarf_Unsigned version = 0;
825 Dwarf_Small * dataptr = 0;
826 int res = 0;
827 if (is_info) {
828 secname = dbg->de_debug_info.dss_name;
829 dis = &dbg->de_info_reading;
830 secdp = &dbg->de_debug_info;
831 } else {
832 secname = dbg->de_debug_types.dss_name;
833 dis = &dbg->de_types_reading;
834 secdp = &dbg->de_debug_types;
835 }
836 section_size = secdp->dss_size;
837
838 signaturedata = dwarfsig8zero;
839 cu_context =
840 (Dwarf_CU_Context)_dwarf_get_alloc(dbg, DW_DLA_CU_CONTEXT, 1);
841 if (!cu_context) {
842 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
843 return DW_DLV_ERROR;
844 }
845 cu_context->cc_dbg = dbg;
846 cu_context->cc_is_info = is_info;
847
848 dataptr = is_info? dbg->de_debug_info.dss_data:
849 dbg->de_debug_types.dss_data;
850 /* Preliminary sanity checking. */
851 if (!dataptr) {
852 local_dealloc_cu_context(dbg,cu_context);
853 _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
854 return DW_DLV_ERROR;
855 }
856 if (offset >= section_size) {
857 local_dealloc_cu_context(dbg,cu_context);
858 _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
859 return DW_DLV_ERROR;
860 }
861 if ((offset+4) > section_size) {
862 local_dealloc_cu_context(dbg,cu_context);
863 _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
864 return DW_DLV_ERROR;
865 }
866 section_end_ptr = dataptr+section_size;
867 cu_ptr = (Dwarf_Byte_Ptr) (dataptr+offset);
868
869 if (section_name_ends_with_dwo(secname)) {
870 cu_context->cc_is_dwo = TRUE;
871 }
872 res = read_info_area_length_and_check(dbg,
873 cu_context,
874 offset,
875 &cu_ptr,
876 section_size,
877 section_end_ptr,
878 &max_cu_global_offset,
879 error);
880 if (res != DW_DLV_OK) {
881 local_dealloc_cu_context(dbg,cu_context);
882 return res;
883 }
884 local_length_size = cu_context->cc_length_size;
885 length = cu_context->cc_length;
886 max_cu_local_offset = length;
887 res = _dwarf_read_cu_version_and_abbrev_offset(dbg,
888 cu_ptr,
889 is_info,
890 local_length_size,
891 cu_context,
892 section_end_ptr,
893 &bytes_read,error);
894 if (res != DW_DLV_OK) {
895 local_dealloc_cu_context(dbg,cu_context);
896 return res;
897 }
898 version = cu_context->cc_version_stamp;
899 cu_ptr += bytes_read;
900 unit_type = cu_context->cc_unit_type;
901 if (cu_ptr > section_end_ptr) {
902 local_dealloc_cu_context(dbg,cu_context);
903 _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
904 return DW_DLV_ERROR;
905 }
906
907 /* In a dwp context, the abbrev_offset is
908 still incomplete.
909 We need to add in the base from the .debug_cu_index
910 or .debug_tu_index . Done below */
911
912 /* At this point, for DW4, the unit_type is not fully
913 correct as we don't know if it is a skeleton or
914 a split_compile or split_type */
915 if (version == DW_CU_VERSION5 ||
916 version == DW_CU_VERSION4) {
917 /* DW4/DW5 header fields, depending on UT type.
918 See DW5 section 7.5.1.x, DW4
919 data is a GNU extension of DW4. */
920 switch(unit_type) {
921 case DW_UT_split_type:
922 case DW_UT_type: {
923 types_extra_len = sizeof(Dwarf_Sig8) /* 8 */ +
924 local_length_size /*type_offset size*/;
925 break;
926 }
927 case DW_UT_skeleton:
928 case DW_UT_split_compile: {
929 types_extra_len = sizeof(Dwarf_Sig8) /* 8 */;
930 break;
931 }
932 case DW_UT_compile: /* No additional fields */
933 case DW_UT_partial: /* No additional fields */
934 break;
935 default:
936 /* Data corruption in libdwarf? */
937 report_local_unit_type_error(dbg, unit_type,
938 "(DW4 or DW5)",error);
939 local_dealloc_cu_context(dbg,cu_context);
940 return DW_DLV_ERROR;
941 }
942 }
943
944 /* Compare the space following the length field
945 to the bytes in the CU header. */
946 if (length <
947 (CU_VERSION_STAMP_SIZE /* is 2 */ +
948 local_length_size /*for debug_abbrev offset */ +
949 CU_ADDRESS_SIZE_SIZE /* is 1 */ +
950 /* and finally size of the rest of the header: */
951 types_extra_len)) {
952
953 local_dealloc_cu_context(dbg,cu_context);
954 _dwarf_error_string(dbg, error, DW_DLE_CU_LENGTH_ERROR,
955 "DW_DLE_CU_LENGTH_ERROR: reading version "
956 "stamp and address size fields");
957 return DW_DLV_ERROR;
958 }
959 /* Now we can read the fields with some confidence,
960 we know the fields of the header are inside
961 the section. */
962
963 cu_context->cc_unit_type = unit_type;
964 switch(unit_type) {
965 case DW_UT_split_type:
966 case DW_UT_type: {
967 int tres = 0;
968 /* ASSERT: DW_CU_VERSION4 or DW_CU_VERSION5,
969 determined by logic above.
970 Now read the debug_types extra header fields of
971 the signature (8 bytes) and the typeoffset.
972 This can be in executable, ordinary object
973 (as in Type Unit),
974 there was no dwo in DWARF4
975 */
976 if ((cu_ptr + sizeof(signaturedata)) > section_end_ptr) {
977 _dwarf_error_string(dbg, error, DW_DLE_CU_LENGTH_ERROR,
978 "DW_DLE_CU_LENGTH_ERROR: reading "
979 "Dwarf_Sig8 signature field");
980 local_dealloc_cu_context(dbg,cu_context);
981 return DW_DLV_ERROR;
982 }
983 memcpy(&signaturedata,cu_ptr,sizeof(signaturedata));
984 cu_ptr += sizeof(signaturedata);
985 tres = _dwarf_read_unaligned_ck_wrapper(dbg,
986 &typeoffset,cu_ptr,local_length_size,
987 section_end_ptr,error);
988 if (tres != DW_DLV_OK ) {
989 local_dealloc_cu_context(dbg,cu_context);
990 return tres;
991 }
992 cu_context->cc_signature = signaturedata;
993 cu_context->cc_signature_present = TRUE;
994 cu_context->cc_signature_offset = typeoffset;
995 if (typeoffset >= max_cu_local_offset) {
996 local_dealloc_cu_context(dbg,cu_context);
997 _dwarf_error(dbg, error,
998 DW_DLE_DEBUG_TYPEOFFSET_BAD);
999 return DW_DLV_ERROR;
1000 }
1001 }
1002 break;
1003 case DW_UT_skeleton:
1004 case DW_UT_split_compile: {
1005 if ((cu_ptr + sizeof(signaturedata)) > section_end_ptr) {
1006 _dwarf_error_string(dbg, error, DW_DLE_CU_LENGTH_ERROR,
1007 "DW_DLE_CU_LENGTH_ERROR: reading "
1008 "Dwarf_Sig8 signature field");
1009 local_dealloc_cu_context(dbg,cu_context);
1010 return DW_DLV_ERROR;
1011 }
1012 /* These unit types make a pair and
1013 paired units have identical signature.*/
1014 memcpy(&signaturedata,cu_ptr,sizeof(signaturedata));
1015 cu_context->cc_signature = signaturedata;
1016 cu_context->cc_signature_present = TRUE;
1017
1018 break;
1019 }
1020 /* The following with no additional fields */
1021 case DW_UT_compile:
1022 case DW_UT_partial:
1023 break;
1024 default: {
1025 /* Data corruption in libdwarf? */
1026 report_local_unit_type_error(dbg, unit_type,
1027 "",error);
1028 local_dealloc_cu_context(dbg,cu_context);
1029 return DW_DLV_ERROR;
1030 }
1031 }
1032 cu_context->cc_abbrev_hash_table =
1033 (Dwarf_Hash_Table) calloc(1,
1034 sizeof(struct Dwarf_Hash_Table_s));
1035 if (!cu_context->cc_abbrev_hash_table) {
1036 local_dealloc_cu_context(dbg,cu_context);
1037 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
1038 return DW_DLV_ERROR;
1039 }
1040 cu_context->cc_debug_offset = offset;
1041
1042 /* This is recording an overall section value for later
1043 sanity checking. */
1044 dis->de_last_offset = max_cu_global_offset;
1045 *context_out = cu_context;
1046 return DW_DLV_OK;
1047 }
1048
1049 static int
1050 reloc_incomplete(int res,Dwarf_Error err)
1051 {
1052 Dwarf_Unsigned e = 0;
1053
1054 if (res == DW_DLV_OK) {
1055 return FALSE;
1056 }
1057 if (res == DW_DLV_NO_ENTRY) {
1058 return FALSE;
1059 }
1060 e = dwarf_errno(err);
1061 switch(e) {
1062 case DW_DLE_RELOC_MISMATCH_INDEX:
1063 case DW_DLE_RELOC_MISMATCH_RELOC_INDEX:
1064 case DW_DLE_RELOC_MISMATCH_STRTAB_INDEX:
1065 case DW_DLE_RELOC_SECTION_MISMATCH:
1066 case DW_DLE_RELOC_SECTION_MISSING_INDEX:
1067 case DW_DLE_RELOC_SECTION_LENGTH_ODD:
1068 case DW_DLE_RELOC_SECTION_PTR_NULL:
1069 case DW_DLE_RELOC_SECTION_MALLOC_FAIL:
1070 case DW_DLE_SEEK_OFF_END:
1071 case DW_DLE_RELOC_INVALID:
1072 case DW_DLE_RELOC_SECTION_SYMBOL_INDEX_BAD:
1073 case DW_DLE_ELF_RELOC_SECTION_ERROR:
1074 case DW_DLE_RELOCATION_SECTION_SIZE_ERROR:
1075 return TRUE;
1076 default: break;
1077 }
1078 return FALSE;
1079 }
1080
1081 /* Returns offset of next compilation-unit thru next_cu_offset
1082 pointer.
1083 It sequentially moves from one
1084 cu to the next. The current cu is recorded
1085 internally by libdwarf. */
1086 int
1087 dwarf_next_cu_header_d(Dwarf_Debug dbg,
1088 Dwarf_Bool is_info,
1089 Dwarf_Unsigned * cu_header_length,
1090 Dwarf_Half * version_stamp,
1091 Dwarf_Unsigned * abbrev_offset,
1092 Dwarf_Half * address_size,
1093 Dwarf_Half * offset_size,
1094 Dwarf_Half * extension_size,
1095 Dwarf_Sig8 * signature,
1096 Dwarf_Unsigned * typeoffset,
1097 Dwarf_Unsigned * next_cu_offset,
1098 Dwarf_Half * header_cu_type,
1099 Dwarf_Error * error)
1100 {
1101 Dwarf_Bool has_signature = FALSE;
1102 int res = 0;
1103
1104 res = _dwarf_next_cu_header_internal(dbg,
1105 is_info,
1106 NULL,
1107 cu_header_length,
1108 version_stamp,
1109 abbrev_offset,
1110 address_size,
1111 offset_size,
1112 extension_size,
1113 signature,
1114 &has_signature,
1115 typeoffset,
1116 next_cu_offset,
1117 header_cu_type,
1118 error);
1119 return res;
1120 }
1121 int
1122 dwarf_next_cu_header_e(Dwarf_Debug dbg,
1123 Dwarf_Bool is_info,
1124 Dwarf_Die * cu_die_out,
1125 Dwarf_Unsigned * cu_header_length,
1126 Dwarf_Half * version_stamp,
1127 Dwarf_Unsigned * abbrev_offset,
1128 Dwarf_Half * address_size,
1129 Dwarf_Half * offset_size,
1130 Dwarf_Half * extension_size,
1131 Dwarf_Sig8 * signature,
1132 Dwarf_Unsigned * typeoffset,
1133 Dwarf_Unsigned * next_cu_offset,
1134 Dwarf_Half * header_cu_type,
1135 Dwarf_Error * error)
1136 {
1137 Dwarf_Bool has_signature = FALSE;
1138 int res = 0;
1139
1140 res = _dwarf_next_cu_header_internal(dbg,
1141 is_info,
1142 cu_die_out,
1143 cu_header_length,
1144 version_stamp,
1145 abbrev_offset,
1146 address_size,
1147 offset_size,
1148 extension_size,
1149 signature,
1150 &has_signature,
1151 typeoffset,
1152 next_cu_offset,
1153 header_cu_type,
1154 error);
1155 if (! dbg->de_debug_addr_version) {
1156 /* To enable printing raw GNU extension .debug_addr */
1157 dbg->de_debug_addr_version = (Dwarf_Half)*version_stamp;
1158 dbg->de_debug_addr_offset_size = (Dwarf_Half)*offset_size;
1159 dbg->de_debug_addr_address_size = (Dwarf_Half)*address_size;
1160 }
1161 return res;
1162 }
1163
1164 static void
1165 local_attrlist_dealloc(Dwarf_Debug dbg,
1166 Dwarf_Signed atcount,
1167 Dwarf_Attribute *alist)
1168 {
1169 Dwarf_Signed i = 0;
1170
1171 for ( ; i < atcount; ++i) {
1172 dwarf_dealloc(dbg,alist[i],DW_DLA_ATTR);
1173 }
1174 dwarf_dealloc(dbg,alist,DW_DLA_LIST);
1175 }
1176
1177 static int
1178 _dwarf_setup_base_address(Dwarf_Debug dbg,
1179 const char *attrname,
1180 Dwarf_Attribute attr,
1181 Dwarf_Signed at_addr_base_attrnum,
1182 Dwarf_CU_Context cucon,
1183 Dwarf_Bool *bad_pc_form,
1184 Dwarf_Error *error)
1185 {
1186 int lres = 0;
1187 Dwarf_Half form = 0;
1188 /* If the form is indexed, we better have
1189 seen DW_AT_addr_base.! */
1190 lres = dwarf_whatform(attr,&form,error);
1191 if (lres != DW_DLV_OK) {
1192 return lres;
1193 }
1194 if (dwarf_addr_form_is_indexed(form)) {
1195 if (at_addr_base_attrnum < 0) {
1196 dwarfstring m;
1197
1198 dwarfstring_constructor(&m);
1199 dwarfstring_append(&m,
1200 "DW_DLE_ATTR_NO_CU_CONTEXT: The ");
1201 dwarfstring_append(&m,(char *)attrname);
1202 dwarfstring_append(&m," CU_DIE uses "
1203 "an indexed attribute yet "
1204 "DW_AT_addr_base is not in the CU DIE.");
1205 _dwarf_error_string(dbg,error,
1206 DW_DLE_ATTR_NO_CU_CONTEXT,
1207 dwarfstring_string(&m));
1208 dwarfstring_destructor(&m);
1209 return DW_DLV_ERROR;
1210 }
1211 }
1212 if (form == DW_FORM_ref_addr) {
1213 /* The old Macrowerks compiler did this
1214 from confusion vs DW_FORM_addr.
1215 Lets just say it is not good rather
1216 than generating an error.
1217 */
1218 *bad_pc_form = TRUE;
1219 return DW_DLV_NO_ENTRY;
1220 }
1221 lres = dwarf_formaddr(attr,
1222 &cucon->cc_low_pc,error);
1223 if (lres == DW_DLV_OK) {
1224 /* Pretending low_pc (ie cu base address for loclists)
1225 if it was DW_AT_entry_pc with no DW_AT_low_pc
1226 Allowing DW_AT_entry_pc */
1227 cucon->cc_low_pc_present = TRUE;
1228 cucon->cc_base_address_present = TRUE;
1229 cucon->cc_base_address = cucon->cc_low_pc;
1230 } else {
1231 /* Something is badly wrong. */
1232 return lres;
1233 }
1234 return lres;
1235 }
1236
1237 static void
1238 _dwarf_set_children_flag(Dwarf_CU_Context cucon,
1239 Dwarf_Die cudie)
1240 {
1241 int chres = 0;
1242 Dwarf_Half flag = 0;
1243
1244 /* always winds up with cc_cu_die_has_children
1245 set intentionally...to something. */
1246 cucon->cc_cu_die_has_children = TRUE;
1247 chres = dwarf_die_abbrev_children_flag(cudie,&flag);
1248 /* If chres is not DW_DLV_OK the assumption
1249 of children remains TRUE. */
1250 if (chres == DW_DLV_OK) {
1251 cucon->cc_cu_die_has_children = flag;
1252 }
1253 }
1254
1255 static int
1256 _dwarf_prod_contains(const char *ck, const char *prod)
1257 {
1258 const char *cp = prod;
1259 size_t len = strlen(ck);
1260
1261 for (; *cp ; ++cp) {
1262 if ( ck[0] != *cp) {
1263 continue;
1264 }
1265 if (strncmp(ck,cp,len)) {
1266 continue;
1267 }
1268 return TRUE;
1269 }
1270 return FALSE;
1271 }
1272
1273 /* Here we make an effort to determine if it is
1274 Metrowerks C */
1275 static void
1276 set_producer_type(Dwarf_Die die,
1277 Dwarf_CU_Context cu_context)
1278 {
1279 int res = 0;
1280 Dwarf_Error error = 0;
1281 char *producer = 0;
1282
1283 res = dwarf_die_text(die,DW_AT_producer,&producer,&error);
1284 if (res == DW_DLV_ERROR) {
1285 dwarf_dealloc_error(cu_context->cc_dbg,error);
1286 error = 0;
1287 return;
1288 }
1289 if (res == DW_DLV_NO_ENTRY) {
1290 return;
1291 }
1292 if (_dwarf_prod_contains("Metrowerks",producer)) {
1293 cu_context->cc_producer = CC_PROD_METROWERKS;
1294 } else if (_dwarf_prod_contains("Apple",producer)) {
1295 cu_context->cc_producer = CC_PROD_Apple;
1296 }
1297 }
1298
1299 /*
1300 For a DWP/DWO the base fields
1301 of a CU are inherited from the skeleton.
1302 DWARF5 section 3.1.3
1303 "Split Full Compilation Unit Entries".
1304 */
1305 static int
1306 find_cu_die_base_fields(Dwarf_Debug dbg,
1307 Dwarf_CU_Context cucon,
1308 Dwarf_Die cudie,
1309 Dwarf_Bool *bad_pc_form,
1310 Dwarf_Error* error)
1311 {
1312 Dwarf_CU_Context cu_context = 0;
1313 Dwarf_Attribute * alist = 0;
1314 Dwarf_Signed atcount = 0;
1315 unsigned version_stamp = 2;
1316 int alres = 0;
1317 Dwarf_Signed i = 0;
1318 Dwarf_Signed low_pc_attrnum = -1;
1319 Dwarf_Signed entry_pc_attrnum = -1;
1320 Dwarf_Signed at_addr_base_attrnum = -1;
1321
1322 cu_context = cudie->di_cu_context;
1323 version_stamp = cu_context->cc_version_stamp;
1324 alres = dwarf_attrlist(cudie, &alist,
1325 &atcount,error);
1326 if (alres != DW_DLV_OK) {
1327 /* Something is badly wrong. No attrlist! */
1328 return alres;
1329 }
1330 /* DW_AT_dwo_id and/or DW_AT_GNU_dwo_id
1331 are only found in some
1332 experimental DWARF4.
1333 Even DWARF3,4 use DW_AT_low_pc as base address
1334 DWARF5 changed CU header contents
1335 to make this attribute unnecessary.
1336 DW_AT_GNU_odr_signature is the same format,
1337 but is in a different namespace so not
1338 appropriate here..
1339 */
1340 for (i = 0; i < atcount; ++i) {
1341 Dwarf_Half attrnum = 0;
1342 Dwarf_Half form = 0;
1343 int ares = 0;
1344 int ares2 = 0;
1345 Dwarf_Attribute attr = alist[i];
1346
1347 ares = dwarf_whatattr(attr,&attrnum,error);
1348 if (ares == DW_DLV_ERROR && error) {
1349 dwarf_dealloc_error(dbg,*error);
1350 *error = 0;
1351 }
1352 ares2 = dwarf_whatform(attr,&form,error);
1353 if (ares2 == DW_DLV_ERROR && error) {
1354 dwarf_dealloc_error(dbg,*error);
1355 *error = 0;
1356 }
1357 /* We are not returning on DW_DLV_NO_ENTRY
1358 or DW_DLV_ERROR here. Such will be
1359 caught later. Lets finish a CU die
1360 scan and finish the cu_context */
1361 if (ares == DW_DLV_OK && ares2 == DW_DLV_OK) {
1362 switch(form) {
1363 case DW_FORM_strx:
1364 case DW_FORM_strx1:
1365 case DW_FORM_strx2:
1366 case DW_FORM_strx3:
1367 case DW_FORM_strx4:
1368 cucon->cc_at_strx_present = TRUE;
1369 break;
1370 default:
1371 break;
1372 }
1373 switch(attrnum) {
1374 case DW_AT_producer:
1375 set_producer_type(cudie,cu_context);
1376 break;
1377 case DW_AT_dwo_id:
1378 case DW_AT_GNU_dwo_id: {
1379 Dwarf_Sig8 signature;
1380 /* This is for DWARF4 with an early
1381 non-standard version
1382 of split dwarf. Not DWARF5. */
1383 int sres = 0;
1384 if (version_stamp != DW_CU_VERSION4 ) {
1385 /* Not supposed to happen. */
1386 local_attrlist_dealloc(dbg,atcount,alist);
1387 _dwarf_error(dbg,error,
1388 DW_DLE_IMPROPER_DWO_ID);
1389 return DW_DLV_ERROR;
1390 }
1391 signature = dwarfsig8zero;
1392 sres = dwarf_formsig8_const(attr,
1393 &signature,error);
1394 if (sres == DW_DLV_OK) {
1395 if (!cucon->cc_signature_present) {
1396 cucon->cc_signature = signature;
1397 cucon->cc_signature_present = TRUE;
1398 } else {
1399 /* Something wrong. Two styles of sig?
1400 Can happen with DWARF4
1401 debug-fission extension DWO_id.
1402 */
1403 if (memcmp(&signature,&cucon->cc_signature,
1404 sizeof(signature))) {
1405 /* The two sigs do not match! */
1406 const char *m="DW_DLE_SIGNATURE_MISMATCH"
1407 "DWARF4 extension fission signature"
1408 " and DW_AT_GNU_dwo_id do not match"
1409 " ignoring DW_AT[_GNU]_dwo_id";
1410 dwarf_insert_harmless_error(dbg,
1411 (char*)m);
1412 }
1413 }
1414 } else {
1415 /* Something is badly wrong. */
1416 local_attrlist_dealloc(dbg,atcount,alist);
1417 return sres;
1418 }
1419 /* Something is badly wrong. */
1420 break;
1421 }
1422 /* If, in .debug_rnglists for a CU the
1423 applicable range has no base address
1424 this attribute provides a base address.
1425 If this is indexed doing this now would
1426 lead to an infinite recursion.
1427 So wait till all the other fields seen.
1428 */
1429 case DW_AT_low_pc: {
1430 low_pc_attrnum = i;
1431 break;
1432 }
1433 /* DW_AT_producer 4.2.1 (Based on Apple Inc. build 5658)
1434 (LLVM build 2336.1.00) uses DW_AT_entry_pc as the
1435 base address (DW_AT_entry_pc
1436 first appears in DWARF3).
1437 So we allow that as an extension,
1438 as a 'low_pc' if there is DW_AT_entry_pc with
1439 no DW_AT_low_pc. 19 May 2022.
1440 */
1441 case DW_AT_entry_pc: {
1442 entry_pc_attrnum = i;
1443 break;
1444 }
1445 case DW_AT_ranges: {
1446 Dwarf_Unsigned at_ranges_offset = 0;
1447 int res = 0;
1448 Dwarf_Bool is_info = cucon->cc_is_info;
1449
1450 res = _dwarf_internal_global_formref_b(attr,
1451 /* avoid recurse creating context */ 1,
1452 &at_ranges_offset,
1453 &is_info,
1454 error);
1455 if (res == DW_DLV_OK) {
1456 cucon->cc_at_ranges_offset = at_ranges_offset;
1457 cucon->cc_at_ranges_offset_present = TRUE;
1458 } else {
1459 local_attrlist_dealloc(dbg,atcount,alist);
1460 return res;
1461 }
1462 break;
1463 }
1464 /* The offset is of the first offset in
1465 .debug_str_offsets that is the string table
1466 offset array for this CU. */
1467 case DW_AT_str_offsets_base:{
1468 int udres = 0;
1469 Dwarf_Bool is_info = cucon->cc_is_info;
1470
1471 udres = _dwarf_internal_global_formref_b(attr,
1472 /* avoid recurse creating context */ 1,
1473 &cucon->cc_str_offsets_array_offset,
1474 &is_info,
1475 error);
1476 if (udres == DW_DLV_OK) {
1477 cucon->cc_str_offsets_array_offset_present = TRUE;
1478 } else {
1479 local_attrlist_dealloc(dbg,atcount,alist);
1480 /* Something is badly wrong. */
1481 return udres;
1482 }
1483 break;
1484 }
1485 /* offset in .debug_loclists of the offsets table
1486 applicable to this CU. */
1487 case DW_AT_loclists_base: {
1488 int udres = 0;
1489 Dwarf_Bool is_info = cucon->cc_is_info;
1490
1491 udres = _dwarf_internal_global_formref_b(attr,
1492 /* avoid recurse creating context */ 1,
1493 &cucon->cc_loclists_base,
1494 &is_info,
1495 error);
1496 if (udres == DW_DLV_OK) {
1497 cucon->cc_loclists_base_present = TRUE;
1498 cucon->cc_loclists_base_via_at = TRUE;
1499 } else {
1500 local_attrlist_dealloc(dbg,atcount,alist);
1501 /* Something is badly wrong. */
1502 return udres;
1503 }
1504 break;
1505 }
1506 /* Base offset in .debug_addr of the addr table
1507 for this CU. DWARF5 (and possibly GNU DWARF4)
1508 So we really want to look in only
1509 this section, not an offset referring
1510 to another (DWARF5 debug_info vs debug_types) */
1511 case DW_AT_addr_base:
1512 case DW_AT_GNU_addr_base: {
1513 int udres = 0;
1514 Dwarf_Bool is_info = cucon->cc_is_info;
1515
1516 at_addr_base_attrnum = i;
1517
1518 udres = _dwarf_internal_global_formref_b(attr,
1519 /* avoid recurse creating context */ 1,
1520 &cucon->cc_addr_base_offset,
1521 &is_info,
1522 error);
1523 if (udres == DW_DLV_OK) {
1524 if (is_info == cucon->cc_is_info) {
1525 /* Only accept if same .debug section,
1526 which is relevant for DWARF4 */
1527 cucon->cc_addr_base_offset_present = TRUE;
1528 }
1529 } else {
1530 local_attrlist_dealloc(dbg,atcount,alist);
1531 /* Something is badly wrong. */
1532 return udres;
1533 }
1534 break;
1535 }
1536 case DW_AT_GNU_ranges_base: {
1537 /* The DW4 ranges base was never used in GNU
1538 but did get emitted in skeletons.
1539 http://llvm.1065342.n5.nabble.com/
1540 DebugInfo-DW-AT-GNU-ranges-base-in-
1541 non-fission-td64194.html
1542 But we accept it anyway.
1543 In dw4 GNU fission extension
1544 it is used and matters.
1545
1546 offset in .debug_rnglists of the offsets table
1547 applicable to this CU.
1548 Or for DW4 GNU .debug_ranges split dwarf
1549 it refers to .debug_ranges.
1550 Note that this base applies when
1551 referencing from the dwp, but NOT
1552 when referencing from the a.out
1553
1554 In DW4 extension split dwarf the .debug_ranges
1555 is always in the tied-file (executable). */
1556
1557 int udres = 0;
1558 Dwarf_Bool is_info = cucon->cc_is_info;
1559
1560 udres = _dwarf_internal_global_formref_b(attr,
1561 /* avoid recurse creating context */ 1,
1562 &cucon->cc_ranges_base,
1563 &is_info,
1564 error);
1565 if (udres == DW_DLV_OK) {
1566 cucon->cc_ranges_base_present = TRUE;
1567 } else {
1568 local_attrlist_dealloc(dbg,atcount,alist);
1569 /* Something is badly wrong. */
1570 return udres;
1571 }
1572 break;
1573 }
1574 case DW_AT_rnglists_base: {
1575 int udres = 0;
1576 Dwarf_Bool is_info = cucon->cc_is_info;
1577
1578 udres = _dwarf_internal_global_formref_b(attr,
1579 /* avoid recurse creating context */ 1,
1580 &cucon->cc_rnglists_base,
1581 &is_info,
1582 error);
1583 if (udres == DW_DLV_OK) {
1584 cucon->cc_rnglists_base_present = TRUE;
1585 cucon->cc_rnglists_base_via_at = TRUE;
1586 } else {
1587 local_attrlist_dealloc(dbg,atcount,alist);
1588 /* Something is badly wrong. */
1589 return udres;
1590 }
1591 break;
1592 }
1593 /* A signature, found in a DWARF5 skeleton
1594 compilation unit. */
1595 case DW_AT_GNU_dwo_name:
1596 case DW_AT_dwo_name: {
1597 int dnres = 0;
1598
1599 dnres = dwarf_formstring(attr,
1600 &cucon->cc_dwo_name,error);
1601 if (dnres != DW_DLV_OK) {
1602 local_attrlist_dealloc(dbg,atcount,alist);
1603 return dnres;
1604 }
1605 cucon->cc_dwo_name_present = TRUE;
1606 break;
1607 }
1608 default: /* do nothing, not an attribute
1609 we need to deal with here. */
1610 break;
1611 }
1612 }
1613 }
1614 /* Only on Apple do we let entry_pc
1615 be used as base address. */
1616 if (entry_pc_attrnum >= 0 &&
1617 cucon->cc_producer == CC_PROD_Apple) {
1618 int battr = 0;
1619
1620 /* Pretending that DW_AT_entry_pc with no
1621 DW_AT_low_pc is a valid base address for
1622 location lists.
1623 DW_AT_producer 4.2.1 (Based on Apple Inc. build 5658)
1624 (LLVM build 2336.1.00) uses DW_AT_entry_pc as the
1625 base address (DW_AT_entry_pc first appears in DWARF3).
1626 So we allow that as an extension,
1627 as a 'low_pc' if there is DW_AT_entry_pc with
1628 no DW_AT_low_pc. 19 May 2022.
1629 Also used by gcc with a DWARF4 split-dwarf extension. */
1630 Dwarf_Attribute attr = alist[entry_pc_attrnum];
1631 battr = _dwarf_setup_base_address(dbg,"DW_AT_entry_pc",
1632 attr,at_addr_base_attrnum, cucon,
1633 bad_pc_form,error);
1634 if (battr != DW_DLV_OK) {
1635 local_attrlist_dealloc(dbg,atcount,alist);
1636 /* Something is wrong */
1637 _dwarf_set_children_flag(cucon,cudie);
1638 return battr;
1639 }
1640 }
1641 if (low_pc_attrnum >= 0 ){
1642 int battr = 0;
1643
1644 Dwarf_Attribute attr = alist[low_pc_attrnum];
1645 battr = _dwarf_setup_base_address(dbg,"DW_AT_low_pc",
1646 attr,at_addr_base_attrnum, cucon,
1647 bad_pc_form,error);
1648 if (battr != DW_DLV_OK) {
1649 local_attrlist_dealloc(dbg,atcount,alist);
1650 /* Something is wrong, possibly
1651 erroneous Macrowerks compiler. */
1652 _dwarf_set_children_flag(cucon,cudie);
1653 return battr;
1654 }
1655 }
1656 local_attrlist_dealloc(dbg,atcount,alist);
1657 alist = 0;
1658 atcount = 0;
1659 _dwarf_set_children_flag(cucon,cudie);
1660 return DW_DLV_OK;
1661 }
1662
1663 /* Called only for DWARF4 and earlier
1664 so there is consistent naming of unit_type
1665 even though there was no such field in
1666 DWARF2-DWARF4. */
1667 static void
1668 assign_correct_unit_type(Dwarf_CU_Context cu_context)
1669 {
1670 Dwarf_Half tag = cu_context->cc_cu_die_tag;
1671 if (!cu_context->cc_cu_die_has_children) {
1672 if (cu_context->cc_signature_present) {
1673 if (tag == DW_TAG_compile_unit ||
1674 tag == DW_TAG_type_unit ) {
1675 cu_context->cc_unit_type = DW_UT_skeleton;
1676 }
1677 }
1678 } else {
1679 if (cu_context->cc_signature_present) {
1680 if (tag == DW_TAG_compile_unit) {
1681 cu_context->cc_unit_type = DW_UT_split_compile;
1682 } else if (tag == DW_TAG_type_unit) {
1683 cu_context->cc_unit_type = DW_UT_split_type;
1684 }
1685 }
1686 }
1687 }
1688
1689 /* If local_cudie_return non-null, and returning DW_DLV_OK,
1690 then we return a valid CU_DIE through
1691 local_cudie_return. */
1692 static int
1693 finish_up_cu_context_from_cudie(Dwarf_Debug dbg,
1694 Dwarf_Unsigned offset,
1695 Dwarf_CU_Context cu_context,
1696 Dwarf_Die *cudie_return,
1697 Dwarf_Error *error)
1698 {
1699 int version = cu_context->cc_version_stamp;
1700 Dwarf_Sig8 signaturedata = cu_context->cc_signature;
1701 int res = 0;
1702
1703 /* Loads and initializes the dwarf .debug_cu_index
1704 and .debug_tu_index split dwarf package
1705 file sections */
1706 res = fill_in_dwp_offsets_if_present(dbg,
1707 cu_context,
1708 &signaturedata,
1709 offset,
1710 error);
1711 if (res != DW_DLV_OK) {
1712 return res;
1713 }
1714 if (cu_context->cc_dwp_offsets.pcu_type) {
1715 Dwarf_Unsigned absize = 0;
1716 Dwarf_Unsigned aboff = 0;
1717
1718 aboff = _dwarf_get_dwp_extra_offset(
1719 &cu_context->cc_dwp_offsets,
1720 DW_SECT_ABBREV, &absize);
1721 cu_context->cc_abbrev_offset += aboff;
1722 }
1723
1724 if (cu_context->cc_abbrev_offset >=
1725 dbg->de_debug_abbrev.dss_size) {
1726 _dwarf_error(dbg, error, DW_DLE_ABBREV_OFFSET_ERROR);
1727 return DW_DLV_ERROR;
1728 }
1729 /* Now we can read the CU die and determine
1730 the correct DW_UT_ type for DWARF4 and some
1731 offset base fields for DW4-fission and DW5,
1732 and even DW3 and DW4 and some non-std DW2 */
1733 {
1734 res = finish_cu_context_via_cudie_inner(dbg,
1735 cu_context,cudie_return, error);
1736 if (res == DW_DLV_ERROR) {
1737 return res;
1738 }
1739 if (res != DW_DLV_OK) {
1740 return res;
1741 }
1742 if (version == DW_CU_VERSION4) {
1743 assign_correct_unit_type(cu_context);
1744 }
1745 if (cu_context->cc_signature_present) {
1746 /* For finding base data from skeleton.
1747 For the few fields inherited
1748 (per the DWARF5 standard but for
1749 .debug_rnglists is not interited
1750 in spite of what DW5 says). */
1751 res = _dwarf_find_all_offsets_via_fission(dbg,
1752 cu_context,error);
1753 if (res == DW_DLV_ERROR) {
1754 /* something seriously wrong. */
1755 return res;
1756 }
1757 }
1758 }
1759 return DW_DLV_OK;
1760 }
1761 /*
1762 CU_Contexts do not overlap.
1763 cu_context we see here is not in the list we
1764 are updating. See _dwarf_find_CU_Context()
1765
1766 Invariant: cc_debug_offset in strictly
1767 ascending order in the list.
1768 Never returns DW_DLV_NO_ENTRY
1769 */
1770 static int
1771 insert_into_cu_context_list(Dwarf_Debug_InfoTypes dis,
1772 Dwarf_CU_Context icu_context)
1773 {
1774 Dwarf_Unsigned ioffset = icu_context->cc_debug_offset;
1775 Dwarf_Unsigned eoffset = 0;
1776 Dwarf_Unsigned hoffset = 0;
1777 Dwarf_Unsigned coffset = 0;
1778 Dwarf_CU_Context next = 0;
1779 Dwarf_CU_Context past = 0;
1780 Dwarf_CU_Context cur = 0;
1781
1782 /* Add the context into the section context list.
1783 This is the one and only place where it is
1784 saved for re-use and eventual dealloc. */
1785 if (!dis->de_cu_context_list) {
1786 /* First cu encountered. */
1787 dis->de_cu_context_list = icu_context;
1788 dis->de_cu_context_list_end = icu_context;
1789 return DW_DLV_OK;
1790 }
1791 if (!dis->de_cu_context_list_end) {
1792 return DW_DLV_ERROR;
1793 }
1794 eoffset = dis->de_cu_context_list_end->cc_debug_offset;
1795 if (eoffset < ioffset) {
1796 /* Normal case, add at end. */
1797 dis->de_cu_context_list_end->cc_next = icu_context;
1798 dis->de_cu_context_list_end = icu_context;
1799 return DW_DLV_OK;
1800 }
1801 hoffset = dis->de_cu_context_list->cc_debug_offset;
1802 if (hoffset > ioffset) {
1803 /* insert as new head. Unusual. */
1804 next = dis->de_cu_context_list;
1805 dis->de_cu_context_list = icu_context;
1806 dis->de_cu_context_list->cc_next = next;
1807 /* No need to touch de_cu_context_list_end */
1808 return DW_DLV_OK;
1809 }
1810 cur = dis->de_cu_context_list;
1811 past = 0;
1812 /* Insert in middle somewhere. Neither at
1813 start nor end.
1814 ASSERT: cur non-null
1815 ASSERT: past non-null */
1816 past = cur;
1817 cur = cur->cc_next;
1818 for ( ; cur ; cur = next) {
1819 next = cur->cc_next;
1820 coffset = cur->cc_debug_offset;
1821 if (coffset > ioffset) {
1822 /* Insert before cur, using past.
1823 ASSERT: past non-null */
1824 past->cc_next = icu_context;
1825 icu_context->cc_next = cur;
1826 return DW_DLV_OK;
1827 }
1828 past = cur;
1829 }
1830 /* Impossible, for end, coffset (ie, eoffset) > ioffset */
1831 /* NOTREACHED */
1832 return DW_DLV_ERROR;
1833 }
1834
1835 Dwarf_Unsigned
1836 _dwarf_calculate_next_cu_context_offset(Dwarf_CU_Context cu_context)
1837 {
1838 Dwarf_Unsigned next_cu_offset = 0;
1839
1840 next_cu_offset = cu_context->cc_debug_offset +
1841 cu_context->cc_length +
1842 cu_context->cc_length_size +
1843 cu_context->cc_extension_size;
1844 return next_cu_offset;
1845 }
1846
1847 /* If local_cudie_return non-null, and returning DW_DLV_OK,
1848 then we return a valid CU_DIE through
1849 local_cudie_return. */
1850 int
1851 _dwarf_create_a_new_cu_context_record_on_list(
1852 Dwarf_Debug dbg,
1853 Dwarf_Debug_InfoTypes dis,
1854 Dwarf_Bool is_info,
1855 Dwarf_Unsigned section_size,
1856 Dwarf_Unsigned new_cu_offset,
1857 Dwarf_CU_Context *context_out,
1858 Dwarf_Die *cudie_return,
1859 Dwarf_Error *error)
1860 {
1861 int res = 0;
1862 Dwarf_CU_Context cu_context = 0;
1863 int icres = 0;
1864
1865 if ((new_cu_offset +
1866 _dwarf_length_of_cu_header_simple(dbg,is_info)) >=
1867 section_size) {
1868 _dwarf_error(dbg, error, DW_DLE_OFFSET_BAD);
1869 return DW_DLV_ERROR;
1870 }
1871 res = _dwarf_make_CU_Context(dbg, new_cu_offset,is_info,
1872 &cu_context,error);
1873 if (res != DW_DLV_OK) {
1874 return res;
1875 }
1876 /* The called func does not dealloc cu_context
1877 in case of error, so we do it here. */
1878 res = finish_up_cu_context_from_cudie(dbg,new_cu_offset,
1879 cu_context,cudie_return,error);
1880 if (res == DW_DLV_ERROR) {
1881 local_dealloc_cu_context(dbg,cu_context);
1882 return res;
1883 }
1884 if (res == DW_DLV_NO_ENTRY) {
1885 local_dealloc_cu_context(dbg,cu_context);
1886 return res;
1887 }
1888 /* Add the new cu_context to a list of contexts
1889 Never returns DW_DLV_NO_ENTRY */
1890 icres = insert_into_cu_context_list(dis,cu_context);
1891 if (icres == DW_DLV_ERROR) {
1892 /* Correcting ossfuzz70721 DW202407-010 */
1893 dwarf_dealloc_die(*cudie_return);
1894 *cudie_return = 0;
1895 local_dealloc_cu_context(dbg,cu_context);
1896 _dwarf_error_string(dbg,error,DW_DLE_DIE_NO_CU_CONTEXT,
1897 "DW_DLE_DIE_NO_CU_CONTEXT"
1898 "Impossible error inserting into internal context list");
1899 return icres;
1900 }
1901 *context_out = cu_context;
1902 return DW_DLV_OK;
1903 }
1904
1905 int
1906 _dwarf_load_die_containing_section(Dwarf_Debug dbg,
1907 Dwarf_Bool is_info,
1908 Dwarf_Error *error)
1909 {
1910 Dwarf_Error err2 = 0;
1911
1912 int resd = is_info?
1913 _dwarf_load_debug_info(dbg, &err2):
1914 _dwarf_load_debug_types(dbg,&err2);
1915 if (resd == DW_DLV_ERROR) {
1916 if (reloc_incomplete(resd,err2)) {
1917 /* We will assume all is ok, though it is not.
1918 Relocation errors need not be fatal. */
1919 char msg_buf[300];
1920 char *dwerrmsg = 0;
1921 char *msgprefix =
1922 "Relocations did not complete successfully, "
1923 "but we are " " ignoring error: ";
1924 size_t totallen = 0;
1925 size_t prefixlen = 0;
1926
1927 dwerrmsg = dwarf_errmsg(err2);
1928 prefixlen = strlen(msgprefix);
1929 totallen = prefixlen + strlen(dwerrmsg);
1930 if ( totallen >= sizeof(msg_buf)) {
1931 const char *m= "Error:corrupted dwarf message table!";
1932 /* Impossible unless something corrupted.
1933 Provide a shorter dwerrmsg*/
1934 _dwarf_safe_strcpy(msg_buf,sizeof(msg_buf),
1935 m,strlen(m));
1936 } else {
1937 _dwarf_safe_strcpy(msg_buf,sizeof(msg_buf),
1938 msgprefix,prefixlen);
1939 _dwarf_safe_strcpy(msg_buf +prefixlen,
1940 sizeof(msg_buf)-prefixlen,
1941 dwerrmsg,strlen(dwerrmsg));
1942 }
1943 dwarf_insert_harmless_error(dbg,msg_buf);
1944 /* Fall thru to use the newly loaded section.
1945 even though it might not be adequately
1946 relocated. */
1947 dwarf_dealloc_error(dbg,err2);
1948 if (error) {
1949 *error = 0;
1950 }
1951 return DW_DLV_OK;
1952 }
1953 if (error) {
1954 *error = err2;
1955 } else {
1956 dwarf_dealloc_error(dbg,err2);
1957 }
1958 return DW_DLV_ERROR;
1959 }
1960 return resd;
1961 }
1962
1963 int
1964 _dwarf_next_cu_header_internal(Dwarf_Debug dbg,
1965 Dwarf_Bool is_info,
1966 Dwarf_Die * cu_die_out,
1967 Dwarf_Unsigned * cu_header_length,
1968 Dwarf_Half * version_stamp,
1969 Dwarf_Unsigned * abbrev_offset,
1970 Dwarf_Half * address_size,
1971 Dwarf_Half * offset_size,
1972 Dwarf_Half * extension_size,
1973 Dwarf_Sig8 * signature_out,
1974 Dwarf_Bool * has_signature,
1975 Dwarf_Unsigned *typeoffset,
1976 Dwarf_Unsigned * next_cu_offset,
1977
1978 /* header_type: DW_UT_compile, DW_UT_partial,
1979 DW_UT_type, returned through the pointer.
1980 A new item in DWARF5, synthesized for earlier DWARF
1981 CUs (& TUs). */
1982 Dwarf_Half * header_type,
1983 Dwarf_Error * error)
1984 {
1985 /* Offset for current and new CU. */
1986 Dwarf_Unsigned new_offset = 0;
1987 Dwarf_Die local_cudie = 0;
1988
1989 /* CU Context for current CU. */
1990 Dwarf_CU_Context cu_context = 0;
1991 Dwarf_Debug_InfoTypes dis = 0;
1992 Dwarf_Unsigned section_size = 0;
1993 Dwarf_Small *dataptr = 0;
1994 struct Dwarf_Section_s *secdp = 0;
1995 int res = 0;
1996
1997 /* ***** BEGIN CODE ***** */
1998
1999 CHECK_DBG(dbg,error,"dwarf_next_cuheader_[d,e]()");
2000 if (is_info) {
2001 dis =&dbg->de_info_reading;
2002 dataptr = dbg->de_debug_info.dss_data;
2003 secdp = &dbg->de_debug_info;
2004 } else {
2005 dis =&dbg->de_types_reading;
2006 dataptr = dbg->de_debug_types.dss_data;
2007 secdp = &dbg->de_debug_types;
2008 }
2009
2010 if (!dataptr) {
2011 res = _dwarf_load_die_containing_section(dbg,
2012 is_info, error);
2013 if (res != DW_DLV_OK) {
2014 return res;
2015 }
2016 }
2017 if (!dis->de_cu_context) {
2018 /* We are leaving new_offset zero. We are at the
2019 start of a section. */
2020 new_offset = 0;
2021 } else {
2022 new_offset = _dwarf_calculate_next_cu_context_offset(
2023 dis->de_cu_context);
2024 }
2025
2026 /* Check that there is room in .debug_info beyond
2027 the new offset for at least a new cu header.
2028 If not, return DW_DLV_NO_ENTRY to indicate end
2029 of debug_info section, and reset
2030 de_cu_debug_info_offset to
2031 enable looping back through the cu's. */
2032 section_size = secdp->dss_size;
2033 if ((new_offset +
2034 _dwarf_length_of_cu_header_simple(dbg,is_info)) >=
2035 section_size) {
2036 /* We must reset as we will not create a proper
2037 de_cu_context here, see comment just above. */
2038 dis->de_cu_context = NULL;
2039 return DW_DLV_NO_ENTRY;
2040 }
2041
2042 /* Check if this CU has been read before. */
2043 cu_context = _dwarf_find_CU_Context(dbg, new_offset,is_info);
2044
2045 /* If not, make CU Context for it. */
2046 if (!cu_context) {
2047 res = _dwarf_create_a_new_cu_context_record_on_list(
2048 dbg,dis,is_info,section_size,new_offset,
2049 &cu_context,&local_cudie,error);
2050 if (res != DW_DLV_OK) {
2051 return res;
2052 }
2053 }
2054 /* Next assignment is what makes
2055 _dwarf_next_cu_header_d()
2056 with no offset presented work to march
2057 through all the CUs in order. Other places
2058 creating a cu_context do not set de_cu_context.
2059 if callers use dwarf_next_cu_header_e() this
2060 is unimportant but not harmful. */
2061 dis->de_cu_context = cu_context;
2062 if (cu_header_length) {
2063 *cu_header_length = cu_context->cc_length;
2064 }
2065 if (version_stamp) {
2066 *version_stamp = cu_context->cc_version_stamp;
2067 }
2068 if (abbrev_offset) {
2069 *abbrev_offset = cu_context->cc_abbrev_offset;
2070 }
2071 if (address_size) {
2072 *address_size = cu_context->cc_address_size;
2073 }
2074 if (offset_size) {
2075 *offset_size = cu_context->cc_length_size;
2076 }
2077 if (extension_size) {
2078 *extension_size = cu_context->cc_extension_size;
2079 }
2080 if (header_type) {
2081 *header_type = cu_context->cc_unit_type;
2082 }
2083 if (typeoffset) {
2084 *typeoffset = cu_context->cc_signature_offset;
2085 }
2086 if (signature_out) {
2087 *signature_out = cu_context->cc_signature;
2088 }
2089 if (has_signature) {
2090 *has_signature = cu_context->cc_signature_present;
2091 }
2092 /* Determine the offset of the next CU. */
2093 new_offset = new_offset + cu_context->cc_length +
2094 cu_context->cc_length_size + cu_context->cc_extension_size;
2095 /* Allowing null argument starting 22 April 2019. */
2096 if (next_cu_offset) {
2097 *next_cu_offset = new_offset;
2098 }
2099 {
2100 Dwarf_Debug tieddbg = 0;
2101 int tres = DW_DLV_OK;
2102 tieddbg = dbg->de_secondary_dbg;
2103 if (DBG_IS_PRIMARY(dbg) && DBG_IS_SECONDARY(tieddbg)) {
2104 /* We are in the main, merge tied
2105 into main cu_context */
2106 tres = _dwarf_merge_all_base_attrs_of_cu_die(
2107 cu_context,
2108 tieddbg,
2109 0 /* we do not want the context returned */,
2110 error);
2111 } /* Else no merge */
2112 if (tres == DW_DLV_ERROR && error) {
2113 /* We'll assume any errors will be
2114 discovered later. Lets get our
2115 finished.
2116 if error NULL it's a caller issue
2117 and there is nothing we can do here */
2118 dwarf_dealloc_error(dbg,*error);
2119 *error = 0;
2120 }
2121 }
2122 if (cu_die_out) {
2123 if (!local_cudie) {
2124 /* This is safe since we know the
2125 correct cu_context */
2126 res = _dwarf_siblingof_internal(dbg,NULL,
2127 cu_context, is_info,&local_cudie,error);
2128 if (res != DW_DLV_OK) {
2129 return res;
2130 }
2131 *cu_die_out = local_cudie;
2132 } else {
2133 *cu_die_out = local_cudie;
2134 }
2135 } else {
2136 if (local_cudie) {
2137 dwarf_dealloc_die(local_cudie);
2138 }
2139 }
2140 return DW_DLV_OK;
2141 }
2142
2143 /* This involves data in a split dwarf or package file.
2144
2145 Given hash signature, return the CU_die of the applicable CU.
2146 The hash is assumed to be from 'somewhere'.
2147 For DWARF 4:
2148 From a skeleton DIE DW_AT_GNU_dwo_id ("cu" case) or
2149 From a DW_FORM_ref_sig8 ("tu" case).
2150 For DWARF5:
2151 From dwo_id in a skeleton CU header (DW_UT_skeleton).
2152 From a DW_FORM_ref_sig8 ("tu" case).
2153
2154 If "tu" request, the CU_die
2155 of of the type unit.
2156 Works on either a dwp package file or a dwo object.
2157
2158 If "cu" request, the CU_die
2159 of the compilation unit.
2160 Works on either a dwp package file or a dwo object.
2161
2162 If the hash passed is not present, returns DW_DLV_NO_ENTRY
2163 (but read the next two paragraphs for more detail).
2164
2165 If a dwp package file with the hash signature
2166 is present in the applicable index but no matching
2167 compilation unit can be found, it returns DW_DLV_ERROR.
2168
2169 If a .dwo object there is no index and we look at the
2170 compilation units (possibly all of them). If not present
2171 then we return DW_DLV_NO_ENTRY.
2172
2173 The returned_die is a CU DIE if the sig_type is "cu".
2174 The returned_die is a type DIE if the sig_type is "tu".
2175 Perhaps both should return CU die.
2176
2177 New 27 April, 2015
2178 */
2179 int
2180 dwarf_die_from_hash_signature(Dwarf_Debug dbg,
2181 Dwarf_Sig8 * hash_sig,
2182 const char * sig_type /* "tu" or "cu"*/,
2183 Dwarf_Die * returned_die,
2184 Dwarf_Error* error)
2185 {
2186 Dwarf_Bool is_type_unit = FALSE;
2187 int sres = 0;
2188
2189 CHECK_DBG(dbg,error,"dwarf_die_from_hash_signature()");
2190 sres = _dwarf_load_debug_info(dbg,error);
2191 if (sres == DW_DLV_ERROR) {
2192 return sres;
2193 }
2194 sres = _dwarf_load_debug_types(dbg,error);
2195 if (sres == DW_DLV_ERROR) {
2196 return sres;
2197 }
2198
2199 if (!strcmp(sig_type,"tu")) {
2200 is_type_unit = TRUE;
2201 } else if (!strcmp(sig_type,"cu")) {
2202 is_type_unit = FALSE;
2203 } else {
2204 _dwarf_error(dbg,error,DW_DLE_SIG_TYPE_WRONG_STRING);
2205 return DW_DLV_ERROR;
2206 }
2207
2208 if (_dwarf_file_has_debug_fission_index(dbg)) {
2209 /* This is a dwp package file. */
2210 int fisres = 0;
2211 Dwarf_Bool is_info2 = TRUE;
2212 Dwarf_Off cu_header_off = 0;
2213 Dwarf_Off cu_size = 0;
2214 Dwarf_Off cu_die_off = 0;
2215 Dwarf_Off typeoffset = 0;
2216 Dwarf_Die cudie = 0;
2217 Dwarf_Die typedie = 0;
2218 Dwarf_CU_Context context = 0;
2219 Dwarf_Debug_Fission_Per_CU fiss;
2220
2221 memset(&fiss,0,sizeof(fiss));
2222 fisres = dwarf_get_debugfission_for_key(dbg,hash_sig,
2223 sig_type,&fiss,error);
2224 if (fisres != DW_DLV_OK) {
2225 return fisres;
2226 }
2227 /* Found it */
2228 if (is_type_unit) {
2229 /* DW4 has debug_types, so look in .debug_types
2230 Else look in .debug_info. */
2231 is_info2 = dbg->de_debug_types.dss_size?FALSE:TRUE;
2232 } else {
2233 is_info2 = TRUE;
2234 }
2235
2236 cu_header_off = _dwarf_get_dwp_extra_offset(&fiss,
2237 is_info2?DW_SECT_INFO:DW_SECT_TYPES,
2238 &cu_size);
2239
2240 fisres = dwarf_get_cu_die_offset_given_cu_header_offset_b(
2241 dbg,cu_header_off,
2242 is_info2,
2243 &cu_die_off,error);
2244 if (fisres != DW_DLV_OK) {
2245 return fisres;
2246 }
2247 fisres = dwarf_offdie_b(dbg,cu_die_off,is_info2,
2248 &cudie,error);
2249 if (fisres != DW_DLV_OK) {
2250 return fisres;
2251 }
2252 if (!is_type_unit) {
2253 *returned_die = cudie;
2254 return DW_DLV_OK;
2255 }
2256 context = cudie->di_cu_context;
2257 typeoffset = context->cc_signature_offset;
2258 typeoffset += cu_header_off;
2259 fisres = dwarf_offdie_b(dbg,typeoffset,is_info2,
2260 &typedie,error);
2261 if (fisres != DW_DLV_OK) {
2262 dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
2263 return fisres;
2264 }
2265 *returned_die = typedie;
2266 dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
2267 return DW_DLV_OK;
2268 }
2269 /* Look thru all the CUs, there is no DWP tu/cu index.
2270 There will be COMDAT sections for the type TUs
2271 (DW_UT_type).
2272 A single non-comdat for the DW_UT_compile. */
2273 /* FIXME: DW_DLE_DEBUG_FISSION_INCOMPLETE */
2274 _dwarf_error(dbg,error,DW_DLE_DEBUG_FISSION_INCOMPLETE);
2275 return DW_DLV_ERROR;
2276 }
2277
2278 static int
2279 _dwarf_ptr_CU_offset(Dwarf_CU_Context cu_context,
2280 Dwarf_Byte_Ptr di_ptr,
2281 Dwarf_Bool is_info,
2282 Dwarf_Off * cu_off)
2283 {
2284 Dwarf_Debug dbg = cu_context->cc_dbg;
2285 Dwarf_Small *dataptr = is_info? dbg->de_debug_info.dss_data:
2286 dbg->de_debug_types.dss_data;
2287 *cu_off = (di_ptr - dataptr);
2288 return DW_DLV_OK;
2289 }
2290 #if 0 /* print_sib_offset() for debugging */
2291 /* Just for debug purposes */
2292 void print_sib_offset(Dwarf_Die sibling)
2293 {
2294 Dwarf_Off sib_off;
2295 Dwarf_Error error;
2296 dwarf_dieoffset(sibling,&sib_off,&error);
2297 fprintf(stderr," SIB OFF = 0x%" DW_PR_XZEROS DW_PR_DUx,sib_off);
2298 }
2299 void print_ptr_offset(Dwarf_CU_Context cu_context,
2300 Dwarf_Byte_Ptr di_ptr)
2301 {
2302 Dwarf_Off ptr_off;
2303 _dwarf_ptr_CU_offset(cu_context,di_ptr,
2304 di_ptr->di_is_info,&ptr_off);
2305 fprintf(stderr," PTR OFF = 0x%" DW_PR_XZEROS DW_PR_DUx,ptr_off);
2306 }
2307 #endif /*0*/
2308
2309 /* Validate the sibling DIE. This only makes sense to call
2310 if the sibling's DIEs have been travsersed and
2311 dwarf_child() called on each,
2312 so that the last DIE dwarf_child saw was the last.
2313 Essentially ensuring that (after such traversal) that we
2314 are in the same place a sibling attribute would identify.
2315 In case we return DW_DLV_ERROR, the global offset of the last
2316 DIE traversed by dwarf_child is returned through *offset
2317
2318 It is essentially guaranteed that dbg->de_last_die
2319 is a stale DIE pointer of a deallocated DIE when we get here.
2320 It must not be used as a DIE pointer here,
2321 just as a sort of anonymous pointer that we just check against
2322 NULL.
2323
2324 There is a (subtle?) dependence on the fact that when we
2325 call this the last dwarf_child() call would have been for
2326 this sibling.
2327 Meaning that this works in a depth-first
2328 traversal even though there
2329 is no stack of 'de_last_die' values.
2330
2331 The check for dbg->de_last_die just ensures sanity.
2332
2333 If one is switching between normal debug_frame and eh_frame
2334 (traversing them in tandem, let us say) in a single
2335 Dwarf_Debug this validator makes no sense.
2336 It works if one processes a .debug_frame (entirely) and
2337 then an eh_frame (or vice versa) though.
2338 Use caution.
2339 */
2340 int
2341 dwarf_validate_die_sibling(Dwarf_Die sibling,Dwarf_Off *offset)
2342 {
2343 Dwarf_Debug dbg = 0;
2344 Dwarf_Error *error = 0;
2345 Dwarf_Debug_InfoTypes dis = 0;
2346
2347 CHECK_DIE(sibling, DW_DLV_ERROR);
2348 dbg = sibling->di_cu_context->cc_dbg;
2349 dis = sibling->di_is_info?
2350 &dbg->de_info_reading: &dbg->de_types_reading;
2351 *offset = 0;
2352 if (dis->de_last_die && dis->de_last_di_ptr) {
2353 if (sibling->di_debug_ptr == dis->de_last_di_ptr) {
2354 return DW_DLV_OK;
2355 }
2356 }
2357 /* Calculate global offset used for error reporting */
2358 _dwarf_ptr_CU_offset(sibling->di_cu_context,
2359 dis->de_last_di_ptr,sibling->di_is_info,offset);
2360 return DW_DLV_ERROR;
2361 }
2362
2363 /* This function does two slightly different things
2364 depending on the input flag want_AT_sibling. If
2365 this flag is TRUE, it checks if the input die has
2366 a DW_AT_sibling attribute. If it does it returns
2367 a pointer to the start of the sibling die in the
2368 .debug_info section. Otherwise it behaves the
2369 same as the want_AT_sibling FALSE case.
2370
2371 If the want_AT_sibling flag is FALSE, it returns
2372 a pointer to the immediately adjacent die in the
2373 .debug_info section.
2374
2375 Die_info_end points to the end of the .debug_info
2376 portion for the cu the die belongs to. It is used
2377 to check that the search for the next die does not
2378 cross the end of the current cu. Cu_info_start points
2379 to the start of the .debug_info portion for the
2380 current cu, and is used to add to the offset for
2381 DW_AT_sibling attributes. Finally, has_die_child
2382 is a pointer to a Dwarf_Bool that is set TRUE if
2383 the present die has children, FALSE otherwise.
2384 However, in case want_AT_child is TRUE and the die
2385 has a DW_AT_sibling attribute *has_die_child is set
2386 FALSE to indicate that the children are being skipped.
2387
2388 die_info_end points to the last byte+1 of the cu. */
2389 static int
2390 _dwarf_next_die_info_ptr(Dwarf_Byte_Ptr die_info_ptr,
2391 Dwarf_CU_Context cu_context,
2392 Dwarf_Byte_Ptr die_info_end,
2393 Dwarf_Byte_Ptr cu_info_start,
2394 Dwarf_Bool want_AT_sibling,
2395 Dwarf_Bool * has_die_child,
2396 Dwarf_Byte_Ptr *next_die_ptr_out,
2397 Dwarf_Error *error)
2398 {
2399 Dwarf_Byte_Ptr info_ptr = 0;
2400 Dwarf_Byte_Ptr abbrev_ptr = 0;
2401 Dwarf_Unsigned abbrev_code = 0;
2402 Dwarf_Abbrev_List abbrev_list = 0;
2403 Dwarf_Unsigned offset = 0;
2404 Dwarf_Unsigned utmp = 0;
2405 Dwarf_Debug dbg = 0;
2406 Dwarf_Byte_Ptr abbrev_end = 0;
2407 int lres = 0;
2408 Dwarf_Unsigned i = 0;
2409 Dwarf_Unsigned highest_code = 0;
2410
2411 dbg = cu_context->cc_dbg;
2412 info_ptr = die_info_ptr;
2413 DECODE_LEB128_UWORD_CK(info_ptr, utmp,dbg,error,die_info_end);
2414 abbrev_code = (Dwarf_Unsigned) utmp;
2415 if (abbrev_code == 0) {
2416 /* Should never happen. Tested before we got here. */
2417 _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_PTR_NULL);
2418 return DW_DLV_ERROR;
2419 }
2420 lres = _dwarf_get_abbrev_for_code(cu_context, abbrev_code,
2421 &abbrev_list,&highest_code,error);
2422 if (lres == DW_DLV_ERROR) {
2423 return lres;
2424 }
2425 if (lres == DW_DLV_NO_ENTRY) {
2426 dwarfstring m;
2427
2428 dwarfstring_constructor(&m);
2429 dwarfstring_append_printf_u(&m,
2430 " DW_DLE_NEXT_DIE_NO_ABBREV_LIST "
2431 "There is no abbrev present for code %u"
2432 " in this compilation unit. ",
2433 abbrev_code);
2434 dwarfstring_append_printf_u(&m,
2435 "The highest known code in any "
2436 "compilation unit is %u.",
2437 highest_code);
2438 _dwarf_error_string(dbg, error,
2439 DW_DLE_NEXT_DIE_NO_ABBREV_LIST,
2440 dwarfstring_string(&m));
2441 dwarfstring_destructor(&m);
2442 return DW_DLV_ERROR;
2443 }
2444
2445 *has_die_child = abbrev_list->abl_has_child;
2446 abbrev_ptr = abbrev_list->abl_abbrev_ptr;
2447 abbrev_end = _dwarf_calculate_abbrev_section_end_ptr(cu_context);
2448
2449 if (!abbrev_list->abl_attr) {
2450 int bres = 0;
2451
2452 bres = _dwarf_fill_in_attr_form_abtable(cu_context,
2453 abbrev_ptr, abbrev_end, abbrev_list,
2454 error);
2455 if (bres != DW_DLV_OK) {
2456 return bres;
2457 }
2458 }
2459 /* ASSERT list->abl_addr and list->abl_form
2460 are non-null and if list->abl_implicit_const_count > 0
2461 list->abl_implicit_const is non-null. */
2462
2463 for ( i = 0; i <abbrev_list->abl_abbrev_count; ++i) {
2464 /* Dwarf_Signed implicit_const = 0; */
2465 Dwarf_Half attr = 0;
2466 Dwarf_Half attr_form = 0;
2467 int res = 0;
2468 Dwarf_Byte_Ptr next_die_ptr = 0;
2469
2470 attr = abbrev_list->abl_attr[i];
2471 attr_form = abbrev_list->abl_form[i];
2472 if (attr_form == DW_FORM_implicit_const) {
2473 /* implicit_const = abbrev_list->abl_implicit_const[i];*/
2474 } else if (attr_form == DW_FORM_indirect) {
2475 Dwarf_Unsigned utmp6;
2476 /* DECODE_LEB128_UWORD updates info_ptr */
2477 DECODE_LEB128_UWORD_CK(info_ptr, utmp6,dbg,error,
2478 die_info_end);
2479 attr_form = (Dwarf_Half) utmp6;
2480 if (attr_form == DW_FORM_implicit_const ||
2481 attr_form == DW_FORM_indirect) {
2482 _dwarf_error_string(dbg, error,
2483 DW_DLE_NEXT_DIE_WRONG_FORM,
2484 "DW_DLE_NEXT_DIE_WRONG_FORM: "
2485 " Reading Attriutes: an indirect "
2486 " or implicit_const form "
2487 "leads to one of the same. "
2488 "Which is not handled. Corrupt Dwarf");
2489 return DW_DLV_ERROR;
2490 }
2491 }
2492 if (attr_form == DW_FORM_implicit_const) {
2493 SKIP_LEB128_CK(abbrev_ptr,dbg,error, abbrev_end);
2494 }
2495
2496 if (want_AT_sibling && attr == DW_AT_sibling) {
2497 switch (attr_form) {
2498 case DW_FORM_ref1:
2499 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2500 info_ptr, sizeof(Dwarf_Small),
2501 error,die_info_end);
2502 break;
2503 case DW_FORM_ref2:
2504 /* READ_UNALIGNED does not update info_ptr */
2505 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2506 info_ptr,DWARF_HALF_SIZE,
2507 error,die_info_end);
2508 break;
2509 case DW_FORM_ref4:
2510 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2511 info_ptr, DWARF_32BIT_SIZE,
2512 error,die_info_end);
2513 break;
2514 case DW_FORM_ref8:
2515 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2516 info_ptr, DWARF_64BIT_SIZE,
2517 error,die_info_end);
2518 break;
2519 case DW_FORM_ref_udata:
2520 DECODE_LEB128_UWORD_CK(info_ptr, offset,
2521 dbg,error,die_info_end);
2522 break;
2523 case DW_FORM_ref_addr:
2524 /* Very unusual. The FORM is intended to refer to
2525 a different CU, but a different CU cannot
2526 be a sibling, can it?
2527 We could ignore this and treat as if no
2528 DW_AT_sibling
2529 present. Or derive the offset from it and if
2530 it is in the same CU use it directly.
2531 The offset here is *supposed* to be a
2532 global offset,
2533 so adding cu_info_start is wrong to any offset
2534 we find here unless cu_info_start
2535 is zero! Lets pretend there is no DW_AT_sibling
2536 attribute. */
2537 goto no_sibling_attr;
2538 default:
2539 _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_WRONG_FORM);
2540 return DW_DLV_ERROR;
2541 }
2542
2543 /* Reset *has_die_child to indicate children skipped. */
2544 *has_die_child = FALSE;
2545
2546 /* A value beyond die_info_end indicates an error.
2547 Exactly at die_info_end means 1-past-cu-end
2548 and simply means we
2549 are at the end, do not return error. Higher level
2550 will detect that we are at the end. */
2551 { /* Care required here. Offset can be garbage. */
2552 Dwarf_Unsigned plen = 0;
2553
2554 /* ptrdiff_t is generated but not named */
2555 plen = (die_info_end >= cu_info_start)?
2556 (die_info_end - cu_info_start):0;
2557 if (offset > plen) {
2558 /* Error case, bad DWARF. */
2559 _dwarf_error_string(dbg, error,
2560 DW_DLE_SIBLING_OFFSET_WRONG,
2561 "DW_DLE_SIBLING_OFFSET_WRONG "
2562 "the offset makes the new die ptr "
2563 "off the end of the section. Corrupt dwarf");
2564 return DW_DLV_ERROR;
2565 }
2566 }
2567 /* At or before end-of-cu */
2568 next_die_ptr = cu_info_start + offset;
2569 if (next_die_ptr <= die_info_ptr) {
2570 /* This is a fix for ossfuzz 57562 */
2571 dwarfstring m;
2572 dwarfstring_constructor(&m);
2573 dwarfstring_append_printf_u(&m,
2574 "DW_DLE_SIBLING_OFFSET_WRONG "
2575 "the DW_AT_sibling offset (%u) puts "
2576 "the sibling DIE ptr "
2577 "equal to or less then the current DIE ptr. "
2578 "Corrupt dwarf",offset);
2579 _dwarf_error_string(dbg, error,
2580 DW_DLE_SIBLING_OFFSET_WRONG,
2581 dwarfstring_string(&m));
2582 dwarfstring_destructor(&m);
2583 return DW_DLV_ERROR;
2584 }
2585 *next_die_ptr_out = next_die_ptr;
2586 return DW_DLV_OK;
2587 }
2588
2589 no_sibling_attr:
2590 if (attr_form != 0 && attr_form != DW_FORM_implicit_const) {
2591 Dwarf_Unsigned sizeofval = 0;
2592 Dwarf_Unsigned ssize = 0;
2593
2594 res = _dwarf_get_size_of_val(cu_context->cc_dbg,
2595 attr_form,
2596 cu_context->cc_version_stamp,
2597 cu_context->cc_address_size,
2598 info_ptr,
2599 cu_context->cc_length_size,
2600 &sizeofval,
2601 die_info_end,
2602 error);
2603 if (res != DW_DLV_OK) {
2604 return res;
2605 }
2606 /* It is ok for info_ptr == die_info_end, as we
2607 will test later before using a too-large info_ptr */
2608 /* ptrdiff_t is generated but not named */
2609 ssize = (die_info_end >= info_ptr)?
2610 (die_info_end - info_ptr): 0;
2611 if (sizeofval > ssize) {
2612 dwarfstring m;
2613
2614 dwarfstring_constructor(&m);
2615 dwarfstring_append_printf_u(&m,
2616 "DW_DLE_NEXT_DIE_PAST_END:"
2617 " the DIE value just checked is %u"
2618 " bytes long, and that would extend"
2619 " past the end of the section.",
2620 sizeofval);
2621 _dwarf_error_string(dbg, error,
2622 DW_DLE_NEXT_DIE_PAST_END,
2623 dwarfstring_string(&m));
2624 dwarfstring_destructor(&m);
2625 return DW_DLV_ERROR;
2626 }
2627 info_ptr += sizeofval;
2628 if (info_ptr > die_info_end) {
2629 dwarfstring m;
2630
2631 dwarfstring_constructor(&m);
2632 dwarfstring_append_printf_u(&m,
2633 "DW_DLE_NEXT_DIE_PAST_END:"
2634 " the DIE value just checked is %u"
2635 " bytes long, and puts us past"
2636 " the end of the section",
2637 sizeofval);
2638 dwarfstring_append_printf_u(&m,
2639 " which is 0x%x",
2640 (Dwarf_Unsigned)(uintptr_t)die_info_end);
2641 _dwarf_error_string(dbg, error,
2642 DW_DLE_NEXT_DIE_PAST_END,
2643 dwarfstring_string(&m));
2644 dwarfstring_destructor(&m);
2645 /* More than one-past-end indicates a bug somewhere,
2646 likely bad dwarf generation. */
2647 return DW_DLV_ERROR;
2648 }
2649 }
2650 }
2651 *next_die_ptr_out = info_ptr;
2652 return DW_DLV_OK;
2653 }
2654
2655 /* Multiple TAGs are in fact compile units.
2656 Allow them all.
2657 Return non-zero if a CU tag.
2658 Else return 0.
2659 */
2660 static int
2661 is_cu_tag(int t)
2662 {
2663 if (t == DW_TAG_compile_unit ||
2664 t == DW_TAG_partial_unit ||
2665 t == DW_TAG_skeleton_unit ||
2666 t == DW_TAG_type_unit) {
2667 return 1;
2668 }
2669 return 0;
2670 }
2671
2672 /* Given a Dwarf_Debug dbg, and a Dwarf_Die die, it returns
2673 a Dwarf_Die for the sibling of die. In case die is NULL,
2674 it returns (thru ptr) a Dwarf_Die for the first die in the current
2675 cu in dbg. Returns DW_DLV_ERROR on error.
2676
2677 It is assumed that every sibling chain including those with
2678 only one element is terminated with a NULL die, except a
2679 chain with only a NULL die.
2680
2681 The algorithm moves from one die to the adjacent one. It
2682 returns when the depth of children it sees equals the number
2683 of sibling chain terminations. A single count, child_depth
2684 is used to track the depth of children and sibling terminations
2685 encountered. Child_depth is incremented when a die has the
2686 Has-Child flag set unless the child happens to be a NULL die.
2687 Child_depth is decremented when a die has Has-Child FALSE,
2688 and the adjacent die is NULL. Algorithm returns when
2689 child_depth is 0.
2690
2691 **NOTE: Do not modify input die, since it is used at the end.
2692
2693 * This is the correct form. On calling with 'die' NULL,
2694 we cannot tell if this is debug_info or debug_types, so
2695 we must be informed!. */
2696 int
2697 dwarf_siblingof_b(Dwarf_Debug dbg,
2698 Dwarf_Die die,
2699 Dwarf_Bool is_info,
2700 Dwarf_Die *caller_ret_die,
2701 Dwarf_Error *error)
2702 {
2703 int res = 0;
2704 Dwarf_CU_Context context = 0;
2705
2706 CHECK_DBG(dbg,error,"dwarf_siblingof_b()");
2707 if (die) {
2708 CHECK_DIE(die,DW_DLV_ERROR);
2709 context = die->di_cu_context;
2710 /* Ignore is_info passed-in, we have the correct
2711 value in cu_context. */
2712 is_info = die->di_cu_context->cc_is_info;
2713 } else {
2714 /* This is the pre-0.9.0 way, and is assuming
2715 that the 'dis' has the correct cu context.
2716 Which might not be TRUE if a caller
2717 used dwarf_next_cu_header_d() twice in a
2718 row before calling dwarf_siblingof_b().
2719 Use dwarf_next_cu_header_e() instead of
2720 dwarf_next_cu_header_d() */
2721 context = is_info? dbg->de_info_reading.de_cu_context:
2722 dbg->de_types_reading.de_cu_context;
2723 }
2724 res = _dwarf_siblingof_internal(dbg,die,
2725 context, is_info,caller_ret_die,error);
2726 return res;
2727 }
2728
2729 int
2730 dwarf_siblingof_c(Dwarf_Die die,
2731 Dwarf_Die * caller_ret_die, Dwarf_Error * error)
2732 {
2733 int res = 0;
2734 Dwarf_Debug dbg = 0;
2735 Dwarf_Bool is_info = FALSE;
2736
2737 CHECK_DIE(die,DW_DLV_ERROR);
2738 dbg = die->di_cu_context->cc_dbg;
2739 is_info = die->di_cu_context->cc_is_info;
2740 res = _dwarf_siblingof_internal(dbg,die,
2741 die->di_cu_context, is_info,
2742 caller_ret_die,error);
2743 return res;
2744 }
2745
2746 static int
2747 dw_start_load_root_die(Dwarf_Debug dbg,
2748 Dwarf_CU_Context context,
2749 Dwarf_Bool is_info,
2750 Dwarf_Small *dataptr,
2751 Dwarf_Byte_Ptr *die_info_ptr,
2752 Dwarf_Byte_Ptr *die_info_end,
2753 Dwarf_Error *error)
2754 {
2755 /* Find root die of cu */
2756 /* die_info_end is untouched here, need not be set in this
2757 branch. */
2758 Dwarf_Off off2 = 0;
2759 Dwarf_Unsigned headerlen = 0;
2760 Dwarf_Byte_Ptr cu_info_start = 0;
2761 int cres = 0;
2762
2763 /* If we've not loaded debug_info
2764 context will be NULL. */
2765 if (!context) {
2766 local_dealloc_cu_context(dbg,context);
2767 _dwarf_error_string(dbg,error,
2768 DW_DLE_DBG_NO_CU_CONTEXT,
2769 "DW_DLE_DBG_NO_CU_CONTEXT:"
2770 " Setting up a new CU failed loading root die");
2771 return DW_DLV_ERROR;
2772 }
2773 off2 = context->cc_debug_offset;
2774 cu_info_start = dataptr + off2;
2775 cres = _dwarf_length_of_cu_header(dbg, off2,is_info,
2776 &headerlen,error);
2777 if (cres != DW_DLV_OK) {
2778 return cres;
2779 }
2780 *die_info_ptr = cu_info_start + headerlen;
2781 *die_info_end = _dwarf_calculate_info_section_end_ptr(context);
2782
2783 /* Recording the CU die pointer so we can later access
2784 for special FORMs relating to .debug_str_offsets
2785 and .debug_addr */
2786 context->cc_cu_die_offset_present = TRUE;
2787 context->cc_cu_die_global_sec_offset = off2 + headerlen;
2788
2789 return DW_DLV_OK;
2790 }
2791
2792 static int
2793 _dwarf_siblingof_internal(Dwarf_Debug dbg,
2794 Dwarf_Die die,
2795 Dwarf_CU_Context context,
2796 Dwarf_Bool is_info,
2797 Dwarf_Die * caller_ret_die, Dwarf_Error * error)
2798 {
2799 Dwarf_Die ret_die = 0;
2800 Dwarf_Byte_Ptr die_info_ptr = 0;
2801 Dwarf_Byte_Ptr cu_info_start = 0;
2802
2803 /* die_info_end points 1-past end of die (once set) */
2804 Dwarf_Byte_Ptr die_info_end = 0;
2805 Dwarf_Unsigned abbrev_code = 0;
2806 Dwarf_Unsigned utmp = 0;
2807 Dwarf_Unsigned highest_code = 0;
2808 int lres = 0;
2809 int dieres = 0;
2810 /* Since die may be NULL, we rely on the input argument. */
2811 Dwarf_Small *dataptr = 0;
2812
2813 if (IS_INVALID_DBG(dbg)) {
2814 _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
2815 return DW_DLV_ERROR;
2816 }
2817 dataptr = is_info? dbg->de_debug_info.dss_data:
2818 dbg->de_debug_types.dss_data;
2819 if (!dataptr) {
2820 return DW_DLV_NO_ENTRY;
2821 }
2822 if (!die) {
2823 dieres = dw_start_load_root_die(dbg,context,is_info,
2824 dataptr,&die_info_ptr,&die_info_end,error);
2825 if (dieres != DW_DLV_OK) {
2826 return dieres;
2827 }
2828 } else {
2829 /* Find sibling die. */
2830 Dwarf_Bool has_child = FALSE;
2831 Dwarf_Signed child_depth = 0;
2832
2833 /* We cannot have a legal die unless debug_info
2834 was loaded, so
2835 no need to load debug_info here. */
2836 CHECK_DIE(die, DW_DLV_ERROR);
2837
2838 die_info_ptr = die->di_debug_ptr;
2839 if (*die_info_ptr == 0) {
2840 return DW_DLV_NO_ENTRY;
2841 }
2842 context = die->di_cu_context;
2843 cu_info_start = dataptr+ context->cc_debug_offset;
2844 die_info_end = _dwarf_calculate_info_section_end_ptr(context);
2845
2846 if ((*die_info_ptr) == 0) {
2847 return DW_DLV_NO_ENTRY;
2848 }
2849 child_depth = 0;
2850 do {
2851 int res2 = 0;
2852 Dwarf_Byte_Ptr die_info_ptr2 = 0;
2853
2854 res2 = _dwarf_next_die_info_ptr(die_info_ptr,
2855 context, die_info_end,
2856 cu_info_start, TRUE, &has_child,
2857 &die_info_ptr2,
2858 error);
2859 if (res2 != DW_DLV_OK) {
2860 return res2;
2861 }
2862 if (die_info_ptr2 == die_info_ptr) {
2863 /* There is something very wrong, our die value
2864 unchanged. Bad DWARF. */
2865 dwarfstring m;
2866
2867 dwarfstring_constructor(&m);
2868 dwarfstring_append_printf_u(&m,
2869 "DW_DLE_NEXT_DIE_LOW_ERROR: "
2870 "Somehow the next die pointer 0x%x",
2871 (Dwarf_Unsigned)(uintptr_t)die_info_ptr2);
2872 dwarfstring_append_printf_u(&m,
2873 " points before the current die "
2874 "pointer 0x%x so an "
2875 "overflow of some sort happened",
2876 (Dwarf_Unsigned)(uintptr_t)die_info_ptr);
2877 _dwarf_error_string(dbg, error,
2878 DW_DLE_NEXT_DIE_LOW_ERROR,
2879 dwarfstring_string(&m));
2880 dwarfstring_destructor(&m);
2881 return DW_DLV_ERROR;
2882 }
2883 if (die_info_ptr2 < die_info_ptr) {
2884 /* There is something very wrong, our die value
2885 decreased. Bad DWARF. */
2886 dwarfstring m;
2887
2888 dwarfstring_constructor(&m);
2889 dwarfstring_append_printf_u(&m,
2890 "DW_DLE_NEXT_DIE_LOW_ERROR: "
2891 "Somehow the next die pointer 0x%x",
2892 (Dwarf_Unsigned)(uintptr_t)die_info_ptr2);
2893 dwarfstring_append_printf_u(&m,
2894 " points before the current die "
2895 "pointer 0x%x so an "
2896 "overflow of some sort happened",
2897 (Dwarf_Unsigned)(uintptr_t)die_info_ptr);
2898 _dwarf_error_string(dbg, error,
2899 DW_DLE_NEXT_DIE_LOW_ERROR,
2900 dwarfstring_string(&m));
2901 dwarfstring_destructor(&m);
2902 return DW_DLV_ERROR;
2903 }
2904 if (die_info_ptr2 > die_info_end) {
2905 dwarfstring m;
2906
2907 dwarfstring_constructor(&m);
2908 dwarfstring_append_printf_u(&m,
2909 "DW_DLE_NEXT_DIE_PAST_END: "
2910 "the next DIE at 0x%x",
2911 (Dwarf_Unsigned)(uintptr_t)die_info_ptr2);
2912 dwarfstring_append_printf_u(&m,
2913 " would be past "
2914 " the end of the section (0x%x),"
2915 " which is an error.",
2916 (Dwarf_Unsigned)(uintptr_t)die_info_end);
2917 _dwarf_error_string(dbg, error,
2918 DW_DLE_NEXT_DIE_PAST_END,
2919 dwarfstring_string(&m));
2920 dwarfstring_destructor(&m);
2921 return DW_DLV_ERROR;
2922 }
2923 die_info_ptr = die_info_ptr2;
2924
2925 /* die_info_end is one past end. Do not read it!
2926 A test for '!= die_info_end' would work as well,
2927 but perhaps < reads more like the meaning. */
2928 if (die_info_ptr < die_info_end) {
2929 if ((*die_info_ptr) == 0 && has_child) {
2930 die_info_ptr++;
2931 has_child = FALSE;
2932 }
2933 }
2934
2935 /* die_info_ptr can be one-past-end. */
2936 if ((die_info_ptr == die_info_end) ||
2937 ((*die_info_ptr) == 0)) {
2938 /* We are at the end of a sibling list.
2939 get back to the next containing
2940 sibling list (looking for a libling
2941 list with more on it).
2942 */
2943 for (;;) {
2944 if (child_depth == 0) {
2945 /* Meaning there is no outer list,
2946 so stop. */
2947 break;
2948 }
2949 if (die_info_ptr == die_info_end) {
2950 /* September 2016: do not deref
2951 if we are past end.
2952 If we are at end at this point
2953 it means the sibling list
2954 inside this CU is not properly
2955 terminated.
2956 August 2019:
2957 We used to declare an error,
2958 DW_DLE_SIBLING_LIST_IMPROPER but
2959 now we just silently
2960 declare this is the end of the list.
2961 Each level of a sibling nest should
2962 have a single NUL byte, but here
2963 things are wrong, the DWARF
2964 is corrupt. */
2965 return DW_DLV_NO_ENTRY;
2966 }
2967 if (*die_info_ptr) {
2968 /* We have a real sibling. */
2969 break;
2970 }
2971 /* Move out one DIE level.
2972 Move past NUL byte marking end of
2973 this sibling list. */
2974 child_depth--;
2975 die_info_ptr++;
2976 }
2977 } else {
2978 child_depth = has_child ?
2979 child_depth + 1 : child_depth;
2980 }
2981 } while (child_depth != 0);
2982 }
2983 /* die_info_ptr > die_info_end is really a bug (possibly in dwarf
2984 generation)(but we are past end, no more DIEs here), whereas
2985 die_info_ptr == die_info_end means 'one past end, no more DIEs
2986 here'. */
2987 if (die_info_ptr >= die_info_end) {
2988 return DW_DLV_NO_ENTRY;
2989 }
2990 if ((*die_info_ptr) == 0) {
2991 /* We are not at the end of the section, but a
2992 valid DIE will not start with a zero byte.
2993 We will just assume it is a padding byte and is
2994 not an error. An error report will appear
2995 later if actually reading DIEs*/
2996 return DW_DLV_NO_ENTRY;
2997 }
2998 ret_die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
2999 if (!ret_die) {
3000 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
3001 return DW_DLV_ERROR;
3002 }
3003
3004 ret_die->di_is_info = is_info;
3005 ret_die->di_debug_ptr = die_info_ptr;
3006 ret_die->di_cu_context =
3007 die == NULL ? context : die->di_cu_context;
3008 dieres = _dwarf_leb128_uword_wrapper(dbg,
3009 &die_info_ptr,die_info_end,&utmp,error);
3010 if (dieres == DW_DLV_ERROR) {
3011 dwarf_dealloc_die(ret_die);
3012 return dieres;
3013 }
3014 if (die_info_ptr > die_info_end) {
3015 /* We managed to go past the end of the CU!.
3016 Something is badly wrong. */
3017 dwarf_dealloc_die(ret_die);
3018 _dwarf_error(dbg, error, DW_DLE_ABBREV_DECODE_ERROR);
3019 return DW_DLV_ERROR;
3020 }
3021 abbrev_code = utmp;
3022 if (abbrev_code == 0) {
3023 /* Zero means a null DIE */
3024 dwarf_dealloc_die(ret_die);
3025 return DW_DLV_NO_ENTRY;
3026 }
3027 ret_die->di_abbrev_code = abbrev_code;
3028 lres = _dwarf_get_abbrev_for_code(ret_die->di_cu_context,
3029 abbrev_code,
3030 &ret_die->di_abbrev_list,
3031 &highest_code,error);
3032 if (lres == DW_DLV_ERROR) {
3033 dwarf_dealloc_die(ret_die);
3034 return lres;
3035 }
3036 if (lres == DW_DLV_NO_ENTRY) {
3037 dwarfstring m;
3038 char buf[130];
3039
3040 buf[0] = 0;
3041 dwarfstring_constructor_static(&m,buf,sizeof(buf));
3042 dwarf_dealloc_die(ret_die);
3043 dwarfstring_append_printf_u(&m,
3044 "DW_DLE_DIE_ABBREV_LIST_NULL: "
3045 "There is no abbrev present for code %u"
3046 " in this compilation unit. ",
3047 abbrev_code);
3048 dwarfstring_append_printf_u(&m,
3049 "The highest known code"
3050 " in any compilation unit is %u .",
3051 highest_code);
3052 _dwarf_error_string(dbg, error,
3053 DW_DLE_DIE_ABBREV_LIST_NULL,dwarfstring_string(&m));
3054 dwarfstring_destructor(&m);
3055 return DW_DLV_ERROR;
3056 }
3057 if (!ret_die->di_abbrev_list->abl_attr) {
3058 int bres = 0;
3059 Dwarf_Byte_Ptr abbrev_ptr =
3060 ret_die->di_abbrev_list->abl_abbrev_ptr;
3061 Dwarf_Byte_Ptr abbrev_end =
3062 _dwarf_calculate_abbrev_section_end_ptr(
3063 ret_die->di_cu_context);
3064 bres = _dwarf_fill_in_attr_form_abtable(
3065 ret_die->di_cu_context,
3066 abbrev_ptr,
3067 abbrev_end,
3068 ret_die->di_abbrev_list,
3069 error);
3070 if (bres != DW_DLV_OK) {
3071 dwarf_dealloc_die(ret_die);
3072 return bres;
3073 }
3074 }
3075
3076 if (die == NULL && !is_cu_tag(ret_die->di_abbrev_list->abl_tag)) {
3077 dwarf_dealloc_die(ret_die);
3078 _dwarf_error(dbg, error, DW_DLE_FIRST_DIE_NOT_CU);
3079 return DW_DLV_ERROR;
3080 }
3081 *caller_ret_die = ret_die;
3082 return DW_DLV_OK;
3083 }
3084
3085 int
3086 dwarf_child(Dwarf_Die die,
3087 Dwarf_Die * caller_ret_die,
3088 Dwarf_Error * error)
3089 {
3090 Dwarf_Byte_Ptr die_info_ptr = 0;
3091 Dwarf_Byte_Ptr die_info_ptr2 = 0;
3092
3093 /* die_info_end points one-past-end of die area. */
3094 Dwarf_Byte_Ptr die_info_end = 0;
3095 Dwarf_Die ret_die = 0;
3096 Dwarf_Bool has_die_child = 0;
3097 Dwarf_Debug dbg;
3098 Dwarf_Unsigned abbrev_code = 0;
3099 Dwarf_Unsigned utmp = 0;
3100 Dwarf_Debug_InfoTypes dis = 0;
3101 int res = 0;
3102 Dwarf_CU_Context context = 0;
3103 int lres = 0;
3104 Dwarf_Unsigned highest_code = 0;
3105
3106 CHECK_DIE(die, DW_DLV_ERROR);
3107 dbg = die->di_cu_context->cc_dbg;
3108 dis = die->di_is_info? &dbg->de_info_reading:
3109 &dbg->de_types_reading;
3110 die_info_ptr = die->di_debug_ptr;
3111
3112 /* We are saving a DIE pointer here, but the pointer
3113 will not be presumed live later, when it is tested. */
3114 dis->de_last_die = die;
3115 dis->de_last_di_ptr = die_info_ptr;
3116
3117 /* NULL die has no child. */
3118 if ((*die_info_ptr) == 0) {
3119 return DW_DLV_NO_ENTRY;
3120 }
3121 context = die->di_cu_context;
3122 die_info_end = _dwarf_calculate_info_section_end_ptr(context);
3123
3124 res = _dwarf_next_die_info_ptr(die_info_ptr,
3125 die->di_cu_context,
3126 die_info_end,
3127 NULL, FALSE,
3128 &has_die_child,
3129 &die_info_ptr2,
3130 error);
3131 if (res != DW_DLV_OK) {
3132 return res;
3133 }
3134 if (die_info_ptr == die_info_end) {
3135 return DW_DLV_NO_ENTRY;
3136 }
3137 die_info_ptr = die_info_ptr2;
3138
3139 dis->de_last_di_ptr = die_info_ptr;
3140
3141 if (!has_die_child) {
3142 /* Look for end of sibling chain. */
3143 while (dis->de_last_di_ptr < die_info_end) {
3144 if (*dis->de_last_di_ptr) {
3145 break;
3146 }
3147 ++dis->de_last_di_ptr;
3148 }
3149 return DW_DLV_NO_ENTRY;
3150 }
3151
3152 ret_die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
3153 if (!ret_die) {
3154 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
3155 return DW_DLV_ERROR;
3156 }
3157 ret_die->di_debug_ptr = die_info_ptr;
3158 ret_die->di_cu_context = die->di_cu_context;
3159 ret_die->di_is_info = die->di_is_info;
3160
3161 res = _dwarf_leb128_uword_wrapper(dbg,&die_info_ptr,
3162 die_info_end, &utmp,error);
3163 if (res != DW_DLV_OK) {
3164 dwarf_dealloc_die(ret_die);
3165 return res;
3166 }
3167 abbrev_code = (Dwarf_Unsigned) utmp;
3168
3169 dis->de_last_di_ptr = die_info_ptr;
3170
3171 if (abbrev_code == 0) {
3172 /* Look for end of sibling chain */
3173 while (dis->de_last_di_ptr < die_info_end) {
3174 if (*dis->de_last_di_ptr) {
3175 break;
3176 }
3177 ++dis->de_last_di_ptr;
3178 }
3179
3180 /* We have arrived at a null DIE,
3181 at the end of a CU or the end
3182 of a list of siblings. */
3183 *caller_ret_die = 0;
3184 dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
3185 ret_die = 0;
3186 return DW_DLV_NO_ENTRY;
3187 }
3188 ret_die->di_abbrev_code = abbrev_code;
3189 lres = _dwarf_get_abbrev_for_code(ret_die->di_cu_context,
3190 abbrev_code,
3191 &ret_die->di_abbrev_list,
3192 &highest_code,error);
3193 if (lres == DW_DLV_ERROR) {
3194 dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
3195 ret_die = 0;
3196 return lres;
3197 }
3198 if (lres == DW_DLV_NO_ENTRY) {
3199 dwarfstring m;
3200
3201 dwarfstring_constructor(&m);
3202 dwarf_dealloc_die(ret_die);
3203 ret_die = 0;
3204 dwarfstring_append_printf_u(&m,
3205 "DW_DLE_ABBREV_MISSING: the abbrev code not found "
3206 " in dwarf_child() is %u. ",abbrev_code);
3207 dwarfstring_append_printf_u(&m,
3208 "The highest known code"
3209 " in any compilation unit is %u.",
3210 highest_code);
3211 _dwarf_error_string(dbg, error, DW_DLE_ABBREV_MISSING,
3212 dwarfstring_string(&m));
3213 dwarfstring_destructor(&m);
3214 return DW_DLV_ERROR;
3215 }
3216 if (!ret_die->di_abbrev_list->abl_attr) {
3217 int bres = 0;
3218 Dwarf_Byte_Ptr abbrev_ptr =
3219 ret_die->di_abbrev_list->abl_abbrev_ptr;
3220 Dwarf_Byte_Ptr abbrev_end =
3221 _dwarf_calculate_abbrev_section_end_ptr(
3222 ret_die->di_cu_context);
3223 bres = _dwarf_fill_in_attr_form_abtable(
3224 ret_die->di_cu_context,
3225 abbrev_ptr,
3226 abbrev_end,
3227 ret_die->di_abbrev_list,
3228 error);
3229 if (bres != DW_DLV_OK) {
3230 dwarf_dealloc_die(ret_die);
3231 return bres;
3232 }
3233 }
3234 *caller_ret_die = ret_die;
3235 return DW_DLV_OK;
3236 }
3237
3238 /* Given a (global, not cu_relative) die offset, this returns
3239 a pointer to a DIE thru *new_die.
3240 It is up to the caller to do a
3241 dwarf_dealloc(dbg,*new_die,DW_DLE_DIE);
3242 The old form only works with debug_info.
3243 The new _b form works with debug_info or debug_types.
3244
3245 */
3246 int
3247 dwarf_offdie_b(Dwarf_Debug dbg,
3248 Dwarf_Off offset, Dwarf_Bool is_info,
3249 Dwarf_Die * new_die, Dwarf_Error * error)
3250 {
3251 Dwarf_CU_Context cu_context = 0;
3252 Dwarf_Small *dataptr = 0;
3253 Dwarf_Off new_cu_offset = 0;
3254 Dwarf_Die die = 0;
3255 Dwarf_Byte_Ptr info_ptr = 0;
3256 Dwarf_Unsigned abbrev_code = 0;
3257 Dwarf_Unsigned utmp = 0;
3258 int lres = 0;
3259 Dwarf_Debug_InfoTypes dis = 0;
3260 Dwarf_Byte_Ptr die_info_end = 0;
3261 Dwarf_Unsigned highest_code = 0;
3262 struct Dwarf_Section_s * secdp = 0;
3263
3264 CHECK_DBG(dbg,error,"dwarf_offdie_b()");
3265 if (is_info) {
3266 dis =&dbg->de_info_reading;
3267 secdp = &dbg->de_debug_info;
3268 dataptr = dbg->de_debug_info.dss_data;
3269 } else {
3270 dis =&dbg->de_types_reading;
3271 secdp = &dbg->de_debug_types;
3272 dataptr = dbg->de_debug_types.dss_data;
3273 }
3274
3275 if (!dataptr) {
3276 lres = _dwarf_load_die_containing_section(dbg,
3277 is_info, error);
3278 if (lres != DW_DLV_OK) {
3279 return lres;
3280 }
3281 }
3282 cu_context = _dwarf_find_CU_Context(dbg, offset,is_info);
3283 if (cu_context == NULL) {
3284 Dwarf_Unsigned section_size = 0;
3285
3286 if (dis->de_cu_context_list_end != NULL) {
3287 new_cu_offset = _dwarf_calculate_next_cu_context_offset(
3288 dis->de_cu_context_list_end);
3289 }/* Else new_cu_offset remains 0, no CUs on list,
3290 a fresh section setup. */
3291 section_size = secdp->dss_size;
3292 do {
3293 /* We do not want this to return cu_die as
3294 we only want the last one to create DIE,
3295 and that will be done just below. */
3296 lres = _dwarf_create_a_new_cu_context_record_on_list(
3297 dbg, dis,is_info,section_size,new_cu_offset,
3298 &cu_context,NULL,error);
3299 if (lres != DW_DLV_OK) {
3300 return lres;
3301 }
3302 new_cu_offset = _dwarf_calculate_next_cu_context_offset(
3303 cu_context);
3304 /* Not setting dis->de_cu_context, leave
3305 that unchanged. */
3306 } while (offset >= new_cu_offset);
3307 }
3308 /* We have a cu_context for this offset. */
3309 die_info_end = _dwarf_calculate_info_section_end_ptr(cu_context);
3310 die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
3311 if (!die) {
3312 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
3313 return DW_DLV_ERROR;
3314 }
3315 die->di_cu_context = cu_context;
3316 die->di_is_info = is_info;
3317 /* dataptr above might be stale if we loaded a section
3318 above. So access dss_data here. */
3319 if (is_info) {
3320 info_ptr = offset + dbg->de_debug_info.dss_data;
3321 } else {
3322 info_ptr = offset + dbg->de_debug_types.dss_data;
3323 }
3324 die->di_debug_ptr = info_ptr;
3325 lres = _dwarf_leb128_uword_wrapper(dbg,&info_ptr,die_info_end,
3326 &utmp,error);
3327 if (lres != DW_DLV_OK) {
3328 dwarf_dealloc_die(die);
3329 return lres;
3330 }
3331 abbrev_code = utmp;
3332 if (abbrev_code == 0) {
3333 /* we are at a null DIE (or there is a bug). */
3334 dwarf_dealloc_die(die);
3335 die = 0;
3336 return DW_DLV_NO_ENTRY;
3337 }
3338 die->di_abbrev_code = abbrev_code;
3339 lres = _dwarf_get_abbrev_for_code(cu_context, abbrev_code,
3340 &die->di_abbrev_list,
3341 &highest_code,error);
3342 if (lres == DW_DLV_ERROR) {
3343 dwarf_dealloc_die(die);
3344 die = 0;
3345 return lres;
3346 }
3347 if (lres == DW_DLV_NO_ENTRY) {
3348 dwarfstring m;
3349
3350 dwarf_dealloc_die(die);
3351 die = 0;
3352 dwarfstring_constructor(&m);
3353 dwarfstring_append_printf_u(&m,
3354 "DW_DLE_DIE_ABBREV_LIST_NULL: "
3355 "There is no abbrev present for code %u"
3356 " in this compilation unit"
3357 " when calling dwarf_offdie_b(). ",
3358 abbrev_code);
3359 dwarfstring_append_printf_u(&m,
3360 "The highest known code "
3361 "in any compilation unit is %u .",
3362 highest_code);
3363 _dwarf_error_string(dbg, error,
3364 DW_DLE_DIE_ABBREV_LIST_NULL,
3365 dwarfstring_string(&m));
3366 dwarfstring_destructor(&m);
3367 return DW_DLV_ERROR;
3368 }
3369 if (!die->di_abbrev_list->abl_attr) {
3370 int bres = 0;
3371 Dwarf_Byte_Ptr abbrev_ptr =
3372 die->di_abbrev_list->abl_abbrev_ptr;
3373 Dwarf_Byte_Ptr abbrev_end =
3374 _dwarf_calculate_abbrev_section_end_ptr(
3375 die->di_cu_context);
3376 bres = _dwarf_fill_in_attr_form_abtable(
3377 die->di_cu_context,
3378 abbrev_ptr,
3379 abbrev_end,
3380 die->di_abbrev_list,
3381 error);
3382 if (bres != DW_DLV_OK) {
3383 dwarf_dealloc_die(die);
3384 return bres;
3385 }
3386 }
3387 *new_die = die;
3388 return DW_DLV_OK;
3389 }
3390
3391 /* New March 2016.
3392 Lets one cross check the abbreviations section and
3393 the DIE information presented by dwarfdump -i -G -v. */
3394 int
3395 dwarf_die_abbrev_global_offset(Dwarf_Die die,
3396 Dwarf_Off * abbrev_goffset,
3397 Dwarf_Unsigned * abbrev_count,
3398 Dwarf_Error* error)
3399 {
3400 Dwarf_Abbrev_List dal = 0;
3401 Dwarf_Debug dbg = 0;
3402
3403 CHECK_DIE(die, DW_DLV_ERROR);
3404 dbg = die->di_cu_context->cc_dbg;
3405 dal = die->di_abbrev_list;
3406 if (!dal) {
3407 _dwarf_error(dbg,error,DW_DLE_DWARF_ABBREV_NULL);
3408 return DW_DLV_ERROR;
3409 }
3410 *abbrev_goffset = dal->abl_goffset;
3411 *abbrev_count = dal->abl_abbrev_count;
3412 return DW_DLV_OK;
3413 }
3414
3415 /* New August 2018.
3416 Because some real compressed sections
3417 have .zdebug instead
3418 of .debug as the leading characters.
3419 actual_sec_name_out points to a static
3420 string so so not free it. */
3421 int
3422 dwarf_get_real_section_name(Dwarf_Debug dbg,
3423 const char *std_section_name,
3424 const char **actual_sec_name_out,
3425 Dwarf_Small *marked_zcompressed, /* zdebug */
3426 Dwarf_Small *marked_zlib_compressed, /* ZLIB string */
3427 Dwarf_Small *marked_shf_compressed, /* SHF_COMPRESSED */
3428 Dwarf_Unsigned *compressed_length,
3429 Dwarf_Unsigned *uncompressed_length,
3430 Dwarf_Error *error)
3431 {
3432 unsigned i = 0;
3433 char tbuf[100] = {0};
3434 size_t std_sec_name_len = 0;
3435
3436 CHECK_DBG(dbg,error,"dwarf_get_real_section_name()");
3437 if (!std_section_name || 0 == std_section_name[0]) {
3438 _dwarf_error_string(dbg,error,DW_DLE_SECTION_NAME_BIG,
3439 "DW_DLE_SECTION_NAME_BIG: Actually the "
3440 "section name is empty, not big.");
3441 return DW_DLV_ERROR;
3442 }
3443 std_sec_name_len = strlen(std_section_name);
3444 /* std_section_name never has the .dwo on the end,
3445 so allow for that and allow one (arbitrarily) more. */
3446 if ((std_sec_name_len + 5) < sizeof(tbuf)) {
3447 _dwarf_safe_strcpy(tbuf,sizeof(tbuf),
3448 std_section_name,std_sec_name_len);
3449 _dwarf_safe_strcpy(tbuf+std_sec_name_len,
3450 sizeof(tbuf)-std_sec_name_len,
3451 ".dwo",4);
3452 }
3453 for (i=0; i < dbg->de_debug_sections_total_entries; i++) {
3454 struct Dwarf_dbg_sect_s *sdata = &dbg->de_debug_sections[i];
3455 struct Dwarf_Section_s *section = sdata->ds_secdata;
3456 const char *std = section->dss_standard_name;
3457
3458 if (!strcmp(std,std_section_name) ||
3459 !strcmp(std,tbuf)) {
3460 const char *used = section->dss_name;
3461 *actual_sec_name_out = used;
3462 if (sdata->ds_have_zdebug) {
3463 *marked_zcompressed = TRUE;
3464 }
3465 if (section->dss_ZLIB_compressed) {
3466 *marked_zlib_compressed = TRUE;
3467 if (uncompressed_length) {
3468 *uncompressed_length =
3469 section->dss_uncompressed_length;
3470 }
3471 if (compressed_length) {
3472 *compressed_length =
3473 section->dss_compressed_length;
3474 }
3475 }
3476 if (section->dss_shf_compressed) {
3477 *marked_shf_compressed = TRUE;
3478 if (uncompressed_length) {
3479 *uncompressed_length =
3480 section->dss_uncompressed_length;
3481 }
3482 if (compressed_length) {
3483 *compressed_length =
3484 section->dss_compressed_length;
3485 }
3486 }
3487 return DW_DLV_OK;
3488 }
3489 }
3490 return DW_DLV_NO_ENTRY;
3491 }
3492 /* This is useful when printing DIE data.
3493 The string pointer returned must not be freed.
3494 With non-elf objects it is possible the
3495 string returned might be empty or NULL,
3496 so callers should be prepared for that kind
3497 of return. */
3498 int
3499 dwarf_get_die_section_name(Dwarf_Debug dbg,
3500 Dwarf_Bool is_info,
3501 const char ** sec_name,
3502 Dwarf_Error * error)
3503 {
3504 struct Dwarf_Section_s *sec = 0;
3505
3506 CHECK_DBG(dbg,error,"dwarf_get_die_section_name()");
3507 if (is_info) {
3508 sec = &dbg->de_debug_info;
3509 } else {
3510 sec = &dbg->de_debug_types;
3511 }
3512 if (sec->dss_size == 0) {
3513 /* We don't have such a section at all. */
3514 return DW_DLV_NO_ENTRY;
3515 }
3516 *sec_name = sec->dss_name;
3517 return DW_DLV_OK;
3518 }
3519
3520 /* This one assumes is_info not known to caller but a DIE is known. */
3521 int
3522 dwarf_get_die_section_name_b(Dwarf_Die die,
3523 const char ** sec_name,
3524 Dwarf_Error * error)
3525 {
3526 Dwarf_CU_Context context = 0;
3527 Dwarf_Bool is_info = 0;
3528 Dwarf_Debug dbg = 0;
3529
3530 CHECK_DIE(die, DW_DLV_ERROR);
3531 context = die->di_cu_context;
3532 dbg = context->cc_dbg;
3533 is_info = context->cc_is_info;
3534 return dwarf_get_die_section_name(dbg,is_info,sec_name,error);
3535 }