xref: /illumos-gate/usr/src/lib/libdwarf/common/dwarf_form.c (revision 4d9fdb46b215739778ebc12079842c9905586999)
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-2018 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 and/or modify it
8   under the terms of version 2.1 of the GNU Lesser General Public License
9   as published by the Free Software Foundation.
10 
11   This program is distributed in the hope that it would be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15   Further, this software is distributed without any warranty that it is
16   free of the rightful claim of any third person regarding infringement
17   or the like.  Any license provided herein, whether implied or
18   otherwise, applies only to this software file.  Patent licenses, if
19   any, provided herein do not apply to combinations of this program with
20   other software, or any other product whatsoever.
21 
22   You should have received a copy of the GNU Lesser General Public
23   License along with this program; if not, write the Free Software
24   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
25   USA.
26 
27 */
28 
29 #include "config.h"
30 #include <stdio.h>
31 #include "dwarf_incl.h"
32 #include "dwarf_alloc.h"
33 #include "dwarfstring.h"
34 #include "dwarf_error.h"
35 #include "dwarf_util.h"
36 #include "dwarf_die_deliv.h"
37 #include "dwarfstring.h"
38 
39 #define TRUE 1
40 #define FALSE 0
41 
42 /*  It is necessary at times to cause errors of this sort
43     in determining what we really have.  So best to avoid
44     too much malloc and free, hence the static constructor
45     dwarfstring will use malloc if we guess too-small
46     for the size of mbuf. */
47 static void
generate_form_error(Dwarf_Debug dbg,Dwarf_Error * error,unsigned form,int err_code,const char * errname,const char * funcname)48 generate_form_error(Dwarf_Debug dbg,
49     Dwarf_Error *error,
50     unsigned form,
51     int err_code,
52     const char *errname,
53     const char *funcname)
54 {
55     dwarfstring m;
56     char mbuf[DWARFSTRING_ALLOC_SIZE];
57     const char * defaultname = "<unknown form>";
58 
59     dwarfstring_constructor_static(&m,mbuf,
60         sizeof(mbuf));
61     dwarfstring_append(&m,(char *)errname);
62     dwarfstring_append(&m,": In function ");
63     dwarfstring_append(&m,(char *)funcname);
64     dwarfstring_append_printf_u(&m,
65         " on seeing form  0x%x ",form);
66     dwarf_get_FORM_name(form,&defaultname);
67     dwarfstring_append_printf_s(&m,
68         " (%s)",(char *)defaultname);
69     _dwarf_error_string(dbg,error,err_code,
70         dwarfstring_string(&m));
71     dwarfstring_destructor(&m);
72 }
73 
74 /* This code was repeated many times, now it
75    is all in one place. */
76 static int
get_attr_dbg(Dwarf_Debug * dbg,Dwarf_CU_Context * cu_context,Dwarf_Attribute attr,Dwarf_Error * error)77 get_attr_dbg(Dwarf_Debug *dbg,
78     Dwarf_CU_Context * cu_context,
79     Dwarf_Attribute attr,
80     Dwarf_Error *error)
81 {
82     Dwarf_CU_Context cup;
83     if (attr == NULL) {
84         _dwarf_error(NULL, error, DW_DLE_ATTR_NULL);
85         return (DW_DLV_ERROR);
86     }
87 
88     cup = attr->ar_cu_context;
89     if (cup == NULL) {
90         _dwarf_error(NULL, error, DW_DLE_ATTR_NO_CU_CONTEXT);
91         return (DW_DLV_ERROR);
92     }
93 
94     if (cup->cc_dbg == NULL) {
95         _dwarf_error(NULL, error, DW_DLE_ATTR_DBG_NULL);
96         return (DW_DLV_ERROR);
97     }
98     *cu_context = cup;
99     *dbg = cup->cc_dbg;
100     return DW_DLV_OK;
101 
102 }
103 
104 int
dwarf_hasform(Dwarf_Attribute attr,Dwarf_Half form,Dwarf_Bool * return_bool,Dwarf_Error * error)105 dwarf_hasform(Dwarf_Attribute attr,
106     Dwarf_Half form,
107     Dwarf_Bool * return_bool, Dwarf_Error * error)
108 {
109     Dwarf_Debug dbg = 0;
110     Dwarf_CU_Context cu_context = 0;
111 
112     int res  =get_attr_dbg(&dbg,&cu_context, attr,error);
113     if (res != DW_DLV_OK) {
114         return res;
115     }
116     *return_bool = (attr->ar_attribute_form == form);
117     return DW_DLV_OK;
118 }
119 
120 /* Not often called, we do not worry about efficiency here.
121    The dwarf_whatform() call does the sanity checks for us.
122 */
123 int
dwarf_whatform_direct(Dwarf_Attribute attr,Dwarf_Half * return_form,Dwarf_Error * error)124 dwarf_whatform_direct(Dwarf_Attribute attr,
125     Dwarf_Half * return_form, Dwarf_Error * error)
126 {
127     int res = dwarf_whatform(attr, return_form, error);
128 
129     if (res != DW_DLV_OK) {
130         return res;
131     }
132 
133     *return_form = attr->ar_attribute_form_direct;
134     return (DW_DLV_OK);
135 }
136 
137 /*  Pass in the content of a block and the length of that
138     content. On success return DW_DLV_OK and set *value_count
139     to the size of the array returned through value_array. */
140 int
dwarf_uncompress_integer_block_a(Dwarf_Debug dbg,Dwarf_Unsigned input_length_in_bytes,void * input_block,Dwarf_Unsigned * value_count,Dwarf_Signed ** value_array,Dwarf_Error * error)141 dwarf_uncompress_integer_block_a(Dwarf_Debug dbg,
142     Dwarf_Unsigned     input_length_in_bytes,
143     void             * input_block,
144     Dwarf_Unsigned   * value_count,
145     Dwarf_Signed    ** value_array,
146     Dwarf_Error      * error)
147 {
148     Dwarf_Unsigned output_length_in_units = 0;
149     Dwarf_Signed * output_block = 0;
150     unsigned i = 0;
151     char * ptr = 0;
152     int remain = 0;
153     Dwarf_Signed * array = 0;
154     Dwarf_Byte_Ptr endptr = (Dwarf_Byte_Ptr)input_block+
155         input_length_in_bytes;
156 
157     output_length_in_units = 0;
158     remain = input_length_in_bytes;
159     ptr = input_block;
160     while (remain > 0) {
161         Dwarf_Unsigned len = 0;
162         Dwarf_Signed value = 0;
163         int rres = 0;
164 
165         rres = _dwarf_decode_s_leb128_chk((unsigned char *)ptr,
166             &len, &value,endptr);
167         if (rres != DW_DLV_OK) {
168             _dwarf_error(NULL, error, DW_DLE_LEB_IMPROPER);
169             return DW_DLV_ERROR;
170         }
171         ptr += len;
172         remain -= len;
173         output_length_in_units++;
174     }
175     if (remain != 0) {
176         _dwarf_error(NULL, error, DW_DLE_ALLOC_FAIL);
177         return DW_DLV_ERROR;
178     }
179 
180     output_block = (Dwarf_Signed*)
181         _dwarf_get_alloc(dbg,
182             DW_DLA_STRING,
183             output_length_in_units * sizeof(Dwarf_Signed));
184     if (!output_block) {
185         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
186         return DW_DLV_ERROR;
187     }
188     array = output_block;
189     remain = input_length_in_bytes;
190     ptr = input_block;
191     for (i=0; i<output_length_in_units && remain>0; i++) {
192         Dwarf_Signed num;
193         Dwarf_Unsigned len;
194         int sres = 0;
195 
196         sres = _dwarf_decode_s_leb128_chk((unsigned char *)ptr,
197             &len, &num,endptr);
198         if (sres != DW_DLV_OK) {
199             dwarf_dealloc(dbg,output_block,DW_DLA_STRING);
200             _dwarf_error(NULL, error, DW_DLE_LEB_IMPROPER);
201             return DW_DLV_ERROR;
202         }
203         ptr += len;
204         remain -= len;
205         array[i] = num;
206     }
207 
208     if (remain != 0) {
209         dwarf_dealloc(dbg, (unsigned char *)output_block,
210             DW_DLA_STRING);
211         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
212         return DW_DLV_ERROR;
213     }
214 
215     *value_count = output_length_in_units;
216     *value_array = output_block;
217     return DW_DLV_OK;
218 }
219 
220 /*  This code was contributed around 2007
221     and the return value is in the wrong form.
222     See dwarf_uncompress_integer_block_a() above.
223 
224     As of 2019 it is not clear that Sun Sparc
225     compilers are in current use, nor whether
226     there is a reason to make reads of
227     this data format safe from corrupted object files.
228 */
229 void *
dwarf_uncompress_integer_block(Dwarf_Debug dbg,Dwarf_Bool unit_is_signed,Dwarf_Small unit_length_in_bits,void * input_block,Dwarf_Unsigned input_length_in_bytes,Dwarf_Unsigned * output_length_in_units_ptr,Dwarf_Error * error)230 dwarf_uncompress_integer_block(
231     Dwarf_Debug      dbg,
232     Dwarf_Bool       unit_is_signed,
233     Dwarf_Small      unit_length_in_bits,
234     void*            input_block,
235     Dwarf_Unsigned   input_length_in_bytes,
236     Dwarf_Unsigned*  output_length_in_units_ptr,
237     Dwarf_Error*     error
238 )
239 {
240     Dwarf_Unsigned output_length_in_units = 0;
241     void * output_block = 0;
242     unsigned i = 0;
243     char * ptr = 0;
244     int remain = 0;
245     /*  This only applies to Sun and there an unsigned
246         is 4 bytes so this works.  As with
247         most linux. */
248     unsigned * array = 0;
249     Dwarf_Byte_Ptr endptr = (Dwarf_Byte_Ptr)input_block+
250         input_length_in_bytes;
251 
252     if (dbg == NULL) {
253         _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
254         return((void *)DW_DLV_BADADDR);
255     }
256 
257 
258     if (unit_is_signed == false ||
259         unit_length_in_bits != 32 ||
260         input_block == NULL ||
261         input_length_in_bytes == 0 ||
262         output_length_in_units_ptr == NULL) {
263 
264         _dwarf_error(NULL, error, DW_DLE_BADBITC);
265         return ((void *) DW_DLV_BADADDR);
266     }
267 
268     /* At this point we assume the format is: signed 32 bit */
269 
270     /* first uncompress everything to find the total size. */
271 
272     output_length_in_units = 0;
273     remain = input_length_in_bytes;
274     ptr = input_block;
275     while (remain > 0) {
276         Dwarf_Unsigned len = 0;
277         Dwarf_Signed value = 0;
278         int rres = 0;
279 
280         rres = _dwarf_decode_s_leb128_chk((unsigned char *)ptr,
281             &len, &value,endptr);
282         if (rres != DW_DLV_OK) {
283             return ((void *)DW_DLV_BADADDR);
284         }
285         ptr += len;
286         remain -= len;
287         output_length_in_units++;
288     }
289 
290     if (remain != 0) {
291         _dwarf_error(NULL, error, DW_DLE_ALLOC_FAIL);
292         return((void *)DW_DLV_BADADDR);
293     }
294 
295     /* then alloc */
296 
297     output_block = (void *)
298         _dwarf_get_alloc(dbg,
299             DW_DLA_STRING,
300             output_length_in_units * (unit_length_in_bits / 8));
301     if (output_block == NULL) {
302         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
303         return((void*)DW_DLV_BADADDR);
304     }
305 
306     /* then uncompress again and copy into new buffer */
307 
308     array = (unsigned *) output_block;
309     remain = input_length_in_bytes;
310     ptr = input_block;
311     for (i=0; i<output_length_in_units && remain>0; i++) {
312         Dwarf_Signed num;
313         Dwarf_Unsigned len;
314         int sres = 0;
315 
316         sres = _dwarf_decode_s_leb128_chk((unsigned char *)ptr,
317             &len, &num,endptr);
318         if (sres != DW_DLV_OK) {
319             dwarf_dealloc(dbg,output_block,DW_DLA_STRING);
320             return ((void *) DW_DLV_BADADDR);
321         }
322         ptr += len;
323         remain -= len;
324         array[i] = num;
325     }
326 
327     if (remain != 0) {
328         dwarf_dealloc(dbg, (unsigned char *)output_block, DW_DLA_STRING);
329         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
330         return((Dwarf_P_Attribute)DW_DLV_BADADDR);
331     }
332 
333     *output_length_in_units_ptr = output_length_in_units;
334     return output_block;
335 }
336 
337 void
dwarf_dealloc_uncompressed_block(Dwarf_Debug dbg,void * space)338 dwarf_dealloc_uncompressed_block(Dwarf_Debug dbg, void * space)
339 {
340     dwarf_dealloc(dbg, space, DW_DLA_STRING);
341 }
342 
343 
344 int
dwarf_whatform(Dwarf_Attribute attr,Dwarf_Half * return_form,Dwarf_Error * error)345 dwarf_whatform(Dwarf_Attribute attr,
346     Dwarf_Half * return_form, Dwarf_Error * error)
347 {
348     Dwarf_CU_Context cu_context = 0;
349     Dwarf_Debug dbg = 0;
350 
351     int res  =get_attr_dbg(&dbg,&cu_context, attr,error);
352     if (res != DW_DLV_OK) {
353         return res;
354     }
355     *return_form = attr->ar_attribute_form;
356     return (DW_DLV_OK);
357 }
358 
359 
360 /*
361     This function is analogous to dwarf_whatform.
362     It returns the attribute in attr instead of
363     the form.
364 */
365 int
dwarf_whatattr(Dwarf_Attribute attr,Dwarf_Half * return_attr,Dwarf_Error * error)366 dwarf_whatattr(Dwarf_Attribute attr,
367     Dwarf_Half * return_attr, Dwarf_Error * error)
368 {
369     Dwarf_CU_Context cu_context = 0;
370     Dwarf_Debug dbg = 0;
371 
372     int res  =get_attr_dbg(&dbg,&cu_context, attr,error);
373     if (res != DW_DLV_OK) {
374         return res;
375     }
376     *return_attr = (attr->ar_attribute);
377     return DW_DLV_OK;
378 }
379 
380 
381 /*  Convert an offset within the local CU into a section-relative
382     debug_info (or debug_types) offset.
383     See dwarf_global_formref() and dwarf_formref()
384     for additional information on conversion rules.
385 */
386 int
dwarf_convert_to_global_offset(Dwarf_Attribute attr,Dwarf_Off offset,Dwarf_Off * ret_offset,Dwarf_Error * error)387 dwarf_convert_to_global_offset(Dwarf_Attribute attr,
388     Dwarf_Off offset, Dwarf_Off * ret_offset, Dwarf_Error * error)
389 {
390     Dwarf_Debug dbg = 0;
391     Dwarf_CU_Context cu_context = 0;
392     int res = 0;
393 
394     res  = get_attr_dbg(&dbg,&cu_context,attr,error);
395     if (res != DW_DLV_OK) {
396         return res;
397     }
398     switch (attr->ar_attribute_form) {
399     case DW_FORM_ref1:
400     case DW_FORM_ref2:
401     case DW_FORM_ref4:
402     case DW_FORM_ref8:
403     case DW_FORM_ref_udata:
404         /*  It is a cu-local offset. Convert to section-global. */
405         /*  It would be nice to put some code to check
406             legality of the offset */
407         /*  cc_debug_offset always has any DWP Package File
408             offset included (when the cu_context created)
409             so there is no extra work for DWP.
410             Globalize the offset */
411         offset += cu_context->cc_debug_offset;
412 
413         break;
414 
415     case DW_FORM_ref_addr:
416         /*  This offset is defined to be debug_info global already, so
417             use this value unaltered.
418 
419             Since a DWP package file is not relocated there
420             is no way that this reference offset to an address in
421             any other CU can be correct for a DWP Package File offset
422             */
423         break;
424     default: {
425         dwarfstring m;
426 
427         dwarfstring_constructor(&m);
428         dwarfstring_append_printf_u(&m,
429             "DW_DLE_BAD_REF_FORM. The form "
430             "code is 0x%x which cannot be converted to a global "
431             " offset by "
432             "dwarf_convert_to_global_offset()",
433             attr->ar_attribute_form);
434         _dwarf_error_string(dbg, error, DW_DLE_BAD_REF_FORM,
435             dwarfstring_string(&m));
436         dwarfstring_destructor(&m);
437         return DW_DLV_ERROR;
438         }
439     }
440 
441     *ret_offset = (offset);
442     return DW_DLV_OK;
443 }
444 
445 
446 /*  A global offset cannot be returned by this interface:
447     see dwarf_global_formref().
448 
449     DW_FORM_ref_addr is considered an incorrect form
450     for this call because DW_FORM_ref_addr is a global-offset into
451     the debug_info section.
452 
453     For the same reason DW_FORM_data4/data8 are not returned
454     from this function.
455 
456     For the same reason DW_FORM_sec_offset is not returned
457     from this function, DW_FORM_sec_offset is a global offset
458     (to various sections, not a CU relative offset.
459 
460     DW_FORM_ref_addr has a value which was documented in
461     DWARF2 as address-size but which was always an offset
462     so should have always been offset size (wording
463     corrected in DWARF3).
464     The dwarfstd.org FAQ "How big is a DW_FORM_ref_addr?"
465     suggested all should use offset-size, but that suggestion
466     seems to have been ignored in favor of doing what the
467     DWARF2 and 3 standards actually say.
468 
469     November, 2010: *ret_offset is always set now.
470     Even in case of error.
471     Set to zero for most errors, but for
472         DW_DLE_ATTR_FORM_OFFSET_BAD
473     *ret_offset is set to the bad offset.
474 
475     DW_FORM_addrx
476     DW_FORM_strx
477     DW_FORM_rnglistx
478     DW_FORM_GNU_addr_index
479     DW_FORM_GNU_str_index
480     are not references to .debug_info/.debug_types,
481     so they are not allowed here. */
482 
483 
484 int
dwarf_formref(Dwarf_Attribute attr,Dwarf_Off * ret_offset,Dwarf_Error * error)485 dwarf_formref(Dwarf_Attribute attr,
486    Dwarf_Off * ret_offset,
487    Dwarf_Error * error)
488 {
489     Dwarf_Debug dbg = 0;
490     Dwarf_Unsigned offset = 0;
491     Dwarf_CU_Context cu_context = 0;
492     Dwarf_Unsigned maximumoffset = 0;
493     int res = DW_DLV_ERROR;
494     Dwarf_Byte_Ptr section_end = 0;
495 
496     *ret_offset = 0;
497     res  = get_attr_dbg(&dbg,&cu_context,attr,error);
498     if (res != DW_DLV_OK) {
499         return res;
500     }
501     section_end =
502         _dwarf_calculate_info_section_end_ptr(cu_context);
503 
504     switch (attr->ar_attribute_form) {
505 
506     case DW_FORM_ref1:
507         offset = *(Dwarf_Small *) attr->ar_debug_ptr;
508         break;
509 
510     case DW_FORM_ref2:
511         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
512             attr->ar_debug_ptr, DWARF_HALF_SIZE,
513             error,section_end);
514         break;
515 
516     case DW_FORM_ref4:
517         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
518             attr->ar_debug_ptr, DWARF_32BIT_SIZE,
519             error,section_end);
520         break;
521 
522     case DW_FORM_ref8:
523         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
524             attr->ar_debug_ptr, DWARF_64BIT_SIZE,
525             error,section_end);
526         break;
527 
528     case DW_FORM_ref_udata: {
529         Dwarf_Byte_Ptr ptr = attr->ar_debug_ptr;
530         Dwarf_Unsigned localoffset = 0;
531 
532         DECODE_LEB128_UWORD_CK(ptr,localoffset,
533             dbg,error,section_end);
534         offset = localoffset;
535         break;
536     }
537     case DW_FORM_ref_sig8:
538         /*  We cannot handle this here.
539             The reference is to .debug_types
540             not a .debug_info CU local offset. */
541         _dwarf_error(dbg, error, DW_DLE_REF_SIG8_NOT_HANDLED);
542         return (DW_DLV_ERROR);
543     default: {
544         dwarfstring m;
545 
546         dwarfstring_constructor(&m);
547         dwarfstring_append_printf_u(&m,
548             "DW_DLE_BAD_REF_FORM. The form "
549             "code is 0x%x which does not have an offset "
550             " for "
551             "dwarf_formref() to return.",
552             attr->ar_attribute_form);
553         _dwarf_error_string(dbg, error, DW_DLE_BAD_REF_FORM,
554             dwarfstring_string(&m));
555         dwarfstring_destructor(&m);
556         return DW_DLV_ERROR;
557         }
558     }
559 
560     /*  Check that offset is within current
561         cu portion of .debug_info. */
562 
563     maximumoffset = cu_context->cc_length +
564         cu_context->cc_length_size +
565         cu_context->cc_extension_size;
566     if (offset >= maximumoffset) {
567         /*  For the DW_TAG_compile_unit is legal to have the
568             DW_AT_sibling attribute outside the current cu portion of
569             .debug_info.
570             In other words, sibling points to the end of the CU.
571             It is used for precompiled headers.
572             The valid condition will be: 'offset == maximumoffset'. */
573         Dwarf_Half tag = 0;
574         int tres = dwarf_tag(attr->ar_die,&tag,error);
575         if (tres != DW_DLV_OK) {
576             if (tres == DW_DLV_NO_ENTRY) {
577                 _dwarf_error(dbg, error, DW_DLE_NO_TAG_FOR_DIE);
578                 return DW_DLV_ERROR;
579             }
580             return DW_DLV_ERROR;
581         }
582 
583         if (DW_TAG_compile_unit != tag &&
584             DW_AT_sibling != attr->ar_attribute &&
585             offset > maximumoffset) {
586             _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_OFFSET_BAD);
587             /* Return the incorrect offset for better error reporting */
588             *ret_offset = (offset);
589             return DW_DLV_ERROR;
590         }
591     }
592     *ret_offset = (offset);
593     return DW_DLV_OK;
594 }
595 
596 static int
_dwarf_formsig8_internal(Dwarf_Attribute attr,int formexpected,int formerrnum,Dwarf_Sig8 * returned_sig_bytes,Dwarf_Error * error)597 _dwarf_formsig8_internal(Dwarf_Attribute attr,
598     int formexpected,
599     int formerrnum,
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         _dwarf_error(dbg, error, formerrnum);
615         return (DW_DLV_ERROR);
616     }
617     section_end =
618         _dwarf_calculate_info_section_end_ptr(cu_context);
619     field_end = attr->ar_debug_ptr + sizeof(Dwarf_Sig8);
620     if (field_end > section_end) {
621         _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_OFFSET_BAD);
622         return (DW_DLV_ERROR);
623     }
624 
625     memcpy(returned_sig_bytes, attr->ar_debug_ptr,
626         sizeof(Dwarf_Sig8));
627     return DW_DLV_OK;
628 }
629 
630 int
dwarf_formsig8_const(Dwarf_Attribute attr,Dwarf_Sig8 * returned_sig_bytes,Dwarf_Error * error)631 dwarf_formsig8_const(Dwarf_Attribute attr,
632     Dwarf_Sig8 * returned_sig_bytes,
633     Dwarf_Error* error)
634 {
635     int res  =_dwarf_formsig8_internal(attr, DW_FORM_data8,
636         DW_DLE_ATTR_FORM_NOT_DATA8,
637         returned_sig_bytes,error);
638     return res;
639 }
640 
641 /*  dwarf_formsig8 returns in the caller-provided 8 byte area
642     the 8 bytes of a DW_FORM_ref_sig8 (copying the bytes
643     directly to the caller).  Not a string, an 8 byte
644     MD5 hash.  This function is new in DWARF4 libdwarf.
645 */
646 int
dwarf_formsig8(Dwarf_Attribute attr,Dwarf_Sig8 * returned_sig_bytes,Dwarf_Error * error)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         DW_DLE_BAD_REF_SIG8_FORM,
653         returned_sig_bytes,error);
654     return res;
655 }
656 
657 
658 
659 
660 /*  Since this returns section-relative debug_info offsets,
661     this can represent all REFERENCE forms correctly
662     and allows all applicable forms.
663 
664     DW_FORM_ref_addr has a value which was documented in
665     DWARF2 as address-size but which was always an offset
666     so should have always been offset size (wording
667     corrected in DWARF3).
668     gcc and Go and libdwarf producer code
669     define the length of the value of DW_FORM_ref_addr
670     per the version. So for V2 it is address-size and V3 and later
671     it is offset-size.
672 
673     See the DWARF4 document for the 3 cases fitting
674     reference forms.  The caller must determine which section the
675     reference 'points' to.  The function added in November 2009,
676     dwarf_get_form_class(), helps in this regard.
677 
678     unlike dwarf_formref(), this allows references to
679     sections other than just .debug_info/.debug_types.
680     See case DW_FORM_sec_offset:
681     case DW_FORM_GNU_ref_alt:   2013 GNU extension
682     case DW_FORM_GNU_strp_alt:  2013 GNU extension
683     case DW_FORM_strp_sup:      DWARF5, sup string section
684     case DW_FORM_line_strp:     DWARF5, .debug_line_str section
685 */
686 
687 int
dwarf_global_formref(Dwarf_Attribute attr,Dwarf_Off * ret_offset,Dwarf_Error * error)688 dwarf_global_formref(Dwarf_Attribute attr,
689     Dwarf_Off * ret_offset, Dwarf_Error * error)
690 {
691     Dwarf_Debug dbg = 0;
692     Dwarf_Unsigned offset = 0;
693     Dwarf_CU_Context cu_context = 0;
694     Dwarf_Half context_version = 0;
695     Dwarf_Byte_Ptr section_end = 0;
696 
697     int res  = get_attr_dbg(&dbg,&cu_context,attr,error);
698     if (res != DW_DLV_OK) {
699         return res;
700     }
701     section_end =
702         _dwarf_calculate_info_section_end_ptr(cu_context);
703     context_version = cu_context->cc_version_stamp;
704     switch (attr->ar_attribute_form) {
705 
706     case DW_FORM_ref1:
707         offset = *(Dwarf_Small *) attr->ar_debug_ptr;
708         goto fixoffset;
709 
710     case DW_FORM_ref2:
711         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
712             attr->ar_debug_ptr, DWARF_HALF_SIZE,
713             error,section_end);
714         goto fixoffset;
715 
716     case DW_FORM_ref4:
717         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
718             attr->ar_debug_ptr, DWARF_32BIT_SIZE,
719             error,section_end);
720         goto fixoffset;
721 
722     case DW_FORM_ref8:
723         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
724             attr->ar_debug_ptr, DWARF_64BIT_SIZE,
725             error,section_end);
726         goto fixoffset;
727 
728     case DW_FORM_ref_udata:
729         {
730         Dwarf_Byte_Ptr ptr = attr->ar_debug_ptr;
731         Dwarf_Unsigned localoffset = 0;
732 
733         DECODE_LEB128_UWORD_CK(ptr,localoffset,
734             dbg,error,section_end);
735         offset = localoffset;
736 
737         fixoffset: /* we have a local offset, make it global */
738 
739         /* check legality of offset */
740         if (offset >= cu_context->cc_length +
741             cu_context->cc_length_size +
742             cu_context->cc_extension_size) {
743             _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_OFFSET_BAD);
744             return (DW_DLV_ERROR);
745         }
746 
747         /* globalize the offset */
748         offset += cu_context->cc_debug_offset;
749         }
750         break;
751 
752     /*  The DWARF2 document did not make clear that
753         DW_FORM_data4( and 8) were references with
754         global offsets to some section.
755         That was first clearly documented in DWARF3.
756         In DWARF4 these two forms are no longer references. */
757     case DW_FORM_data4:
758         if (context_version >= DW_CU_VERSION4) {
759             _dwarf_error(dbg, error, DW_DLE_NOT_REF_FORM);
760             return (DW_DLV_ERROR);
761         }
762         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
763             attr->ar_debug_ptr, DWARF_32BIT_SIZE,
764             error, section_end);
765         /* The offset is global. */
766         break;
767     case DW_FORM_data8:
768         if (context_version >= DW_CU_VERSION4) {
769             _dwarf_error(dbg, error, DW_DLE_NOT_REF_FORM);
770             return (DW_DLV_ERROR);
771         }
772         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
773             attr->ar_debug_ptr, DWARF_64BIT_SIZE,
774             error,section_end);
775         /* The offset is global. */
776         break;
777     case DW_FORM_ref_addr:
778         {
779             /*  In Dwarf V2 DW_FORM_ref_addr was defined
780                 as address-size even though it is a .debug_info
781                 offset.  Fixed in Dwarf V3 to be offset-size.
782                 */
783             unsigned length_size = 0;
784             if (context_version == 2) {
785                 length_size = cu_context->cc_address_size;
786             } else {
787                 length_size = cu_context->cc_length_size;
788             }
789             if (length_size == 4) {
790                 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
791                     attr->ar_debug_ptr, DWARF_32BIT_SIZE,
792                     error,section_end);
793             } else if (length_size == 8) {
794                 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
795                     attr->ar_debug_ptr, DWARF_64BIT_SIZE,
796                     error,section_end);
797             } else {
798                 _dwarf_error(dbg, error, DW_DLE_FORM_SEC_OFFSET_LENGTH_BAD);
799                 return (DW_DLV_ERROR);
800             }
801         }
802         break;
803     /*  Index into .debug_rnglist section.
804         Return the index itself. */
805     case DW_FORM_rnglistx: {
806         unsigned length_size = cu_context->cc_length_size;
807         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
808             attr->ar_debug_ptr, length_size,
809             error,section_end);
810         }
811         break;
812     case DW_FORM_sec_offset:
813     case DW_FORM_GNU_ref_alt:  /* 2013 GNU extension */
814     case DW_FORM_GNU_strp_alt: /* 2013 GNU extension */
815     case DW_FORM_strp_sup:     /* DWARF5, sup string section */
816     case DW_FORM_line_strp:    /* DWARF5, .debug_line_str section */
817         {
818             /*  DW_FORM_sec_offset first exists in DWARF4.*/
819             /*  It is up to the caller to know what the offset
820                 of DW_FORM_sec_offset, DW_FORM_strp_sup
821                 or DW_FORM_GNU_strp_alt etc refer to,
822                 the offset is not going to refer to .debug_info! */
823             unsigned length_size = cu_context->cc_length_size;
824             if (length_size == 4) {
825                 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
826                     attr->ar_debug_ptr, DWARF_32BIT_SIZE,
827                     error,section_end);
828             } else if (length_size == 8) {
829                 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
830                     attr->ar_debug_ptr, DWARF_64BIT_SIZE,
831                     error,section_end);
832             } else {
833                 _dwarf_error(dbg, error, DW_DLE_FORM_SEC_OFFSET_LENGTH_BAD);
834                 return (DW_DLV_ERROR);
835             }
836         }
837         break;
838     case DW_FORM_ref_sig8: /* FIXME */
839         /*  We cannot handle this yet.
840             The reference is to .debug_types, and
841             this function only returns an offset in
842             .debug_info at this point. */
843         _dwarf_error(dbg, error, DW_DLE_REF_SIG8_NOT_HANDLED);
844         return (DW_DLV_ERROR);
845     default: {
846         dwarfstring m;
847         int formcode = attr->ar_attribute_form;
848         int fcres = 0;
849         const char *name = 0;
850 
851         dwarfstring_constructor(&m);
852         dwarfstring_append_printf_u(&m,
853             "DW_DLE_BAD_REF_FORM: The form code is 0x%x ",
854             formcode);
855         fcres  = dwarf_get_FORM_name (formcode,&name);
856         if (fcres != DW_DLV_OK) {
857             name="<UnknownFormCode>";
858         }
859         dwarfstring_append_printf_s(&m,
860             " %s.",(char *)name);
861         _dwarf_error_string(dbg, error, DW_DLE_BAD_REF_FORM,
862             dwarfstring_string(&m));
863         dwarfstring_destructor(&m);
864         return DW_DLV_ERROR;
865         }
866     }
867 
868     /*  We do not know what section the offset refers to, so
869         we have no way to check it for correctness. */
870     *ret_offset = offset;
871     return DW_DLV_OK;
872 }
873 
874 /*  Part of DebugFission.  So a consumer can get the index when
875     the object with the actual debug_addr  is
876     elsewhere.  New May 2014*/
877 
878 int
_dwarf_get_addr_index_itself(int theform,Dwarf_Small * info_ptr,Dwarf_Debug dbg,Dwarf_CU_Context cu_context,Dwarf_Unsigned * val_out,Dwarf_Error * error)879 _dwarf_get_addr_index_itself(int theform,
880     Dwarf_Small *info_ptr,
881     Dwarf_Debug dbg,
882     Dwarf_CU_Context cu_context,
883     Dwarf_Unsigned *val_out,
884     Dwarf_Error * error)
885 {
886     Dwarf_Unsigned index = 0;
887     Dwarf_Byte_Ptr section_end = 0;
888 
889     section_end =
890         _dwarf_calculate_info_section_end_ptr(cu_context);
891     switch(theform){
892     case DW_FORM_GNU_addr_index:
893     case DW_FORM_addrx:
894         DECODE_LEB128_UWORD_CK(info_ptr,index,
895             dbg,error,section_end);
896         break;
897     case DW_FORM_addrx1:
898         READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
899             info_ptr, 1,
900             error,section_end);
901         break;
902     case DW_FORM_addrx2:
903         READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
904             info_ptr, 2,
905             error,section_end);
906         break;
907     case DW_FORM_addrx3:
908         READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
909             info_ptr, 3,
910             error,section_end);
911         break;
912     case DW_FORM_addrx4:
913         READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
914             info_ptr, 4,
915             error,section_end);
916         break;
917     default:
918         _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_NOT_ADDR_INDEX);
919         return DW_DLV_ERROR;
920     }
921     *val_out = index;
922     return DW_DLV_OK;
923 }
924 
925 int
dwarf_get_debug_addr_index(Dwarf_Attribute attr,Dwarf_Unsigned * return_index,Dwarf_Error * error)926 dwarf_get_debug_addr_index(Dwarf_Attribute attr,
927     Dwarf_Unsigned * return_index,
928     Dwarf_Error * error)
929 {
930     int theform = 0;
931     Dwarf_CU_Context cu_context = 0;
932     Dwarf_Debug dbg = 0;
933 
934     int res  = get_attr_dbg(&dbg,&cu_context,attr,error);
935     if (res != DW_DLV_OK) {
936         return res;
937     }
938     theform = attr->ar_attribute_form;
939     if (dwarf_addr_form_is_indexed(theform)) {
940         Dwarf_Unsigned index = 0;
941 
942         res = _dwarf_get_addr_index_itself(theform,
943             attr->ar_debug_ptr,dbg,cu_context,&index,error);
944         *return_index = index;
945         return res;
946     }
947 
948     _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_NOT_ADDR_INDEX);
949     return DW_DLV_ERROR;
950 }
951 
952 static int
dw_read_index_val_itself(Dwarf_Debug dbg,unsigned theform,Dwarf_Small * info_ptr,Dwarf_Small * section_end,Dwarf_Unsigned * return_index,Dwarf_Error * error)953 dw_read_index_val_itself(Dwarf_Debug dbg,
954    unsigned theform,
955    Dwarf_Small *info_ptr,
956    Dwarf_Small *section_end,
957    Dwarf_Unsigned *return_index,
958    Dwarf_Error *error)
959 {
960     Dwarf_Unsigned index = 0;
961 
962     switch(theform) {
963     case DW_FORM_strx:
964     case DW_FORM_GNU_str_index:
965         DECODE_LEB128_UWORD_CK(info_ptr,index,
966             dbg,error,section_end);
967         break;
968     case DW_FORM_strx1:
969         READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
970             info_ptr, 1,
971             error,section_end);
972         break;
973     case DW_FORM_strx2:
974         READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
975             info_ptr, 2,
976             error,section_end);
977         break;
978     case DW_FORM_strx3:
979         READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
980             info_ptr, 3,
981             error,section_end);
982         break;
983     case DW_FORM_strx4:
984         READ_UNALIGNED_CK(dbg, index, Dwarf_Unsigned,
985             info_ptr, 4,
986             error,section_end);
987         break;
988     default:
989         _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_NOT_STR_INDEX);
990         return DW_DLV_ERROR;
991     }
992     *return_index = index;
993     return DW_DLV_OK;
994 }
995 
996 /*  Part of DebugFission.  So a dwarf dumper application
997     can get the index and print it for the user.
998     A convenience function.  New May 2014
999     Also used with DWARF5 forms.  */
1000 int
dwarf_get_debug_str_index(Dwarf_Attribute attr,Dwarf_Unsigned * return_index,Dwarf_Error * error)1001 dwarf_get_debug_str_index(Dwarf_Attribute attr,
1002     Dwarf_Unsigned *return_index,
1003     Dwarf_Error *error)
1004 {
1005     int theform = attr->ar_attribute_form;
1006     Dwarf_CU_Context cu_context = 0;
1007     Dwarf_Debug dbg = 0;
1008     int res  = 0;
1009     Dwarf_Byte_Ptr section_end =  0;
1010     Dwarf_Unsigned index = 0;
1011     Dwarf_Small *info_ptr = 0;
1012     int indxres = 0;
1013 
1014     res = get_attr_dbg(&dbg,&cu_context,attr,error);
1015     if (res != DW_DLV_OK) {
1016         return res;
1017     }
1018     section_end =
1019         _dwarf_calculate_info_section_end_ptr(cu_context);
1020     info_ptr = attr->ar_debug_ptr;
1021 
1022     indxres = dw_read_index_val_itself(dbg, theform, info_ptr,
1023         section_end, &index,error);
1024     if (indxres == DW_DLV_OK) {
1025         *return_index = index;
1026         return indxres;
1027     }
1028     return indxres;
1029 }
1030 
1031 
1032 int
_dwarf_extract_data16(Dwarf_Debug dbg,Dwarf_Small * data,Dwarf_Small * section_start,Dwarf_Small * section_end,Dwarf_Form_Data16 * returned_val,Dwarf_Error * error)1033 _dwarf_extract_data16(Dwarf_Debug dbg,
1034     Dwarf_Small *data,
1035     Dwarf_Small *section_start,
1036     Dwarf_Small *section_end,
1037     Dwarf_Form_Data16  * returned_val,
1038     Dwarf_Error *error)
1039 {
1040     Dwarf_Small *data16end = 0;
1041 
1042     data16end = data + sizeof(Dwarf_Form_Data16);
1043     if (data  < section_start ||
1044         section_end < data16end) {
1045         _dwarf_error(dbg, error,DW_DLE_DATA16_OUTSIDE_SECTION);
1046         return DW_DLV_ERROR;
1047     }
1048     memcpy(returned_val, data, sizeof(Dwarf_Form_Data16));
1049     return DW_DLV_OK;
1050 
1051 }
1052 
1053 int
dwarf_formdata16(Dwarf_Attribute attr,Dwarf_Form_Data16 * returned_val,Dwarf_Error * error)1054 dwarf_formdata16(Dwarf_Attribute attr,
1055     Dwarf_Form_Data16  * returned_val,
1056     Dwarf_Error*     error)
1057 {
1058     Dwarf_Half attrform = 0;
1059     Dwarf_CU_Context cu_context = 0;
1060     Dwarf_Debug dbg = 0;
1061     int res  = 0;
1062     Dwarf_Small *section_end = 0;
1063     Dwarf_Unsigned section_length = 0;
1064     Dwarf_Small *section_start = 0;
1065 
1066     if (attr == NULL) {
1067         _dwarf_error(NULL, error, DW_DLE_ATTR_NULL);
1068         return DW_DLV_ERROR;
1069     }
1070     if (returned_val == NULL) {
1071         _dwarf_error(NULL, error, DW_DLE_ATTR_NULL);
1072         return DW_DLV_ERROR;
1073     }
1074     attrform = attr->ar_attribute_form;
1075     if (attrform != DW_FORM_data16) {
1076         generate_form_error(dbg,error,attrform,
1077             DW_DLE_ATTR_FORM_BAD,
1078             "DW_DLE_ATTR_FORM_BAD",
1079             "dwarf_formdata16");
1080         return DW_DLV_ERROR;
1081     }
1082     res  = get_attr_dbg(&dbg,&cu_context,attr,error);
1083     if (res != DW_DLV_OK) {
1084         return res;
1085     }
1086     section_start = _dwarf_calculate_info_section_start_ptr(
1087         cu_context,&section_length);
1088     section_end = section_start + section_length;
1089 
1090     res = _dwarf_extract_data16(dbg, attr->ar_debug_ptr,
1091         section_start, section_end,
1092         returned_val,  error);
1093     return res;
1094 }
1095 
1096 /*  The *addrx are DWARF5 standard.
1097     The GNU form was non-standard gcc DWARF4 */
1098 Dwarf_Bool
dwarf_addr_form_is_indexed(int form)1099 dwarf_addr_form_is_indexed(int form)
1100 {
1101     if (form == DW_FORM_addrx ||
1102         form == DW_FORM_addrx1 ||
1103         form == DW_FORM_addrx2 ||
1104         form == DW_FORM_addrx3 ||
1105         form == DW_FORM_addrx4 ||
1106         form == DW_FORM_GNU_addr_index) {
1107         return TRUE;
1108     }
1109     return FALSE;
1110 }
1111 
1112 int
dwarf_formaddr(Dwarf_Attribute attr,Dwarf_Addr * return_addr,Dwarf_Error * error)1113 dwarf_formaddr(Dwarf_Attribute attr,
1114     Dwarf_Addr * return_addr, Dwarf_Error * error)
1115 {
1116     Dwarf_Debug dbg = 0;
1117     Dwarf_Addr ret_addr = 0;
1118     Dwarf_CU_Context cu_context = 0;
1119     Dwarf_Half attrform = 0;
1120 
1121     int res  = get_attr_dbg(&dbg,&cu_context,attr,error);
1122     if (res != DW_DLV_OK) {
1123         return res;
1124     }
1125     attrform = attr->ar_attribute_form;
1126     if (dwarf_addr_form_is_indexed(attrform)) {
1127         res = _dwarf_look_in_local_and_tied(
1128             attrform,
1129             cu_context,
1130             attr->ar_debug_ptr,
1131             return_addr,
1132             error);
1133         return res;
1134     }
1135     if (attrform == DW_FORM_addr
1136         /*  || attrform == DW_FORM_ref_addr Allowance of
1137             DW_FORM_ref_addr was a mistake. The value returned in that
1138             case is NOT an address it is a global debug_info offset (ie,
1139             not CU-relative offset within the CU in debug_info). The
1140             DWARF2 document refers to it as an address (misleadingly) in
1141             sec 6.5.4 where it describes the reference form. It is
1142             address-sized so that the linker can easily update it, but
1143             it is a reference inside the debug_info section. No longer
1144             allowed. */
1145         ) {
1146         Dwarf_Small *section_end =
1147             _dwarf_calculate_info_section_end_ptr(cu_context);
1148 
1149         READ_UNALIGNED_CK(dbg, ret_addr, Dwarf_Addr,
1150             attr->ar_debug_ptr,
1151             cu_context->cc_address_size,
1152             error,section_end);
1153         *return_addr = ret_addr;
1154         return (DW_DLV_OK);
1155     }
1156     generate_form_error(dbg,error,attrform,
1157         DW_DLE_ATTR_FORM_BAD,
1158         "DW_DLE_ATTR_FORM_BAD",
1159         "dwarf_formaddr");
1160     return DW_DLV_ERROR;
1161 }
1162 
1163 
1164 int
dwarf_formflag(Dwarf_Attribute attr,Dwarf_Bool * ret_bool,Dwarf_Error * error)1165 dwarf_formflag(Dwarf_Attribute attr,
1166     Dwarf_Bool * ret_bool, Dwarf_Error * error)
1167 {
1168     Dwarf_CU_Context cu_context = 0;
1169     Dwarf_Debug dbg = 0;
1170 
1171     if (attr == NULL) {
1172         _dwarf_error(NULL, error, DW_DLE_ATTR_NULL);
1173         return (DW_DLV_ERROR);
1174     }
1175 
1176     cu_context = attr->ar_cu_context;
1177     if (cu_context == NULL) {
1178         _dwarf_error(NULL, error, DW_DLE_ATTR_NO_CU_CONTEXT);
1179         return (DW_DLV_ERROR);
1180     }
1181     dbg = cu_context->cc_dbg;
1182 
1183     if (dbg == NULL) {
1184         _dwarf_error(NULL, error, DW_DLE_ATTR_DBG_NULL);
1185         return (DW_DLV_ERROR);
1186     }
1187     if (attr->ar_attribute_form == DW_FORM_flag_present) {
1188         /*  Implicit means we don't read any data at all. Just
1189             the existence of the Form does it. DWARF4. */
1190         *ret_bool = 1;
1191         return (DW_DLV_OK);
1192     }
1193 
1194     if (attr->ar_attribute_form == DW_FORM_flag) {
1195         *ret_bool = *(Dwarf_Small *)(attr->ar_debug_ptr);
1196         return (DW_DLV_OK);
1197     }
1198     generate_form_error(dbg,error,attr->ar_attribute_form,
1199         DW_DLE_ATTR_FORM_BAD,
1200         "DW_DLE_ATTR_FORM_BAD",
1201         "dwarf_formflat");
1202     return (DW_DLV_ERROR);
1203 }
1204 
1205 Dwarf_Bool
_dwarf_allow_formudata(unsigned form)1206 _dwarf_allow_formudata(unsigned form)
1207 {
1208     switch(form) {
1209     case DW_FORM_data1:
1210     case DW_FORM_data2:
1211     case DW_FORM_data4:
1212     case DW_FORM_data8:
1213     case DW_FORM_udata:
1214     case DW_FORM_loclistx:
1215     case DW_FORM_rnglistx:
1216     return TRUE;
1217     }
1218     return FALSE;
1219 }
1220 /*  If the form is DW_FORM_constx and the .debug_addr section
1221     is missing, this returns DW_DLV_ERROR and the error number
1222     in the Dwarf_Error is  DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION.
1223     When that arises, a consumer should call
1224     dwarf_get_debug_addr_index() and use that on the appropriate
1225     .debug_addr section in the executable or another object. */
1226 
1227 int
_dwarf_formudata_internal(Dwarf_Debug dbg,unsigned form,Dwarf_Byte_Ptr data,Dwarf_Byte_Ptr section_end,Dwarf_Unsigned * return_uval,Dwarf_Unsigned * bytes_read,Dwarf_Error * error)1228 _dwarf_formudata_internal(Dwarf_Debug dbg,
1229     unsigned form,
1230     Dwarf_Byte_Ptr data,
1231     Dwarf_Byte_Ptr section_end,
1232     Dwarf_Unsigned *return_uval,
1233     Dwarf_Unsigned *bytes_read,
1234     Dwarf_Error *error)
1235 {
1236     Dwarf_Unsigned ret_value = 0;
1237 
1238     switch (form) {
1239     case DW_FORM_data1:
1240         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1241             data, sizeof(Dwarf_Small),
1242             error,section_end);
1243         *return_uval = ret_value;
1244         *bytes_read = 1;
1245         return DW_DLV_OK;
1246 
1247     /*  READ_UNALIGNED does the right thing as it reads
1248         the right number bits and generates host order.
1249         So we can just assign to *return_uval. */
1250     case DW_FORM_data2:{
1251         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1252             data, DWARF_HALF_SIZE,
1253             error,section_end);
1254         *return_uval = ret_value;
1255         *bytes_read = 2;
1256         return DW_DLV_OK;
1257         }
1258 
1259     case DW_FORM_data4:{
1260         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1261             data,
1262             DWARF_32BIT_SIZE,
1263             error,section_end);
1264         *return_uval = ret_value;
1265         *bytes_read = DWARF_32BIT_SIZE;;
1266         return DW_DLV_OK;
1267         }
1268 
1269     case DW_FORM_data8:{
1270         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
1271             data,
1272             DWARF_64BIT_SIZE,
1273             error,section_end);
1274         *return_uval = ret_value;
1275         *bytes_read = DWARF_64BIT_SIZE;
1276         return DW_DLV_OK;
1277         }
1278         break;
1279     /* real udata */
1280     case DW_FORM_loclistx:
1281     case DW_FORM_rnglistx:
1282     case DW_FORM_udata: {
1283         Dwarf_Unsigned leblen = 0;
1284         DECODE_LEB128_UWORD_LEN_CK(data, ret_value,leblen,
1285             dbg,error,section_end);
1286         *return_uval = ret_value;
1287         *bytes_read = leblen;
1288         return DW_DLV_OK;
1289     }
1290         /*  IRIX bug 583450. We do not allow reading
1291             sdata from a udata
1292             value. Caller can retry, calling sdata */
1293     default:
1294         break;
1295     }
1296     generate_form_error(dbg,error,form,
1297         DW_DLE_ATTR_FORM_BAD,
1298         "DW_DLE_ATTR_FORM_BAD",
1299         "formudata (internal function)");
1300     return (DW_DLV_ERROR);
1301 }
1302 
1303 int
dwarf_formudata(Dwarf_Attribute attr,Dwarf_Unsigned * return_uval,Dwarf_Error * error)1304 dwarf_formudata(Dwarf_Attribute attr,
1305     Dwarf_Unsigned * return_uval, Dwarf_Error * error)
1306 {
1307     Dwarf_Debug dbg = 0;
1308     Dwarf_CU_Context cu_context = 0;
1309     Dwarf_Byte_Ptr section_end = 0;
1310     Dwarf_Unsigned bytes_read = 0;
1311     Dwarf_Byte_Ptr data =  attr->ar_debug_ptr;
1312     unsigned form = 0;
1313 
1314     int res  = get_attr_dbg(&dbg,&cu_context,attr,error);
1315     if (res != DW_DLV_OK) {
1316         return res;
1317     }
1318     section_end =
1319         _dwarf_calculate_info_section_end_ptr(cu_context);
1320     form = attr->ar_attribute_form;
1321 
1322     res = _dwarf_formudata_internal(dbg,
1323         form, data, section_end, return_uval,
1324         &bytes_read, error);
1325     return res;
1326 }
1327 
1328 
1329 int
dwarf_formsdata(Dwarf_Attribute attr,Dwarf_Signed * return_sval,Dwarf_Error * error)1330 dwarf_formsdata(Dwarf_Attribute attr,
1331     Dwarf_Signed * return_sval, Dwarf_Error * error)
1332 {
1333     Dwarf_Signed ret_value = 0;
1334     Dwarf_Debug dbg = 0;
1335     Dwarf_CU_Context cu_context = 0;
1336     Dwarf_Byte_Ptr section_end = 0;
1337 
1338     int res  = get_attr_dbg(&dbg,&cu_context,attr,error);
1339     if (res != DW_DLV_OK) {
1340         return res;
1341     }
1342     section_end =
1343         _dwarf_calculate_info_section_end_ptr(cu_context);
1344     switch (attr->ar_attribute_form) {
1345 
1346     case DW_FORM_data1:
1347         if ( attr->ar_debug_ptr >= section_end) {
1348             _dwarf_error(dbg, error, DW_DLE_DIE_BAD);
1349             return DW_DLV_ERROR;
1350         }
1351         *return_sval = (*(Dwarf_Sbyte *) attr->ar_debug_ptr);
1352         return DW_DLV_OK;
1353 
1354     /*  READ_UNALIGNED does not sign extend.
1355         So we have to use a cast to get the
1356         value sign extended in the right way for each case. */
1357     case DW_FORM_data2:{
1358         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Signed,
1359             attr->ar_debug_ptr,
1360             DWARF_HALF_SIZE,
1361             error,section_end);
1362         *return_sval = (Dwarf_Shalf) ret_value;
1363         return DW_DLV_OK;
1364 
1365         }
1366 
1367     case DW_FORM_data4:{
1368         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Signed,
1369             attr->ar_debug_ptr,
1370             DWARF_32BIT_SIZE,
1371             error,section_end);
1372         SIGN_EXTEND(ret_value,DWARF_32BIT_SIZE);
1373         *return_sval = (Dwarf_Signed) ret_value;
1374         return DW_DLV_OK;
1375         }
1376 
1377     case DW_FORM_data8:{
1378         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Signed,
1379             attr->ar_debug_ptr,
1380             DWARF_64BIT_SIZE,
1381             error,section_end);
1382         /* No SIGN_EXTEND needed, we are filling all bytes already.*/
1383         *return_sval = (Dwarf_Signed) ret_value;
1384         return DW_DLV_OK;
1385         }
1386 
1387     /*  DW_FORM_implicit_const  is a value in the
1388         abbreviations, not in the DIEs. */
1389     case DW_FORM_implicit_const:
1390         *return_sval = attr->ar_implicit_const;
1391         return DW_DLV_OK;
1392 
1393     case DW_FORM_sdata: {
1394         Dwarf_Byte_Ptr tmp = attr->ar_debug_ptr;
1395 
1396         DECODE_LEB128_SWORD_CK(tmp, ret_value,
1397             dbg,error,section_end);
1398         *return_sval = ret_value;
1399         return DW_DLV_OK;
1400 
1401     }
1402 
1403         /* IRIX bug 583450. We do not allow reading sdata from a udata
1404             value. Caller can retry, calling udata */
1405 
1406     default:
1407         break;
1408     }
1409     generate_form_error(dbg,error,attr->ar_attribute_form,
1410         DW_DLE_ATTR_FORM_BAD,
1411         "DW_DLE_ATTR_FORM_BAD",
1412         "dwarf_formsdata");
1413     return DW_DLV_ERROR;
1414 }
1415 
1416 int
_dwarf_formblock_internal(Dwarf_Debug dbg,Dwarf_Attribute attr,Dwarf_CU_Context cu_context,Dwarf_Block * return_block,Dwarf_Error * error)1417 _dwarf_formblock_internal(Dwarf_Debug dbg,
1418     Dwarf_Attribute attr,
1419     Dwarf_CU_Context cu_context,
1420     Dwarf_Block * return_block,
1421     Dwarf_Error * error)
1422 {
1423     Dwarf_Small *section_start = 0;
1424     Dwarf_Small *section_end = 0;
1425     Dwarf_Unsigned section_length = 0;
1426     Dwarf_Unsigned length = 0;
1427     Dwarf_Small *data = 0;
1428 
1429     section_end =
1430         _dwarf_calculate_info_section_end_ptr(cu_context);
1431     section_start =
1432         _dwarf_calculate_info_section_start_ptr(cu_context,
1433         &section_length);
1434 
1435     switch (attr->ar_attribute_form) {
1436 
1437     case DW_FORM_block1:
1438         length = *(Dwarf_Small *) attr->ar_debug_ptr;
1439         data = attr->ar_debug_ptr + sizeof(Dwarf_Small);
1440         break;
1441 
1442     case DW_FORM_block2:
1443         READ_UNALIGNED_CK(dbg, length, Dwarf_Unsigned,
1444             attr->ar_debug_ptr, DWARF_HALF_SIZE,
1445             error,section_end);
1446         data = attr->ar_debug_ptr + DWARF_HALF_SIZE;
1447         break;
1448 
1449     case DW_FORM_block4:
1450         READ_UNALIGNED_CK(dbg, length, Dwarf_Unsigned,
1451             attr->ar_debug_ptr, DWARF_32BIT_SIZE,
1452             error,section_end);
1453         data = attr->ar_debug_ptr + DWARF_32BIT_SIZE;
1454         break;
1455 
1456     case DW_FORM_block: {
1457         Dwarf_Byte_Ptr tmp = attr->ar_debug_ptr;
1458         Dwarf_Unsigned leblen = 0;
1459 
1460         DECODE_LEB128_UWORD_LEN_CK(tmp, length, leblen,
1461             dbg,error,section_end);
1462         data = attr->ar_debug_ptr + leblen;
1463         break;
1464         }
1465     default:
1466         generate_form_error(dbg,error,attr->ar_attribute_form,
1467             DW_DLE_ATTR_FORM_BAD,
1468             "DW_DLE_ATTR_FORM_BAD",
1469             "dwarf_formblock");
1470         return DW_DLV_ERROR;
1471     }
1472     /*  We have the data. Check for errors. */
1473     if (length >= section_length) {
1474         /*  Sanity test looking for wraparound:
1475             when length actually added in
1476             it would not be caught.
1477             Test could be just >, but >= ok here too.*/
1478         _dwarf_error_string(dbg, error,
1479             DW_DLE_FORM_BLOCK_LENGTH_ERROR,
1480             "DW_DLE_FORM_BLOCK_LENGTH_ERROR: "
1481             "The length of the block is greater "
1482             "than the section length! Corrupt Dwarf.");
1483         return DW_DLV_ERROR;
1484     }
1485     if ((attr->ar_debug_ptr + length) > section_end) {
1486         _dwarf_error_string(dbg, error,
1487             DW_DLE_FORM_BLOCK_LENGTH_ERROR,
1488             "DW_DLE_FORM_BLOCK_LENGTH_ERROR: "
1489             "The block length means the block "
1490             "runs off the end of the section length!"
1491             " Corrupt Dwarf.");
1492         return DW_DLV_ERROR;
1493     }
1494     if (data > section_end) {
1495         _dwarf_error_string(dbg, error,
1496             DW_DLE_FORM_BLOCK_LENGTH_ERROR,
1497             "DW_DLE_FORM_BLOCK_LENGTH_ERROR: "
1498             "The block content is "
1499             "past the end of the section!"
1500             " Corrupt Dwarf.");
1501         _dwarf_error(dbg, error, DW_DLE_FORM_BLOCK_LENGTH_ERROR);
1502         return DW_DLV_ERROR;
1503     }
1504     if ((data + length) > section_end) {
1505         _dwarf_error_string(dbg, error,
1506             DW_DLE_FORM_BLOCK_LENGTH_ERROR,
1507             "DW_DLE_FORM_BLOCK_LENGTH_ERROR: "
1508             "The end of the block content is "
1509             "past the end of the section!"
1510             " Corrupt Dwarf.");
1511         return DW_DLV_ERROR;
1512     }
1513     return_block->bl_len = length;
1514     return_block->bl_data = data;
1515     /*  This struct is public so use the old name instead
1516         of what we now would call it:  bl_kind  */
1517     return_block->bl_from_loclist =  DW_LKIND_expression;
1518     return_block->bl_section_offset =  data - section_start;
1519     return DW_DLV_OK;
1520 }
1521 
1522 int
dwarf_formblock(Dwarf_Attribute attr,Dwarf_Block ** return_block,Dwarf_Error * error)1523 dwarf_formblock(Dwarf_Attribute attr,
1524     Dwarf_Block ** return_block, Dwarf_Error * error)
1525 {
1526     Dwarf_CU_Context cu_context = 0;
1527     Dwarf_Debug dbg = 0;
1528     Dwarf_Block local_block;
1529     Dwarf_Block *out_block = 0;
1530     int res = 0;
1531 
1532     memset(&local_block,0,sizeof(local_block));
1533     res  = get_attr_dbg(&dbg,&cu_context,attr,error);
1534     if (res != DW_DLV_OK) {
1535         return res;
1536     }
1537     res = _dwarf_formblock_internal(dbg,attr,
1538         cu_context, &local_block, error);
1539     if (res != DW_DLV_OK) {
1540         return res;
1541     }
1542     out_block = (Dwarf_Block *)
1543         _dwarf_get_alloc(dbg, DW_DLA_BLOCK, 1);
1544     if (out_block == NULL) {
1545         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
1546         return DW_DLV_ERROR;
1547     }
1548     *out_block = local_block;
1549     *return_block = out_block;
1550     return DW_DLV_OK;
1551 }
1552 
1553 int
_dwarf_extract_string_offset_via_str_offsets(Dwarf_Debug dbg,Dwarf_Small * data_ptr,Dwarf_Small * end_data_ptr,UNUSEDARG Dwarf_Half attrnum,Dwarf_Half attrform,Dwarf_CU_Context cu_context,Dwarf_Unsigned * str_sect_offset_out,Dwarf_Error * error)1554 _dwarf_extract_string_offset_via_str_offsets(Dwarf_Debug dbg,
1555     Dwarf_Small *data_ptr,
1556     Dwarf_Small *end_data_ptr,
1557     UNUSEDARG Dwarf_Half   attrnum,
1558     Dwarf_Half   attrform,
1559     Dwarf_CU_Context cu_context,
1560     Dwarf_Unsigned *str_sect_offset_out,
1561     Dwarf_Error *error)
1562 {
1563     Dwarf_Unsigned offset_base = 0;
1564     Dwarf_Unsigned index_to_offset_entry = 0;
1565     Dwarf_Unsigned offsetintable = 0;
1566     Dwarf_Unsigned end_offsetintable = 0;
1567     int res = 0;
1568     int idxres = 0;
1569 
1570     res = _dwarf_load_section(dbg, &dbg->de_debug_str_offsets,error);
1571     if (res != DW_DLV_OK) {
1572         return res;
1573     }
1574     idxres = dw_read_index_val_itself(dbg,
1575         attrform,data_ptr,end_data_ptr,&index_to_offset_entry,error);
1576     if ( idxres != DW_DLV_OK) {
1577         return idxres;
1578     }
1579 
1580     /*  DW_FORM_GNU_str_index has no 'base' value.
1581         DW_FORM_strx* has a base value
1582         for the offset table */
1583     if( attrform != DW_FORM_GNU_str_index) {
1584         res = _dwarf_get_string_base_attr_value(dbg,cu_context,
1585             &offset_base,error);
1586         if (res != DW_DLV_OK) {
1587             /*  DW_DLV_NO_ENTRY could be acceptable when
1588                 a producer knows that the base offset will be zero.
1589                 Hence DW_AT_str_offsets_base missing.
1590                 DWARF5 draft as of September 2015 allows the attribute
1591                 to be missing (it's up to the compilation tools to
1592                 make sure that has the correct effect).
1593             */
1594             return res;
1595         }
1596     }
1597 
1598     offsetintable = (index_to_offset_entry*cu_context->cc_length_size )
1599         + offset_base;
1600     {
1601         Dwarf_Unsigned fissoff = 0;
1602         Dwarf_Unsigned size = 0;
1603         fissoff = _dwarf_get_dwp_extra_offset(&cu_context->cc_dwp_offsets,
1604             DW_SECT_STR_OFFSETS, &size);
1605         offsetintable += fissoff;
1606     }
1607     end_offsetintable = offsetintable + cu_context->cc_length_size;
1608     /*  The offsets table is a series of offset-size entries.
1609         The == case in the test applies when we are at the last table
1610         entry, so == is not an error, hence only test >
1611     */
1612     if (end_offsetintable > dbg->de_debug_str_offsets.dss_size ) {
1613         _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_SIZE_BAD);
1614         return (DW_DLV_ERROR);
1615     }
1616 
1617     {
1618         Dwarf_Unsigned offsettostr = 0;
1619         Dwarf_Small *offsets_start = dbg->de_debug_str_offsets.dss_data;
1620         Dwarf_Small *offsets_end   = offsets_start +
1621             dbg->de_debug_str_offsets.dss_size;
1622         /* Now read the string offset from the offset table. */
1623         READ_UNALIGNED_CK(dbg,offsettostr,Dwarf_Unsigned,
1624             offsets_start+ offsetintable,
1625             cu_context->cc_length_size,error,offsets_end);
1626         *str_sect_offset_out = offsettostr;
1627     }
1628     return DW_DLV_OK;
1629 }
1630 
1631 int
_dwarf_extract_local_debug_str_string_given_offset(Dwarf_Debug dbg,unsigned attrform,Dwarf_Unsigned offset,char ** return_str,Dwarf_Error * error)1632 _dwarf_extract_local_debug_str_string_given_offset(Dwarf_Debug dbg,
1633     unsigned attrform,
1634     Dwarf_Unsigned offset,
1635     char ** return_str,
1636     Dwarf_Error * error)
1637 {
1638     if (attrform == DW_FORM_strp ||
1639         attrform == DW_FORM_line_strp ||
1640         attrform == DW_FORM_GNU_str_index ||
1641         attrform == DW_FORM_strx1 ||
1642         attrform == DW_FORM_strx2 ||
1643         attrform == DW_FORM_strx3 ||
1644         attrform == DW_FORM_strx4 ||
1645         attrform == DW_FORM_strx) {
1646         /*  The 'offset' into .debug_str or .debug_line_str is given,
1647             here we turn that into a pointer. */
1648         Dwarf_Small   *secend = 0;
1649         Dwarf_Small   *secbegin = 0;
1650         Dwarf_Small   *strbegin = 0;
1651         Dwarf_Unsigned secsize = 0;
1652         int errcode = 0;
1653         int res = 0;
1654 
1655         if(attrform == DW_FORM_line_strp) {
1656             res = _dwarf_load_section(dbg, &dbg->de_debug_line_str,error);
1657             if (res != DW_DLV_OK) {
1658                 return res;
1659             }
1660             errcode = DW_DLE_STRP_OFFSET_BAD;
1661             secsize = dbg->de_debug_line_str.dss_size;
1662             secbegin = dbg->de_debug_line_str.dss_data;
1663             strbegin= dbg->de_debug_line_str.dss_data + offset;
1664         } else {
1665             /* DW_FORM_strp  etc */
1666             res = _dwarf_load_section(dbg, &dbg->de_debug_str,error);
1667             if (res != DW_DLV_OK) {
1668                 return res;
1669             }
1670             errcode = DW_DLE_STRING_OFFSET_BAD;
1671             secsize = dbg->de_debug_str.dss_size;
1672             secbegin = dbg->de_debug_str.dss_data;
1673             strbegin= dbg->de_debug_str.dss_data + offset;
1674             secend = dbg->de_debug_str.dss_data + secsize;
1675         }
1676         if (offset >= secsize) {
1677             /*  Badly damaged DWARF here. */
1678             _dwarf_error(dbg, error, errcode);
1679             return (DW_DLV_ERROR);
1680         }
1681         res= _dwarf_check_string_valid(dbg,secbegin,strbegin, secend,
1682             errcode,error);
1683         if (res != DW_DLV_OK) {
1684             return res;
1685         }
1686 
1687         *return_str = (char *)strbegin;
1688         return DW_DLV_OK;
1689     }
1690     generate_form_error(dbg,error,attrform,
1691         DW_DLE_ATTR_FORM_BAD,
1692         "DW_DLE_ATTR_FORM_BAD",
1693         "extract debug_str string");
1694     return (DW_DLV_ERROR);
1695 }
1696 
1697 /* Contrary to pre-2005 documentation,
1698    The string pointer returned thru return_str must
1699    never have dwarf_dealloc() applied to it.
1700    Documentation fixed July 2005.
1701 */
1702 int
dwarf_formstring(Dwarf_Attribute attr,char ** return_str,Dwarf_Error * error)1703 dwarf_formstring(Dwarf_Attribute attr,
1704     char **return_str, Dwarf_Error * error)
1705 {
1706     Dwarf_CU_Context cu_context = 0;
1707     Dwarf_Debug dbg = 0;
1708     Dwarf_Unsigned offset = 0;
1709     int res = DW_DLV_ERROR;
1710     Dwarf_Small *secdataptr = 0;
1711     Dwarf_Small *secend = 0;
1712     Dwarf_Unsigned secdatalen = 0;
1713     Dwarf_Small *infoptr = attr->ar_debug_ptr;
1714     Dwarf_Small *contextend = 0;
1715 
1716     res  = get_attr_dbg(&dbg,&cu_context,attr,error);
1717     if (res != DW_DLV_OK) {
1718         return res;
1719     }
1720     if (cu_context->cc_is_info) {
1721         secdataptr = (Dwarf_Small *)dbg->de_debug_info.dss_data;
1722         secdatalen = dbg->de_debug_info.dss_size;
1723     } else {
1724         secdataptr = (Dwarf_Small *)dbg->de_debug_types.dss_data;
1725         secdatalen = dbg->de_debug_types.dss_size;
1726     }
1727     contextend = secdataptr +
1728         cu_context->cc_debug_offset +
1729         cu_context->cc_length +
1730         cu_context->cc_length_size +
1731         cu_context->cc_extension_size;
1732     secend = secdataptr + secdatalen;
1733     if (contextend < secend) {
1734         secend = contextend;
1735     }
1736     switch(attr->ar_attribute_form) {
1737     case DW_FORM_string: {
1738         Dwarf_Small *begin = attr->ar_debug_ptr;
1739 
1740         res= _dwarf_check_string_valid(dbg,secdataptr,begin, secend,
1741             DW_DLE_FORM_STRING_BAD_STRING,error);
1742         if (res != DW_DLV_OK) {
1743             return res;
1744         }
1745         *return_str = (char *) (begin);
1746         return DW_DLV_OK;
1747     }
1748     case DW_FORM_GNU_strp_alt:
1749     case DW_FORM_strp_sup:  {
1750         Dwarf_Error alterr = 0;
1751         /*  See dwarfstd.org issue 120604.1
1752             This is the offset in the .debug_str section
1753             of another object file.
1754             The 'tied' file notion should apply.
1755             It is not clear whether both a supplementary
1756             and a split object might be needed at the same time
1757             (hence two 'tied' files simultaneously). */
1758         Dwarf_Off soffset = 0;
1759 
1760         res = dwarf_global_formref(attr, &soffset,error);
1761         if (res != DW_DLV_OK) {
1762             return res;
1763         }
1764         res = _dwarf_get_string_from_tied(dbg, soffset,
1765             return_str, &alterr);
1766         if (res == DW_DLV_ERROR) {
1767             if (dwarf_errno(alterr) == DW_DLE_NO_TIED_FILE_AVAILABLE) {
1768                 dwarf_dealloc(dbg,alterr,DW_DLA_ERROR);
1769                 if( attr->ar_attribute_form == DW_FORM_GNU_strp_alt) {
1770                     *return_str =
1771                         (char *)"<DW_FORM_GNU_strp_alt-no-tied-file>";
1772                 } else {
1773                     *return_str =
1774                         (char *)"<DW_FORM_strp_sup-no-tied-file>";
1775                 }
1776                 return DW_DLV_OK;
1777             }
1778             if (error) {
1779                 *error = alterr;
1780             }
1781             return res;
1782         }
1783         if (res == DW_DLV_NO_ENTRY) {
1784             if( attr->ar_attribute_form == DW_FORM_GNU_strp_alt) {
1785                 *return_str =
1786                     (char *)"<DW_FORM_GNU_strp_alt-no-tied-file>";
1787             }else {
1788                 *return_str =
1789                     (char *)"<DW_FORM_strp_sup-no-tied-file>";
1790             }
1791         }
1792         return res;
1793     }
1794     case DW_FORM_GNU_str_index:
1795     case DW_FORM_strx:
1796     case DW_FORM_strx1:
1797     case DW_FORM_strx2:
1798     case DW_FORM_strx3:
1799     case DW_FORM_strx4: {
1800         Dwarf_Unsigned offsettostr= 0;
1801         res = _dwarf_extract_string_offset_via_str_offsets(dbg,
1802             infoptr,
1803             secend,
1804             attr->ar_attribute,
1805             attr->ar_attribute_form,
1806             cu_context,
1807             &offsettostr,
1808             error);
1809         if (res != DW_DLV_OK) {
1810             return res;
1811         }
1812         offset = offsettostr;
1813         break;
1814     }
1815     case DW_FORM_strp:
1816     case DW_FORM_line_strp:{
1817         READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
1818             infoptr,
1819             cu_context->cc_length_size,error,secend);
1820         break;
1821     }
1822     default:
1823         _dwarf_error(dbg, error, DW_DLE_STRING_FORM_IMPROPER);
1824         return DW_DLV_ERROR;
1825     }
1826     /*  Now we have offset so read the string from
1827         debug_str or debug_line_str. */
1828     res = _dwarf_extract_local_debug_str_string_given_offset(dbg,
1829         attr->ar_attribute_form,
1830         offset,
1831         return_str,
1832         error);
1833     return res;
1834 }
1835 
1836 
1837 int
_dwarf_get_string_from_tied(Dwarf_Debug dbg,Dwarf_Unsigned offset,char ** return_str,Dwarf_Error * error)1838 _dwarf_get_string_from_tied(Dwarf_Debug dbg,
1839     Dwarf_Unsigned offset,
1840     char **return_str,
1841     Dwarf_Error*error)
1842 {
1843     Dwarf_Debug tieddbg = 0;
1844     Dwarf_Small *secend = 0;
1845     Dwarf_Small *secbegin = 0;
1846     Dwarf_Small *strbegin = 0;
1847     int res = DW_DLV_ERROR;
1848     Dwarf_Error localerror = 0;
1849 
1850     /* Attach errors to dbg, not tieddbg. */
1851     tieddbg = dbg->de_tied_data.td_tied_object;
1852     if (!tieddbg) {
1853         _dwarf_error(dbg, error, DW_DLE_NO_TIED_FILE_AVAILABLE);
1854         return  DW_DLV_ERROR;
1855     }
1856     /* The 'offset' into .debug_str is set. */
1857     res = _dwarf_load_section(tieddbg, &tieddbg->de_debug_str,&localerror);
1858     if (res == DW_DLV_ERROR) {
1859         Dwarf_Unsigned lerrno = dwarf_errno(localerror);
1860         dwarf_dealloc(tieddbg,localerror,DW_DLA_ERROR);
1861         _dwarf_error(dbg,error,lerrno);
1862         return res;
1863     } else if (res == DW_DLV_NO_ENTRY) {
1864         return res;
1865     }
1866     if (offset >= tieddbg->de_debug_str.dss_size) {
1867         /*  Badly damaged DWARF here. */
1868         _dwarf_error(dbg, error,  DW_DLE_NO_TIED_STRING_AVAILABLE);
1869         return (DW_DLV_ERROR);
1870     }
1871     secbegin = tieddbg->de_debug_str.dss_data;
1872     strbegin= tieddbg->de_debug_str.dss_data + offset;
1873     secend = tieddbg->de_debug_str.dss_data +
1874         tieddbg->de_debug_str.dss_size;
1875 
1876     /*  Ensure the offset lies within the .debug_str */
1877     if (offset >= tieddbg->de_debug_str.dss_size) {
1878         _dwarf_error(dbg, error,  DW_DLE_NO_TIED_STRING_AVAILABLE);
1879         return (DW_DLV_ERROR);
1880     }
1881     res= _dwarf_check_string_valid(tieddbg,secbegin,strbegin, secend,
1882         DW_DLE_NO_TIED_STRING_AVAILABLE,
1883         &localerror);
1884     if (res == DW_DLV_ERROR) {
1885         Dwarf_Unsigned lerrno = dwarf_errno(localerror);
1886         dwarf_dealloc(tieddbg,localerror,DW_DLA_ERROR);
1887         _dwarf_error(dbg,error,lerrno);
1888         return res;
1889     } else if (res == DW_DLV_NO_ENTRY) {
1890         return res;
1891     }
1892     *return_str = (char *) (tieddbg->de_debug_str.dss_data + offset);
1893     return DW_DLV_OK;
1894 }
1895 
1896 
1897 
1898 
1899 int
dwarf_formexprloc(Dwarf_Attribute attr,Dwarf_Unsigned * return_exprlen,Dwarf_Ptr * block_ptr,Dwarf_Error * error)1900 dwarf_formexprloc(Dwarf_Attribute attr,
1901     Dwarf_Unsigned * return_exprlen,
1902     Dwarf_Ptr  * block_ptr,
1903     Dwarf_Error * error)
1904 {
1905     Dwarf_Debug dbg = 0;
1906     Dwarf_CU_Context cu_context = 0;
1907 
1908     int res  = get_attr_dbg(&dbg,&cu_context,attr,error);
1909     if (res != DW_DLV_OK) {
1910         return res;
1911     }
1912     if (dbg == NULL) {
1913         _dwarf_error(NULL, error, DW_DLE_ATTR_DBG_NULL);
1914         return (DW_DLV_ERROR);
1915     }
1916     if (attr->ar_attribute_form == DW_FORM_exprloc ) {
1917         Dwarf_Die die = 0;
1918         Dwarf_Unsigned leb_len = 0;
1919         Dwarf_Byte_Ptr section_start = 0;
1920         Dwarf_Unsigned section_len = 0;
1921         Dwarf_Byte_Ptr section_end = 0;
1922         Dwarf_Byte_Ptr info_ptr = 0;
1923         Dwarf_Unsigned exprlen = 0;
1924         Dwarf_Small * addr = attr->ar_debug_ptr;
1925 
1926         info_ptr = addr;
1927         section_start =
1928             _dwarf_calculate_info_section_start_ptr(cu_context,
1929             &section_len);
1930         section_end = section_start + section_len;
1931 
1932         DECODE_LEB128_UWORD_LEN_CK(info_ptr, exprlen, leb_len,
1933             dbg,error,section_end);
1934         if (exprlen > section_len) {
1935             /* Corrupted dwarf!  */
1936             dwarfstring m;
1937 
1938             dwarfstring_constructor(&m);
1939             dwarfstring_append_printf_u(&m,
1940                 "DW_DLE_ATTR_OUTSIDE_SECTION: "
1941                 "The expression length is %u,",exprlen);
1942             dwarfstring_append_printf_u(&m,
1943                 " but the section length is just %u. "
1944                 "Corrupt Dwarf.",section_len);
1945             _dwarf_error_string(dbg, error,
1946                 DW_DLE_ATTR_OUTSIDE_SECTION,
1947                 dwarfstring_string(&m));
1948             dwarfstring_destructor(&m);
1949             return DW_DLV_ERROR;
1950         }
1951         die = attr->ar_die;
1952         /*  Is the block entirely in the section, or is
1953             there bug somewhere?
1954             Here the final addr may be 1 past end of section. */
1955         if (_dwarf_reference_outside_section(die,
1956             (Dwarf_Small *)addr,
1957             ((Dwarf_Small *)addr)+exprlen +leb_len)) {
1958             dwarfstring m;
1959 
1960             dwarfstring_constructor(&m);
1961             dwarfstring_append_printf_u(&m,
1962                 "DW_DLE_ATTR_OUTSIDE_SECTION: "
1963                 "The expression length %u,",exprlen);
1964             dwarfstring_append_printf_u(&m,
1965                 " plus the leb value length of "
1966                 "%u ",leb_len);
1967             dwarfstring_append(&m,
1968                 " runs past the end of the section. "
1969                 "Corrupt Dwarf.");
1970             _dwarf_error_string(dbg, error,
1971                 DW_DLE_ATTR_OUTSIDE_SECTION,
1972                 dwarfstring_string(&m));
1973             dwarfstring_destructor(&m);
1974             return DW_DLV_ERROR;
1975         }
1976         *return_exprlen = exprlen;
1977         *block_ptr = addr + leb_len;
1978         return DW_DLV_OK;
1979 
1980     }
1981     {
1982         dwarfstring m;
1983         const char *name = "<name not known>";
1984         unsigned  mform = attr->ar_attribute_form;
1985 
1986         dwarfstring_constructor(&m);
1987 
1988         dwarf_get_FORM_name (mform,&name);
1989         dwarfstring_append_printf_u(&m,
1990             "DW_DLE_ATTR_EXPRLOC_FORM_BAD: "
1991             "The form is 0x%x ", mform);
1992         dwarfstring_append_printf_s(&m,
1993             "(%s) but should be DW_FORM_exprloc. "
1994             "Corrupt Dwarf.",(char *)name);
1995         _dwarf_error_string(dbg, error, DW_DLE_ATTR_EXPRLOC_FORM_BAD,
1996             dwarfstring_string(&m));
1997         dwarfstring_destructor(&m);
1998     }
1999     return DW_DLV_ERROR;
2000 }
2001