Debian Codegraph @

src/lib/libdwarf/dwarf_form.c

raw
1 /*
2 Copyright (C) 2000,2002,2004,2005 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
4 Portions Copyright 2008-2021 David Anderson. All rights reserved.
5 Portions Copyright 2010-2012 SN Systems Ltd. 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 <string.h> /* memcpy() memset() */
35 #include <stdio.h> /* 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_string.h"
48 #include "dwarf_error.h"
49 #include "dwarf_util.h"
50 #include "dwarf_die_deliv.h"
51 #include "dwarf_str_offsets.h"
52 #include "dwarf_string.h"
53 #if 0 /* dump_bytes */
54 static void
55 dump_bytes(const char *msg,int line,
56 Dwarf_Small * start, long len)
57 {
58 Dwarf_Small *end = start + len;
59 Dwarf_Small *cur = start;
60 printf("%s (0x%lx) line %d\n ",msg,(unsigned long)start,line);
61 for (; cur < end; cur++) {
62 printf("%02x", *cur);
63 }
64 printf("\n");
65 }
66 #endif /*0*/
67
68 /* It is necessary at times to cause errors of this sort
69 in determining what we really have. So best to avoid
70 too much malloc and free, hence the static constructor
71 dwarfstring will use malloc if we guess too-small
72 for the size of mbuf. */
73 static void
74 generate_form_error(Dwarf_Debug dbg,
75 Dwarf_Error *error,
76 unsigned form,
77 int err_code,
78 const char *errname,
79 const char *funcname)
80 {
81 dwarfstring m; /* constructor_static ok */
82 char mbuf[DWARFSTRING_ALLOC_SIZE];
83 const char * defaultname = "<unknown form>";
84
85 dwarfstring_constructor_static(&m,mbuf,
86 sizeof(mbuf));
87 dwarfstring_append(&m,(char *)errname);
88 dwarfstring_append(&m,": In function ");
89 dwarfstring_append(&m,(char *)funcname);
90 dwarfstring_append_printf_u(&m,
91 " on seeing form 0x%x ",form);
92 dwarf_get_FORM_name(form,&defaultname);
93 dwarfstring_append_printf_s(&m,
94 " (%s)",(char *)defaultname);
95 _dwarf_error_string(dbg,error,err_code,
96 dwarfstring_string(&m));
97 dwarfstring_destructor(&m);
98 }
99
100 /* This code was repeated many times, now it
101 is all in one place.
102 Never returns DW_DLV_NO_ENTRY */
103 static int
104 get_attr_dbg(Dwarf_Debug *dbg_out,
105 Dwarf_CU_Context * cu_context_out,
106 Dwarf_Attribute attr,
107 Dwarf_Error *error)
108 {
109 Dwarf_CU_Context cup = 0;
110 Dwarf_Debug dbg = 0;
111
112 if (!attr) {
113 _dwarf_error(NULL, error, DW_DLE_ATTR_NULL);
114 return DW_DLV_ERROR;
115 }
116 cup = attr->ar_cu_context;
117 if (!cup) {
118 _dwarf_error(NULL, error, DW_DLE_ATTR_NO_CU_CONTEXT);
119 return DW_DLV_ERROR;
120 }
121 dbg = cup->cc_dbg;
122 if (IS_INVALID_DBG(dbg)) {
123 _dwarf_error_string(NULL, error, DW_DLE_ATTR_DBG_NULL,
124 "DW_DLE_ATTR_DBG_NULL: Stale or null Dwarf_Debug"
125 "in a Dwarf_CU_Context" );
126 return DW_DLV_ERROR;
127 }
128 if (dbg != attr->ar_dbg) {
129 _dwarf_error_string(NULL, error, DW_DLE_ATTR_DBG_NULL,
130 "DW_DLE_ATTR_DBG_NULL: an attribute and its "
131 "cu_context do not have the same Dwarf_Debug" );
132 return DW_DLV_ERROR;
133 }
134 *cu_context_out = cup;
135 *dbg_out = dbg;
136 return DW_DLV_OK;
137
138 }
139
140 /* This checks the final-form (after any DW_FORM_indirect
141 converted to final form). */
142 int
143 dwarf_hasform(Dwarf_Attribute attr,
144 Dwarf_Half dw_form,
145 Dwarf_Bool * dw_return_bool, Dwarf_Error * error)
146 {
147 Dwarf_Debug dbg = 0;
148 Dwarf_CU_Context cu_context = 0;
149 int res = 0;
150
151 res =get_attr_dbg(&dbg,&cu_context, attr,error);
152 if (res != DW_DLV_OK) {
153 return res;
154 }
155 if (!dw_return_bool) {
156 _dwarf_error_string(dbg,error,
157 DW_DLE_INVALID_NULL_ARGUMENT,
158 " DW_DLE_INVALID_NULL_ARGUMENT "
159 "calling dwarf_hasform: "
160 "dw_return_bool must be passed"
161 " as a non-NULL valid pointer");
162 return DW_DLV_ERROR;
163 }
164 *dw_return_bool = (attr->ar_attribute_form == dw_form);
165 return DW_DLV_OK;
166 }
167
168 /* Not often called, we do not worry about efficiency here.
169 The dwarf_whatform() call does the sanity checks for us.
170 The form returned is the original form, which could
171 be DW_FORM_indirect.
172 */
173 int
174 dwarf_whatform_direct(Dwarf_Attribute attr,
175 Dwarf_Half * return_form, Dwarf_Error * error)
176 {
177 int res = dwarf_whatform(attr, return_form, error);
178
179 if (res != DW_DLV_OK) {
180 return res;
181 }
182
183 *return_form = attr->ar_attribute_form_direct;
184 return DW_DLV_OK;
185 }
186
187 /* This code was contributed around 2007.
188 As of 2021 it is not clear that Sun Sparc
189 compilers are in current use, nor whether
190 there is a reason to make reads of
191 this data format safe from corrupted object files.
192
193 Pass in the content of a block and the length of that
194 content. On success return DW_DLV_OK and set *value_count
195 to the size of the array returned through value_array. */
196 int
197 dwarf_uncompress_integer_block_a(Dwarf_Debug dbg,
198 Dwarf_Unsigned input_length_in_bytes,
199 void * input_block,
200 Dwarf_Unsigned * value_count,
201 Dwarf_Signed ** value_array,
202 Dwarf_Error * error)
203 {
204 Dwarf_Unsigned output_length_in_units = 0;
205 Dwarf_Signed *output_block = 0;
206 unsigned i = 0;
207 char *ptr = 0;
208 Dwarf_Signed remain = 0;
209 Dwarf_Signed *array = 0;
210 Dwarf_Byte_Ptr endptr = (Dwarf_Byte_Ptr)input_block+
211 input_length_in_bytes;
212
213 CHECK_DBG(dbg,error,"dwarf_uncompress_integer_block_a()");
214 output_length_in_units = 0;
215 remain = (Dwarf_Signed)input_length_in_bytes;
216 ptr = input_block;
217 while (remain > 0) {
218 Dwarf_Unsigned len = 0;
219 Dwarf_Signed value = 0;
220 int rres = 0;
221
222 rres = dwarf_decode_signed_leb128((char *)ptr,
223 &len, &value,(char *)endptr);
224 if (rres != DW_DLV_OK) {
225 _dwarf_error(NULL, error, DW_DLE_LEB_IMPROPER);
226 return DW_DLV_ERROR;
227 }
228 ptr += len;
229 remain -= (Dwarf_Signed)len;
230 output_length_in_units++;
231 }
232 if (remain != 0) {
233 _dwarf_error(NULL, error, DW_DLE_ALLOC_FAIL);
234 return DW_DLV_ERROR;
235 }
236
237 output_block = (Dwarf_Signed*)
238 _dwarf_get_alloc(dbg,
239 DW_DLA_STRING,
240 output_length_in_units * sizeof(Dwarf_Signed));
241 if (!output_block) {
242 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
243 return DW_DLV_ERROR;
244 }
245 array = output_block;
246 remain = input_length_in_bytes;
247 ptr = input_block;
248 for (i=0; i<output_length_in_units && remain>0; i++) {
249 Dwarf_Signed num;
250 Dwarf_Unsigned len;
251 int sres = 0;
252
253 sres = dwarf_decode_signed_leb128((char *)ptr,
254 &len, &num,(char *)endptr);
255 if (sres != DW_DLV_OK) {
256 dwarf_dealloc(dbg,output_block,DW_DLA_STRING);
257 _dwarf_error(NULL, error, DW_DLE_LEB_IMPROPER);
258 return DW_DLV_ERROR;
259 }
260 ptr += len;
261 remain -= (Dwarf_Signed)len;
262 array[i] = num;
263 }
264
265 if (remain != 0) {
266 dwarf_dealloc(dbg, (unsigned char *)output_block,
267 DW_DLA_STRING);
268 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
269 return DW_DLV_ERROR;
270 }
271
272 *value_count = output_length_in_units;
273 *value_array = output_block;
274 return DW_DLV_OK;
275 }
276
277 /* This code was contributed around 2007
278 and the return value is in the wrong form.
279 See dwarf_uncompress_integer_block_a() above.
280
281 As of 2019 it is not clear that Sun Sparc
282 compilers are in current use, nor whether
283 there is a reason to make reads of
284 this data format safe from corrupted object files.
285 */
286
287 void
288 dwarf_dealloc_uncompressed_block(Dwarf_Debug dbg, void * space)
289 {
290 dwarf_dealloc(dbg, space, DW_DLA_STRING);
291 }
292
293 /* Never returns DW_DLV_NO_ENTRY
294 Returns the final form after any DW_FORM_indirect
295 resolved to final form. */
296 int
297 dwarf_whatform(Dwarf_Attribute attr,
298 Dwarf_Half * return_form, Dwarf_Error * error)
299 {
300 Dwarf_CU_Context cu_context = 0;
301 Dwarf_Debug dbg = 0;
302
303 int res =get_attr_dbg(&dbg,&cu_context, attr,error);
304 if (res != DW_DLV_OK) {
305 return res;
306 }
307 *return_form = attr->ar_attribute_form;
308 return DW_DLV_OK;
309 }
310
311 /*
312 This function is analogous to dwarf_whatform.
313 It returns the attribute in attr instead of
314 the form.
315 */
316 int
317 dwarf_whatattr(Dwarf_Attribute attr,
318 Dwarf_Half * return_attr, Dwarf_Error * error)
319 {
320 Dwarf_CU_Context cu_context = 0;
321 Dwarf_Debug dbg = 0;
322
323 int res =get_attr_dbg(&dbg,&cu_context, attr,error);
324 if (res != DW_DLV_OK) {
325 return res;
326 }
327 *return_attr = (attr->ar_attribute);
328 return DW_DLV_OK;
329 }
330
331 /* Convert an offset within the local CU into a section-relative
332 debug_info (or debug_types) offset.
333 See dwarf_global_formref() and dwarf_formref()
334 for additional information on conversion rules.
335 */
336 int
337 dwarf_convert_to_global_offset(Dwarf_Attribute attr,
338 Dwarf_Off offset,
339 Dwarf_Off * ret_offset,
340 Dwarf_Error * error)
341 {
342 Dwarf_Debug dbg = 0;
343 Dwarf_CU_Context cu_context = 0;
344 int res = 0;
345
346 res = get_attr_dbg(&dbg,&cu_context,attr,error);
347 if (res != DW_DLV_OK) {
348 return res;
349 }
350 switch (attr->ar_attribute_form) {
351 case DW_FORM_ref1:
352 case DW_FORM_ref2:
353 case DW_FORM_ref4:
354 case DW_FORM_ref8:
355 case DW_FORM_ref_udata:
356 /* It is a cu-local offset. Convert to section-global. */
357 /* It would be nice to put some code to check
358 legality of the offset */
359 /* cc_debug_offset always has any DWP Package File
360 offset included (when the cu_context created)
361 so there is no extra work for DWP.
362 Globalize the offset */
363 offset += cu_context->cc_debug_offset;
364
365 break;
366
367 case DW_FORM_ref_addr:
368 /* This offset is defined to be debug_info global already, so
369 use this value unaltered.
370
371 Since a DWP package file is not relocated there
372 is no way that this reference offset to an address in
373 any other CU can be correct for a DWP Package File offset
374 */
375 break;
376 default: {
377 dwarfstring m;
378
379 dwarfstring_constructor(&m);
380 dwarfstring_append_printf_u(&m,
381 "DW_DLE_BAD_REF_FORM. The form "
382 "code is 0x%x which cannot be converted to a global "
383 " offset by "
384 "dwarf_convert_to_global_offset()",
385 attr->ar_attribute_form);
386 _dwarf_error_string(dbg, error, DW_DLE_BAD_REF_FORM,
387 dwarfstring_string(&m));
388 dwarfstring_destructor(&m);
389 return DW_DLV_ERROR;
390 }
391 }
392
393 *ret_offset = (offset);
394 return DW_DLV_OK;
395 }
396
397 /* A global offset cannot be returned by this interface:
398 see dwarf_global_formref().
399
400 DW_FORM_ref_addr is considered an incorrect form
401 for this call because DW_FORM_ref_addr is a global-offset into
402 the debug_info section.
403
404 For the same reason DW_FORM_data4/data8 are not returned
405 from this function.
406
407 For the same reason DW_FORM_sec_offset is not returned
408 from this function, DW_FORM_sec_offset is a global offset
409 (to various sections, not a CU relative offset.
410
411 DW_FORM_ref_addr has a value which was documented in
412 DWARF2 as address-size but which was always an offset
413 so should have always been offset size (wording
414 corrected in DWARF3).
415 The dwarfstd.org FAQ "How big is a DW_FORM_ref_addr?"
416 suggested all should use offset-size, but that suggestion
417 seems to have been ignored in favor of doing what the
418 DWARF2 and 3 standards actually say.
419
420 November, 2010: *ret_offset is always set now.
421 Even in case of error.
422 Set to zero for most errors, but for
423 DW_DLE_ATTR_FORM_OFFSET_BAD
424 *ret_offset is set to the bad offset.
425
426 DW_FORM_addrx
427 DW_FORM_strx
428 DW_FORM_LLVM_addrx_offset
429 DW_FORM_rnglistx
430 DW_FORM_GNU_addr_index
431 DW_FORM_GNU_str_index
432 are not references to .debug_info/.debug_types,
433 so they are not allowed here. */
434
435 static void
436 show_not_ref_error(Dwarf_Debug dbg,
437 Dwarf_Error *error,
438 Dwarf_Half form,
439 Dwarf_Half attr)
440 {
441 dwarfstring m;
442 const char *fname = 0;
443 const char *aname = 0;
444
445 /*DW_DLE_NOT_REF_FORM */
446 dwarfstring_constructor(&m);
447
448 dwarf_get_FORM_name(form,&fname);
449 dwarf_get_AT_name(attr,&aname);
450 dwarfstring_append(&m,"DW_DLE_NOT_REF_FORM: ");
451 dwarfstring_append(&m,(char *)fname);
452 dwarfstring_append_printf_s(&m," (on attribute %s )",
453 (char *)aname);
454 dwarfstring_append(&m," for versions >= 4 ");
455 dwarfstring_append(&m,"is not a valid reference form");
456 _dwarf_error_string(dbg,error,DW_DLE_NOT_REF_FORM,
457 dwarfstring_string(&m));
458 dwarfstring_destructor(&m);
459 }
460
461 int
462 dwarf_formref(Dwarf_Attribute attr,
463 Dwarf_Off * ret_offset,
464 Dwarf_Bool * ret_is_info,
465 Dwarf_Error * error)
466 {
467 Dwarf_Debug dbg = 0;
468 Dwarf_Unsigned offset = 0;
469 Dwarf_CU_Context cu_context = 0;
470 Dwarf_Unsigned maximumoffset = 0;
471 int res = DW_DLV_ERROR;
472 Dwarf_Byte_Ptr section_end = 0;
473 Dwarf_Bool is_info = TRUE;
474
475 *ret_offset = 0;
476 res = get_attr_dbg(&dbg,&cu_context,attr,error);
477 if (res != DW_DLV_OK) {
478 return res;
479 }
480 section_end =
481 _dwarf_calculate_info_section_end_ptr(cu_context);
482 is_info = cu_context->cc_is_info;
483
484 switch (attr->ar_attribute_form) {
485
486 case DW_FORM_ref1:
487 offset = *(Dwarf_Small *) attr->ar_debug_ptr;
488 break;
489
490 case DW_FORM_ref2:
491 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
492 attr->ar_debug_ptr, DWARF_HALF_SIZE,
493 error,section_end);
494 break;
495
496 case DW_FORM_ref4:
497 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
498 attr->ar_debug_ptr, DWARF_32BIT_SIZE,
499 error,section_end);
500 break;
501
502 case DW_FORM_ref8:
503 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
504 attr->ar_debug_ptr, DWARF_64BIT_SIZE,
505 error,section_end);
506 break;
507
508 case DW_FORM_ref_udata: {
509 Dwarf_Byte_Ptr ptr = attr->ar_debug_ptr;
510 Dwarf_Unsigned localoffset = 0;
511 DECODE_LEB128_UWORD_CK(ptr,localoffset,
512 dbg,error,section_end);
513 offset = localoffset;
514 break;
515 }
516 case DW_FORM_ref_sig8: {
517 /* We need to look for a local reference here.
518 The function we are in is only CU_local
519 offsets returned. */
520 #if 0 /* check for a local sig8 reference unimplemented. */
521 Dwarf_Sig8 sig8;
522 memcpy(&sig8,ptr,sizeof(Dwarf_Sig8));
523 res = dwarf_find_die_given_sig8(dbg,
524 &sig8, ...
525 We could look, then determine if
526 resulting offset is actually local.
527 #endif /*0*/
528
529 /* We cannot handle this here.
530 The reference could be to .debug_types
531 or another CU!
532 not a .debug_info CU local offset. */
533 _dwarf_error(dbg, error, DW_DLE_REF_SIG8_NOT_HANDLED);
534 return DW_DLV_ERROR;
535 }
536 default: {
537 dwarfstring m;
538 const char * fname = 0;
539 const char * aname = 0;
540
541 dwarf_get_FORM_name(attr->ar_attribute_form,&fname);
542 dwarf_get_AT_name(attr->ar_attribute,&aname);
543 dwarfstring_constructor(&m);
544 dwarfstring_append_printf_u(&m,
545 "DW_DLE_BAD_REF_FORM. The form "
546 "code is 0x%x ",
547 attr->ar_attribute_form);
548 dwarfstring_append(&m,(char *)fname);
549 dwarfstring_append_printf_s(&m," on attribute %s, "
550 "which does not have an offset"
551 " for dwarf_formref() to return.",(char *)aname);
552 _dwarf_error_string(dbg, error, DW_DLE_BAD_REF_FORM,
553 dwarfstring_string(&m));
554 dwarfstring_destructor(&m);
555 return DW_DLV_ERROR;
556 }
557 }
558
559 /* Check that offset is within current
560 cu portion of .debug_info. */
561
562 maximumoffset = cu_context->cc_length +
563 cu_context->cc_length_size +
564 cu_context->cc_extension_size;
565 if (offset >= maximumoffset) {
566 /* For the DW_TAG_compile_unit is legal to have the
567 DW_AT_sibling attribute outside the current cu portion of
568 .debug_info.
569 In other words, sibling points to the end of the CU.
570 It is used for precompiled headers.
571 The valid condition will be: 'offset == maximumoffset'. */
572 Dwarf_Half tag = 0;
573 int tres = dwarf_tag(attr->ar_die,&tag,error);
574 if (tres != DW_DLV_OK) {
575 if (tres == DW_DLV_NO_ENTRY) {
576 _dwarf_error(dbg, error, DW_DLE_NO_TAG_FOR_DIE);
577 return DW_DLV_ERROR;
578 }
579 return DW_DLV_ERROR;
580 }
581
582 if (DW_TAG_compile_unit != tag &&
583 DW_AT_sibling != attr->ar_attribute &&
584 offset > maximumoffset) {
585 _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_OFFSET_BAD);
586 /* Return the incorrect offset for better
587 error reporting */
588 *ret_offset = (offset);
589 return DW_DLV_ERROR;
590 }
591 }
592 *ret_is_info = is_info;
593 *ret_offset = (offset);
594 return DW_DLV_OK;
595 }
596
597 static int
598 _dwarf_formsig8_internal(Dwarf_Attribute attr,
599 int formexpected,
600 Dwarf_Sig8 * returned_sig_bytes,
601 Dwarf_Error* error)
602 {
603 Dwarf_Debug dbg = 0;
604 Dwarf_CU_Context cu_context = 0;
605 Dwarf_Byte_Ptr field_end = 0;
606 Dwarf_Byte_Ptr section_end = 0;
607
608 int res = get_attr_dbg(&dbg,&cu_context,attr,error);
609 if (res != DW_DLV_OK) {
610 return res;
611 }
612
613 if (attr->ar_attribute_form != formexpected) {
614 return DW_DLV_NO_ENTRY;
615 }
616 section_end =
617 _dwarf_calculate_info_section_end_ptr(cu_context);
618 field_end = attr->ar_debug_ptr + sizeof(Dwarf_Sig8);
619 if (field_end > section_end) {
620 _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_OFFSET_BAD);
621 return DW_DLV_ERROR;
622 }
623
624 memcpy(returned_sig_bytes, attr->ar_debug_ptr,
625 sizeof(*returned_sig_bytes));
626 return DW_DLV_OK;
627 }
628
629 int
630 dwarf_formsig8_const(Dwarf_Attribute attr,
631 Dwarf_Sig8 * returned_sig_bytes,
632 Dwarf_Error* error)
633 {
634 int res =_dwarf_formsig8_internal(attr, DW_FORM_data8,
635 returned_sig_bytes,error);
636 return res;
637 }
638
639 /* dwarf_formsig8 returns in the caller-provided 8 byte area
640 the 8 bytes of a DW_FORM_ref_sig8 (copying the bytes
641 directly to the caller). Not a string, an 8 byte
642 MD5 hash or a signature.
643 This function is new in DWARF4 libdwarf and used in
644 more places in DWARF5.
645 */
646 int
647 dwarf_formsig8(Dwarf_Attribute attr,
648 Dwarf_Sig8 * returned_sig_bytes,
649 Dwarf_Error* error)
650 {
651 int res = _dwarf_formsig8_internal(attr, DW_FORM_ref_sig8,
652 returned_sig_bytes,error);
653 return res;
654 }
655
656 /* This finds a target via a sig8 and if
657 DWARF4 is likely finding a reference from .debug_info
658 to .debug_types. So the offset may or may not be
659 in the same section if DWARF4.
660 context_level prevents infinite loop
661 during CU_Context creation */
662 static int
663 find_sig8_target_as_global_offset(Dwarf_Attribute attr,
664 int context_level,
665 Dwarf_Sig8 *sig8,
666 Dwarf_Bool *is_info,
667 Dwarf_Off *targoffset,
668 Dwarf_Error *error)
669 {
670 Dwarf_Die targdie = 0;
671 Dwarf_Bool targ_is_info = 0;
672 Dwarf_Off localoff = 0;
673 int res = 0;
674 int resb = 0;
675
676 targ_is_info = attr->ar_cu_context->cc_is_info;
677 memcpy(sig8,attr->ar_debug_ptr,sizeof(*sig8));
678 res = _dwarf_internal_find_die_given_sig8(attr->ar_dbg,
679 context_level,
680 sig8,&targdie,&targ_is_info,error);
681 if (res != DW_DLV_OK) {
682 return res;
683 }
684 resb = dwarf_die_offsets(targdie,targoffset,&localoff,error);
685 if (resb != DW_DLV_OK) {
686 dwarf_dealloc_die(targdie);
687 return resb;
688 }
689 *is_info = targdie->di_cu_context->cc_is_info;
690 dwarf_dealloc_die(targdie);
691 return DW_DLV_OK;
692 }
693
694 /* Since this returns section-relative debug_info offsets,
695 this can represent all REFERENCE forms correctly
696 and allows all applicable forms.
697
698 DW_FORM_ref_addr has a value which was documented in
699 DWARF2 as address-size but which was always an offset
700 so should have always been offset size (wording
701 corrected in DWARF3).
702 gcc and Go and libdwarf producer code
703 define the length of the value of DW_FORM_ref_addr
704 per the version. So for V2 it is address-size and V3 and later
705 it is offset-size.
706
707 See the DWARF4 document for the 3 cases fitting
708 reference forms. The caller must determine which section the
709 reference 'points' to. The function added in November 2009,
710 dwarf_get_form_class(), helps in this regard.
711
712 unlike dwarf_formref(), this allows references to
713 sections other than just .debug_info/.debug_types.
714 See case DW_FORM_sec_offset:
715 case DW_FORM_GNU_ref_alt: 2013 GNU extension
716 case DW_FORM_GNU_strp_alt: 2013 GNU extension
717 case DW_FORM_strp_sup: DWARF5, sup string section
718 case DW_FORM_line_strp: DWARF5, .debug_line_str section
719 */
720
721 /* This follows DW_FORM_ref_sig8 so could got
722 to any CU and from debug_info to debug_types
723 (or vice versa?)
724 dwarf_global_formref_b is aimed at for DIE references.
725 Only the DW_FORM_ref_sig8 form can change
726 from a cu_context in .debug_info
727 to one in .debug_types (DWARF4 only).
728 For references to other sections it is simpler
729 to call the original: dwarf_global_formref.
730 */
731 int
732 dwarf_global_formref(Dwarf_Attribute attr,
733 Dwarf_Off * ret_offset,
734 Dwarf_Error * error)
735 {
736 Dwarf_Bool is_info = 0;
737 int res = 0;
738
739 res = dwarf_global_formref_b(attr,ret_offset,
740 &is_info,error);
741 return res;
742 }
743
744 /* If context_level is 0, normal call
745 But if non-zero will avoid creating CU Context. */
746 int
747 dwarf_global_formref_b(Dwarf_Attribute attr,
748 Dwarf_Off * ret_offset,
749 Dwarf_Bool * offset_is_info,
750 Dwarf_Error * error)
751 {
752 int res = 0;
753 int context_level = 0;
754 res = _dwarf_internal_global_formref_b( attr,
755 context_level,
756 ret_offset,
757 offset_is_info,
758 error);
759 return res;
760 }
761 /* If context_level is 0, normal call
762 But if non-zero will avoid creating CU Context. */
763 int
764 _dwarf_internal_global_formref_b(Dwarf_Attribute attr,
765 int context_level,
766 Dwarf_Off * ret_offset,
767 Dwarf_Bool * offset_is_info,
768 Dwarf_Error * error)
769 {
770 Dwarf_Debug dbg = 0;
771 Dwarf_Unsigned offset = 0;
772 Dwarf_CU_Context cu_context = 0;
773 Dwarf_Half context_version = 0;
774 Dwarf_Byte_Ptr section_end = 0;
775 Dwarf_Bool is_info = TRUE;
776
777 int res = get_attr_dbg(&dbg,&cu_context,attr,error);
778 if (res != DW_DLV_OK) {
779 return res;
780 }
781 section_end =
782 _dwarf_calculate_info_section_end_ptr(cu_context);
783 context_version = cu_context->cc_version_stamp;
784 is_info = cu_context->cc_is_info;
785 switch (attr->ar_attribute_form) {
786
787 case DW_FORM_ref1:
788 if (attr->ar_debug_ptr >= section_end) {
789 _dwarf_error_string(dbg, error,
790 DW_DLE_ATTR_FORM_OFFSET_BAD,
791 "DW_DLE_ATTR_FORM_OFFSET_BAD: "
792 "DW_FORM_ref1 outside of the section.");
793 return DW_DLV_ERROR;
794 }
795 offset = *(Dwarf_Small *) attr->ar_debug_ptr;
796 goto fixoffset;
797
798 case DW_FORM_ref2:
799 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
800 attr->ar_debug_ptr, DWARF_HALF_SIZE,
801 error,section_end);
802 goto fixoffset;
803
804 case DW_FORM_ref4:
805 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
806 attr->ar_debug_ptr, DWARF_32BIT_SIZE,
807 error,section_end);
808 goto fixoffset;
809
810 case DW_FORM_ref8:
811 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
812 attr->ar_debug_ptr, DWARF_64BIT_SIZE,
813 error,section_end);
814 goto fixoffset;
815
816 case DW_FORM_ref_udata:
817 {
818 Dwarf_Byte_Ptr ptr = attr->ar_debug_ptr;
819 Dwarf_Unsigned localoffset = 0;
820
821 DECODE_LEB128_UWORD_CK(ptr,localoffset,
822 dbg,error,section_end);
823 offset = localoffset;
824
825 fixoffset: /* we have a local offset, make it global */
826
827 /* check legality of offset */
828 if (offset >= cu_context->cc_length +
829 cu_context->cc_length_size +
830 cu_context->cc_extension_size) {
831 _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_OFFSET_BAD);
832 return DW_DLV_ERROR;
833 }
834
835 /* globalize the offset */
836 offset += cu_context->cc_debug_offset;
837 }
838 break;
839
840 /* The DWARF2 document did not make clear that
841 DW_FORM_data4( and 8) were references with
842 global offsets to some section.
843 That was first clearly documented in DWARF3.
844 In DWARF4 these two forms are no longer references. */
845 case DW_FORM_data4:
846 if (context_version >= DW_CU_VERSION4) {
847 show_not_ref_error(dbg,error,attr->ar_attribute_form,
848 attr->ar_attribute);
849 return DW_DLV_ERROR;
850 }
851 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
852 attr->ar_debug_ptr, DWARF_32BIT_SIZE,
853 error, section_end);
854 /* The offset is global. */
855 break;
856 case DW_FORM_data8:
857 if (context_version >= DW_CU_VERSION4) {
858 show_not_ref_error(dbg,error,attr->ar_attribute_form,
859 attr->ar_attribute);
860 return DW_DLV_ERROR;
861 }
862 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
863 attr->ar_debug_ptr, DWARF_64BIT_SIZE,
864 error,section_end);
865 /* The offset is global. */
866 break;
867 case DW_FORM_ref_addr:
868 {
869 /* In Dwarf V2 DW_FORM_ref_addr was defined
870 as address-size even though it is a .debug_info
871 offset. Fixed in Dwarf V3 to be offset-size.
872 */
873 unsigned length_size = 0;
874 if (context_version == 2) {
875 length_size = cu_context->cc_address_size;
876 } else {
877 length_size = cu_context->cc_length_size;
878 }
879 if (length_size == 4) {
880 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
881 attr->ar_debug_ptr, DWARF_32BIT_SIZE,
882 error,section_end);
883 } else if (length_size == 8) {
884 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
885 attr->ar_debug_ptr, DWARF_64BIT_SIZE,
886 error,section_end);
887 } else {
888 _dwarf_error(dbg, error,
889 DW_DLE_FORM_SEC_OFFSET_LENGTH_BAD);
890 return DW_DLV_ERROR;
891 }
892 }
893 break;
894 /* Index into .debug_rnglists/.debug_loclists section.
895 Return the index itself. */
896 case DW_FORM_loclistx:
897 case DW_FORM_rnglistx: {
898 Dwarf_Unsigned val = 0;
899 DECODE_LEB128_UWORD_CK(attr->ar_debug_ptr,
900 val, dbg,error,section_end);
901 offset = val;
902 }
903 break;
904 case DW_FORM_sec_offset:
905 case DW_FORM_GNU_ref_alt: /* 2013 GNU extension */
906 case DW_FORM_GNU_strp_alt: /* 2013 GNU extension */
907 case DW_FORM_strp_sup: /* DWARF5, sup string section */
908 case DW_FORM_line_strp: /* DWARF5, .debug_line_str section */
909 {
910 /* DW_FORM_sec_offset first exists in DWARF4.*/
911 /* It is up to the caller to know what the offset
912 of DW_FORM_sec_offset, DW_FORM_strp_sup
913 or DW_FORM_GNU_strp_alt etc refer to,
914 the offset is not going to refer to .debug_info! */
915 unsigned length_size = cu_context->cc_length_size;
916 if (length_size == 4) {
917 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
918 attr->ar_debug_ptr, DWARF_32BIT_SIZE,
919 error,section_end);
920 } else if (length_size == 8) {
921 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
922 attr->ar_debug_ptr, DWARF_64BIT_SIZE,
923 error,section_end);
924 } else {
925 _dwarf_error(dbg, error,
926 DW_DLE_FORM_SEC_OFFSET_LENGTH_BAD);
927 return DW_DLV_ERROR;
928 }
929 }
930 break;
931 case DW_FORM_ref_sig8: {
932 /* This, in DWARF4, is how
933 .debug_info refers to .debug_types. */
934 Dwarf_Sig8 sig8;
935 Dwarf_Bool t_is_info = TRUE;
936 Dwarf_Unsigned t_offset = 0;
937
938 if ((attr->ar_debug_ptr + sizeof(Dwarf_Sig8)) > section_end) {
939 _dwarf_error_string(dbg, error,
940 DW_DLE_REF_SIG8_NOT_HANDLED,
941 "DW_DLE_REF_SIG8_NOT_HANDLED: "
942 " Dwarf_Sig8 content runs off the end of "
943 "its section");
944 return DW_DLV_ERROR;
945 }
946 memcpy(&sig8,attr->ar_debug_ptr,sizeof(Dwarf_Sig8));
947 res = find_sig8_target_as_global_offset(attr,
948 context_level,
949 &sig8,&t_is_info,&t_offset,error);
950 if (res == DW_DLV_ERROR) {
951
952 /* Lets construct an easily usable error number.
953 Avoiding resizing strings and avoiding
954 using the stack for strings possibly
955 a few hundred bytes long */
956 if (error) {
957 dwarfstring m;
958 dwarfstring k;
959
960 dwarfstring_constructor_fixed(&m,400);
961 dwarfstring_constructor_fixed(&k,200);
962 /* *error non null */
963 dwarfstring_append(&k,dwarf_errmsg(*error));
964 dwarfstring_append(&m,
965 "DW_DLE_REF_SIG8_NOT_HANDLED: "
966 " problem finding target. ");
967 dwarf_dealloc_error(dbg,*error);/* *error nonnull*/
968 *error = 0; /*error nonnull*/
969 dwarfstring_append(&m,dwarfstring_string(&k));
970 dwarfstring_destructor(&k);
971 _dwarf_error_string(dbg, error,
972 DW_DLE_REF_SIG8_NOT_HANDLED,
973 dwarfstring_string(&m));
974 dwarfstring_destructor(&m);
975 }
976 return DW_DLV_ERROR;
977 }
978 if (res == DW_DLV_NO_ENTRY) {
979 return res;
980 }
981 is_info = t_is_info;
982 offset = t_offset;
983 break;
984 }
985 default: {
986 dwarfstring m;
987 int formcode = attr->ar_attribute_form;
988 int fcres = 0;
989 const char *name = 0;
990
991 dwarfstring_constructor(&m);
992 dwarfstring_append_printf_u(&m,
993 "DW_DLE_BAD_REF_FORM: The form code is 0x%x.. ",
994 formcode);
995 fcres = dwarf_get_FORM_name (formcode,&name);
996 if (fcres != DW_DLV_OK) {
997 name="<UnknownFormCode>";
998 }
999 dwarfstring_append_printf_s(&m,
1000 " %s.",(char *)name);
1001 _dwarf_error_string(dbg, error, DW_DLE_BAD_REF_FORM,
1002 dwarfstring_string(&m));
1003 dwarfstring_destructor(&m);
1004 return DW_DLV_ERROR;
1005 }
1006 }
1007
1008 *offset_is_info = is_info;
1009 *ret_offset = offset;
1010 return DW_DLV_OK;
1011 }
1012
1013 /* Part of DebugFission. So a consumer can get the index when
1014 the object with the actual debug_addr is
1015 elsewhere. New May 2014*/
1016
1017 int
1018 _dwarf_get_addr_index_itself(int theform,
1019 Dwarf_Small *info_ptr,
1020 Dwarf_Debug dbg,
1021 Dwarf_CU_Context cu_context,
1022 Dwarf_Unsigned *val_out,
1023 Dwarf_Error * error)
1024 {
1025 Dwarf_Unsigned index = 0;
1026 Dwarf_Byte_Ptr section_end = 0;
1027
1028 section_end =
1029 _dwarf_calculate_info_section_end_ptr(cu_context);
1030 switch(theform){
1031 case DW_FORM_LLVM_addrx_offset: {
1032 Dwarf_Unsigned tmp = 0;
1033 Dwarf_Unsigned tmp2 = 0;
1034 DECODE_LEB128_UWORD_CK(info_ptr,tmp,
1035 dbg,error,section_end);
1036 READ_UNALIGNED_CK(dbg, tmp2, Dwarf_Unsigned,
1037 info_ptr, SIZEOFT32,
1038 error,section_end);
1039 index = (tmp<<32) | tmp2;
1040 break;
1041 }
1042 case DW_FORM_GNU_addr_index:
1043 case DW_FORM_addrx:
1044 DECODE_LEB128_UWORD_CK(info_ptr,index,
1045 dbg,error,section_end);
1046 break;
1047 case DW_FORM_addrx1:
1048 READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
1049 info_ptr, 1,
1050 error,section_end);
1051 break;
1052 case DW_FORM_addrx2:
1053 READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
1054 info_ptr, 2,
1055 error,section_end);
1056 break;
1057 case DW_FORM_addrx3:
1058 READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
1059 info_ptr, 3,
1060 error,section_end);
1061 break;
1062 case DW_FORM_addrx4:
1063 READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
1064 info_ptr, 4,
1065 error,section_end);
1066 break;
1067 default:
1068 _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_NOT_ADDR_INDEX);
1069 return DW_DLV_ERROR;
1070 }
1071 /* At this point we do not know for sure
1072 if the index refers
1073 to a local .debug_addr or a tied file .debug_addr
1074 so lets be cautious. */
1075 #if 0 /* Attempted check for index uncertain, unwise. Ignore. */
1076 /* See de_secondary_dbg before using this */
1077 if (!dbg->de_tied_data.td_tied_object &&
1078 index > dbg->de_filesize) {
1079 _dwarf_error_string(dbg,error,DW_DLE_ATTR_FORM_OFFSET_BAD,
1080 "DW_DLE_ATTR_FORM_OFFSET_BAD "
1081 "reading an indexed form addr the index "
1082 "read is impossibly large (no tied file "
1083 "available). Corrupt Dwarf.");
1084 return DW_DLV_ERROR;
1085 }
1086 #endif
1087 *val_out = index;
1088 return DW_DLV_OK;
1089 }
1090
1091 int
1092 dwarf_get_debug_addr_index(Dwarf_Attribute attr,
1093 Dwarf_Unsigned * return_index,
1094 Dwarf_Error * error)
1095 {
1096 int theform = 0;
1097 Dwarf_CU_Context cu_context = 0;
1098 Dwarf_Debug dbg = 0;
1099
1100 int res = get_attr_dbg(&dbg,&cu_context,attr,error);
1101 if (res != DW_DLV_OK) {
1102 return res;
1103 }
1104 theform = attr->ar_attribute_form;
1105 if (dwarf_addr_form_is_indexed(theform)) {
1106 Dwarf_Unsigned index = 0;
1107
1108 res = _dwarf_get_addr_index_itself(theform,
1109 attr->ar_debug_ptr,dbg,cu_context,&index,error);
1110 *return_index = index;
1111 return res;
1112 }
1113
1114 _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_NOT_ADDR_INDEX);
1115 return DW_DLV_ERROR;
1116 }
1117
1118 /* The index value here is the value of the
1119 attribute with this form.
1120 FORMs passed in are always strx forms. */
1121 static int
1122 dw_read_str_index_val_itself(Dwarf_Debug dbg,
1123 unsigned theform,
1124 Dwarf_Small *info_ptr,
1125 Dwarf_Small *section_end,
1126 Dwarf_Unsigned *return_index,
1127 Dwarf_Error *error)
1128 {
1129 Dwarf_Unsigned index = 0;
1130
1131 switch(theform) {
1132 case DW_FORM_strx:
1133 case DW_FORM_GNU_str_index:
1134 DECODE_LEB128_UWORD_CK(info_ptr,index,
1135 dbg,error,section_end);
1136 break;
1137 case DW_FORM_strx1:
1138 READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
1139 info_ptr, 1,
1140 error,section_end);
1141 break;
1142 case DW_FORM_strx2:
1143 READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
1144 info_ptr, 2,
1145 error,section_end);
1146 break;
1147 case DW_FORM_strx3:
1148 READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
1149 info_ptr, 3,
1150 error,section_end);
1151 break;
1152 case DW_FORM_strx4:
1153 READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
1154 info_ptr, 4,
1155 error,section_end);
1156 break;
1157 default:
1158 _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_NOT_STR_INDEX);
1159 return DW_DLV_ERROR;
1160 }
1161 *return_index = index;
1162 return DW_DLV_OK;
1163 }
1164
1165 /* Part of DebugFission. So a dwarf dumper application
1166 can get the index and print it for the user.
1167 A convenience function. New May 2014
1168 Also used with DWARF5 forms. */
1169 int
1170 dwarf_get_debug_str_index(Dwarf_Attribute attr,
1171 Dwarf_Unsigned *return_index,
1172 Dwarf_Error *error)
1173 {
1174 int theform = attr->ar_attribute_form;
1175 Dwarf_CU_Context cu_context = 0;
1176 Dwarf_Debug dbg = 0;
1177 int res = 0;
1178 Dwarf_Byte_Ptr section_end = 0;
1179 Dwarf_Unsigned index = 0;
1180 Dwarf_Small *info_ptr = 0;
1181 int indxres = 0;
1182 Dwarf_Unsigned length_size = 0;
1183 Dwarf_Unsigned sectionlen = 0;
1184
1185 res = get_attr_dbg(&dbg,&cu_context,attr,error);
1186 if (res != DW_DLV_OK) {
1187 return res;
1188 }
1189 section_end =
1190 _dwarf_calculate_info_section_end_ptr(cu_context);
1191 info_ptr = attr->ar_debug_ptr;
1192
1193 indxres = dw_read_str_index_val_itself(dbg, theform, info_ptr,
1194 section_end, &index,error);
1195 if (indxres == DW_DLV_OK) {
1196 *return_index = index;
1197 return indxres;
1198 }
1199 length_size = cu_context->cc_length_size;
1200 sectionlen = dbg->de_debug_str_offsets.dss_size;
1201 if (index > sectionlen ||
1202 (index *length_size) > sectionlen) {
1203 _dwarf_error_string(dbg, error,
1204 DW_DLE_ATTR_FORM_SIZE_BAD,
1205 "DW_DLE_ATTR_FORM_SIZE_BAD: "
1206 "An Attribute Value (index into "
1207 ".debug_str_offsets) is Impossibly "
1208 "large. Corrupt Dwarf.");
1209 return DW_DLV_ERROR;
1210 }
1211 return indxres;
1212 }
1213
1214 int
1215 _dwarf_extract_data16(Dwarf_Debug dbg,
1216 Dwarf_Small *data,
1217 Dwarf_Small *section_start,
1218 Dwarf_Small *section_end,
1219 Dwarf_Form_Data16 * returned_val,
1220 Dwarf_Error *error)
1221 {
1222 Dwarf_Small *data16end = 0;
1223
1224 data16end = data + sizeof(Dwarf_Form_Data16);
1225 if (data < section_start ||
1226 section_end < data16end) {
1227 _dwarf_error(dbg, error,DW_DLE_DATA16_OUTSIDE_SECTION);
1228 return DW_DLV_ERROR;
1229 }
1230 memcpy(returned_val, data, sizeof(*returned_val));
1231 return DW_DLV_OK;
1232
1233 }
1234
1235 int
1236 dwarf_formdata16(Dwarf_Attribute attr,
1237 Dwarf_Form_Data16 * returned_val,
1238 Dwarf_Error* error)
1239 {
1240 Dwarf_Half attrform = 0;
1241 Dwarf_CU_Context cu_context = 0;
1242 Dwarf_Debug dbg = 0;
1243 int res = 0;
1244 Dwarf_Small *section_end = 0;
1245 Dwarf_Unsigned section_length = 0;
1246 Dwarf_Small *section_start = 0;
1247
1248 if (!attr) {
1249 _dwarf_error(NULL, error, DW_DLE_ATTR_NULL);
1250 return DW_DLV_ERROR;
1251 }
1252 if (!returned_val ) {
1253 _dwarf_error(NULL, error, DW_DLE_ATTR_NULL);
1254 return DW_DLV_ERROR;
1255 }
1256 res = get_attr_dbg(&dbg,&cu_context,attr,error);
1257 if (res != DW_DLV_OK) {
1258 return res;
1259 }
1260 attrform = attr->ar_attribute_form;
1261 if (attrform != DW_FORM_data16) {
1262 generate_form_error(dbg,error,attrform,
1263 DW_DLE_ATTR_FORM_BAD,
1264 "DW_DLE_ATTR_FORM_BAD",
1265 "dwarf_formdata16");
1266 return DW_DLV_ERROR;
1267 }
1268 section_start = _dwarf_calculate_info_section_start_ptr(
1269 cu_context,&section_length);
1270 section_end = section_start + section_length;
1271
1272 res = _dwarf_extract_data16(dbg, attr->ar_debug_ptr,
1273 section_start, section_end,
1274 returned_val, error);
1275 return res;
1276 }
1277
1278 /* The *addrx are DWARF5 standard.
1279 The GNU form is non-standard gcc DWARF4
1280 The LLVM form is the newest. */
1281 Dwarf_Bool
1282 dwarf_addr_form_is_indexed(int form)
1283 {
1284 switch(form) {
1285 case DW_FORM_addrx:
1286 case DW_FORM_addrx1:
1287 case DW_FORM_addrx2:
1288 case DW_FORM_addrx3:
1289 case DW_FORM_addrx4:
1290 case DW_FORM_GNU_addr_index:
1291 case DW_FORM_LLVM_addrx_offset:
1292 return TRUE;
1293 default: break;
1294 }
1295 return FALSE;
1296 }
1297
1298 int
1299 dwarf_formaddr(Dwarf_Attribute attr,
1300 Dwarf_Addr * return_addr, Dwarf_Error * error)
1301 {
1302 Dwarf_Debug dbg = 0;
1303 Dwarf_Addr ret_addr = 0;
1304 Dwarf_CU_Context cu_context = 0;
1305 Dwarf_Half attrform = 0;
1306 int res = 0;
1307
1308 res = get_attr_dbg(&dbg,&cu_context,attr,error);
1309 if (res != DW_DLV_OK) {
1310 return res;
1311 }
1312 attrform = attr->ar_attribute_form;
1313 if (dwarf_addr_form_is_indexed(attrform)) {
1314 res = _dwarf_look_in_local_and_tied(
1315 attrform,
1316 cu_context,
1317 attr->ar_debug_ptr,
1318 return_addr,
1319 error);
1320 return res;
1321 }
1322 if (attrform == DW_FORM_addr ||
1323 (cu_context->cc_producer == CC_PROD_METROWERKS &&
1324 attrform == DW_FORM_ref_addr)
1325 /* Allowance of
1326 DW_FORM_ref_addr was a mistake. The value returned in that
1327 case is NOT an address it is a global debug_info
1328 offset (ie, not CU-relative offset within the CU
1329 in debug_info).
1330 A Metrowerks old C generates ref_adder!
1331 The DWARF2 document refers to it as an address
1332 (misleadingly) in sec 6.5.4 where it describes
1333 the reference form. It is
1334 address-sized so that the linker can easily update it, but
1335 it is a reference inside the debug_info section. No longer
1336 allowed except for Metrowerks C. */
1337 ) {
1338 Dwarf_Small *section_end =
1339 _dwarf_calculate_info_section_end_ptr(cu_context);
1340
1341 READ_UNALIGNED_CK(dbg, ret_addr, Dwarf_Addr,
1342 attr->ar_debug_ptr,
1343 cu_context->cc_address_size,
1344 error,section_end);
1345 *return_addr = ret_addr;
1346 return DW_DLV_OK;
1347 }
1348 generate_form_error(dbg,error,attrform,
1349 DW_DLE_ATTR_FORM_BAD,
1350 "DW_DLE_ATTR_FORM_BAD",
1351 "dwarf_formaddr");
1352 return DW_DLV_ERROR;
1353 }
1354
1355 int
1356 dwarf_formflag(Dwarf_Attribute attr,
1357 Dwarf_Bool * ret_bool, Dwarf_Error * error)
1358 {
1359 Dwarf_CU_Context cu_context = 0;
1360 Dwarf_Debug dbg = 0;
1361
1362 if (!attr) {
1363 _dwarf_error(NULL, error, DW_DLE_ATTR_NULL);
1364 return DW_DLV_ERROR;
1365 }
1366 cu_context = attr->ar_cu_context;
1367 if (!cu_context) {
1368 _dwarf_error(NULL, error, DW_DLE_ATTR_NO_CU_CONTEXT);
1369 return DW_DLV_ERROR;
1370 }
1371 dbg = cu_context->cc_dbg;
1372 if (IS_INVALID_DBG(dbg)) {
1373 _dwarf_error_string(NULL, error, DW_DLE_ATTR_DBG_NULL,
1374 "DW_DLE_ATTR_DBG_NULL: dwarf_formflag() attribute"
1375 " passed in has NULL or stale Dwarf_Debug pointer");
1376 return DW_DLV_ERROR;
1377 }
1378 if (dbg != attr->ar_dbg) {
1379 _dwarf_error_string(NULL, error, DW_DLE_ATTR_DBG_NULL,
1380 "DW_DLE_ATTR_DBG_NULL: an attribute and its "
1381 "cu_context do not have the same Dwarf_Debug" );
1382 return DW_DLV_ERROR;
1383 }
1384 if (attr->ar_attribute_form == DW_FORM_flag_present) {
1385 /* Implicit means we don't read any data at all. Just
1386 the existence of the Form does it. DWARF4. */
1387 *ret_bool = 1;
1388 return DW_DLV_OK;
1389 }
1390
1391 if (attr->ar_attribute_form == DW_FORM_flag) {
1392 *ret_bool = *(Dwarf_Small *)(attr->ar_debug_ptr);
1393 return DW_DLV_OK;
1394 }
1395 generate_form_error(dbg,error,attr->ar_attribute_form,
1396 DW_DLE_ATTR_FORM_BAD,
1397 "DW_DLE_ATTR_FORM_BAD",
1398 "dwarf_formflat");
1399 return DW_DLV_ERROR;
1400 }
1401
1402 Dwarf_Bool
1403 _dwarf_allow_formudata(unsigned form)
1404 {
1405 switch(form) {
1406 case DW_FORM_data1:
1407 case DW_FORM_data2:
1408 case DW_FORM_data4:
1409 case DW_FORM_data8:
1410 case DW_FORM_flag:
1411 case DW_FORM_flag_present:
1412 case DW_FORM_loclistx:
1413 case DW_FORM_rnglistx:
1414 return TRUE;
1415 default:
1416 break;
1417 }
1418 return FALSE;
1419 }
1420 /* If the form is DW_FORM_constx and the .debug_addr section
1421 is missing, this returns DW_DLV_ERROR and the error number
1422 in the Dwarf_Error is DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION.
1423 When that arises, a consumer should call
1424 dwarf_get_debug_addr_index() and use that on the appropriate
1425 .debug_addr section in the executable or another object.
1426
1427 Since this accept some signed values, callers
1428 must not assume a DW_DLV_OK means
1429 the value is unsigned. The form is the first clue here.
1430 If DW_FORM_sdata, then signed. Else unknown sign or
1431 is unsigned.
1432 */
1433
1434 int
1435 _dwarf_formudata_internal(Dwarf_Debug dbg,
1436 Dwarf_Attribute attr,
1437 unsigned form,
1438 Dwarf_Byte_Ptr data,
1439 Dwarf_Byte_Ptr section_end,
1440 Dwarf_Unsigned *return_uval,
1441 Dwarf_Unsigned *bytes_read,
1442 Dwarf_Error *error)
1443 {
1444 Dwarf_Unsigned ret_value = 0;
1445
1446 switch (form) {
1447 case DW_FORM_data1:
1448 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1449 data, sizeof(Dwarf_Small),
1450 error,section_end);
1451 *return_uval = ret_value;
1452 *bytes_read = 1;
1453 return DW_DLV_OK;
1454 case DW_FORM_flag_present:
1455 *return_uval = 1;
1456 *bytes_read = 0;
1457 return DW_DLV_OK;
1458 case DW_FORM_flag:
1459 /* equivalent to dwarf_formflag() */
1460 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1461 data, sizeof(Dwarf_Small),
1462 error,section_end);
1463 *return_uval = ret_value;
1464 *bytes_read = 1;
1465 return DW_DLV_OK;
1466
1467 /* READ_UNALIGNED does the right thing as it reads
1468 the right number bits and generates host order.
1469 So we can just assign to *return_uval. */
1470 case DW_FORM_data2:{
1471 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1472 data, DWARF_HALF_SIZE,
1473 error,section_end);
1474 *return_uval = ret_value;
1475 *bytes_read = DWARF_HALF_SIZE;
1476 return DW_DLV_OK;
1477 }
1478
1479 case DW_FORM_data4:{
1480 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1481 data,
1482 DWARF_32BIT_SIZE,
1483 error,section_end);
1484 *return_uval = ret_value;
1485 *bytes_read = DWARF_32BIT_SIZE;;
1486 return DW_DLV_OK;
1487 }
1488
1489 case DW_FORM_data8:{
1490 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1491 data,
1492 DWARF_64BIT_SIZE,
1493 error,section_end);
1494 *return_uval = ret_value;
1495 *bytes_read = DWARF_64BIT_SIZE;
1496 return DW_DLV_OK;
1497 }
1498 break;
1499 /* real udata */
1500 case DW_FORM_loclistx:
1501 case DW_FORM_rnglistx:
1502 case DW_FORM_udata: {
1503 Dwarf_Unsigned leblen = 0;
1504 DECODE_LEB128_UWORD_LEN_CK(data, ret_value,leblen,
1505 dbg,error,section_end);
1506 *return_uval = ret_value;
1507 *bytes_read = leblen;
1508 return DW_DLV_OK;
1509 }
1510 /* IRIX bug 583450. We do not allow reading
1511 sdata from a udata
1512 value. Caller can retry, calling sdata */
1513 default:
1514 break;
1515 }
1516 if (attr) {
1517 int res = 0;
1518 Dwarf_Signed s = 0;
1519 res = dwarf_formsdata(attr,&s,error);
1520 if (res != DW_DLV_OK) {
1521 return res;
1522 }
1523 if (s < 0 ) {
1524 _dwarf_error(dbg, error, DW_DLE_UDATA_VALUE_NEGATIVE);
1525 return DW_DLV_ERROR;
1526 }
1527 *return_uval = (Dwarf_Unsigned)s;
1528 *bytes_read = 0;
1529 return DW_DLV_OK;
1530 }
1531 generate_form_error(dbg,error,form,
1532 DW_DLE_ATTR_FORM_BAD,
1533 "DW_DLE_ATTR_FORM_BAD",
1534 "formudata (internal function)");
1535 return DW_DLV_ERROR;
1536 }
1537
1538 int
1539 dwarf_formudata(Dwarf_Attribute attr,
1540 Dwarf_Unsigned * return_uval, Dwarf_Error * error)
1541 {
1542 Dwarf_Debug dbg = 0;
1543 Dwarf_CU_Context cu_context = 0;
1544 Dwarf_Byte_Ptr section_end = 0;
1545 Dwarf_Unsigned bytes_read = 0;
1546 Dwarf_Byte_Ptr data = attr->ar_debug_ptr;
1547 unsigned form = 0;
1548
1549 int res = get_attr_dbg(&dbg,&cu_context,attr,error);
1550 if (res != DW_DLV_OK) {
1551 return res;
1552 }
1553 section_end =
1554 _dwarf_calculate_info_section_end_ptr(cu_context);
1555 form = attr->ar_attribute_form;
1556
1557 res = _dwarf_formudata_internal(dbg,
1558 attr,
1559 form, data, section_end, return_uval,
1560 &bytes_read, error);
1561 return res;
1562 }
1563
1564 int
1565 dwarf_formsdata(Dwarf_Attribute attr,
1566 Dwarf_Signed * return_sval, Dwarf_Error * error)
1567 {
1568 Dwarf_Signed ret_value = 0;
1569 Dwarf_Debug dbg = 0;
1570 Dwarf_CU_Context cu_context = 0;
1571 Dwarf_Byte_Ptr section_end = 0;
1572
1573 int res = get_attr_dbg(&dbg,&cu_context,attr,error);
1574 if (res != DW_DLV_OK) {
1575 return res;
1576 }
1577 section_end =
1578 _dwarf_calculate_info_section_end_ptr(cu_context);
1579 switch (attr->ar_attribute_form) {
1580
1581 case DW_FORM_data1:
1582 if ( attr->ar_debug_ptr >= section_end) {
1583 _dwarf_error(dbg, error, DW_DLE_DIE_BAD);
1584 return DW_DLV_ERROR;
1585 }
1586 *return_sval = (*(Dwarf_Sbyte *) attr->ar_debug_ptr);
1587 return DW_DLV_OK;
1588
1589 /* READ_UNALIGNED does not sign extend.
1590 So we have to use a cast to get the
1591 value sign extended in the right way for each case. */
1592 case DW_FORM_data2:{
1593 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Signed,
1594 attr->ar_debug_ptr,
1595 DWARF_HALF_SIZE,
1596 error,section_end);
1597 *return_sval = (Dwarf_Shalf) ret_value;
1598 return DW_DLV_OK;
1599
1600 }
1601
1602 case DW_FORM_data4:{
1603 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Signed,
1604 attr->ar_debug_ptr,
1605 DWARF_32BIT_SIZE,
1606 error,section_end);
1607 SIGN_EXTEND(ret_value,DWARF_32BIT_SIZE);
1608 *return_sval = (Dwarf_Signed) ret_value;
1609 return DW_DLV_OK;
1610 }
1611
1612 case DW_FORM_data8:{
1613 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Signed,
1614 attr->ar_debug_ptr,
1615 DWARF_64BIT_SIZE,
1616 error,section_end);
1617 /* No SIGN_EXTEND needed, we are filling all bytes already.*/
1618 *return_sval = (Dwarf_Signed) ret_value;
1619 return DW_DLV_OK;
1620 }
1621
1622 /* DW_FORM_implicit_const is a value in the
1623 abbreviations, not in the DIEs. */
1624 case DW_FORM_implicit_const:
1625 *return_sval = attr->ar_implicit_const;
1626 return DW_DLV_OK;
1627
1628 case DW_FORM_sdata: {
1629 Dwarf_Byte_Ptr tmp = attr->ar_debug_ptr;
1630
1631 DECODE_LEB128_SWORD_CK(tmp, ret_value,
1632 dbg,error,section_end);
1633 *return_sval = ret_value;
1634 return DW_DLV_OK;
1635
1636 }
1637
1638 /* IRIX bug 583450. We do not allow reading sdata from a udata
1639 value. Caller can retry, calling udata */
1640
1641 default:
1642 break;
1643 }
1644 generate_form_error(dbg,error,attr->ar_attribute_form,
1645 DW_DLE_ATTR_FORM_BAD,
1646 "DW_DLE_ATTR_FORM_BAD",
1647 "dwarf_formsdata");
1648 return DW_DLV_ERROR;
1649 }
1650
1651 int
1652 _dwarf_formblock_internal(Dwarf_Debug dbg,
1653 Dwarf_Attribute attr,
1654 Dwarf_CU_Context cu_context,
1655 Dwarf_Block * return_block,
1656 Dwarf_Error * error)
1657 {
1658 Dwarf_Small *section_start = 0;
1659 Dwarf_Small *section_end = 0;
1660 Dwarf_Unsigned section_length = 0;
1661 Dwarf_Unsigned length = 0;
1662 Dwarf_Small *data = 0;
1663
1664 section_end =
1665 _dwarf_calculate_info_section_end_ptr(cu_context);
1666 section_start =
1667 _dwarf_calculate_info_section_start_ptr(cu_context,
1668 &section_length);
1669 switch (attr->ar_attribute_form) {
1670 case DW_FORM_block1: {
1671 Dwarf_Small *start = attr->ar_debug_ptr;
1672 Dwarf_Small *incremented = start + 1;
1673 if ( incremented < start ||
1674 incremented >= section_end) {
1675 /* Error if +1 overflows or if points out of section. */
1676 generate_form_error(dbg,error,attr->ar_attribute_form,
1677 DW_DLE_ATTR_FORM_BAD,
1678 "DW_DLE_ATTR_FORM_BAD",
1679 " DW_FORM_block1 offset invalid");
1680 return DW_DLV_ERROR;
1681 }
1682 length = *start;
1683 data = incremented;
1684 break;
1685 }
1686 case DW_FORM_block2:
1687 READ_UNALIGNED_CK(dbg, length, Dwarf_Unsigned,
1688 attr->ar_debug_ptr, DWARF_HALF_SIZE,
1689 error,section_end);
1690 data = attr->ar_debug_ptr + DWARF_HALF_SIZE;
1691 break;
1692
1693 case DW_FORM_block4:
1694 READ_UNALIGNED_CK(dbg, length, Dwarf_Unsigned,
1695 attr->ar_debug_ptr, DWARF_32BIT_SIZE,
1696 error,section_end);
1697 data = attr->ar_debug_ptr + DWARF_32BIT_SIZE;
1698 break;
1699
1700 case DW_FORM_exprloc:
1701 case DW_FORM_block: {
1702 Dwarf_Byte_Ptr tmp = attr->ar_debug_ptr;
1703 Dwarf_Unsigned leblen = 0;
1704
1705 DECODE_LEB128_UWORD_LEN_CK(tmp, length, leblen,
1706 dbg,error,section_end);
1707 data = attr->ar_debug_ptr + leblen;
1708 break;
1709 }
1710 default:
1711 generate_form_error(dbg,error,attr->ar_attribute_form,
1712 DW_DLE_ATTR_FORM_BAD,
1713 "DW_DLE_ATTR_FORM_BAD",
1714 "dwarf_formblock() finds unknown form");
1715 return DW_DLV_ERROR;
1716 }
1717 /* We have the data. Check for errors. */
1718 if (length >= section_length) {
1719 /* Sanity test looking for wraparound:
1720 when length actually added in
1721 it would not be caught.
1722 Test could be just >, but >= ok here too.*/
1723 _dwarf_error_string(dbg, error,
1724 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
1725 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: "
1726 "The length of the block is greater "
1727 "than the section length! Corrupt Dwarf.");
1728 return DW_DLV_ERROR;
1729 }
1730 if ((attr->ar_debug_ptr + length) > section_end) {
1731 _dwarf_error_string(dbg, error,
1732 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
1733 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: "
1734 "The block length means the block "
1735 "runs off the end of the section length!"
1736 " Corrupt Dwarf.");
1737 return DW_DLV_ERROR;
1738 }
1739 if (data > section_end) {
1740 _dwarf_error_string(dbg, error,
1741 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
1742 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: "
1743 "The block content is "
1744 "past the end of the section!"
1745 " Corrupt Dwarf.");
1746 _dwarf_error(dbg, error, DW_DLE_FORM_BLOCK_LENGTH_ERROR);
1747 return DW_DLV_ERROR;
1748 }
1749 if ((data + length) > section_end) {
1750 _dwarf_error_string(dbg, error,
1751 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
1752 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: "
1753 "The end of the block content is "
1754 "past the end of the section!"
1755 " Corrupt Dwarf.");
1756 return DW_DLV_ERROR;
1757 }
1758 return_block->bl_len = length;
1759 return_block->bl_data = data;
1760 /* This struct is public so use the old name instead
1761 of what we now would call it: bl_kind */
1762 return_block->bl_from_loclist = DW_LKIND_expression;
1763 return_block->bl_section_offset = data - section_start;
1764 return DW_DLV_OK;
1765 }
1766
1767 int
1768 dwarf_formblock(Dwarf_Attribute attr,
1769 Dwarf_Block ** return_block, Dwarf_Error * error)
1770 {
1771 Dwarf_CU_Context cu_context = 0;
1772 Dwarf_Debug dbg = 0;
1773 Dwarf_Block local_block;
1774 Dwarf_Block *out_block = 0;
1775 int res = 0;
1776
1777 memset(&local_block,0,sizeof(local_block));
1778 res = get_attr_dbg(&dbg,&cu_context,attr,error);
1779 if (res != DW_DLV_OK) {
1780 return res;
1781 }
1782 res = _dwarf_formblock_internal(dbg,attr,
1783 cu_context, &local_block, error);
1784 if (res != DW_DLV_OK) {
1785 return res;
1786 }
1787 out_block = (Dwarf_Block *)
1788 _dwarf_get_alloc(dbg, DW_DLA_BLOCK, 1);
1789 if (!out_block) {
1790 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
1791 return DW_DLV_ERROR;
1792 }
1793 *out_block = local_block;
1794 *return_block = out_block;
1795 return DW_DLV_OK;
1796 }
1797
1798 /* This is called for attribute with strx form
1799 or macro5 with strx form.
1800 No relation to the Name Table or
1801 to FIXME */
1802 int
1803 _dwarf_extract_string_offset_via_str_offsets(Dwarf_Debug dbg,
1804 Dwarf_Small *data_ptr,
1805 Dwarf_Small *end_data_ptr,
1806 Dwarf_Half attrform,
1807 Dwarf_CU_Context cu_context,
1808 Dwarf_Unsigned *str_sect_offset_out,
1809 Dwarf_Error *error)
1810 {
1811 Dwarf_Unsigned index_to_offset_entry = 0;
1812 Dwarf_Unsigned offsettotable = 0;
1813 Dwarf_Unsigned end_offsettotable = 0;
1814 Dwarf_Unsigned indexoffset = 0;
1815 Dwarf_Unsigned baseoffset = 0;
1816 Dwarf_Unsigned table_offset_to_array = 0;
1817 int res = 0;
1818 int idxres = 0;
1819 Dwarf_Small *sectionptr = 0;
1820 Dwarf_Unsigned sectionlen = 0;
1821 Dwarf_Small *sof_start = 0;
1822 Dwarf_Small *sof_end = 0;
1823 Dwarf_Unsigned str_sect_offset = 0;
1824 Dwarf_Unsigned length_size = 0;
1825 Dwarf_Bool have_array_offset = FALSE;
1826
1827 res = _dwarf_load_section(dbg, &dbg->de_debug_str_offsets,error);
1828 if (res != DW_DLV_OK) {
1829 return res;
1830 }
1831 sectionptr = dbg->de_debug_str_offsets.dss_data;
1832 sectionlen = dbg->de_debug_str_offsets.dss_size;
1833 length_size = cu_context->cc_length_size;
1834 /* If this is a dwp we look there, but I suppose
1835 we could also look for the section in the tied
1836 executable object file it is not here. FIXME */
1837 idxres = dw_read_str_index_val_itself(dbg,
1838 attrform,data_ptr,end_data_ptr,&index_to_offset_entry,error);
1839 if ( idxres != DW_DLV_OK) {
1840 return idxres;
1841 }
1842 if (cu_context->cc_str_offsets_array_offset_present) {
1843 baseoffset = cu_context->cc_str_offsets_array_offset;
1844 have_array_offset = TRUE;
1845 } else if (cu_context->cc_str_offsets_tab_present) {
1846 baseoffset = cu_context->cc_str_offsets_header_offset;
1847 baseoffset += cu_context->cc_str_offsets_tab_to_array;
1848 have_array_offset = TRUE;
1849 } else { /* do nothing */}
1850 if (baseoffset > sectionlen ||
1851 (baseoffset+length_size) > sectionlen ||
1852 (baseoffset+(index_to_offset_entry *length_size)) >
1853 sectionlen) {
1854 _dwarf_error_string(dbg, error,
1855 DW_DLE_ATTR_FORM_SIZE_BAD,
1856 "DW_DLE_ATTR_FORM_SIZE_BAD: "
1857 "An Attribute value (offset into "
1858 ".debug_str_offsets) is impossibly "
1859 "large. Corrupt Dwarf.");
1860 return DW_DLV_ERROR;
1861 }
1862 indexoffset = index_to_offset_entry* length_size;
1863 if (!have_array_offset) {
1864 /* missing any connection to a specific
1865 str_offsets table this guesses at table zero.
1866 When the compiler/linker have
1867 combined str offsets into a
1868 single table this works. */
1869 Dwarf_Unsigned headeroffset = 0;
1870 if (cu_context->cc_version_stamp == DW_CU_VERSION5 ) {
1871 /* A base offset of 0 is ok for either
1872 DWARF5. but some early GNU compilers emitted
1873 DWARF4 .debug_str_offsets, so lets check
1874 the first table. */
1875 Dwarf_Unsigned stsize =
1876 dbg->de_debug_str_offsets.dss_size;
1877 Dwarf_Unsigned length = 0;
1878 Dwarf_Unsigned table_length = 0;
1879 Dwarf_Half local_offset_size = 0;
1880 Dwarf_Half local_extension_size = 0;
1881 Dwarf_Half version = 0;
1882 Dwarf_Half padding = 0;
1883
1884 res = _dwarf_trial_read_dwarf_five_hdr(dbg,
1885 headeroffset,stsize,
1886 &table_offset_to_array,
1887 &table_length,
1888 &length, &local_offset_size,
1889 &local_extension_size,
1890 &version,
1891 &padding,
1892 error);
1893 if (res == DW_DLV_OK) {
1894 baseoffset = table_offset_to_array+
1895 headeroffset;
1896 } else {
1897 if (res == DW_DLV_ERROR && error) {
1898 dwarf_dealloc_error(dbg,*error);
1899 *error = 0;
1900 } else {}
1901 }
1902 }
1903 }
1904 offsettotable = indexoffset+ baseoffset;
1905 end_offsettotable = offsettotable + length_size;
1906 /* The offsets table is a series of offset-size entries.
1907 The == case in the test applies when we are at the last table
1908 entry, so == is not an error, hence only test >
1909 */
1910 if (offsettotable > sectionlen ||
1911 end_offsettotable > sectionlen) {
1912 dwarfstring m;
1913
1914 dwarfstring_constructor(&m);
1915 dwarfstring_append_printf_u(&m,
1916 "DW_DLE_ATTR_FORM_SIZE_BAD: The end offset of "
1917 "a .debug_str_offsets table is 0x%x ",
1918 end_offsettotable);
1919 dwarfstring_append_printf_u(&m,
1920 "but the object section is just 0x%x "
1921 "bytes long",
1922 sectionlen);
1923 _dwarf_error_string(dbg, error,
1924 DW_DLE_ATTR_FORM_SIZE_BAD,
1925 dwarfstring_string(&m));
1926 dwarfstring_destructor(&m);
1927 return DW_DLV_ERROR;
1928 }
1929 sof_start = sectionptr+ offsettotable;
1930 sof_end = sectionptr + end_offsettotable;
1931 /* Now read the string offset from the offset table. */
1932 READ_UNALIGNED_CK(dbg,str_sect_offset,Dwarf_Unsigned,
1933 sof_start,
1934 length_size,error,sof_end);
1935 *str_sect_offset_out = str_sect_offset;
1936 return DW_DLV_OK;
1937 }
1938
1939 int
1940 _dwarf_extract_local_debug_str_string_given_offset(Dwarf_Debug dbg,
1941 unsigned attrform,
1942 Dwarf_Unsigned offset,
1943 char ** return_str,
1944 Dwarf_Error * error)
1945 {
1946 if (attrform == DW_FORM_strp ||
1947 attrform == DW_FORM_line_strp ||
1948 attrform == DW_FORM_GNU_str_index ||
1949 attrform == DW_FORM_strx1 ||
1950 attrform == DW_FORM_strx2 ||
1951 attrform == DW_FORM_strx3 ||
1952 attrform == DW_FORM_strx4 ||
1953 attrform == DW_FORM_strx) {
1954 /* The 'offset' into .debug_str or .debug_line_str is given,
1955 here we turn that into a pointer. */
1956 Dwarf_Small *secend = 0;
1957 Dwarf_Small *secbegin = 0;
1958 Dwarf_Small *strbegin = 0;
1959 Dwarf_Unsigned secsize = 0;
1960 int errcode = 0;
1961 const char *errname = 0;
1962 int res = 0;
1963
1964 if (attrform == DW_FORM_line_strp) {
1965 res = _dwarf_load_section(dbg,
1966 &dbg->de_debug_line_str,error);
1967 if (res != DW_DLV_OK) {
1968 return res;
1969 }
1970 errcode = DW_DLE_STRP_OFFSET_BAD;
1971 errname = "DW_DLE_STRP_OFFSET_BAD";
1972 secsize = dbg->de_debug_line_str.dss_size;
1973 secbegin = dbg->de_debug_line_str.dss_data;
1974 strbegin= dbg->de_debug_line_str.dss_data + offset;
1975 secend = dbg->de_debug_line_str.dss_data + secsize;
1976 } else {
1977 /* DW_FORM_strp etc */
1978 res = _dwarf_load_section(dbg, &dbg->de_debug_str,error);
1979 if (res != DW_DLV_OK) {
1980 return res;
1981 }
1982 errcode = DW_DLE_STRING_OFFSET_BAD;
1983 errname = "DW_DLE_STRING_OFFSET_BAD";
1984 secsize = dbg->de_debug_str.dss_size;
1985 secbegin = dbg->de_debug_str.dss_data;
1986 strbegin= dbg->de_debug_str.dss_data + offset;
1987 secend = dbg->de_debug_str.dss_data + secsize;
1988 }
1989 if (offset >= secsize) {
1990 dwarfstring m;
1991 const char *name = "<unknownform>";
1992
1993 dwarf_get_FORM_name(attrform,&name);
1994
1995 dwarfstring_constructor(&m);
1996 dwarfstring_append(&m,(char *)errname);
1997 dwarfstring_append_printf_s(&m,
1998 " Form %s ",(char *)name);
1999 dwarfstring_append_printf_u(&m,
2000 "string offset of 0x%" DW_PR_DUx " ",
2001 offset);
2002 dwarfstring_append_printf_u(&m,
2003 "is larger than the string section "
2004 "size of 0x%" DW_PR_DUx,
2005 secsize);
2006 _dwarf_error_string(dbg, error, errcode,
2007 dwarfstring_string(&m));
2008 dwarfstring_destructor(&m);
2009 /* Badly damaged DWARF here. */
2010 return DW_DLV_ERROR;
2011 }
2012 res= _dwarf_check_string_valid(dbg,secbegin,strbegin, secend,
2013 errcode,error);
2014 if (res != DW_DLV_OK) {
2015 return res;
2016 }
2017
2018 *return_str = (char *)strbegin;
2019 return DW_DLV_OK;
2020 }
2021 generate_form_error(dbg,error,attrform,
2022 DW_DLE_ATTR_FORM_BAD,
2023 "DW_DLE_ATTR_FORM_BAD",
2024 "extract debug_str string");
2025 return DW_DLV_ERROR;
2026 }
2027
2028 /* Contrary to pre-2005 documentation,
2029 The string pointer returned thru return_str must
2030 never have dwarf_dealloc() applied to it.
2031 Documentation fixed July 2005.
2032 */
2033 int
2034 dwarf_formstring(Dwarf_Attribute attr,
2035 char **return_str, Dwarf_Error * error)
2036 {
2037 Dwarf_CU_Context cu_context = 0;
2038 Dwarf_Debug dbg = 0;
2039 Dwarf_Unsigned offset = 0;
2040 int res = DW_DLV_ERROR;
2041 Dwarf_Small *secdataptr = 0;
2042 Dwarf_Small *secend = 0;
2043 Dwarf_Unsigned secdatalen = 0;
2044 Dwarf_Small *infoptr = attr->ar_debug_ptr;
2045 Dwarf_Small *contextend = 0;
2046
2047 res = get_attr_dbg(&dbg,&cu_context,attr,error);
2048 if (res != DW_DLV_OK) {
2049 return res;
2050 }
2051 if (cu_context->cc_is_info) {
2052 secdataptr = (Dwarf_Small *)dbg->de_debug_info.dss_data;
2053 secdatalen = dbg->de_debug_info.dss_size;
2054 } else {
2055 secdataptr = (Dwarf_Small *)dbg->de_debug_types.dss_data;
2056 secdatalen = dbg->de_debug_types.dss_size;
2057 }
2058 contextend = secdataptr +
2059 cu_context->cc_debug_offset +
2060 cu_context->cc_length +
2061 cu_context->cc_length_size +
2062 cu_context->cc_extension_size;
2063 secend = secdataptr + secdatalen;
2064 if (contextend < secend) {
2065 secend = contextend;
2066 }
2067 switch(attr->ar_attribute_form) {
2068 case DW_FORM_string: {
2069 Dwarf_Small *begin = attr->ar_debug_ptr;
2070
2071 res= _dwarf_check_string_valid(dbg,secdataptr,begin, secend,
2072 DW_DLE_FORM_STRING_BAD_STRING,error);
2073 if (res != DW_DLV_OK) {
2074 return res;
2075 }
2076 *return_str = (char *) (begin);
2077 return DW_DLV_OK;
2078 }
2079 case DW_FORM_GNU_strp_alt:
2080 case DW_FORM_strp_sup: {
2081 Dwarf_Error alterr = 0;
2082 Dwarf_Bool is_info = TRUE;
2083 /* See dwarfstd.org issue 120604.1
2084 This is the offset in the .debug_str section
2085 of another object file.
2086 The 'tied' file notion should apply.
2087 It is not clear whether both a supplementary
2088 and a split object might be needed at the same time
2089 (hence two 'tied' files simultaneously). */
2090 Dwarf_Off soffset = 0;
2091
2092 res = dwarf_global_formref_b(attr, &soffset,
2093 &is_info,error);
2094 if (res != DW_DLV_OK) {
2095 return res;
2096 }
2097 res = _dwarf_get_string_from_tied(dbg, soffset,
2098 return_str, &alterr);
2099 if (res == DW_DLV_ERROR) {
2100 if (dwarf_errno(alterr) ==
2101 DW_DLE_NO_TIED_FILE_AVAILABLE) {
2102
2103 dwarf_dealloc_error(dbg,alterr);
2104 if ( attr->ar_attribute_form ==
2105 DW_FORM_GNU_strp_alt) {
2106 *return_str =
2107 (char *)"<DW_FORM_GNU_strp_alt-no-tied-file>";
2108 } else {
2109 *return_str =
2110 (char *)"<DW_FORM_strp_sup-no-tied-file>";
2111 }
2112 return DW_DLV_OK;
2113 }
2114 if (error) {
2115 *error = alterr;
2116 } else {
2117 dwarf_dealloc_error(dbg,alterr);
2118 alterr = 0;
2119 }
2120 return res;
2121 }
2122 if (res == DW_DLV_NO_ENTRY) {
2123 if ( attr->ar_attribute_form == DW_FORM_GNU_strp_alt) {
2124 *return_str =
2125 (char *)"<DW_FORM_GNU_strp_alt-no-tied-file>";
2126 }else {
2127 *return_str =
2128 (char *)"<DW_FORM_strp_sup-no-tied-file>";
2129 }
2130 }
2131 return res;
2132 }
2133 case DW_FORM_GNU_str_index:
2134 case DW_FORM_strx:
2135 case DW_FORM_strx1:
2136 case DW_FORM_strx2:
2137 case DW_FORM_strx3:
2138 case DW_FORM_strx4: {
2139 Dwarf_Unsigned offsettostr= 0;
2140
2141 res = _dwarf_extract_string_offset_via_str_offsets(dbg,
2142 infoptr,
2143 secend,
2144 attr->ar_attribute_form,
2145 cu_context,
2146 &offsettostr,
2147 error);
2148 if (res != DW_DLV_OK) {
2149 return res;
2150 }
2151 offset = offsettostr;
2152 break;
2153 }
2154 case DW_FORM_strp:
2155 case DW_FORM_line_strp:{
2156 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2157 infoptr,
2158 cu_context->cc_length_size,error,secend);
2159 break;
2160 }
2161 default:
2162 _dwarf_error(dbg, error, DW_DLE_STRING_FORM_IMPROPER);
2163 return DW_DLV_ERROR;
2164 }
2165 /* Now we have offset so read the string from
2166 debug_str or debug_line_str. */
2167 res = _dwarf_extract_local_debug_str_string_given_offset(dbg,
2168 attr->ar_attribute_form,
2169 offset,
2170 return_str,
2171 error);
2172 return res;
2173 }
2174
2175 int
2176 _dwarf_get_string_from_tied(Dwarf_Debug dbg,
2177 Dwarf_Unsigned offset,
2178 char **return_str,
2179 Dwarf_Error*error)
2180 {
2181 Dwarf_Debug tieddbg = 0;
2182 Dwarf_Small *secend = 0;
2183 Dwarf_Small *secbegin = 0;
2184 Dwarf_Small *strbegin = 0;
2185 int res = DW_DLV_ERROR;
2186 Dwarf_Error localerror = 0;
2187
2188 /* Attach errors to dbg, not tieddbg. */
2189 if (!DBG_HAS_SECONDARY(dbg)) {
2190 _dwarf_error(dbg, error, DW_DLE_NO_TIED_FILE_AVAILABLE);
2191 return DW_DLV_ERROR;
2192 }
2193 tieddbg = dbg->de_secondary_dbg;
2194 /* The 'offset' into .debug_str is set. */
2195 res = _dwarf_load_section(tieddbg, &tieddbg->de_debug_str,
2196 &localerror);
2197 if (res == DW_DLV_ERROR) {
2198 Dwarf_Unsigned lerrno = dwarf_errno(localerror);
2199 dwarf_dealloc_error(tieddbg,localerror);
2200 _dwarf_error(dbg,error,lerrno);
2201 return res;
2202 }
2203 if (res == DW_DLV_NO_ENTRY) {
2204 return res;
2205 }
2206 if (offset >= tieddbg->de_debug_str.dss_size) {
2207 /* Badly damaged DWARF here. */
2208 _dwarf_error(dbg, error, DW_DLE_NO_TIED_STRING_AVAILABLE);
2209 return DW_DLV_ERROR;
2210 }
2211 secbegin = tieddbg->de_debug_str.dss_data;
2212 strbegin= tieddbg->de_debug_str.dss_data + offset;
2213 secend = tieddbg->de_debug_str.dss_data +
2214 tieddbg->de_debug_str.dss_size;
2215
2216 /* Ensure the offset lies within the .debug_str */
2217 if (offset >= tieddbg->de_debug_str.dss_size) {
2218 _dwarf_error(dbg, error, DW_DLE_NO_TIED_STRING_AVAILABLE);
2219 return DW_DLV_ERROR;
2220 }
2221 res= _dwarf_check_string_valid(tieddbg,secbegin,strbegin, secend,
2222 DW_DLE_NO_TIED_STRING_AVAILABLE,
2223 &localerror);
2224 if (res == DW_DLV_ERROR) {
2225 Dwarf_Unsigned lerrno = dwarf_errno(localerror);
2226 dwarf_dealloc_error(tieddbg,localerror);
2227 _dwarf_error(dbg,error,lerrno);
2228 return res;
2229 }
2230 if (res == DW_DLV_NO_ENTRY) {
2231 return res;
2232 }
2233 *return_str = (char *) (tieddbg->de_debug_str.dss_data + offset);
2234 return DW_DLV_OK;
2235 }
2236
2237 int
2238 dwarf_formexprloc(Dwarf_Attribute attr,
2239 Dwarf_Unsigned * return_exprlen,
2240 Dwarf_Ptr * block_ptr,
2241 Dwarf_Error * error)
2242 {
2243 Dwarf_Debug dbg = 0;
2244 Dwarf_CU_Context cu_context = 0;
2245
2246 int res = get_attr_dbg(&dbg,&cu_context,attr,error);
2247 if (res != DW_DLV_OK) {
2248 return res;
2249 }
2250 if (attr->ar_attribute_form == DW_FORM_exprloc ) {
2251 Dwarf_Die die = 0;
2252 Dwarf_Unsigned leb_len = 0;
2253 Dwarf_Byte_Ptr section_start = 0;
2254 Dwarf_Unsigned section_len = 0;
2255 Dwarf_Byte_Ptr section_end = 0;
2256 Dwarf_Byte_Ptr info_ptr = 0;
2257 Dwarf_Unsigned exprlen = 0;
2258 Dwarf_Small * addr = attr->ar_debug_ptr;
2259
2260 info_ptr = addr;
2261 section_start =
2262 _dwarf_calculate_info_section_start_ptr(cu_context,
2263 &section_len);
2264 section_end = section_start + section_len;
2265
2266 DECODE_LEB128_UWORD_LEN_CK(info_ptr, exprlen, leb_len,
2267 dbg,error,section_end);
2268 if (exprlen > section_len) {
2269 /* Corrupted dwarf! */
2270 dwarfstring m;
2271
2272 dwarfstring_constructor(&m);
2273 dwarfstring_append_printf_u(&m,
2274 "DW_DLE_ATTR_OUTSIDE_SECTION: "
2275 "The expression length is %u,",exprlen);
2276 dwarfstring_append_printf_u(&m,
2277 " but the section length is just %u. "
2278 "Corrupt Dwarf.",section_len);
2279 _dwarf_error_string(dbg, error,
2280 DW_DLE_ATTR_OUTSIDE_SECTION,
2281 dwarfstring_string(&m));
2282 dwarfstring_destructor(&m);
2283 return DW_DLV_ERROR;
2284 }
2285 die = attr->ar_die;
2286 /* Is the block entirely in the section, or is
2287 there bug somewhere?
2288 Here the final addr may be 1 past end of section. */
2289 if (_dwarf_reference_outside_section(die,
2290 (Dwarf_Small *)addr,
2291 ((Dwarf_Small *)addr)+exprlen +leb_len)) {
2292 dwarfstring m;
2293
2294 dwarfstring_constructor(&m);
2295 dwarfstring_append_printf_u(&m,
2296 "DW_DLE_ATTR_OUTSIDE_SECTION: "
2297 "The expression length %u,",exprlen);
2298 dwarfstring_append_printf_u(&m,
2299 " plus the leb value length of "
2300 "%u ",leb_len);
2301 dwarfstring_append(&m,
2302 " runs past the end of the section. "
2303 "Corrupt Dwarf.");
2304 _dwarf_error_string(dbg, error,
2305 DW_DLE_ATTR_OUTSIDE_SECTION,
2306 dwarfstring_string(&m));
2307 dwarfstring_destructor(&m);
2308 return DW_DLV_ERROR;
2309 }
2310 *return_exprlen = exprlen;
2311 *block_ptr = addr + leb_len;
2312 return DW_DLV_OK;
2313
2314 }
2315 {
2316 dwarfstring m;
2317 const char *name = "<name not known>";
2318 unsigned mform = attr->ar_attribute_form;
2319
2320 dwarfstring_constructor(&m);
2321
2322 dwarf_get_FORM_name (mform,&name);
2323 dwarfstring_append_printf_u(&m,
2324 "DW_DLE_ATTR_EXPRLOC_FORM_BAD: "
2325 "The form is 0x%x ", mform);
2326 dwarfstring_append_printf_s(&m,
2327 "(%s) but should be DW_FORM_exprloc. "
2328 "Corrupt Dwarf.",(char *)name);
2329 _dwarf_error_string(dbg, error, DW_DLE_ATTR_EXPRLOC_FORM_BAD,
2330 dwarfstring_string(&m));
2331 dwarfstring_destructor(&m);
2332 }
2333 return DW_DLV_ERROR;
2334 }