Debian Codegraph @

src/lib/libdwarf/dwarf_query.c

raw
1 /*
2 Cropyright (C) 2000,2002,2004 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 Portions Copyright 2020 Google All rights reserved.
6
7 This program is free software; you can redistribute it
8 and/or modify it under the terms of version 2.1 of the
9 GNU Lesser General Public License as published by the Free
10 Software Foundation.
11
12 This program is distributed in the hope that it would be
13 useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE.
16
17 Further, this software is distributed without any warranty
18 that it is free of the rightful claim of any third person
19 regarding infringement or the like. Any license provided
20 herein, whether implied or otherwise, applies only to this
21 software file. Patent licenses, if any, provided herein
22 do not apply to combinations of this program with other
23 software, or any other product whatsoever.
24
25 You should have received a copy of the GNU Lesser General
26 Public License along with this program; if not, write the
27 Free Software Foundation, Inc., 51 Franklin Street - Fifth
28 Floor, Boston MA 02110-1301, USA.
29
30 */
31
32 #include <config.h>
33
34 #include <stddef.h> /* NULL size_t */
35 #include <stdio.h> /* debugging printf */
36
37 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
38 #include "stdafx.h"
39 #endif /* HAVE_STDAFX_H */
40
41 #include "dwarf.h"
42 #include "libdwarf.h"
43 #include "libdwarf_private.h"
44 #include "dwarf_base_types.h"
45 #include "dwarf_opaque.h"
46 #include "dwarf_alloc.h"
47 #include "dwarf_error.h"
48 #include "dwarf_util.h"
49 #include "dwarf_die_deliv.h"
50 #include "dwarf_string.h"
51
52 static int _dwarf_die_attr_unsigned_constant(Dwarf_Die die,
53 Dwarf_Half attr,
54 Dwarf_Unsigned *return_val,
55 Dwarf_Error *error);
56
57 int dwarf_get_offset_size(Dwarf_Debug dbg,
58 Dwarf_Half * offset_size,
59 Dwarf_Error * error)
60 {
61 CHECK_DBG(dbg,error,"dwarf_get_offset_size()");
62 *offset_size = dbg->de_length_size;
63 return DW_DLV_OK;
64 }
65
66 #if 0 /* dump_bytes */
67 static void
68 dump_bytes(char * msg,Dwarf_Small * start, long len)
69 {
70 Dwarf_Small *end = start + len;
71 Dwarf_Small *cur = start;
72
73 printf("%s ",msg);
74 for (; cur < end; cur++) {
75 printf("%02x ", *cur);
76 }
77 printf("\n");
78 }
79 #endif /*0*/
80
81 /* This is normally reliable.
82 But not always.
83 If different compilation
84 units have different address sizes
85 this may not give the correct value in all contexts.
86 If the Elf offset size != address_size
87 (for example if address_size = 4 but recorded in elf64 object)
88 this may not give the correct value in all contexts.
89 */
90 int
91 dwarf_get_address_size(Dwarf_Debug dbg,
92 Dwarf_Half *ret_addr_size, Dwarf_Error *error)
93 {
94 Dwarf_Half address_size = 0;
95
96 CHECK_DBG(dbg,error,"dwarf_get_address_size()");
97 address_size = dbg->de_pointer_size;
98 *ret_addr_size = address_size;
99 return DW_DLV_OK;
100 }
101
102 /* This will be correct in all contexts where the
103 CU context of a DIE is known.
104 */
105 int
106 dwarf_get_die_address_size(Dwarf_Die die,
107 Dwarf_Half * ret_addr_size, Dwarf_Error *error)
108 {
109 Dwarf_Half address_size = 0;
110 CHECK_DIE(die, DW_DLV_ERROR);
111 address_size = die->di_cu_context->cc_address_size;
112 *ret_addr_size = address_size;
113 return DW_DLV_OK;
114 }
115
116 int
117 dwarf_dieoffset(Dwarf_Die die,
118 Dwarf_Off *ret_offset, Dwarf_Error *error)
119 {
120 Dwarf_Small *dataptr = 0;
121 Dwarf_Debug dbg = 0;
122
123 CHECK_DIE(die, DW_DLV_ERROR);
124 dbg = die->di_cu_context->cc_dbg;
125 dataptr = die->di_is_info? dbg->de_debug_info.dss_data:
126 dbg->de_debug_types.dss_data;
127
128 *ret_offset = (die->di_debug_ptr - dataptr);
129 return DW_DLV_OK;
130 }
131
132 /* This function returns the offset of
133 the die relative to the start of its
134 compilation-unit rather than .debug_info.
135 Returns DW_DLV_ERROR on error. */
136 int
137 dwarf_die_CU_offset(Dwarf_Die die,
138 Dwarf_Off *cu_off, Dwarf_Error *error)
139 {
140 Dwarf_CU_Context cu_context = 0;
141 Dwarf_Small *dataptr = 0;
142 Dwarf_Debug dbg = 0;
143
144 CHECK_DIE(die, DW_DLV_ERROR);
145 cu_context = die->di_cu_context;
146 dbg = die->di_cu_context->cc_dbg;
147 dataptr = die->di_is_info? dbg->de_debug_info.dss_data:
148 dbg->de_debug_types.dss_data;
149
150 *cu_off = (die->di_debug_ptr - dataptr -
151 cu_context->cc_debug_offset);
152 return DW_DLV_OK;
153 }
154
155 /* A common function to get both offsets (local and global)
156 It's unusual in that it sets both return offsets
157 to zero on entry. Normally we only set any
158 output-args (through their pointers) in case
159 of success. */
160 int
161 dwarf_die_offsets(Dwarf_Die die,
162 Dwarf_Off *off,
163 Dwarf_Off *cu_off,
164 Dwarf_Error *error)
165 {
166 int res = 0;
167 Dwarf_Off lcuoff = 0;
168 Dwarf_Off loff = 0;
169
170 res = dwarf_dieoffset(die,&loff,error);
171 if (res == DW_DLV_OK) {
172 res = dwarf_die_CU_offset(die,&lcuoff,error);
173 }
174 if (res == DW_DLV_OK) {
175 /* Waiting till both succeed before
176 returning any value at all to retain
177 normal libdwarf call semantics. */
178 *off = loff;
179 *cu_off = lcuoff;
180 } else {
181 *off = 0;
182 *cu_off = 0;
183 }
184 return res;
185 }
186
187 /* This function returns the global offset
188 (meaning the section offset) and length of
189 the CU that this die is a part of.
190 Used for correctness checking by dwarfdump. */
191 int
192 dwarf_die_CU_offset_range(Dwarf_Die die,
193 Dwarf_Off *cu_off,
194 Dwarf_Off *cu_length,
195 Dwarf_Error *error)
196 {
197 Dwarf_CU_Context cu_context = 0;
198
199 CHECK_DIE(die, DW_DLV_ERROR);
200 cu_context = die->di_cu_context;
201
202 *cu_off = cu_context->cc_debug_offset;
203 *cu_length = cu_context->cc_length + cu_context->cc_length_size
204 + cu_context->cc_extension_size;
205 return DW_DLV_OK;
206 }
207
208 int
209 dwarf_tag(Dwarf_Die die, Dwarf_Half *tag, Dwarf_Error *error)
210 {
211 CHECK_DIE(die, DW_DLV_ERROR);
212 *tag = die->di_abbrev_list->abl_tag;
213 return DW_DLV_OK;
214 }
215
216 static void
217 free_dwarf_offsets_chain(Dwarf_Debug dbg, Dwarf_Chain_2 head_chain)
218 {
219 Dwarf_Chain_2 cur = head_chain;
220 Dwarf_Chain_2 next = 0;
221
222 for ( ; cur ; cur = next ) {
223 next = cur->ch_next;
224 dwarf_dealloc(dbg, cur, DW_DLA_CHAIN_2);
225 }
226 }
227
228 /* Returns the children offsets for the given offset */
229 int
230 dwarf_offset_list(Dwarf_Debug dbg,
231 Dwarf_Off offset, Dwarf_Bool is_info,
232 Dwarf_Off **offbuf, Dwarf_Unsigned *offcnt,
233 Dwarf_Error *error)
234 {
235 Dwarf_Die die = 0;
236 Dwarf_Die child = 0;
237 Dwarf_Die sib_die = 0;
238 Dwarf_Die cur_die = 0;
239 int res = 0;
240 Dwarf_Unsigned off_count = 0;
241 Dwarf_Unsigned i = 0;
242 Dwarf_Off *ret_offsets = 0;
243 Dwarf_Chain_2 curr_chain = 0;
244 Dwarf_Chain_2 head_chain = 0;
245 Dwarf_Chain_2 *plast = &head_chain;
246
247 CHECK_DBG(dbg,error,"dwarf_offset_list()");
248 *offbuf = NULL;
249 *offcnt = 0;
250 res = dwarf_offdie_b(dbg,offset,is_info,&die,error);
251 if (DW_DLV_OK != res) {
252 return res;
253 }
254
255 res = dwarf_child(die,&child,error);
256 if (DW_DLV_ERROR == res || DW_DLV_NO_ENTRY == res) {
257 return res;
258 }
259 dwarf_dealloc_die(die);
260 cur_die = child;
261 child = 0;
262 for (;;) {
263 if (DW_DLV_OK == res) {
264 int dres = 0;
265 Dwarf_Off cur_off = 0;
266
267 dres = dwarf_dieoffset(cur_die,&cur_off,error);
268 if (dres == DW_DLV_OK) {
269 /* Normal. use cur_off. */
270 } else if (dres == DW_DLV_ERROR) {
271 free_dwarf_offsets_chain(dbg,head_chain);
272 dwarf_dealloc_die(cur_die);
273 return DW_DLV_ERROR;
274 } else { /* DW_DLV_NO_ENTRY */
275 /* Impossible, dwarf_dieoffset never returns this */
276 }
277 /* Record offset in current entry chain */
278 curr_chain = (Dwarf_Chain_2)_dwarf_get_alloc(
279 dbg,DW_DLA_CHAIN_2,1);
280 if (curr_chain == NULL) {
281 free_dwarf_offsets_chain(dbg,head_chain);
282 dwarf_dealloc_die(cur_die);
283 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
284 return DW_DLV_ERROR;
285 }
286
287 /* Put current offset on singly_linked list. */
288 curr_chain->ch_item = cur_off;
289 ++off_count;
290 (*plast) = curr_chain;
291 plast = &(curr_chain->ch_next);
292 }
293 /* Move to next sibling next sibling */
294 sib_die = 0;
295 res = dwarf_siblingof_c(cur_die,&sib_die,error);
296 if (cur_die != die) {
297 dwarf_dealloc(dbg,cur_die,DW_DLA_DIE);
298 }
299 if (DW_DLV_ERROR == res) {
300 free_dwarf_offsets_chain(dbg,head_chain);
301 return res;
302 }
303 if (DW_DLV_NO_ENTRY == res) {
304 /* Done at this level. */
305 break;
306 }
307 /* res == DW_DLV_OK */
308 cur_die = sib_die;
309 }
310
311 /* Points to contiguous block of Dwarf_Off's. */
312 ret_offsets = (Dwarf_Off *) _dwarf_get_alloc(dbg,
313 DW_DLA_UARRAY, off_count);
314 if (ret_offsets == NULL) {
315 free_dwarf_offsets_chain(dbg,head_chain);
316 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
317 return DW_DLV_ERROR;
318 }
319
320 /* Store offsets in contiguous block,
321 and deallocate the chain. */
322 curr_chain = head_chain;
323 for (i = 0; i < off_count; i++) {
324 Dwarf_Chain_2 prev =0;
325
326 ret_offsets[i] = curr_chain->ch_item;
327 prev = curr_chain;
328 curr_chain = curr_chain->ch_next;
329 dwarf_dealloc(dbg, prev, DW_DLA_CHAIN_2);
330 }
331 *offbuf = ret_offsets;
332 *offcnt = off_count;
333 return DW_DLV_OK;
334 }
335
336 static void
337 empty_local_attrlist(Dwarf_Debug dbg,
338 Dwarf_Attribute attr)
339 {
340 Dwarf_Attribute cur = 0;
341 Dwarf_Attribute next = 0;
342
343 for (cur = attr; cur ; cur = next) {
344 next = cur->ar_next;
345 dwarf_dealloc(dbg,cur,DW_DLA_ATTR);
346 }
347 }
348
349 int
350 dwarf_attrlist(Dwarf_Die die,
351 Dwarf_Attribute **attrbuf,
352 Dwarf_Signed *attrcnt, Dwarf_Error *error)
353 {
354 Dwarf_Unsigned attr_count = 0;
355 Dwarf_Unsigned attr = 0;
356 Dwarf_Unsigned attr_form = 0;
357 Dwarf_Unsigned i = 0;
358 Dwarf_Abbrev_List abbrev_list = 0;
359 Dwarf_Attribute head_attr = NULL;
360 Dwarf_Attribute curr_attr = NULL;
361 Dwarf_Attribute *last_attr = &head_attr;
362 Dwarf_Debug dbg = 0;
363 Dwarf_Byte_Ptr info_ptr = 0;
364 Dwarf_Byte_Ptr die_info_end = 0;
365 int lres = 0;
366 int bres = 0;
367 Dwarf_CU_Context context = 0;
368 Dwarf_Unsigned highest_code = 0;
369
370 CHECK_DIE(die, DW_DLV_ERROR);
371 context = die->di_cu_context;
372 dbg = context->cc_dbg;
373 die_info_end =
374 _dwarf_calculate_info_section_end_ptr(context);
375 lres = _dwarf_get_abbrev_for_code(context,
376 die->di_abbrev_list->abl_code,
377 &abbrev_list,
378 &highest_code,error);
379 if (lres == DW_DLV_ERROR) {
380 return lres;
381 }
382 if (lres == DW_DLV_NO_ENTRY) {
383 dwarfstring m;
384
385 dwarfstring_constructor(&m);
386 dwarfstring_append_printf_u(&m,
387 "DW_DLE_ABBREV_MISSING "
388 "There is no abbrev present for code %u "
389 "in this compilation unit. ",
390 die->di_abbrev_list->abl_code);
391 dwarfstring_append_printf_u(&m,
392 "The highest known code "
393 "in any compilation unit is %u .",
394 highest_code);
395 _dwarf_error_string(dbg, error,
396 DW_DLE_ABBREV_MISSING,
397 dwarfstring_string(&m));
398 dwarfstring_destructor(&m);
399 return DW_DLV_ERROR;
400 }
401
402 info_ptr = die->di_debug_ptr;
403 {
404 /* SKIP_LEB128 */
405 Dwarf_Unsigned ignore_this = 0;
406 Dwarf_Unsigned len = 0;
407
408 lres = dwarf_decode_leb128((char *)info_ptr,
409 &len,&ignore_this,(char *)die_info_end);
410 if (lres == DW_DLV_ERROR) {
411 /* Stepped off the end SKIPping the leb */
412 dwarfstring m;
413
414 dwarfstring_constructor(&m);
415 dwarfstring_append_printf_u(&m,
416 "DW_DLE_DIE_BAD: In building an attrlist "
417 "we run off the end of the DIE while skipping "
418 " the DIE tag, seeing the leb length as 0x%u ",
419 len);
420 _dwarf_error_string(dbg, error, DW_DLE_DIE_BAD,
421 dwarfstring_string(&m));
422 dwarfstring_destructor(&m);
423 return DW_DLV_ERROR;
424 }
425 info_ptr += len;
426 }
427
428 if (!abbrev_list->abl_attr) {
429 Dwarf_Byte_Ptr abbrev_ptr = abbrev_list->abl_abbrev_ptr;
430 Dwarf_Byte_Ptr abbrev_end =
431 _dwarf_calculate_abbrev_section_end_ptr(context);
432 /* FIXME */
433 bres = _dwarf_fill_in_attr_form_abtable(context,
434 abbrev_ptr, abbrev_end, abbrev_list,
435 error);
436 if (bres != DW_DLV_OK) {
437 return bres;
438 }
439 /* Here we are guaranteed abbrev_list->abl_attr
440 is non-null */
441 }
442 /* ASSERT list->abl_addr and list->abl_form
443 are non-null and if list->abl_implicit_const_count > 0
444 list->abl_implicit_const is non-null. */
445
446 for ( i = 0; i <abbrev_list->abl_abbrev_count; ++i) {
447 Dwarf_Signed implicit_const = 0;
448 Dwarf_Half newattr_form = 0;
449 int ires = 0;
450
451 attr = abbrev_list->abl_attr[i];
452 attr_form = abbrev_list->abl_form[i];
453 if (attr > DW_AT_hi_user) {
454 empty_local_attrlist(dbg,head_attr);
455 _dwarf_error(dbg, error,DW_DLE_ATTR_CORRUPT);
456 return DW_DLV_ERROR;
457 }
458 if (attr_form == DW_FORM_implicit_const) {
459 implicit_const = abbrev_list->abl_implicit_const[i];
460 }
461 if (!_dwarf_valid_form_we_know(attr_form,attr)) {
462 empty_local_attrlist(dbg,head_attr);
463 _dwarf_error(dbg, error, DW_DLE_UNKNOWN_FORM);
464 return DW_DLV_ERROR;
465 }
466 newattr_form = (Dwarf_Half)attr_form;
467 if (attr_form == DW_FORM_indirect) {
468 Dwarf_Unsigned utmp6 = 0;
469
470 if (_dwarf_reference_outside_section(die,
471 (Dwarf_Small*) info_ptr,
472 ((Dwarf_Small*) info_ptr )+1)) {
473 empty_local_attrlist(dbg,head_attr);
474 _dwarf_error_string(dbg, error,
475 DW_DLE_ATTR_OUTSIDE_SECTION,
476 "DW_DLE_ATTR_OUTSIDE_SECTION: "
477 " Reading Attriutes: "
478 "For DW_FORM_indirect there is"
479 " no room for the form. Corrupt Dwarf");
480 return DW_DLV_ERROR;
481 }
482 ires = _dwarf_leb128_uword_wrapper(dbg,
483 &info_ptr,die_info_end,&utmp6,error);
484 if (ires != DW_DLV_OK) {
485 empty_local_attrlist(dbg,head_attr);
486 _dwarf_error_string(dbg, error,
487 DW_DLE_ATTR_OUTSIDE_SECTION,
488 "DW_DLE_ATTR_OUTSIDE_SECTION: "
489 "Reading target of a DW_FORM_indirect "
490 "from an abbreviation failed. Corrupt Dwarf");
491 return DW_DLV_ERROR;
492 }
493 attr_form = (Dwarf_Half) utmp6;
494 if (attr_form == DW_FORM_implicit_const) {
495 empty_local_attrlist(dbg,head_attr);
496 _dwarf_error_string(dbg, error,
497 DW_DLE_ATTR_OUTSIDE_SECTION,
498 "DW_DLE_ATTR_OUTSIDE_SECTION: "
499 " Reading Attriutes: an indirect form "
500 "leads to a DW_FORM_implicit_const "
501 "which is not handled. Corrupt Dwarf");
502 return DW_DLV_ERROR;
503 }
504 if (!_dwarf_valid_form_we_know(attr_form,attr)) {
505 dwarfstring m;
506
507 dwarfstring_constructor(&m);
508 dwarfstring_append_printf_u(&m,
509 "DW_DLE_UNKNOWN_FORM "
510 " form indirect leads to form"
511 " of 0x%x which is unknown",
512 attr_form);
513 _dwarf_error_string(dbg, error,
514 DW_DLE_UNKNOWN_FORM,
515 dwarfstring_string(&m));
516 dwarfstring_destructor(&m);
517 empty_local_attrlist(dbg,head_attr);
518 return DW_DLV_ERROR;
519 }
520 newattr_form = (Dwarf_Half)attr_form;
521 }
522
523 if (attr) {
524 Dwarf_Attribute new_attr = 0;
525
526 new_attr = (Dwarf_Attribute)
527 _dwarf_get_alloc(dbg, DW_DLA_ATTR, 1);
528 if (!new_attr) {
529 empty_local_attrlist(dbg,head_attr);
530 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
531 "DW_DLE_ALLOC_FAIL: attempting to allocate"
532 " a Dwarf_Attribute record");
533 return DW_DLV_ERROR;
534 }
535 new_attr->ar_attribute = (Dwarf_Half)attr;
536 new_attr->ar_attribute_form_direct =
537 (Dwarf_Half)attr_form;
538 new_attr->ar_attribute_form = (Dwarf_Half)newattr_form;
539 /* Here the final address must be *inside* the
540 section, as we will read from there, and read
541 at least one byte, we think.
542 We do not want info_ptr to point past end so
543 we add 1 to the end-pointer. */
544 new_attr->ar_cu_context = die->di_cu_context;
545 new_attr->ar_debug_ptr = info_ptr;
546 new_attr->ar_die = die;
547 new_attr->ar_dbg = dbg;
548 if ( attr_form != DW_FORM_implicit_const &&
549 _dwarf_reference_outside_section(die,
550 (Dwarf_Small*) info_ptr,
551 ((Dwarf_Small*) info_ptr )+1)) {
552 dwarf_dealloc_attribute(new_attr);
553 empty_local_attrlist(dbg,head_attr);
554 _dwarf_error_string(dbg, error,
555 DW_DLE_ATTR_OUTSIDE_SECTION,
556 "DW_DLE_ATTR_OUTSIDE_SECTION: "
557 " Reading Attriutes: "
558 "We have run off the end of the section. "
559 "Corrupt Dwarf");
560 return DW_DLV_ERROR;
561 }
562 if (attr_form == DW_FORM_implicit_const) {
563 /* The value is here, not in a DIE.
564 Do not increment info_ptr */
565 new_attr->ar_implicit_const = implicit_const;
566 } else {
567 Dwarf_Unsigned sov = 0;
568 int vres = 0;
569
570 vres = _dwarf_get_size_of_val(dbg,
571 attr_form,
572 die->di_cu_context->cc_version_stamp,
573 die->di_cu_context->cc_address_size,
574 info_ptr,
575 die->di_cu_context->cc_length_size,
576 &sov,
577 die_info_end,
578 error);
579 if (vres!= DW_DLV_OK) {
580 dwarf_dealloc_attribute(new_attr);
581 empty_local_attrlist(dbg,head_attr);
582 return vres;
583 }
584 info_ptr += sov;
585 }
586 /* Add to single linked list */
587 *last_attr = new_attr;
588 last_attr = &new_attr->ar_next;
589 new_attr = 0;
590 attr_count++;
591 }
592 }
593 if (!attr_count) {
594 *attrbuf = NULL;
595 *attrcnt = 0;
596 return DW_DLV_NO_ENTRY;
597 }
598 {
599 Dwarf_Attribute *attr_ptr = 0;
600
601 attr_ptr = (Dwarf_Attribute *)
602 _dwarf_get_alloc(dbg, DW_DLA_LIST, attr_count);
603 if (attr_ptr == NULL) {
604 empty_local_attrlist(dbg,head_attr);
605 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
606 return DW_DLV_ERROR;
607 }
608 curr_attr = head_attr;
609 for (i = 0; i < attr_count; i++) {
610 *(attr_ptr + i) = curr_attr;
611 curr_attr = curr_attr->ar_next;
612 }
613 *attrbuf = attr_ptr;
614 *attrcnt = attr_count;
615 }
616 return DW_DLV_OK;
617 }
618
619 static void
620 build_alloc_qu_error(Dwarf_Debug dbg,
621 const char *fieldname,
622 Dwarf_Error *error)
623 {
624 dwarfstring m;
625
626 dwarfstring_constructor(&m);
627 dwarfstring_append_printf_s(&m,
628 "DW_DLE_ALLOC_FAIL :"
629 " Attempt to malloc space for %s failed",
630 (char *)fieldname);
631 _dwarf_error_string(dbg,error,DW_DLE_ALLOC_FAIL,
632 dwarfstring_string(&m));
633 dwarfstring_destructor(&m);
634 }
635
636 /*
637 This function takes a die, and an attr, and returns
638 a pointer to the start of the value of that attr in
639 the given die in the .debug_info section. The form
640 is returned in *attr_form.
641
642 If the attr_form is DW_FORM_implicit_const
643 (known signed, so most callers)
644 that is fine, but in that case we do not
645 need to actually set the *ptr_to_value.
646
647 Returns NULL on error, or if attr is not found.
648 However, *attr_form is 0 on error, and positive
649 otherwise.
650 */
651 static int
652 _dwarf_get_value_ptr(Dwarf_Die die,
653 Dwarf_Half attrnum_in,
654 Dwarf_Half *attr_form,
655 Dwarf_Byte_Ptr *ptr_to_value,
656 Dwarf_Signed *implicit_const_out,
657 Dwarf_Error *error)
658 {
659 Dwarf_Byte_Ptr abbrev_ptr = 0;
660 Dwarf_Byte_Ptr abbrev_end = 0;
661 Dwarf_Abbrev_List abbrev_list;
662 Dwarf_Byte_Ptr info_ptr = 0;
663 Dwarf_CU_Context context = die->di_cu_context;
664 Dwarf_Byte_Ptr die_info_end = 0;
665 Dwarf_Debug dbg = 0;
666 int lres = 0;
667 Dwarf_Unsigned i = 0;
668 Dwarf_Unsigned highest_code = 0;
669
670 if (!context) {
671 _dwarf_error(NULL,error,DW_DLE_DIE_NO_CU_CONTEXT);
672 return DW_DLV_ERROR;
673 }
674 dbg = context->cc_dbg;
675 die_info_end =
676 _dwarf_calculate_info_section_end_ptr(context);
677
678 lres = _dwarf_get_abbrev_for_code(context,
679 die->di_abbrev_list->abl_code,
680 &abbrev_list,&highest_code,error);
681 if (lres == DW_DLV_ERROR) {
682 return lres;
683 }
684 if (lres == DW_DLV_NO_ENTRY) {
685 dwarfstring m;
686
687 dwarfstring_constructor(&m);
688 dwarfstring_append_printf_u(&m,
689 "DW_DLE_CU_DIE_NO_ABBREV_LIST "
690 "There is no abbrev present for code %u "
691 "in this compilation unit. ",
692 die->di_abbrev_list->abl_code);
693 dwarfstring_append_printf_u(&m,
694 "The highest known code "
695 "in any compilation unit is %u.",
696 highest_code);
697 _dwarf_error_string(dbg, error,
698 DW_DLE_CU_DIE_NO_ABBREV_LIST,
699 dwarfstring_string(&m));
700 dwarfstring_destructor(&m);
701 return DW_DLV_ERROR;
702 }
703 abbrev_ptr = abbrev_list->abl_abbrev_ptr;
704 abbrev_end = _dwarf_calculate_abbrev_section_end_ptr(context);
705 info_ptr = die->di_debug_ptr;
706 /* This ensures and checks die_info_end >= info_ptr */
707 {
708 /* SKIP_LEB128 */
709 Dwarf_Unsigned ignore_this = 0;
710 Dwarf_Unsigned len = 0;
711
712 lres = dwarf_decode_leb128((char *)info_ptr,
713 &len,&ignore_this,(char *)die_info_end);
714 if (lres == DW_DLV_ERROR) {
715 /* Stepped off the end SKIPping the leb */
716 dwarfstring m;
717
718 dwarfstring_constructor(&m);
719 dwarfstring_append_printf_u(&m,
720 "DW_DLE_DIE_BAD: In building an attrlist "
721 "we run off the end of the DIE while skipping "
722 " the DIE tag, seeing the leb length as 0x%u ",
723 len);
724 _dwarf_error_string(dbg, error, DW_DLE_DIE_BAD,
725 dwarfstring_string(&m));
726 dwarfstring_destructor(&m);
727 return DW_DLV_ERROR;
728 }
729 info_ptr += len;
730 }
731 if (!abbrev_list->abl_attr) {
732 int bres = 0;
733 /* FIXME */
734 bres = _dwarf_fill_in_attr_form_abtable(context,
735 abbrev_ptr, abbrev_end, abbrev_list,
736 error);
737 if (bres != DW_DLV_OK) {
738 return bres;
739 }
740 }
741 if (!abbrev_list->abl_form) {
742 build_alloc_qu_error(dbg,"abbrev_list->abl_form"
743 " in _dwarf_get_value_ptr()", error);
744 return DW_DLV_ERROR;
745 }
746 if (!abbrev_list->abl_attr) {
747 build_alloc_qu_error(dbg,"abbrev_list->abl_attr"
748 " in _dwarf_get_value_ptr()", error);
749 return DW_DLV_ERROR;
750 }
751 for (i = 0; i < abbrev_list->abl_abbrev_count; ++i) {
752 Dwarf_Unsigned curr_attr_form = 0;
753 Dwarf_Unsigned curr_attr = 0;
754 Dwarf_Unsigned value_size=0;
755 Dwarf_Signed implicit_const = 0;
756 int res = 0;
757
758 curr_attr = abbrev_list->abl_attr[i];
759 curr_attr_form = abbrev_list->abl_form[i];
760 if (curr_attr_form == DW_FORM_indirect) {
761 Dwarf_Unsigned utmp6;
762
763 /* DECODE_LEB128_UWORD updates info_ptr */
764 DECODE_LEB128_UWORD_CK(info_ptr, utmp6,dbg,
765 error,die_info_end);
766 curr_attr_form = (Dwarf_Half) utmp6;
767 }
768 if (curr_attr_form == DW_FORM_indirect) {
769 _dwarf_error_string(dbg,error,DW_DLE_ATTR_FORM_BAD,
770 "DW_DLE_ATTR_FORM_BAD: "
771 "A DW_FORM_indirect in an abbreviation "
772 " indirects to another "
773 "DW_FORM_indirect, which is inappropriate.");
774 return DW_DLV_ERROR;
775 }
776 if (curr_attr_form == DW_FORM_implicit_const) {
777 if (!abbrev_list->abl_implicit_const) {
778 _dwarf_error_string(dbg,error,DW_DLE_ATTR_FORM_BAD,
779 "DW_DLE_ATTR_FORM_BAD: "
780 "A DW_FORM_implicit_const in an abbreviation "
781 "has no implicit const value. Corrupt dwarf.");
782 return DW_DLV_ERROR;
783 }
784 implicit_const = abbrev_list->abl_implicit_const[i];
785 }
786 if (curr_attr == attrnum_in) {
787 *attr_form = (Dwarf_Half)curr_attr_form;
788 if (implicit_const_out) {
789 *implicit_const_out = implicit_const;
790 }
791 *ptr_to_value = info_ptr;
792 return DW_DLV_OK;
793 }
794 res = _dwarf_get_size_of_val(dbg,
795 curr_attr_form,
796 die->di_cu_context->cc_version_stamp,
797 die->di_cu_context->cc_address_size,
798 info_ptr,
799 die->di_cu_context->cc_length_size,
800 &value_size,
801 die_info_end,
802 error);
803 if (res != DW_DLV_OK) {
804 return res;
805 }
806 {
807 Dwarf_Unsigned len = 0;
808
809 /* ptrdiff_t is generated but not named */
810 len = (die_info_end >= info_ptr)?
811 (die_info_end - info_ptr):0;
812 if (value_size > len) {
813 /* Something badly wrong. We point past end
814 of debug_info or debug_types or a
815 section is unreasonably sized or we are
816 pointing to two different sections? */
817 _dwarf_error_string(dbg,error,
818 DW_DLE_DIE_ABBREV_BAD,
819 "DW_DLE_DIE_ABBREV_BAD: in calculating the "
820 "size of a value based on abbreviation data "
821 "we find there is not enough room in "
822 "the .debug_info "
823 "section to contain the attribute value.");
824 return DW_DLV_ERROR;
825 }
826 }
827 info_ptr+= value_size;
828 }
829 return DW_DLV_NO_ENTRY;
830 }
831
832 int
833 dwarf_die_text(Dwarf_Die die,
834 Dwarf_Half attrnum,
835 char **ret_name,
836 Dwarf_Error *error)
837 {
838 Dwarf_Debug dbg = 0;
839 int res = DW_DLV_ERROR;
840 Dwarf_Attribute attr = 0;
841 Dwarf_Error lerr = 0;
842
843 CHECK_DIE(die, DW_DLV_ERROR);
844
845 res = dwarf_attr(die,attrnum,&attr,&lerr);
846 dbg = die->di_cu_context->cc_dbg;
847 if (res == DW_DLV_ERROR) {
848 dwarf_dealloc_error(dbg,lerr);
849 return DW_DLV_NO_ENTRY;
850 }
851 if (res == DW_DLV_NO_ENTRY) {
852 return res;
853 }
854 res = dwarf_formstring(attr,ret_name,error);
855 dwarf_dealloc(dbg,attr, DW_DLA_ATTR);
856 attr = 0;
857 return res;
858 }
859
860 int
861 dwarf_diename(Dwarf_Die die,
862 char **ret_name,
863 Dwarf_Error *error)
864 {
865 return dwarf_die_text(die,DW_AT_name,ret_name,error);
866 }
867
868 /* Never returns DW_DLV_NO_ENTRY */
869 int
870 dwarf_hasattr(Dwarf_Die die,
871 Dwarf_Half attr,
872 Dwarf_Bool *return_bool, Dwarf_Error *error)
873 {
874 Dwarf_Half attr_form = 0;
875 Dwarf_Byte_Ptr info_ptr = 0;
876 int res = 0;
877 Dwarf_Signed implicit_const;
878
879 CHECK_DIE(die, DW_DLV_ERROR);
880
881 res = _dwarf_get_value_ptr(die, attr, &attr_form,&info_ptr,
882 &implicit_const,error);
883 if (res == DW_DLV_ERROR) {
884 return res;
885 }
886 if (res == DW_DLV_NO_ENTRY) {
887 *return_bool = FALSE;
888 } else {
889 *return_bool = TRUE;
890 }
891 return DW_DLV_OK;
892 }
893
894 int
895 dwarf_attr(Dwarf_Die die,
896 Dwarf_Half attr,
897 Dwarf_Attribute *ret_attr, Dwarf_Error *error)
898 {
899 Dwarf_Half attr_form = 0;
900 Dwarf_Attribute attrib = 0;
901 Dwarf_Byte_Ptr info_ptr = 0;
902 Dwarf_Debug dbg = 0;
903 int res = 0;
904 int lres = 0;
905 Dwarf_Signed implicit_const = 0;
906 Dwarf_Abbrev_List abbrev_list = 0;
907 Dwarf_Unsigned highest_code = 0;
908 Dwarf_CU_Context context = 0;
909
910 context = die->di_cu_context;
911 dbg = context->cc_dbg;
912
913 CHECK_DIE(die, DW_DLV_ERROR);
914 lres = _dwarf_get_abbrev_for_code(die->di_cu_context,
915 die->di_abbrev_list->abl_code,
916 &abbrev_list,
917 &highest_code,error);
918 if (lres == DW_DLV_ERROR) {
919 return lres;
920 }
921 if (!abbrev_list->abl_attr) {
922 Dwarf_Byte_Ptr abbrev_ptr = abbrev_list->abl_abbrev_ptr;
923 Dwarf_Byte_Ptr abbrev_end =
924 _dwarf_calculate_abbrev_section_end_ptr(context);
925 int bres = 0;
926
927 bres = _dwarf_fill_in_attr_form_abtable(
928 die->di_cu_context,
929 abbrev_ptr, abbrev_end, abbrev_list,
930 error);
931 if (bres != DW_DLV_OK) {
932 return bres;
933 }
934 }
935 if (!abbrev_list->abl_form) {
936 build_alloc_qu_error(dbg,"abbrev_list->abl_form"
937 " in dwarf_attr()", error);
938 return DW_DLV_ERROR;
939 }
940 res = _dwarf_get_value_ptr(die, attr, &attr_form,&info_ptr,
941 &implicit_const,error);
942 if (res == DW_DLV_ERROR) {
943 return res;
944 }
945 if (res == DW_DLV_NO_ENTRY) {
946 return res;
947 }
948
949 attrib = (Dwarf_Attribute) _dwarf_get_alloc(dbg, DW_DLA_ATTR, 1);
950 if (!attrib) {
951 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
952 "DW_DLE_ALLOC_FAIL allocating a single Dwarf_Attribute"
953 " in function dwarf_attr().");
954 return DW_DLV_ERROR;
955 }
956
957 attrib->ar_attribute = attr;
958 attrib->ar_attribute_form = attr_form;
959 attrib->ar_attribute_form_direct = attr_form;
960 attrib->ar_cu_context = die->di_cu_context;
961
962 /* Only nonzero if DW_FORM_implicit_const */
963 attrib->ar_implicit_const = implicit_const;
964 /* Only nonnull if not DW_FORM_implicit_const */
965 attrib->ar_debug_ptr = info_ptr;
966 attrib->ar_die = die;
967 attrib->ar_dbg = dbg;
968 *ret_attr = attrib;
969 return DW_DLV_OK;
970 }
971
972 /* A DWP (.dwp) package object never contains .debug_addr,
973 only a normal .o or executable object.
974 Error returned here is on dbg, not tieddbg.
975 This looks for DW_AT_addr_base and if present
976 adds it in appropriately.
977 Use _dwarf_look_in_local_and_tied_by_index()
978 instead of this, in general.
979 */
980 static int
981 _dwarf_extract_address_from_debug_addr(Dwarf_Debug dbg,
982 Dwarf_CU_Context context,
983 Dwarf_Unsigned index_to_addr,
984 Dwarf_Addr *addr_out,
985 Dwarf_Error *error)
986 {
987 Dwarf_Unsigned address_base = 0;
988 Dwarf_Unsigned addrindex = index_to_addr;
989 Dwarf_Unsigned addr_offset = 0;
990 Dwarf_Unsigned ret_addr = 0;
991 int res = 0;
992 Dwarf_Byte_Ptr sectionstart = 0;
993 Dwarf_Byte_Ptr sectionend = 0;
994 Dwarf_Unsigned sectionsize = 0;
995
996 address_base = context->cc_addr_base_offset;
997 res = _dwarf_load_section(dbg, &dbg->de_debug_addr,error);
998 if (res != DW_DLV_OK) {
999 /* Ignore the inner error, report something meaningful */
1000 if (res == DW_DLV_ERROR && error) {
1001 dwarf_dealloc_error(dbg,*error);
1002 *error = 0;
1003 }
1004 _dwarf_error(dbg,error,
1005 DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION);
1006 return DW_DLV_ERROR;
1007 }
1008 /* DW_FORM_addrx has a base value from the CU die:
1009 DW_AT_addr_base. DW_OP_addrx and DW_OP_constx
1010 rely on DW_AT_addr_base too. */
1011 /* DW_FORM_GNU_addr_index relies on DW_AT_GNU_addr_base
1012 which is in the CU die. */
1013
1014 sectionstart = dbg->de_debug_addr.dss_data;
1015 addr_offset = address_base +
1016 (addrindex * context->cc_address_size);
1017 /* The offsets table is a series of address-size entries
1018 but with a base. */
1019 sectionsize = dbg->de_debug_addr.dss_size;
1020 sectionend = sectionstart + sectionsize;
1021 /* At this point we have a local .debug_addr table
1022 Might get here on dbg or tied-dbg. Check either way
1023 ASSERT: cc_address_size is sensible (small) */
1024 if (addrindex >= sectionsize ||
1025 (addrindex*context->cc_address_size) >= sectionsize ||
1026 addr_offset > sectionsize ||
1027 addr_offset > (sectionsize - context->cc_address_size)) {
1028 dwarfstring m;
1029
1030 /* Was DW_DLE_ATTR_FORM_SIZE_BAD. Regression issue */
1031 dwarfstring_constructor(&m);
1032 dwarfstring_append_printf_u(&m,
1033 "DW_DLE_ATTR_FORM_OFFSET_BAD: "
1034 "Extracting an address from .debug_addr fails"
1035 "as the offset is 0x%x ",
1036 addr_offset);
1037 dwarfstring_append_printf_u(&m,
1038 "but the object section is just 0x%x "
1039 "bytes long so there not enough space"
1040 " for an address.",
1041 sectionsize);
1042 _dwarf_error_string(dbg, error,
1043 DW_DLE_ATTR_FORM_OFFSET_BAD,
1044 dwarfstring_string(&m));
1045 dwarfstring_destructor(&m);
1046 return DW_DLV_ERROR;
1047 }
1048 READ_UNALIGNED_CK(dbg,ret_addr,Dwarf_Addr,
1049 sectionstart + addr_offset,
1050 context->cc_address_size,
1051 error,sectionend);
1052 *addr_out = ret_addr;
1053 return DW_DLV_OK;
1054 }
1055
1056 /* Looks for an address (Dwarf_Addr) value vi an
1057 index into debug_addr. If it fails with
1058 DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION we
1059 find a context in tieddbg and look there. */
1060 int
1061 _dwarf_look_in_local_and_tied_by_index(
1062 Dwarf_Debug dbg,
1063 Dwarf_CU_Context context,
1064 Dwarf_Unsigned index,
1065 Dwarf_Addr *return_addr,
1066 Dwarf_Error *error)
1067 {
1068 int res2 = 0;
1069
1070 res2 = _dwarf_extract_address_from_debug_addr(dbg,
1071 context, index, return_addr, error);
1072 if (res2 != DW_DLV_OK) {
1073 if (res2 == DW_DLV_ERROR &&
1074 error && dwarf_errno(*error) ==
1075 DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION
1076 && dbg->de_tied_data.td_tied_object) {
1077 /* see also DBG_HAS_SECONDARY macro */
1078 int res3 = 0;
1079
1080 /* We do not want to leak error structs... */
1081 /* *error safe */
1082 dwarf_dealloc_error(dbg,*error);
1083 *error = 0; /* *error safe */
1084 /* Any error is returned on dbg,
1085 not tieddbg. */
1086 res3 = _dwarf_get_addr_from_tied(dbg,
1087 context,index,return_addr,error);
1088 return res3;
1089 }
1090 return res2;
1091 }
1092 return DW_DLV_OK;
1093 }
1094
1095 /* The DIE here can be any DIE in the relevant CU.
1096 index is an index into .debug_addr */
1097 int
1098 dwarf_debug_addr_index_to_addr(Dwarf_Die die,
1099 Dwarf_Unsigned index,
1100 Dwarf_Addr *return_addr,
1101 Dwarf_Error *error)
1102 {
1103 Dwarf_Debug dbg = 0;
1104 Dwarf_CU_Context context = 0;
1105 int res = 0;
1106
1107 CHECK_DIE(die, DW_DLV_ERROR);
1108 context = die->di_cu_context;
1109 dbg = context->cc_dbg;
1110
1111 /* error is returned on dbg, not tieddbg. */
1112 res = _dwarf_look_in_local_and_tied_by_index(dbg,
1113 context,
1114 index,
1115 return_addr,
1116 error);
1117 return res;
1118 }
1119 /* ASSERT:
1120 attr_form == DW_FORM_GNU_addr_index ||
1121 attr_form == DW_FORM_addrx
1122 We are looking for data in .debug_addr,
1123 which could be in base file or in tied-file..
1124 */
1125 int
1126 _dwarf_look_in_local_and_tied(Dwarf_Half attr_form,
1127 Dwarf_CU_Context context,
1128 Dwarf_Small *info_ptr,
1129 Dwarf_Addr *return_addr,
1130 Dwarf_Error *error)
1131 {
1132 int res2 = 0;
1133 Dwarf_Unsigned index_to_addr = 0;
1134 Dwarf_Debug dbg = 0;
1135
1136 /* We get the index. It might apply here
1137 or in tied object. Checking that next. */
1138 dbg = context->cc_dbg;
1139 res2 = _dwarf_get_addr_index_itself(attr_form,
1140 info_ptr,dbg,context, &index_to_addr,error);
1141 if (res2 != DW_DLV_OK) {
1142 return res2;
1143 }
1144 /* error is returned on dbg, not tieddbg. */
1145 res2 = _dwarf_look_in_local_and_tied_by_index(
1146 dbg,context,index_to_addr,return_addr,error);
1147 return res2;
1148
1149 }
1150
1151 static int
1152 _dwarf_lowpc_internal(Dwarf_Die die,
1153 Dwarf_Half attrnum,
1154 const char *msg,
1155 Dwarf_Addr *return_addr,
1156 Dwarf_Error *error)
1157 {
1158 Dwarf_Addr ret_addr = 0;
1159 Dwarf_Byte_Ptr info_ptr = 0;
1160 Dwarf_Half attr_form = 0;
1161 Dwarf_Debug dbg = 0;
1162 Dwarf_Half address_size = 0;
1163 Dwarf_Half offset_size = 0;
1164 int version = 0;
1165 enum Dwarf_Form_Class class = DW_FORM_CLASS_UNKNOWN;
1166 int res = 0;
1167 Dwarf_CU_Context context = 0;
1168 Dwarf_Small *die_info_end = 0;
1169
1170 CHECK_DIE(die, DW_DLV_ERROR);
1171 context = die->di_cu_context;
1172 dbg = context->cc_dbg;
1173 address_size = context->cc_address_size;
1174 offset_size = context->cc_length_size;
1175 res = _dwarf_get_value_ptr(die, attrnum,
1176 &attr_form,&info_ptr,0,error);
1177 if (res == DW_DLV_ERROR) {
1178 return res;
1179 }
1180 if (res == DW_DLV_NO_ENTRY) {
1181 return res;
1182 }
1183 version = context->cc_version_stamp;
1184 class = dwarf_get_form_class(version,attrnum,
1185 offset_size,attr_form);
1186 if (class != DW_FORM_CLASS_ADDRESS) {
1187 /* Not a correct FORM for low_pc or entry_pc */
1188 _dwarf_error_string(dbg, error, DW_DLE_LOWPC_WRONG_CLASS,
1189 (char *)msg);
1190 return DW_DLV_ERROR;
1191 }
1192
1193 if (attr_form == DW_FORM_GNU_addr_index ||
1194 attr_form == DW_FORM_addrx) {
1195 /* error is returned on dbg, not tieddbg. */
1196 res = _dwarf_look_in_local_and_tied(
1197 attr_form,
1198 context,
1199 info_ptr,
1200 return_addr,
1201 error);
1202 return res;
1203 }
1204 die_info_end = _dwarf_calculate_info_section_end_ptr(context);
1205 READ_UNALIGNED_CK(dbg, ret_addr, Dwarf_Addr,
1206 info_ptr, address_size,
1207 error,die_info_end);
1208
1209 *return_addr = ret_addr;
1210 return DW_DLV_OK;
1211 }
1212 int
1213 dwarf_lowpc(Dwarf_Die die,
1214 Dwarf_Addr *return_addr,
1215 Dwarf_Error *error)
1216 {
1217 int res = 0;
1218
1219 res = _dwarf_lowpc_internal (die,DW_AT_low_pc,
1220 "DW_AT_low_pc data unavailable",
1221 return_addr,error);
1222 return res;
1223 }
1224 int
1225 _dwarf_entrypc(Dwarf_Die die,
1226 Dwarf_Addr *return_addr,
1227 Dwarf_Error *error)
1228 {
1229 int res = 0;
1230
1231 res = _dwarf_lowpc_internal (die,DW_AT_entry_pc,
1232 "DW_AT_entry_pc and DW_AT_low_pc data unavailable",
1233 return_addr,error);
1234 return res;
1235 }
1236
1237 /* If 'die' contains the DW_AT_type attribute, it returns
1238 the (global) offset referenced by the attribute through
1239 the return_off pointer.
1240 Returns through return_is_info which section applies.
1241 In case of DW_DLV_NO_ENTRY or DW_DLV_ERROR it sets offset zero. */
1242 int
1243 dwarf_dietype_offset(Dwarf_Die die,
1244 Dwarf_Off *return_off,
1245 Dwarf_Bool *return_is_info,
1246 Dwarf_Error *error)
1247 {
1248 int res = 0;
1249 Dwarf_Off offset = 0;
1250 Dwarf_Attribute attr = 0;
1251 Dwarf_Bool is_info = 0;
1252
1253 CHECK_DIE(die, DW_DLV_ERROR);
1254 /* Lets see where the input die is */
1255 is_info = dwarf_get_die_infotypes_flag(die);
1256 res = dwarf_attr(die,DW_AT_type,&attr,error);
1257 if (res == DW_DLV_OK) {
1258 if (attr->ar_attribute_form_direct == DW_FORM_ref_sig8){
1259 is_info = FALSE;
1260 }
1261 res = dwarf_global_formref(attr,&offset,error);
1262 if (res == DW_DLV_OK) {
1263 *return_off = offset;
1264 *return_is_info = is_info;
1265 }
1266 dwarf_dealloc_attribute(attr);
1267 }
1268 return res;
1269 }
1270
1271 /* Only a few values are inherited from the tied
1272 file. Not rnglists or loclists base offsets?
1273 merging into main context (dwp) from tieddbg (Skeleton).
1274 and returning a pointer to the tiedcontext created here.
1275 (such contexts are freed by dwarf_finish on the tied
1276 object file).
1277 */
1278 int
1279 _dwarf_merge_all_base_attrs_of_cu_die(Dwarf_CU_Context context,
1280 Dwarf_Debug tieddbg,
1281 Dwarf_CU_Context *tiedcontext_out,
1282 Dwarf_Error *error)
1283 {
1284 Dwarf_CU_Context tiedcontext = 0;
1285 int res = 0;
1286
1287 if (!tieddbg) {
1288 return DW_DLV_NO_ENTRY;
1289 }
1290 if (!context->cc_signature_present) {
1291 return DW_DLV_NO_ENTRY;
1292 }
1293 res = _dwarf_search_for_signature(tieddbg,
1294 context->cc_signature,
1295 &tiedcontext,
1296 error);
1297 if ( res == DW_DLV_ERROR) {
1298 return res;
1299 }
1300 if ( res == DW_DLV_NO_ENTRY) {
1301 return res;
1302 }
1303 if (tiedcontext->cc_low_pc_present) {
1304 /* A dwo/dwp will not have this, merge from tied
1305 Needed for rnglists/loclists
1306 */
1307 context->cc_low_pc_present =
1308 tiedcontext->cc_low_pc_present;
1309 context-> cc_low_pc =
1310 tiedcontext->cc_low_pc;
1311 }
1312
1313 if (tiedcontext->cc_base_address_present) {
1314 context->cc_base_address_present =
1315 tiedcontext->cc_base_address_present;
1316 context-> cc_base_address =
1317 tiedcontext->cc_base_address;
1318 }
1319 if (tiedcontext->cc_addr_base_offset_present) {
1320 /* This is a base-offset, not an address. */
1321 context-> cc_addr_base_offset_present =
1322 tiedcontext->cc_addr_base_offset_present;
1323 context-> cc_addr_base_offset=
1324 tiedcontext->cc_addr_base_offset;
1325 }
1326 if (context->cc_version_stamp == DW_CU_VERSION4 ||
1327 context->cc_version_stamp == DW_CU_VERSION5) {
1328 #if 0 /* we do not inherit cc_rnglists_base_present */
1329 /* This inheritance has been removed from DWARF6
1330 during 2024. */
1331
1332 if (!context->cc_rnglists_base_present) {
1333 context->cc_rnglists_base_present =
1334 tiedcontext->cc_rnglists_base_present;
1335 context->cc_rnglists_base =
1336 tiedcontext->cc_rnglists_base;
1337 }
1338 #endif
1339 if (!context->cc_ranges_base_present) {
1340 context->cc_ranges_base_present=
1341 tiedcontext->cc_ranges_base_present;
1342 context->cc_ranges_base=
1343 tiedcontext->cc_ranges_base;
1344 }
1345 }
1346 if (!context->cc_str_offsets_tab_present) {
1347 context-> cc_str_offsets_tab_present =
1348 tiedcontext->cc_str_offsets_tab_present;
1349 context-> cc_str_offsets_header_offset=
1350 tiedcontext->cc_str_offsets_header_offset;
1351 context-> cc_str_offsets_tab_to_array=
1352 tiedcontext->cc_str_offsets_tab_to_array;
1353 context-> cc_str_offsets_table_size=
1354 tiedcontext->cc_str_offsets_table_size;
1355 context-> cc_str_offsets_version=
1356 tiedcontext->cc_str_offsets_version;
1357 context-> cc_str_offsets_offset_size=
1358 tiedcontext->cc_str_offsets_offset_size;
1359 }
1360 if (tiedcontext_out) {
1361 *tiedcontext_out = tiedcontext;
1362 }
1363 return DW_DLV_OK;
1364 }
1365
1366 int
1367 _dwarf_get_string_base_attr_value(Dwarf_Debug dbg,
1368 Dwarf_CU_Context context,
1369 Dwarf_Unsigned *sbase_out,
1370 Dwarf_Error *error)
1371 {
1372 (void)dbg;
1373 (void)error;
1374 if (context->cc_str_offsets_tab_present) {
1375 *sbase_out = context->cc_str_offsets_header_offset;
1376 return DW_DLV_OK;
1377 }
1378 *sbase_out = 0;
1379 return DW_DLV_OK;
1380 }
1381 /* Goes to the CU die and finds the DW_AT_GNU_addr_base
1382 (or DW_AT_addr_base ) and gets the value from that CU die
1383 and returns it through abase_out. If we cannot find the value
1384 it is a serious error in the DWARF.
1385 */
1386
1387 /* This works for all versions of DWARF.
1388 The consumer has to check the return_form or
1389 return_class to decide if the value returned
1390 through return_value is an address or an address-offset.
1391
1392 See DWARF4 section 2.17.2,
1393 "Contiguous Address Range".
1394 */
1395 int
1396 dwarf_highpc_b(Dwarf_Die die,
1397 Dwarf_Addr *return_value,
1398 Dwarf_Half *return_form,
1399 enum Dwarf_Form_Class *return_class,
1400 Dwarf_Error *error)
1401 {
1402 Dwarf_Byte_Ptr info_ptr = 0;
1403 Dwarf_Half attr_form = 0;
1404 Dwarf_Debug dbg = 0;
1405 Dwarf_Half address_size = 0;
1406 Dwarf_Half offset_size = 0;
1407 enum Dwarf_Form_Class class = DW_FORM_CLASS_UNKNOWN;
1408 Dwarf_Half version = 0;
1409 Dwarf_Byte_Ptr die_info_end = 0;
1410 int res = 0;
1411
1412 CHECK_DIE(die, DW_DLV_ERROR);
1413 dbg = die->di_cu_context->cc_dbg;
1414 address_size = die->di_cu_context->cc_address_size;
1415
1416 res = _dwarf_get_value_ptr(die, DW_AT_high_pc,
1417 &attr_form,&info_ptr,0,error);
1418 if (res == DW_DLV_ERROR) {
1419 return res;
1420 }
1421 if (res == DW_DLV_NO_ENTRY) {
1422 return res;
1423 }
1424 die_info_end = _dwarf_calculate_info_section_end_ptr(
1425 die->di_cu_context);
1426
1427 version = die->di_cu_context->cc_version_stamp;
1428 offset_size = die->di_cu_context->cc_length_size;
1429 class = dwarf_get_form_class(version,DW_AT_high_pc,
1430 offset_size,attr_form);
1431
1432 if (class == DW_FORM_CLASS_ADDRESS) {
1433 Dwarf_Addr addr = 0;
1434 if (dwarf_addr_form_is_indexed(attr_form)) {
1435 Dwarf_Unsigned addr_out = 0;
1436 Dwarf_Unsigned index_to_addr = 0;
1437 int res2 = 0;
1438 Dwarf_CU_Context context = die->di_cu_context;
1439
1440 /* index_to_addr we get here might apply
1441 to this dbg or to tieddbg. */
1442 /* error is returned on dbg, not tied */
1443 res2 = _dwarf_get_addr_index_itself(attr_form,
1444 info_ptr,dbg,context,&index_to_addr,error);
1445 if (res2 != DW_DLV_OK) {
1446 return res2;
1447 }
1448 res2= _dwarf_look_in_local_and_tied_by_index(dbg,
1449 context,
1450 index_to_addr,
1451 &addr_out,
1452 error);
1453 if (res2 != DW_DLV_OK) {
1454 return res2;
1455 }
1456 }
1457 READ_UNALIGNED_CK(dbg, addr, Dwarf_Addr,
1458 info_ptr, address_size,
1459 error,die_info_end);
1460 *return_value = addr;
1461 } else {
1462 int res3 = 0;
1463 Dwarf_Unsigned v = 0;
1464 res3 = _dwarf_die_attr_unsigned_constant(die,DW_AT_high_pc,
1465 &v,error);
1466 if (res3 != DW_DLV_OK) {
1467 Dwarf_Byte_Ptr info_ptr2 = 0;
1468
1469 res3 = _dwarf_get_value_ptr(die, DW_AT_high_pc,
1470 &attr_form,&info_ptr2,0,error);
1471 if (res3 == DW_DLV_ERROR) {
1472 return res3;
1473 }
1474 if (res3 == DW_DLV_NO_ENTRY) {
1475 return res3;
1476 }
1477 if (attr_form == DW_FORM_sdata) {
1478 Dwarf_Signed sval = 0;
1479
1480 /* DWARF4 defines the value as an unsigned offset
1481 in section 2.17.2. */
1482 DECODE_LEB128_UWORD_CK(info_ptr2, sval,
1483 dbg,error,die_info_end);
1484 *return_value = (Dwarf_Unsigned)sval;
1485 } else {
1486 _dwarf_error(dbg, error, DW_DLE_HIGHPC_WRONG_FORM);
1487 return DW_DLV_ERROR;
1488 }
1489 } else {
1490 *return_value = v;
1491 }
1492 }
1493 /* Allow null args starting 22 April 2019. */
1494 if (return_form) {
1495 *return_form = attr_form;
1496 }
1497 if (return_class) {
1498 *return_class = class;
1499 }
1500 return DW_DLV_OK;
1501 }
1502
1503 /* The dbg and context here are a file with DW_FORM_addrx
1504 but missing .debug_addr. So go to the tied file
1505 and using the signature from the current context
1506 locate the target CU in the tied file Then
1507 get the address.
1508
1509 */
1510 int
1511 _dwarf_get_addr_from_tied(Dwarf_Debug primary_dbg,
1512 Dwarf_CU_Context context,
1513 Dwarf_Unsigned index,
1514 Dwarf_Addr *addr_out,
1515 Dwarf_Error*error)
1516 {
1517 Dwarf_Debug tieddbg = 0;
1518 int res = 0;
1519 Dwarf_Addr local_addr = 0;
1520 Dwarf_CU_Context tiedcontext = 0;
1521 Dwarf_Unsigned addrtabsize = 0;
1522
1523 if (!context->cc_signature_present) {
1524 _dwarf_error(primary_dbg, error,
1525 DW_DLE_NO_SIGNATURE_TO_LOOKUP);
1526 return DW_DLV_ERROR;
1527 }
1528 if (!DBG_HAS_SECONDARY(primary_dbg)) {
1529 _dwarf_error(primary_dbg, error,
1530 DW_DLE_NO_TIED_ADDR_AVAILABLE);
1531 return DW_DLV_ERROR;
1532 }
1533 tieddbg = primary_dbg->de_secondary_dbg;
1534 res = _dwarf_search_for_signature(tieddbg,
1535 context->cc_signature,
1536 &tiedcontext,
1537 error);
1538 if (res == DW_DLV_ERROR) {
1539 return res;
1540 }
1541 if ( res == DW_DLV_NO_ENTRY) {
1542 return res;
1543 }
1544 /* We have .debug_addr */
1545 addrtabsize = tieddbg->de_debug_addr.dss_size;
1546 if ( (index > tieddbg->de_filesize ||
1547 index > addrtabsize ||
1548 (index*tiedcontext->cc_address_size) > addrtabsize)) {
1549 _dwarf_error_string(primary_dbg,error,
1550 DW_DLE_ATTR_FORM_OFFSET_BAD,
1551 "DW_DLE_ATTR_FORM_OFFSET_BAD "
1552 "Looking for an index from an addr FORM "
1553 "we find an impossibly large index value for the tied "
1554 "object. Corrupt DWARF");
1555 return DW_DLV_ERROR;
1556 }
1557 res = _dwarf_extract_address_from_debug_addr(tieddbg,
1558 tiedcontext,
1559 index,
1560 &local_addr,
1561 error);
1562 if ( res == DW_DLV_ERROR) {
1563 return res;
1564 }
1565 if ( res == DW_DLV_NO_ENTRY) {
1566 return res;
1567 }
1568 *addr_out = local_addr;
1569 return DW_DLV_OK;
1570 }
1571
1572 /*
1573 Takes a die, an attribute attr, and checks if attr
1574 occurs in die. Attr is required to be an attribute
1575 whose form is in the "constant" class. If attr occurs
1576 in die, the value is returned.
1577
1578 It does not really allow for a signed constant, and
1579 DWARF does not always specify that only non-negative
1580 values are allowed..
1581
1582 Returns DW_DLV_OK, DW_DLV_ERROR, or DW_DLV_NO_ENTRY as
1583 appropriate. Sets the value thru the pointer return_val.
1584
1585 This function is meant to do all the
1586 processing for dwarf_bytesize, dwarf_bitsize, dwarf_bitoffset,
1587 and dwarf_srclang. And it helps in dwarf_highpc_with_form().
1588 */
1589 static int
1590 _dwarf_die_attr_unsigned_constant(Dwarf_Die die,
1591 Dwarf_Half attr,
1592 Dwarf_Unsigned *return_val,
1593 Dwarf_Error *error)
1594 {
1595 Dwarf_Byte_Ptr info_ptr = 0;
1596 Dwarf_Half attr_form = 0;
1597 Dwarf_Unsigned ret_value = 0;
1598 Dwarf_Signed implicit_const_value = 0;
1599 Dwarf_Debug dbg = 0;
1600 int res = 0;
1601 Dwarf_Byte_Ptr die_info_end = 0;
1602
1603 CHECK_DIE(die, DW_DLV_ERROR);
1604
1605 die_info_end =
1606 _dwarf_calculate_info_section_end_ptr(die->di_cu_context);
1607 dbg = die->di_cu_context->cc_dbg;
1608 res = _dwarf_get_value_ptr(die,attr,&attr_form,
1609 &info_ptr,&implicit_const_value,error);
1610 if (res != DW_DLV_OK) {
1611 return res;
1612 }
1613 switch (attr_form) {
1614 case DW_FORM_data1:
1615 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1616 info_ptr, sizeof(Dwarf_Small),
1617 error,die_info_end);
1618 *return_val = ret_value;
1619 return DW_DLV_OK;
1620
1621 case DW_FORM_data2:
1622 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1623 info_ptr, sizeof(Dwarf_Shalf),
1624 error,die_info_end);
1625 *return_val = ret_value;
1626 return DW_DLV_OK;
1627
1628 case DW_FORM_data4:
1629 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1630 info_ptr, DWARF_32BIT_SIZE,
1631 error,die_info_end);
1632 *return_val = ret_value;
1633 return DW_DLV_OK;
1634
1635 case DW_FORM_data8:
1636 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1637 info_ptr, DWARF_64BIT_SIZE,
1638 error,die_info_end);
1639 *return_val = ret_value;
1640 return DW_DLV_OK;
1641
1642 case DW_FORM_udata: {
1643 Dwarf_Unsigned v = 0;
1644
1645 DECODE_LEB128_UWORD_CK(info_ptr, v,dbg,error,die_info_end);
1646 *return_val = v;
1647 return DW_DLV_OK;
1648
1649 }
1650 case DW_FORM_implicit_const: {
1651 if (implicit_const_value < (Dwarf_Signed)0) {
1652 dwarfstring m;
1653
1654 dwarfstring_constructor(&m);
1655 dwarfstring_append_printf_i(&m,
1656 "DW_DLE_NEGATIVE_SIZE "
1657 "An implicit const value of "
1658 "%d is inappropriate as a size",
1659 implicit_const_value);
1660 _dwarf_error_string(dbg, error,
1661 DW_DLE_NEGATIVE_SIZE,
1662 dwarfstring_string(&m));
1663 dwarfstring_destructor(&m);
1664 return DW_DLV_ERROR;
1665 }
1666 *return_val = implicit_const_value;
1667 return DW_DLV_OK;
1668 }
1669
1670 default:
1671 _dwarf_error(dbg, error, DW_DLE_DIE_BAD);
1672 return DW_DLV_ERROR;
1673 }
1674 }
1675
1676 /* Size Value >= 0 is not specified in DWARF5, but
1677 a negative value is surely not meaningful. */
1678 int
1679 dwarf_bytesize(Dwarf_Die die,
1680 Dwarf_Unsigned *ret_size, Dwarf_Error *error)
1681 {
1682 Dwarf_Unsigned luns = 0;
1683 int res = _dwarf_die_attr_unsigned_constant(die, DW_AT_byte_size,
1684 &luns, error);
1685 *ret_size = luns;
1686 return res;
1687 }
1688
1689 /* Size Value >= 0 is not specified in DWARF5, but
1690 a negative value is not meaningful. */
1691 int
1692 dwarf_bitsize(Dwarf_Die die,
1693 Dwarf_Unsigned *ret_size, Dwarf_Error *error)
1694 {
1695 Dwarf_Unsigned luns = 0;
1696 int res = _dwarf_die_attr_unsigned_constant(die, DW_AT_bit_size,
1697 &luns, error);
1698 *ret_size = luns;
1699 return res;
1700 }
1701
1702 /* Size Value >= 0 required. DWARF5 sec5.7.6
1703 The definition of DW_AT_data_bit_offset
1704 (DWARF4, DWARF5) is radically
1705 different from DW_AT_bit_offset (DWARF2,
1706 DWARF3. */
1707 int
1708 dwarf_bitoffset(Dwarf_Die die,
1709 Dwarf_Half *attribute,
1710 Dwarf_Unsigned *ret_offset,
1711 Dwarf_Error *error)
1712 {
1713 Dwarf_Unsigned luns = 0;
1714 int res = 0;
1715 /* DWARF4,5 case */
1716 res = _dwarf_die_attr_unsigned_constant(die,
1717 DW_AT_data_bit_offset, &luns, error);
1718 if (res == DW_DLV_NO_ENTRY) {
1719 /* DWARF2, DWARF3 case. */
1720 res = _dwarf_die_attr_unsigned_constant(die,
1721 DW_AT_bit_offset, &luns, error);
1722 if (res == DW_DLV_OK) {
1723 *attribute = DW_AT_bit_offset;
1724 *ret_offset = luns;
1725 return DW_DLV_OK;
1726 }
1727 } else if (res == DW_DLV_OK) {
1728 *attribute = DW_AT_data_bit_offset;
1729 *ret_offset = luns;
1730 return DW_DLV_OK;
1731 } else { /* fall to return */ }
1732 return res;
1733 }
1734
1735 /* Refer section 3.1, page 21 in Dwarf Definition.
1736 Language codes are always non-negative
1737 and specified in the DWARF standard*/
1738 int
1739 dwarf_srclang(Dwarf_Die die,
1740 Dwarf_Unsigned *ret_size, Dwarf_Error *error)
1741 {
1742 Dwarf_Unsigned luns = 0;
1743 int res = _dwarf_die_attr_unsigned_constant(die, DW_AT_language,
1744 &luns, error);
1745 *ret_size = luns;
1746 return res;
1747 }
1748
1749 /* Refer section 5.4, page 37 in Dwarf Definition.
1750 array order values are always non-negative
1751 and specified in the DWARF standard*/
1752 int
1753 dwarf_arrayorder(Dwarf_Die die,
1754 Dwarf_Unsigned *ret_size, Dwarf_Error *error)
1755 {
1756 Dwarf_Unsigned luns = 0;
1757 int res = _dwarf_die_attr_unsigned_constant(die, DW_AT_ordering,
1758 &luns, error);
1759 *ret_size = luns;
1760 return res;
1761 }
1762
1763 /* Return DW_DLV_OK if ok
1764 DW_DLV_ERROR if failure.
1765
1766 attr must be a valid attribute pointer.
1767
1768 If the die and the attr are not related the result is
1769 meaningless. */
1770 int
1771 dwarf_attr_offset(Dwarf_Die die, Dwarf_Attribute attr,
1772 Dwarf_Off *offset /* return offset thru this ptr */,
1773 Dwarf_Error *error)
1774 {
1775 Dwarf_Off attroff = 0;
1776 Dwarf_Small *dataptr = 0;
1777 Dwarf_Debug dbg = 0;
1778
1779 CHECK_DIE(die, DW_DLV_ERROR);
1780 dbg = die->di_cu_context->cc_dbg;
1781 dataptr = die->di_is_info? dbg->de_debug_info.dss_data:
1782 dbg->de_debug_types.dss_data;
1783
1784 attroff = (attr->ar_debug_ptr - dataptr);
1785 *offset = attroff;
1786 return DW_DLV_OK;
1787 }
1788
1789 Dwarf_Unsigned
1790 dwarf_die_abbrev_code(Dwarf_Die die)
1791 {
1792 if (!die) {
1793 return 0;
1794 }
1795 return die->di_abbrev_code;
1796 }
1797
1798 /* Returns a flag through ablhas_child. Non-zero if
1799 the DIE has children, zero if it does not.
1800 It has no Dwarf_Error arg!
1801 */
1802 int
1803 dwarf_die_abbrev_children_flag(Dwarf_Die die,Dwarf_Half *ab_has_child)
1804 {
1805 if (!die) {
1806 return DW_DLV_ERROR;
1807 }
1808 if (die->di_abbrev_list) {
1809 *ab_has_child = die->di_abbrev_list->abl_has_child;
1810 return DW_DLV_OK;
1811 }
1812 return DW_DLV_ERROR;
1813 }
1814
1815 /* Helper function for finding form class.
1816 Only called for FORMs that might be offsets
1817 to one or another section. */
1818 static enum Dwarf_Form_Class
1819 dw_get_special_offset(Dwarf_Half attrnum,
1820 Dwarf_Half dwversion)
1821 {
1822 switch (attrnum) {
1823 case DW_AT_stmt_list:
1824 return DW_FORM_CLASS_LINEPTR;
1825 case DW_AT_macro_info: /* DWARF2-DWARF4 */
1826 return DW_FORM_CLASS_MACPTR;
1827 case DW_AT_start_scope:
1828 case DW_AT_ranges: {
1829 if (dwversion <= 4) {
1830 return DW_FORM_CLASS_RANGELISTPTR;
1831 }
1832 return DW_FORM_CLASS_RNGLIST;
1833 }
1834 case DW_AT_GNU_ranges_base: /* DWARF5-like */
1835 case DW_AT_rnglists_base: /* DWARF5 */
1836 return DW_FORM_CLASS_RNGLISTSPTR;
1837 case DW_AT_GNU_macros: /* DWARF5-like */
1838 case DW_AT_macros: /* DWARF5 */
1839 return DW_FORM_CLASS_MACROPTR;
1840 case DW_AT_loclists_base: /* DWARF5 */
1841 return DW_FORM_CLASS_LOCLISTSPTR;
1842 case DW_AT_GNU_addr_base: /* DWARF55-like */
1843 case DW_AT_addr_base: /* DWARF5 */
1844 return DW_FORM_CLASS_ADDRPTR;
1845 case DW_AT_str_offsets_base: /* DWARF5 */
1846 return DW_FORM_CLASS_STROFFSETSPTR;
1847
1848 case DW_AT_location:
1849 case DW_AT_string_length:
1850 case DW_AT_return_addr:
1851 case DW_AT_data_member_location:
1852 case DW_AT_frame_base:
1853 case DW_AT_segment:
1854 case DW_AT_static_link:
1855 case DW_AT_use_location:
1856 case DW_AT_vtable_elem_location: {
1857 if (dwversion <= 4) {
1858 return DW_FORM_CLASS_LOCLIST;
1859 }
1860 return DW_FORM_CLASS_LOCLISTPTR;
1861 }
1862 case DW_AT_GNU_locviews:
1863 case DW_AT_sibling:
1864 case DW_AT_byte_size :
1865 case DW_AT_bit_offset :
1866 case DW_AT_bit_size :
1867 case DW_AT_discr :
1868 case DW_AT_import :
1869 case DW_AT_common_reference:
1870 case DW_AT_containing_type:
1871 case DW_AT_default_value:
1872 case DW_AT_lower_bound:
1873 case DW_AT_bit_stride:
1874 case DW_AT_upper_bound:
1875 case DW_AT_abstract_origin:
1876 case DW_AT_base_types:
1877 case DW_AT_count:
1878 case DW_AT_friend:
1879 case DW_AT_namelist_item:
1880 case DW_AT_priority:
1881 case DW_AT_specification:
1882 case DW_AT_type:
1883 case DW_AT_allocated:
1884 case DW_AT_associated:
1885 case DW_AT_byte_stride:
1886 case DW_AT_extension:
1887 case DW_AT_trampoline:
1888 case DW_AT_small:
1889 case DW_AT_object_pointer:
1890 case DW_AT_signature:
1891 return DW_FORM_CLASS_REFERENCE;
1892 case DW_AT_GNU_entry_view:
1893 return DW_FORM_CLASS_CONSTANT;
1894 case DW_AT_MIPS_fde: /* SGI/IRIX extension */
1895 return DW_FORM_CLASS_FRAMEPTR;
1896 default: break;
1897 }
1898 return DW_FORM_CLASS_UNKNOWN;
1899 }
1900
1901 static int
1902 block_means_locexpr(Dwarf_Half attr)
1903 {
1904 switch(attr) {
1905 case DW_AT_bit_size:
1906 case DW_AT_byte_size:
1907 case DW_AT_call_data_location:
1908 case DW_AT_call_data_value:
1909 case DW_AT_call_value:
1910 case DW_AT_data_member_location:
1911 case DW_AT_frame_base:
1912 case DW_AT_GNU_call_site_target:
1913 case DW_AT_GNU_call_site_value:
1914 case DW_AT_location:
1915 case DW_AT_return_addr:
1916 case DW_AT_segment:
1917 case DW_AT_static_link:
1918 case DW_AT_string_length:
1919 case DW_AT_use_location:
1920 case DW_AT_vtable_elem_location:
1921 return TRUE;
1922 default: break;
1923 }
1924 return FALSE;
1925 }
1926
1927 /* It takes 4 pieces of data (including the FORM)
1928 to accurately determine the form 'class' as documented
1929 in the DWARF spec. This is per DWARF4, but will work
1930 for DWARF2 or 3 as well. */
1931 enum Dwarf_Form_Class
1932 dwarf_get_form_class(
1933 Dwarf_Half dwversion,
1934 Dwarf_Half attrnum,
1935 Dwarf_Half offset_size,
1936 Dwarf_Half form)
1937 {
1938 switch (form) {
1939 case DW_FORM_addr: return DW_FORM_CLASS_ADDRESS;
1940 case DW_FORM_data2: return DW_FORM_CLASS_CONSTANT;
1941
1942 case DW_FORM_data4:
1943 if (dwversion <= 3 && offset_size == 4) {
1944 enum Dwarf_Form_Class class =
1945 dw_get_special_offset(attrnum, dwversion);
1946 if (class != DW_FORM_CLASS_UNKNOWN) {
1947 return class;
1948 }
1949 }
1950 return DW_FORM_CLASS_CONSTANT;
1951 case DW_FORM_data8:
1952 if (dwversion <= 3 && offset_size == 8) {
1953 enum Dwarf_Form_Class class =
1954 dw_get_special_offset(attrnum, dwversion);
1955 if (class != DW_FORM_CLASS_UNKNOWN) {
1956 return class;
1957 }
1958 }
1959 return DW_FORM_CLASS_CONSTANT;
1960 case DW_FORM_sec_offset:
1961 {
1962 enum Dwarf_Form_Class class =
1963 dw_get_special_offset(attrnum, dwversion);
1964 if (class != DW_FORM_CLASS_UNKNOWN) {
1965 return class;
1966 }
1967 }
1968 /* We do not know what this is. */
1969 return DW_FORM_CLASS_UNKNOWN;
1970 break;
1971
1972 case DW_FORM_string: return DW_FORM_CLASS_STRING;
1973 case DW_FORM_strp: return DW_FORM_CLASS_STRING;
1974
1975 case DW_FORM_block:
1976 case DW_FORM_block1:
1977 case DW_FORM_block2:
1978 case DW_FORM_block4:
1979 if (dwversion <= 3) {
1980 if (block_means_locexpr(attrnum)) {
1981 return DW_FORM_CLASS_EXPRLOC;
1982 }
1983 }
1984 return DW_FORM_CLASS_BLOCK;
1985 /* DWARF4 and DWARF5 */
1986 case DW_FORM_exprloc: return DW_FORM_CLASS_EXPRLOC;
1987
1988 case DW_FORM_data16: return DW_FORM_CLASS_CONSTANT;
1989 case DW_FORM_data1: return DW_FORM_CLASS_CONSTANT;
1990 case DW_FORM_sdata: return DW_FORM_CLASS_CONSTANT;
1991 case DW_FORM_udata: return DW_FORM_CLASS_CONSTANT;
1992
1993 case DW_FORM_ref_addr: return DW_FORM_CLASS_REFERENCE;
1994 case DW_FORM_ref1: return DW_FORM_CLASS_REFERENCE;
1995 case DW_FORM_ref2: return DW_FORM_CLASS_REFERENCE;
1996 case DW_FORM_ref4: return DW_FORM_CLASS_REFERENCE;
1997 case DW_FORM_ref8: return DW_FORM_CLASS_REFERENCE;
1998 case DW_FORM_ref_udata: return DW_FORM_CLASS_REFERENCE;
1999 case DW_FORM_ref_sig8: return DW_FORM_CLASS_REFERENCE;
2000
2001 case DW_FORM_flag: return DW_FORM_CLASS_FLAG;
2002 case DW_FORM_flag_present: return DW_FORM_CLASS_FLAG;
2003
2004 case DW_FORM_addrx:
2005 case DW_FORM_addrx1:
2006 case DW_FORM_addrx2:
2007 case DW_FORM_addrx3:
2008 case DW_FORM_addrx4:
2009 return DW_FORM_CLASS_ADDRESS; /* DWARF5 */
2010 case DW_FORM_GNU_addr_index: return DW_FORM_CLASS_ADDRESS;
2011 case DW_FORM_strx: /* DWARF5 */
2012 case DW_FORM_strx1: /* DWARF5 */
2013 case DW_FORM_strx2: /* DWARF5 */
2014 case DW_FORM_strx3: /* DWARF5 */
2015 case DW_FORM_line_strp: /* DWARF5 */
2016 case DW_FORM_strp_sup: /* DWARF5 */
2017 case DW_FORM_GNU_strp_alt:
2018 return DW_FORM_CLASS_STRING;
2019 case DW_FORM_GNU_str_index: return DW_FORM_CLASS_STRING;
2020
2021 case DW_FORM_rnglistx:
2022 return DW_FORM_CLASS_RNGLIST; /* DWARF5 */
2023 case DW_FORM_loclistx:
2024 return DW_FORM_CLASS_LOCLIST; /* DWARF5 */
2025
2026 case DW_FORM_GNU_ref_alt: return DW_FORM_CLASS_REFERENCE;
2027 case DW_FORM_implicit_const:
2028 return DW_FORM_CLASS_CONSTANT; /* DWARF5 */
2029
2030 case DW_FORM_indirect:
2031 default:
2032 break;
2033 };
2034 return DW_FORM_CLASS_UNKNOWN;
2035 }
2036
2037 /* Given a DIE, figure out what the CU's DWARF version is
2038 and the size of an offset
2039 and return it through the *version pointer and return
2040 DW_DLV_OK.
2041
2042 If we cannot find a CU,
2043 return DW_DLV_ERROR on error.
2044 In case of error no Dwarf_Debug was available,
2045 so setting a Dwarf_Error is somewhat futile.
2046 Never returns DW_DLV_NO_ENTRY.
2047 */
2048 int
2049 dwarf_get_version_of_die(Dwarf_Die die,
2050 Dwarf_Half *version,
2051 Dwarf_Half *offset_size)
2052 {
2053 Dwarf_CU_Context cucontext = 0;
2054 if (!die) {
2055 return DW_DLV_ERROR;
2056 }
2057 cucontext = die->di_cu_context;
2058 if (!cucontext) {
2059 return DW_DLV_ERROR;
2060 }
2061 *version = cucontext->cc_version_stamp;
2062 *offset_size = cucontext->cc_length_size;
2063 return DW_DLV_OK;
2064 }
2065
2066 Dwarf_Byte_Ptr
2067 _dwarf_calculate_info_section_start_ptr(Dwarf_CU_Context context,
2068 Dwarf_Unsigned *section_len)
2069 {
2070 Dwarf_Debug dbg = 0;
2071 Dwarf_Small *dataptr = 0;
2072 struct Dwarf_Section_s *sec = 0;
2073
2074 dbg = context->cc_dbg;
2075 sec = context->cc_is_info? &dbg->de_debug_info:
2076 &dbg->de_debug_types;
2077 dataptr = sec->dss_data;
2078 *section_len = sec->dss_size;
2079 return dataptr;
2080 }
2081
2082 Dwarf_Byte_Ptr
2083 _dwarf_calculate_info_section_end_ptr(Dwarf_CU_Context context)
2084 {
2085 Dwarf_Debug dbg = 0;
2086 Dwarf_Byte_Ptr info_end = 0;
2087 Dwarf_Byte_Ptr info_start = 0;
2088 Dwarf_Off off2 = 0;
2089 Dwarf_Small *dataptr = 0;
2090
2091 dbg = context->cc_dbg;
2092 dataptr = context->cc_is_info? dbg->de_debug_info.dss_data:
2093 dbg->de_debug_types.dss_data;
2094 off2 = context->cc_debug_offset;
2095 info_start = dataptr + off2;
2096 info_end = info_start + context->cc_length +
2097 context->cc_length_size +
2098 context->cc_extension_size;
2099 return info_end;
2100 }
2101 Dwarf_Byte_Ptr
2102 _dwarf_calculate_abbrev_section_end_ptr(Dwarf_CU_Context context)
2103 {
2104 Dwarf_Debug dbg = 0;
2105 Dwarf_Byte_Ptr abbrev_end = 0;
2106 Dwarf_Byte_Ptr abbrev_start = 0;
2107 struct Dwarf_Section_s *sec = 0;
2108
2109 dbg = context->cc_dbg;
2110 sec = &dbg->de_debug_abbrev;
2111 abbrev_start = sec->dss_data;
2112 abbrev_end = abbrev_start + sec->dss_size;
2113 return abbrev_end;
2114 }
2115
2116 /* New December 2020. Any Dwarf_Die will work.
2117 The values returned are about the CU itself, not a DIE.
2118 extension_size is set zero unless it offset_size
2119 is 64 and it is standard Dwarf, in which case
2120 extension_size is set to 4.
2121 If there is no signature *signature is set zero,
2122 offset_of_length is the section offset of the first
2123 byte of the compilation-unit length field.
2124 total_byte_length includes the length field and
2125 all the CU data.
2126 The offset of the first byte of the CU is therefore
2127 offset_of_lenth + offset_size + extension_size.
2128 is_info is always non-zero except if the section
2129 of the CU is DWARF4 .debug_types.
2130 */
2131 int
2132 dwarf_cu_header_basics(Dwarf_Die die,
2133 Dwarf_Half *version,
2134 Dwarf_Bool *is_info,
2135 Dwarf_Bool *is_dwo,
2136 Dwarf_Half *offset_size,
2137 Dwarf_Half *address_size,
2138 Dwarf_Half *extension_size,
2139 Dwarf_Sig8 **signature,
2140 Dwarf_Off *offset_of_length,
2141 Dwarf_Unsigned *total_byte_length,
2142 Dwarf_Error *error)
2143 {
2144 Dwarf_CU_Context context = 0;
2145 CHECK_DIE(die, DW_DLV_ERROR);
2146
2147 context= die->di_cu_context;
2148 if (version) {
2149 *version = context->cc_version_stamp;
2150 }
2151 if (is_info) {
2152 /* ASSERT: matches context->cc_is_info */
2153 *is_info = die->di_is_info;
2154 }
2155 if (is_dwo) {
2156 *is_dwo = context->cc_is_dwo;
2157 }
2158 if (offset_size) {
2159 *offset_size = context->cc_length_size;
2160 }
2161 if (address_size) {
2162 *address_size = context->cc_address_size;
2163 }
2164 if (extension_size) {
2165 *extension_size = context->cc_extension_size;
2166 }
2167 if (signature) {
2168 if (context->cc_signature_present) {
2169 *signature = &context->cc_signature;
2170 } else {
2171 *signature = 0;
2172 }
2173 }
2174 if (offset_of_length) {
2175 *offset_of_length = context->cc_debug_offset;
2176 }
2177 if (total_byte_length) {
2178 *total_byte_length = context->cc_length +
2179 context->cc_length_size + context->cc_extension_size;
2180 }
2181 return DW_DLV_OK;
2182 }
2183
2184 int
2185 dwarf_get_universalbinary_count(
2186 Dwarf_Debug dbg,
2187 Dwarf_Unsigned *current_index,
2188 Dwarf_Unsigned *available_count)
2189 {
2190 if (IS_INVALID_DBG(dbg)) {
2191 return DW_DLV_NO_ENTRY;
2192 }
2193 if (!dbg->de_universalbinary_count ) {
2194 return DW_DLV_NO_ENTRY;
2195 }
2196 if (current_index) {
2197 *current_index = dbg->de_universalbinary_index;
2198 }
2199 if (available_count) {
2200 *available_count = dbg->de_universalbinary_count;
2201 }
2202 return DW_DLV_OK;
2203 }
2204
2205 /* Never returns DW_DLV_ERROR */
2206 int
2207 dwarf_machine_architecture(Dwarf_Debug dbg,
2208 Dwarf_Small *dw_ftype,
2209 Dwarf_Small *dw_obj_pointersize,
2210 Dwarf_Bool *dw_obj_is_big_endian,
2211 Dwarf_Unsigned *dw_obj_machine,
2212 Dwarf_Unsigned *dw_obj_flags,
2213 Dwarf_Small *dw_path_source,
2214 Dwarf_Unsigned *dw_ub_offset,
2215 Dwarf_Unsigned *dw_ub_count,
2216 Dwarf_Unsigned *dw_ub_index,
2217 Dwarf_Unsigned *dw_comdat_groupnumber)
2218 {
2219 if (IS_INVALID_DBG(dbg)) {
2220 return DW_DLV_NO_ENTRY;
2221 }
2222 if (dw_ftype) {
2223 *dw_ftype = dbg->de_ftype;
2224 }
2225 if (dw_obj_pointersize) {
2226 *dw_obj_pointersize = dbg->de_pointer_size;
2227 }
2228 if (dw_obj_is_big_endian) {
2229 *dw_obj_is_big_endian = dbg->de_big_endian_object;
2230 }
2231 if (dw_obj_machine) {
2232 *dw_obj_machine = dbg->de_obj_machine;
2233 }
2234 if (dw_obj_flags) {
2235 *dw_obj_flags = dbg->de_obj_flags;
2236 }
2237 if (dw_path_source) {
2238 *dw_path_source = dbg->de_path_source;
2239 }
2240 if (dw_ub_offset) {
2241 *dw_ub_offset = dbg->de_obj_ub_offset;
2242 }
2243 if (dw_ub_count) {
2244 *dw_ub_count = dbg->de_universalbinary_count;
2245 }
2246 if (dw_ub_index) {
2247 *dw_ub_index = dbg->de_universalbinary_index;
2248 }
2249 if (dw_comdat_groupnumber) {
2250 *dw_comdat_groupnumber = dbg->de_groupnumber;
2251 }
2252 return DW_DLV_OK;
2253 }