xref: /illumos-gate/usr/src/lib/libdwarf/common/dwarf_die_deliv.c (revision 4d9fdb46b215739778ebc12079842c9905586999)
1 /*
2   Copyright (C) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
3   Portions Copyright (C) 2007-2019 David Anderson. All Rights Reserved.
4   Portions Copyright 2012 SN Systems Ltd. All rights reserved.
5 
6   This program is free software; you can redistribute it
7   and/or modify it under the terms of version 2.1 of the
8   GNU Lesser General Public License as published by the Free
9   Software Foundation.
10 
11   This program is distributed in the hope that it would be
12   useful, but WITHOUT ANY WARRANTY; without even the implied
13   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14   PURPOSE.
15 
16   Further, this software is distributed without any warranty
17   that it is free of the rightful claim of any third person
18   regarding infringement or the like.  Any license provided
19   herein, whether implied or otherwise, applies only to this
20   software file.  Patent licenses, if any, provided herein
21   do not apply to combinations of this program with other
22   software, or any other product whatsoever.
23 
24   You should have received a copy of the GNU Lesser General
25   Public License along with this program; if not, write the
26   Free Software Foundation, Inc., 51 Franklin Street - Fifth
27   Floor, Boston MA 02110-1301, USA.
28 
29 */
30 
31 #include "config.h"
32 #include <stdio.h>
33 #include "dwarf_incl.h"
34 #include "dwarf_alloc.h"
35 #include "dwarf_error.h"
36 #include "dwarf_util.h"
37 #include "dwarfstring.h"
38 #include "dwarf_die_deliv.h"
39 
40 #define FALSE 0
41 #define TRUE 1
42 
43 /* These are sanity checks, not 'rules'. */
44 #define MINIMUM_ADDRESS_SIZE 2
45 #define MAXIMUM_ADDRESS_SIZE 8
46 
47 static void assign_correct_unit_type(Dwarf_CU_Context cu_context);
48 static int find_cu_die_base_fields(Dwarf_Debug dbg,
49     Dwarf_CU_Context cucon,
50     Dwarf_Die cudie,
51     Dwarf_Error*    error);
52 
53 static int _dwarf_siblingof_internal(Dwarf_Debug dbg,
54     Dwarf_Die die,
55     Dwarf_CU_Context context,
56     Dwarf_Bool is_info,
57     Dwarf_Die * caller_ret_die, Dwarf_Error * error);
58 
59 /*  see cuandunit.txt for an overview of the
60     DWARF5 split dwarf sections and values
61     and the DWARF4 GNU cc version of a draft
62     version of DWARF5 (quite different from
63     the final DWARF5).
64 */
65 
66 /*  New October 2011.  Enables client code to know if
67     it is a debug_info or debug_types context. */
68 Dwarf_Bool
dwarf_get_die_infotypes_flag(Dwarf_Die die)69 dwarf_get_die_infotypes_flag(Dwarf_Die die)
70 {
71     return die->di_is_info;
72 }
73 
74 #if 0
75 static void
76 dump_bytes(char * msg,Dwarf_Small * start, long len)
77 {
78     Dwarf_Small *end = start + len;
79     Dwarf_Small *cur = start;
80 
81     printf("%s ",msg);
82     for (; cur < end; cur++) {
83         printf("%02x ", *cur);
84     }
85     printf("\n");
86 }
87 #endif
88 
89 /*
90     For a given Dwarf_Debug dbg, this function checks
91     if a CU that includes the given offset has been read
92     or not.  If yes, it returns the Dwarf_CU_Context
93     for the CU.  Otherwise it returns NULL.  Being an
94     internal routine, it is assumed that a valid dbg
95     is passed.
96 
97     **This is a sequential search.  May be too slow.
98 
99     If debug_info and debug_abbrev not loaded, this will
100     wind up returning NULL. So no need to load before calling
101     this.
102 */
103 static Dwarf_CU_Context
_dwarf_find_CU_Context(Dwarf_Debug dbg,Dwarf_Off offset,Dwarf_Bool is_info)104 _dwarf_find_CU_Context(Dwarf_Debug dbg, Dwarf_Off offset,Dwarf_Bool is_info)
105 {
106     Dwarf_CU_Context cu_context = 0;
107     Dwarf_Debug_InfoTypes dis = is_info? &dbg->de_info_reading:
108         &dbg->de_types_reading;
109 
110     if (offset >= dis->de_last_offset)
111         return (NULL);
112 
113     if (dis->de_cu_context != NULL &&
114         dis->de_cu_context->cc_next != NULL &&
115         dis->de_cu_context->cc_next->cc_debug_offset == offset) {
116 
117         return (dis->de_cu_context->cc_next);
118     }
119 
120     if (dis->de_cu_context != NULL &&
121         dis->de_cu_context->cc_debug_offset <= offset) {
122 
123         for (cu_context = dis->de_cu_context;
124             cu_context != NULL;
125             cu_context = cu_context->cc_next) {
126 
127             if (offset >= cu_context->cc_debug_offset &&
128                 offset < cu_context->cc_debug_offset +
129                 cu_context->cc_length + cu_context->cc_length_size
130                 + cu_context->cc_extension_size) {
131 
132                 return (cu_context);
133             }
134         }
135     }
136 
137     for (cu_context = dis->de_cu_context_list;
138         cu_context != NULL;
139         cu_context = cu_context->cc_next) {
140 
141         if (offset >= cu_context->cc_debug_offset &&
142             offset < cu_context->cc_debug_offset +
143             cu_context->cc_length + cu_context->cc_length_size
144             + cu_context->cc_extension_size) {
145 
146             return (cu_context);
147         }
148     }
149 
150     return (NULL);
151 }
152 
153 int
dwarf_get_debugfission_for_die(Dwarf_Die die,struct Dwarf_Debug_Fission_Per_CU_s * fission_out,Dwarf_Error * error)154 dwarf_get_debugfission_for_die(Dwarf_Die die,
155     struct Dwarf_Debug_Fission_Per_CU_s *fission_out,
156     Dwarf_Error *error)
157 {
158     Dwarf_CU_Context context = 0;
159     Dwarf_Debug dbg = 0;
160     struct Dwarf_Debug_Fission_Per_CU_s * percu = 0;
161 
162     CHECK_DIE(die, DW_DLV_ERROR);
163     context = die->di_cu_context;
164     dbg = context->cc_dbg;
165     if (!_dwarf_file_has_debug_fission_index(dbg)) {
166         return DW_DLV_NO_ENTRY;
167     }
168 
169     /*  Logic should work for DW4 and DW5. */
170     if (context->cc_unit_type == DW_UT_type||
171         context->cc_unit_type == DW_UT_split_type ) {
172         if (!_dwarf_file_has_debug_fission_tu_index(dbg)) {
173             return DW_DLV_NO_ENTRY;
174         }
175     } else if (context->cc_unit_type == DW_UT_split_compile) {
176         if (!_dwarf_file_has_debug_fission_cu_index(dbg)) {
177             return DW_DLV_NO_ENTRY;
178         }
179     }
180     percu = &context->cc_dwp_offsets;
181     if (!percu->pcu_type) {
182         return DW_DLV_NO_ENTRY;
183     }
184     *fission_out = *percu;
185     return DW_DLV_OK;
186 }
187 
188 static Dwarf_Bool
is_unknown_UT_value(int ut)189 is_unknown_UT_value(int ut)
190 {
191     switch(ut) {
192     case DW_UT_compile:
193     case DW_UT_type:
194     case DW_UT_partial:
195         return FALSE;
196     case DW_UT_skeleton:
197     case DW_UT_split_compile:
198     case DW_UT_split_type:
199         return FALSE;
200     }
201     return TRUE;
202 }
203 
204 
205 /*  ASSERT: whichone is a DW_SECT* macro value. */
206 Dwarf_Unsigned
_dwarf_get_dwp_extra_offset(struct Dwarf_Debug_Fission_Per_CU_s * dwp,unsigned whichone,Dwarf_Unsigned * size)207 _dwarf_get_dwp_extra_offset(struct Dwarf_Debug_Fission_Per_CU_s* dwp,
208     unsigned whichone, Dwarf_Unsigned * size)
209 {
210     Dwarf_Unsigned sectoff = 0;
211     if (!dwp->pcu_type) {
212         return 0;
213     }
214     sectoff = dwp->pcu_offset[whichone];
215     *size = dwp->pcu_size[whichone];
216     return sectoff;
217 }
218 
219 
220 /*  _dwarf_get_fission_addition_die returns DW_DLV_OK etc.
221 */
222 int
_dwarf_get_fission_addition_die(Dwarf_Die die,int dw_sect_index,Dwarf_Unsigned * offset,Dwarf_Unsigned * size,Dwarf_Error * error)223 _dwarf_get_fission_addition_die(Dwarf_Die die, int dw_sect_index,
224    Dwarf_Unsigned *offset,
225    Dwarf_Unsigned *size,
226    Dwarf_Error *error)
227 {
228     /* We do not yet know the DIE hash, so we cannot use it
229         to identify the offset. */
230     Dwarf_CU_Context context = 0;
231     Dwarf_Unsigned dwpadd = 0;
232     Dwarf_Unsigned dwpsize = 0;
233 
234     CHECK_DIE(die, DW_DLV_ERROR);
235     context = die->di_cu_context;
236     dwpadd =  _dwarf_get_dwp_extra_offset(
237         &context->cc_dwp_offsets,
238         dw_sect_index,&dwpsize);
239     *offset = dwpadd;
240     *size = dwpsize;
241     return DW_DLV_OK;
242 }
243 
244 /*  Not sure if this is the only way to be sure early on in
245     reading a compile unit.  */
246 static int
section_name_ends_with_dwo(const char * name)247 section_name_ends_with_dwo(const char *name)
248 {
249     int lenstr = 0;
250     int dotpos = 0;
251     if (!name) {
252         return FALSE;
253     }
254     lenstr = strlen(name);
255     if (lenstr < 5) {
256         return FALSE;
257     }
258     dotpos = lenstr - 4;
259     if(strcmp(name+dotpos,".dwo")) {
260         return FALSE;
261     }
262     return TRUE;
263 }
264 
265 void
_dwarf_create_address_size_dwarf_error(Dwarf_Debug dbg,Dwarf_Error * error,Dwarf_Unsigned addrsize,int errcode,const char * errname)266 _dwarf_create_address_size_dwarf_error(Dwarf_Debug dbg,
267     Dwarf_Error *error,
268     Dwarf_Unsigned addrsize,
269     int errcode,const char *errname)
270 {
271     dwarfstring m;
272     const char *bites = "bytes";
273     if (addrsize == 1) {
274         bites = "byte";
275     }
276 
277     dwarfstring_constructor(&m);
278     dwarfstring_append(&m,(char *)errname);
279     dwarfstring_append_printf_u(&m,
280         ": Address size of %u ",
281         addrsize);
282     dwarfstring_append_printf_s(&m,
283         "%s is not supported. Corrupt DWARF.",
284         (char *)bites);
285     _dwarf_error_string(dbg,error,errcode,
286         dwarfstring_string(&m));
287     dwarfstring_destructor(&m);
288 }
289 
290 /*  New January 2017 */
291 static int
_dwarf_read_cu_version_and_abbrev_offset(Dwarf_Debug dbg,Dwarf_Small * data,Dwarf_Bool is_info,UNUSEDARG unsigned group_number,unsigned offset_size,Dwarf_CU_Context cu_context,Dwarf_Small * end_data,Dwarf_Unsigned * bytes_read_out,Dwarf_Error * error)292 _dwarf_read_cu_version_and_abbrev_offset(Dwarf_Debug dbg,
293     Dwarf_Small *data,
294     Dwarf_Bool is_info,
295     UNUSEDARG unsigned group_number,
296     unsigned offset_size, /* 4 or 8 */
297     Dwarf_CU_Context cu_context,
298     /* end_data used for sanity checking */
299     Dwarf_Small *    end_data,
300     Dwarf_Unsigned * bytes_read_out,
301     Dwarf_Error *    error)
302 {
303     Dwarf_Small *  data_start = data;
304     Dwarf_Small *  dataptr = data;
305     int            unit_type = 0;
306     Dwarf_Ubyte    addrsize =  0;
307     Dwarf_Unsigned abbrev_offset = 0;
308     Dwarf_Half version = 0;
309 
310     READ_UNALIGNED_CK(dbg, version, Dwarf_Half,
311         dataptr,DWARF_HALF_SIZE,error,end_data);
312     dataptr += DWARF_HALF_SIZE;
313     if (version == DW_CU_VERSION5) {
314         Dwarf_Ubyte unit_typeb = 0;
315 
316         READ_UNALIGNED_CK(dbg, unit_typeb, Dwarf_Ubyte,
317             dataptr, sizeof(unit_typeb),error,end_data);
318         dataptr += sizeof(unit_typeb);
319 
320         unit_type = unit_typeb;
321         /* We do not need is_info flag in DWARF5 */
322         if (is_unknown_UT_value(unit_type)) {
323             /*  DWARF5 object file is corrupt. Invalid value */
324             dwarfstring m;
325             dwarfstring_constructor(&m);
326             dwarfstring_append_printf_u(&m,
327                 "DW_DLE_CU_UT_TYPE_ERROR: we do not know "
328                 " the CU header unit_type 0x%x",unit_type);
329             dwarfstring_append_printf_u(&m," (%u) so cannot"
330                 "process this compilation_unit. A valid type ",
331                 unit_type);
332             dwarfstring_append(&m,"would be DW_UT_compile"
333                 ", for example");
334             _dwarf_error_string(dbg, error,
335                 DW_DLE_CU_UT_TYPE_ERROR,
336                 dwarfstring_string(&m));
337             dwarfstring_destructor(&m);
338             return DW_DLV_ERROR;
339         }
340         READ_UNALIGNED_CK(dbg, addrsize, unsigned char,
341             dataptr, sizeof(addrsize),error,end_data);
342         dataptr += sizeof(char);
343 
344         READ_UNALIGNED_CK(dbg, abbrev_offset, Dwarf_Unsigned,
345             dataptr, offset_size,error,end_data);
346         dataptr += offset_size;
347 
348     } else if (version == DW_CU_VERSION2 ||
349         version == DW_CU_VERSION3 ||
350         version == DW_CU_VERSION4) {
351         /*  DWARF2,3,4  */
352         READ_UNALIGNED_CK(dbg, abbrev_offset, Dwarf_Unsigned,
353             dataptr, offset_size,error,end_data);
354         dataptr += offset_size;
355 
356         READ_UNALIGNED_CK(dbg, addrsize, Dwarf_Ubyte,
357             dataptr, sizeof(addrsize),error,end_data);
358         dataptr += sizeof(addrsize);
359 
360         /*  This is an initial approximation of unit_type.
361             For DW4 we will refine this after we
362             have built the CU header (by reading
363             CU_die)
364         */
365         unit_type = is_info?DW_UT_compile:DW_UT_type;
366     } else {
367         _dwarf_error(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
368         return DW_DLV_ERROR;
369     }
370     cu_context->cc_version_stamp = version;
371     cu_context->cc_unit_type = unit_type;
372     cu_context->cc_address_size = addrsize;
373     cu_context->cc_abbrev_offset = abbrev_offset;
374     if (!addrsize) {
375         _dwarf_error(dbg,error,DW_DLE_ADDRESS_SIZE_ZERO);
376         return DW_DLV_ERROR;
377     }
378     if (addrsize < MINIMUM_ADDRESS_SIZE ||
379         addrsize > MAXIMUM_ADDRESS_SIZE ) {
380         _dwarf_create_address_size_dwarf_error(dbg,error,addrsize,
381             DW_DLE_ADDRESS_SIZE_ERROR,
382             "DW_DLE_ADDRESS_SIZE_ERROR::");
383         return DW_DLV_ERROR;
384     }
385     if (addrsize  > sizeof(Dwarf_Addr)) {
386         _dwarf_create_address_size_dwarf_error(dbg,error,addrsize,
387             DW_DLE_ADDRESS_SIZE_ERROR,
388             "DW_DLE_ADDRESS_SIZE_ERROR: not representable"
389             " in Dwarf_Addr field.");
390         return DW_DLV_ERROR;
391     }
392 
393 
394 
395     /* We are ignoring this. Can get it from DWARF5. */
396     cu_context->cc_segment_selector_size = 0;
397     *bytes_read_out = (dataptr - data_start);
398     return DW_DLV_OK;
399 }
400 
401 /*  .debug_info[.dwo]   .debug_types[.dwo]
402     the latter only DWARF4. */
403 static int
read_info_area_length_and_check(Dwarf_Debug dbg,Dwarf_CU_Context cu_context,Dwarf_Unsigned offset,Dwarf_Byte_Ptr * cu_ptr_io,Dwarf_Unsigned section_size,Dwarf_Byte_Ptr section_end_ptr,Dwarf_Unsigned * max_cu_global_offset_out,Dwarf_Error * error)404 read_info_area_length_and_check(Dwarf_Debug dbg,
405     Dwarf_CU_Context cu_context,
406     Dwarf_Unsigned offset,
407     Dwarf_Byte_Ptr *cu_ptr_io,
408     Dwarf_Unsigned section_size,
409     Dwarf_Byte_Ptr section_end_ptr,
410     Dwarf_Unsigned *max_cu_global_offset_out,
411     Dwarf_Error *error)
412 {
413     Dwarf_Byte_Ptr  cu_ptr = 0;
414     int local_length_size = 0;
415     int local_extension_size = 0;
416     Dwarf_Unsigned max_cu_global_offset = 0;
417     Dwarf_Unsigned length = 0;
418 
419     cu_ptr = *cu_ptr_io;
420     /* READ_AREA_LENGTH updates cu_ptr for consumed bytes */
421     READ_AREA_LENGTH_CK(dbg, length, Dwarf_Unsigned,
422         cu_ptr, local_length_size, local_extension_size,
423         error,section_size,section_end_ptr);
424     if (!length) {
425         return DW_DLV_NO_ENTRY;
426     }
427 
428     cu_context->cc_length_size = local_length_size;
429     cu_context->cc_extension_size = local_extension_size;
430     cu_context->cc_length = length;
431 
432     /*  This is a bare minimum, not the real max offset.
433         A preliminary sanity check. */
434     max_cu_global_offset =  offset + length +
435         local_extension_size + local_length_size;
436     if(length > section_size) {
437         _dwarf_error(dbg, error, DW_DLE_CU_LENGTH_ERROR);
438         return DW_DLV_ERROR;
439     }
440     if(max_cu_global_offset > section_size) {
441         _dwarf_error(dbg, error, DW_DLE_CU_LENGTH_ERROR);
442         return DW_DLV_ERROR;
443     }
444     *cu_ptr_io = cu_ptr;
445     *max_cu_global_offset_out = max_cu_global_offset;
446     return DW_DLV_OK;
447 }
448 
449 
450 /*  In DWARF4  GNU dwp there is a problem.
451     We cannot read the CU die  and it's
452     DW_AT_GNU_dwo_id until we know the
453     section offsets from the index files.
454     Hence we do not know how to search the
455     index files by key. So search by offset.
456 
457     There is no such problem in DWARF5.
458 
459     We have not yet corrected the unit_type so, for DWARF4,
460     we check for simpler unit types.
461 */
462 
463 static int
fill_in_dwp_offsets_if_present(Dwarf_Debug dbg,Dwarf_CU_Context cu_context,Dwarf_Sig8 * signaturedata,Dwarf_Off offset,Dwarf_Error * error)464 fill_in_dwp_offsets_if_present(Dwarf_Debug dbg,
465     Dwarf_CU_Context cu_context,
466     Dwarf_Sig8 * signaturedata,
467     Dwarf_Off    offset,
468     Dwarf_Error *error)
469 {
470     Dwarf_Half unit_type = cu_context->cc_unit_type;
471     const char * typename = 0;
472     Dwarf_Half ver = cu_context->cc_version_stamp;
473 
474     if (unit_type == DW_UT_split_type ||
475         (ver == DW_CU_VERSION4 && unit_type == DW_UT_type)){
476         typename = "tu";
477         if (!_dwarf_file_has_debug_fission_tu_index(dbg) ){
478             /* nothing to do. */
479             return DW_DLV_OK;
480         }
481     } else if (unit_type == DW_UT_split_compile ||
482         (ver == DW_CU_VERSION4 &&
483         unit_type == DW_UT_compile)){
484         typename = "cu";
485         if (!_dwarf_file_has_debug_fission_cu_index(dbg) ){
486             /* nothing to do. */
487             return DW_DLV_OK;
488         }
489     } else {
490         /* nothing to do. */
491         return DW_DLV_OK;
492     }
493 
494     if (cu_context->cc_signature_present) {
495         int resdf = 0;
496 
497         resdf = dwarf_get_debugfission_for_key(dbg,
498             signaturedata,
499             typename,
500             &cu_context->cc_dwp_offsets,
501             error);
502         if (resdf == DW_DLV_ERROR) {
503             return resdf;
504         } else if (resdf == DW_DLV_NO_ENTRY) {
505             _dwarf_error_string(dbg, error,
506                 DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH,
507                 "DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH: "
508                 " dwarf_get_debugfission_for_key returned"
509                 " DW_DLV_NO_ENTRY, something is wrong");
510             return DW_DLV_ERROR;
511         }
512     } else {
513         int resdf = 0;
514 
515         resdf = _dwarf_get_debugfission_for_offset(dbg,
516             offset,
517             typename,
518             &cu_context->cc_dwp_offsets,
519             error);
520         if (resdf == DW_DLV_ERROR) {
521             return resdf;
522         } else if (resdf == DW_DLV_NO_ENTRY) {
523             _dwarf_error_string(dbg, error,
524                 DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH,
525                 "DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH: "
526                 " dwarf_get_debugfission_for_offset returned"
527                 " DW_DLV_NO_ENTRY, something is wrong");
528             return DW_DLV_ERROR;
529         }
530         cu_context->cc_signature =
531             cu_context->cc_dwp_offsets.pcu_hash;
532         cu_context->cc_signature_present = TRUE;
533     }
534     return DW_DLV_OK;
535 }
536 
537 static Dwarf_Bool
_dwarf_may_have_base_fields(Dwarf_CU_Context cu_context)538 _dwarf_may_have_base_fields(Dwarf_CU_Context cu_context)
539 {
540     if (cu_context->cc_version_stamp < DW_CU_VERSION4) {
541         return FALSE;
542     }
543     return TRUE;
544 }
545 
546 static int
finish_cu_context_via_cudie_inner(Dwarf_Debug dbg,Dwarf_CU_Context cu_context,Dwarf_Error * error)547 finish_cu_context_via_cudie_inner(
548     Dwarf_Debug dbg,
549     Dwarf_CU_Context cu_context,
550     Dwarf_Error *error)
551 {
552     if (_dwarf_may_have_base_fields(cu_context)) {
553         /*  DW4: Look for DW_AT_dwo_id and
554             if there is one pick up the hash
555             DW5: hash in skeleton CU die
556             Also pick up cc_str_offset_base and
557             any other base values. */
558 
559         Dwarf_Die cudie = 0;
560         int resdwo = 0;
561 
562         /*  Must call the internal siblingof so
563             we do not depend on the dbg...de_cu_context
564             used by and for dwarf_cu_header_* calls. */
565         resdwo = _dwarf_siblingof_internal(dbg,NULL,
566             cu_context,
567             cu_context->cc_is_info,
568             &cudie, error);
569         if (resdwo == DW_DLV_OK) {
570             Dwarf_Half cutag = 0;
571             int resdwob = 0;
572             resdwob = find_cu_die_base_fields(dbg,
573                 cu_context,
574                 cudie,
575                 error);
576             if (resdwob == DW_DLV_NO_ENTRY) {
577                 /* The CU die has no children */
578                 dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
579                 cudie = 0;
580                 cu_context->cc_cu_die_has_children = FALSE;
581                 return DW_DLV_OK;
582             } else if (resdwob == DW_DLV_ERROR) {
583                 /*  Not applicable or an error */
584                 dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
585                 cudie = 0;
586                 return resdwob;
587             }
588             resdwob = dwarf_tag(cudie,&cutag,error);
589             if (resdwob == DW_DLV_OK) {
590                 cu_context->cc_cu_die_tag = cutag;
591             }
592             dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
593             return resdwob;
594         } else  if (resdwo == DW_DLV_NO_ENTRY) {
595             /* no cudie. Empty CU. */
596             return DW_DLV_OK;
597         } else {
598             /* no cudie. Error.*/
599             return resdwo;
600         }
601     }
602     return DW_DLV_OK;
603 }
604 
605 static void
local_dealloc_cu_context(Dwarf_Debug dbg,Dwarf_CU_Context context)606 local_dealloc_cu_context(Dwarf_Debug dbg,
607     Dwarf_CU_Context context)
608 {
609     Dwarf_Hash_Table hash_table = 0;
610 
611     if (!context) {
612         return;
613     }
614     hash_table = context->cc_abbrev_hash_table;
615     if (hash_table) {
616         _dwarf_free_abbrev_hash_table_contents(dbg,hash_table);
617         hash_table->tb_entries = 0;
618         dwarf_dealloc(dbg,hash_table, DW_DLA_HASH_TABLE);
619         context->cc_abbrev_hash_table = 0;
620     }
621     dwarf_dealloc(dbg, context, DW_DLA_CU_CONTEXT);
622 }
623 
624 static void
report_local_unit_type_error(Dwarf_Debug dbg,int unit_type,const char * msg,Dwarf_Error * err)625 report_local_unit_type_error(Dwarf_Debug dbg,
626     int unit_type,
627     const char *msg,
628     Dwarf_Error *err)
629 {
630     dwarfstring m;
631 
632     dwarfstring_constructor(&m);
633     dwarfstring_append_printf_s(&m,
634         "DW_DLE_CU_UT_TYPE_VALUE: %s ",(char *)msg);
635     dwarfstring_append_printf_u(&m,
636         "the compilation unit unit_type is 0x%x,"
637         " which is unknown to libdwarf. Corrupt DWARF.",
638         unit_type);
639     _dwarf_error_string(dbg,err,DW_DLE_CU_UT_TYPE_VALUE,
640         dwarfstring_string(&m));
641     dwarfstring_destructor(&m);
642 }
643 
644 /*  This function is used to create a CU Context for
645     a compilation-unit that begins at offset in
646     .debug_info.  The CU Context is attached to the
647     list of CU Contexts for this dbg.  It is assumed
648     that the CU at offset has not been read before,
649     and so do not call this routine before making
650     sure of this with _dwarf_find_CU_Context().
651     Returns NULL on error.  As always, being an
652     internal routine, assumes a good dbg.
653 
654     The offset argument is global offset, the offset
655     in the section, irrespective of CUs.
656     The offset has the DWP Package File offset built in
657     as it comes from the actual section.
658 
659     max_cu_local_offset is a local offset in this CU.
660     So zero of this field is immediately following the length
661     field of the CU header. so max_cu_local_offset is
662     identical to the CU length field.
663     max_cu_global_offset is the offset one-past the end
664     of this entire CU.  */
665 static int
_dwarf_make_CU_Context(Dwarf_Debug dbg,Dwarf_Off offset,Dwarf_Bool is_info,Dwarf_CU_Context * context_out,Dwarf_Error * error)666 _dwarf_make_CU_Context(Dwarf_Debug dbg,
667     Dwarf_Off offset,Dwarf_Bool is_info,
668     Dwarf_CU_Context * context_out,Dwarf_Error * error)
669 {
670     Dwarf_CU_Context cu_context = 0;
671     Dwarf_Unsigned   length = 0;
672     Dwarf_Unsigned   typeoffset = 0;
673     Dwarf_Sig8       signaturedata;
674     Dwarf_Unsigned   types_extra_len = 0;
675     Dwarf_Unsigned   max_cu_local_offset =  0;
676     Dwarf_Unsigned   max_cu_global_offset =  0;
677     Dwarf_Byte_Ptr   cu_ptr = 0;
678     Dwarf_Byte_Ptr   section_end_ptr = 0;
679     int              local_length_size = 0;
680     Dwarf_Unsigned   bytes_read = 0;
681     const char *     secname = is_info?dbg->de_debug_info.dss_name:
682         dbg->de_debug_types.dss_name;
683     Dwarf_Debug_InfoTypes dis = is_info? &dbg->de_info_reading:
684         &dbg->de_types_reading;
685     Dwarf_Unsigned   section_size = is_info? dbg->de_debug_info.dss_size:
686         dbg->de_debug_types.dss_size;
687     int              unit_type = 0;
688     int              version = 0;
689     Dwarf_Small *    dataptr = 0;
690     int              res = 0;
691 
692     memset(&signaturedata,0,sizeof(signaturedata));
693     cu_context =
694         (Dwarf_CU_Context)_dwarf_get_alloc(dbg, DW_DLA_CU_CONTEXT, 1);
695     if (!cu_context) {
696         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
697         return DW_DLV_ERROR;
698     }
699     cu_context->cc_dbg = dbg;
700     cu_context->cc_is_info = is_info;
701 
702     dataptr = is_info? dbg->de_debug_info.dss_data:
703         dbg->de_debug_types.dss_data;
704     /*  Preliminary sanity checking. */
705     if (!dataptr) {
706         local_dealloc_cu_context(dbg,cu_context);
707         _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
708         return DW_DLV_ERROR;
709     }
710     if (offset >= section_size) {
711         local_dealloc_cu_context(dbg,cu_context);
712         return DW_DLV_ERROR;
713     }
714     if ((offset+4) > section_size) {
715         local_dealloc_cu_context(dbg,cu_context);
716         return DW_DLV_ERROR;
717     }
718     section_end_ptr = dataptr+section_size;
719     cu_ptr = (Dwarf_Byte_Ptr) (dataptr+offset);
720 
721     if (section_name_ends_with_dwo(secname)) {
722         cu_context->cc_is_dwo = TRUE;
723     }
724     res = read_info_area_length_and_check(dbg,
725         cu_context,
726         offset,
727         &cu_ptr,
728         section_size,
729         section_end_ptr,
730         &max_cu_global_offset,
731         error);
732     if (res != DW_DLV_OK) {
733         local_dealloc_cu_context(dbg,cu_context);
734         return res;
735     }
736     local_length_size = cu_context->cc_length_size;
737     length = cu_context->cc_length;
738     max_cu_local_offset =  length;
739     res  = _dwarf_read_cu_version_and_abbrev_offset(dbg,
740         cu_ptr,
741         is_info,
742         dbg->de_groupnumber,
743         local_length_size,
744         cu_context,
745         section_end_ptr,
746         &bytes_read,error);
747     if (res != DW_DLV_OK) {
748         local_dealloc_cu_context(dbg,cu_context);
749         return res;
750     }
751     version = cu_context->cc_version_stamp;
752     cu_ptr += bytes_read;
753     unit_type = cu_context->cc_unit_type;
754     if (cu_ptr > section_end_ptr) {
755         local_dealloc_cu_context(dbg,cu_context);
756         _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
757         return DW_DLV_ERROR;
758     }
759 
760     /*  In a dwp context, the abbrev_offset is
761         still  incomplete.
762         We need to add in the base from the .debug_cu_index
763         or .debug_tu_index . Done below */
764 
765     /*  At this point, for DW4, the unit_type is not fully
766         correct as we don't know if it is a skeleton or
767         a split_compile or split_type */
768     if (version ==  DW_CU_VERSION5 ||
769         version == DW_CU_VERSION4) {
770         /*  DW4/DW5  header fields, depending on UT type.
771             See DW5  section 7.5.1.x, DW4
772             data is a GNU extension of DW4. */
773         switch(unit_type) {
774         case DW_UT_split_type:
775         case DW_UT_type: {
776             types_extra_len = sizeof(Dwarf_Sig8) /* 8 */ +
777                 local_length_size /*type_offset size*/;
778             break;
779         }
780         case DW_UT_skeleton:
781         case DW_UT_split_compile: {
782             types_extra_len = sizeof(Dwarf_Sig8) /* 8 */;
783             break;
784         }
785         case DW_UT_compile: /*  No additional fields */
786         case DW_UT_partial: /*  No additional fields */
787             break;
788         default:
789             /*  Data corruption in libdwarf? */
790             report_local_unit_type_error(dbg, unit_type,
791                 "(DW4 or DW5)",error);
792             local_dealloc_cu_context(dbg,cu_context);
793             return DW_DLV_ERROR;
794         }
795     }
796 
797     /*  Compare the space following the length field
798         to the bytes in the CU header. */
799     if (length <
800         (CU_VERSION_STAMP_SIZE /* is 2 */ +
801         local_length_size /*for debug_abbrev offset */ +
802         CU_ADDRESS_SIZE_SIZE /* is 1 */ +
803         /* and finally size of the rest of the header: */
804         types_extra_len)) {
805 
806         local_dealloc_cu_context(dbg,cu_context);
807         _dwarf_error(dbg, error, DW_DLE_CU_LENGTH_ERROR);
808         return DW_DLV_ERROR;
809     }
810     /*  Now we can read the fields with some confidence,
811         we know the fields of the header are inside
812         the section. */
813 
814     cu_context->cc_unit_type = unit_type;
815     switch(unit_type) {
816     case DW_UT_split_type:
817     case DW_UT_type: {
818         int tres = 0;
819         /*  ASSERT: DW_CU_VERSION4 or DW_CU_VERSION5,
820             determined by logic above.
821             Now read the debug_types extra header fields of
822             the signature (8 bytes) and the typeoffset.
823             This can be in executable, ordinary object
824             (as in Type Unit),
825             there was no dwo in DWARF4
826         */
827         memcpy(&signaturedata,cu_ptr,sizeof(signaturedata));
828         cu_ptr += sizeof(signaturedata);
829 #if 0
830         READ_UNALIGNED_CK(dbg, typeoffset, Dwarf_Unsigned,
831             cu_ptr, local_length_size,error,section_end_ptr);
832 #endif
833         tres = _dwarf_read_unaligned_ck_wrapper(dbg,
834             &typeoffset,cu_ptr,local_length_size,
835             section_end_ptr,error);
836         if (tres != DW_DLV_OK ) {
837             local_dealloc_cu_context(dbg,cu_context);
838             return tres;
839         }
840         cu_context->cc_signature = signaturedata;
841         cu_context->cc_signature_present = TRUE;
842         cu_context->cc_signature_offset = typeoffset;
843         if (typeoffset >= max_cu_local_offset) {
844             local_dealloc_cu_context(dbg,cu_context);
845             _dwarf_error(dbg, error,
846                 DW_DLE_DEBUG_TYPEOFFSET_BAD);
847             return DW_DLV_ERROR;
848         }
849         }
850         break;
851     case DW_UT_skeleton:
852     case DW_UT_split_compile: {
853         /*  These unit types make a pair and
854             paired units have identical signature.*/
855         memcpy(&signaturedata,cu_ptr,sizeof(signaturedata));
856         cu_context->cc_signature = signaturedata;
857         cu_context->cc_signature_present = TRUE;
858 
859         break;
860         }
861     /* The following with no additional fields */
862     case DW_UT_compile:
863     case DW_UT_partial:
864         break;
865     default: {
866         /*  Data corruption in libdwarf? */
867         report_local_unit_type_error(dbg, unit_type,
868             "",error);
869         local_dealloc_cu_context(dbg,cu_context);
870         return DW_DLV_ERROR;
871         }
872     }
873     cu_context->cc_abbrev_hash_table =
874         (Dwarf_Hash_Table) _dwarf_get_alloc(dbg, DW_DLA_HASH_TABLE, 1);
875     if (cu_context->cc_abbrev_hash_table == NULL) {
876         local_dealloc_cu_context(dbg,cu_context);
877         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
878         return DW_DLV_ERROR;
879     }
880 
881     cu_context->cc_debug_offset = offset;
882 
883     /*  This is recording an overall section value for later
884         sanity checking. */
885     dis->de_last_offset = max_cu_global_offset;
886     *context_out  = cu_context;
887     return DW_DLV_OK;
888 }
889 
890 static int
reloc_incomplete(int res,Dwarf_Error err)891 reloc_incomplete(int res,Dwarf_Error err)
892 {
893     int e = 0;
894 
895     if (res == DW_DLV_OK) {
896         return FALSE;
897     }
898     if (res == DW_DLV_NO_ENTRY) {
899         return FALSE;
900     }
901     e = dwarf_errno(err);
902     if (e == DW_DLE_RELOC_MISMATCH_INDEX        ||
903         e == DW_DLE_RELOC_MISMATCH_RELOC_INDEX  ||
904         e == DW_DLE_RELOC_MISMATCH_STRTAB_INDEX ||
905         e == DW_DLE_RELOC_SECTION_MISMATCH      ||
906         e == DW_DLE_RELOC_SECTION_MISSING_INDEX ||
907         e == DW_DLE_RELOC_SECTION_LENGTH_ODD    ||
908         e == DW_DLE_RELOC_SECTION_PTR_NULL      ||
909         e == DW_DLE_RELOC_SECTION_MALLOC_FAIL   ||
910         e == DW_DLE_SEEK_OFF_END                ||
911         e == DW_DLE_RELOC_INVALID               ||
912         e == DW_DLE_RELOC_SECTION_SYMBOL_INDEX_BAD ) {
913         return TRUE;
914     }
915     return FALSE;
916 }
917 
918 
919 
920 /*  Returns offset of next compilation-unit thru next_cu_offset
921     pointer.
922     It sequentially moves from one
923     cu to the next.  The current cu is recorded
924     internally by libdwarf.
925 
926     The _b form is new for DWARF4 adding new returned fields.  */
927 int
dwarf_next_cu_header(Dwarf_Debug dbg,Dwarf_Unsigned * cu_header_length,Dwarf_Half * version_stamp,Dwarf_Unsigned * abbrev_offset,Dwarf_Half * address_size,Dwarf_Unsigned * next_cu_offset,Dwarf_Error * error)928 dwarf_next_cu_header(Dwarf_Debug dbg,
929     Dwarf_Unsigned * cu_header_length,
930     Dwarf_Half * version_stamp,
931     Dwarf_Unsigned * abbrev_offset,
932     Dwarf_Half * address_size,
933     Dwarf_Unsigned * next_cu_offset,
934     Dwarf_Error * error)
935 {
936     Dwarf_Bool is_info = true;
937     Dwarf_Half header_type = 0;
938     return _dwarf_next_cu_header_internal(dbg,
939         is_info,
940         cu_header_length,
941         version_stamp,
942         abbrev_offset,
943         address_size,
944         0,0,0,0,0,
945         next_cu_offset,
946         &header_type,
947         error);
948 }
949 int
dwarf_next_cu_header_b(Dwarf_Debug dbg,Dwarf_Unsigned * cu_header_length,Dwarf_Half * version_stamp,Dwarf_Unsigned * abbrev_offset,Dwarf_Half * address_size,Dwarf_Half * offset_size,Dwarf_Half * extension_size,Dwarf_Unsigned * next_cu_offset,Dwarf_Error * error)950 dwarf_next_cu_header_b(Dwarf_Debug dbg,
951     Dwarf_Unsigned * cu_header_length,
952     Dwarf_Half * version_stamp,
953     Dwarf_Unsigned * abbrev_offset,
954     Dwarf_Half * address_size,
955     Dwarf_Half * offset_size,
956     Dwarf_Half * extension_size,
957     Dwarf_Unsigned * next_cu_offset,
958     Dwarf_Error * error)
959 {
960     Dwarf_Bool is_info = true;
961     Dwarf_Half header_type = 0;
962     return _dwarf_next_cu_header_internal(dbg,
963         is_info,
964         cu_header_length,
965         version_stamp,
966         abbrev_offset,
967         address_size,
968         offset_size,extension_size,
969         0,0,0,
970         next_cu_offset,
971         &header_type,
972         error);
973 }
974 
975 int
dwarf_next_cu_header_c(Dwarf_Debug dbg,Dwarf_Bool is_info,Dwarf_Unsigned * cu_header_length,Dwarf_Half * version_stamp,Dwarf_Unsigned * abbrev_offset,Dwarf_Half * address_size,Dwarf_Half * offset_size,Dwarf_Half * extension_size,Dwarf_Sig8 * signature,Dwarf_Unsigned * typeoffset,Dwarf_Unsigned * next_cu_offset,Dwarf_Error * error)976 dwarf_next_cu_header_c(Dwarf_Debug dbg,
977     Dwarf_Bool is_info,
978     Dwarf_Unsigned * cu_header_length,
979     Dwarf_Half * version_stamp,
980     Dwarf_Unsigned * abbrev_offset,
981     Dwarf_Half * address_size,
982     Dwarf_Half * offset_size,
983     Dwarf_Half * extension_size,
984     Dwarf_Sig8 * signature,
985     Dwarf_Unsigned * typeoffset,
986     Dwarf_Unsigned * next_cu_offset,
987     Dwarf_Error * error)
988 {
989     Dwarf_Half header_type = 0;
990     int res =_dwarf_next_cu_header_internal(dbg,
991         is_info,
992         cu_header_length,
993         version_stamp,
994         abbrev_offset,
995         address_size,
996         offset_size,
997         extension_size,
998         signature,
999         0,
1000         typeoffset,
1001         next_cu_offset,
1002         &header_type,
1003         error);
1004     return res;
1005 }
1006 int
dwarf_next_cu_header_d(Dwarf_Debug dbg,Dwarf_Bool is_info,Dwarf_Unsigned * cu_header_length,Dwarf_Half * version_stamp,Dwarf_Unsigned * abbrev_offset,Dwarf_Half * address_size,Dwarf_Half * offset_size,Dwarf_Half * extension_size,Dwarf_Sig8 * signature,Dwarf_Unsigned * typeoffset,Dwarf_Unsigned * next_cu_offset,Dwarf_Half * header_cu_type,Dwarf_Error * error)1007 dwarf_next_cu_header_d(Dwarf_Debug dbg,
1008     Dwarf_Bool is_info,
1009     Dwarf_Unsigned * cu_header_length,
1010     Dwarf_Half * version_stamp,
1011     Dwarf_Unsigned * abbrev_offset,
1012     Dwarf_Half * address_size,
1013     Dwarf_Half * offset_size,
1014     Dwarf_Half * extension_size,
1015     Dwarf_Sig8 * signature,
1016     Dwarf_Unsigned * typeoffset,
1017     Dwarf_Unsigned * next_cu_offset,
1018     Dwarf_Half * header_cu_type,
1019     Dwarf_Error * error)
1020 {
1021     /* Faking has_signature to do nothing. */
1022     Dwarf_Bool* has_signature = 0;
1023     int res = 0;
1024 
1025     res = _dwarf_next_cu_header_internal(dbg,
1026         is_info,
1027         cu_header_length,
1028         version_stamp,
1029         abbrev_offset,
1030         address_size,
1031         offset_size,
1032         extension_size,
1033         signature,
1034         has_signature,
1035         typeoffset,
1036         next_cu_offset,
1037         header_cu_type,
1038         error);
1039     return res;
1040 }
1041 
1042 
1043 static void
local_attrlist_dealloc(Dwarf_Debug dbg,Dwarf_Signed atcount,Dwarf_Attribute * alist)1044 local_attrlist_dealloc(Dwarf_Debug dbg,
1045     Dwarf_Signed atcount,
1046     Dwarf_Attribute *alist)
1047 {
1048     Dwarf_Signed i = 0;
1049 
1050     for ( ; i < atcount; ++i) {
1051         dwarf_dealloc(dbg,alist[i],DW_DLA_ATTR);
1052     }
1053     dwarf_dealloc(dbg,alist,DW_DLA_LIST);
1054 }
1055 
1056 
1057 
1058 /*
1059     A DWO/DWP CU has different base fields than
1060     a normal object/executable, but this finds
1061     the base fields for both types.
1062 */
1063 static int
find_cu_die_base_fields(Dwarf_Debug dbg,Dwarf_CU_Context cucon,Dwarf_Die cudie,Dwarf_Error * error)1064 find_cu_die_base_fields(Dwarf_Debug dbg,
1065     Dwarf_CU_Context cucon,
1066     Dwarf_Die cudie,
1067     Dwarf_Error*    error)
1068 {
1069     Dwarf_CU_Context  cu_context = 0;
1070     Dwarf_Attribute * alist = 0;
1071     Dwarf_Signed      atcount = 0;
1072     unsigned          version_stamp = 2;
1073     int               alres = 0;
1074     Dwarf_Signed      i = 0;
1075     Dwarf_Signed low_pc_attrnum = -1;
1076     Dwarf_Signed at_addr_base_attrnum = -1;
1077 
1078     cu_context = cudie->di_cu_context;
1079     version_stamp = cu_context->cc_version_stamp;
1080 
1081     alres = dwarf_attrlist(cudie, &alist,
1082         &atcount,error);
1083     if(alres != DW_DLV_OK) {
1084         /* Something is badly wrong. No attrlist! */
1085         return alres;
1086     }
1087     /*  DW_AT_dwo_id and/or DW_AT_GNU_dwo_id
1088         are only found  in some
1089         experimental DWARF4.
1090         DWARF5 changed CU header contents
1091         to make this attribute unnecessary.
1092         DW_AT_GNU_odr_signature is the same format,
1093         but is in a different namespace so not
1094         appropriate here..
1095     */
1096     for(i = 0;  i < atcount; ++i) {
1097         Dwarf_Half attrnum;
1098         int ares = 0;
1099         Dwarf_Attribute attr = alist[i];
1100         ares = dwarf_whatattr(attr,&attrnum,error);
1101         if (ares == DW_DLV_OK) {
1102             switch(attrnum) {
1103             case DW_AT_dwo_id:
1104             case DW_AT_GNU_dwo_id: {
1105                 Dwarf_Sig8 signature;
1106                 /*  This is for DWARF4 with an early
1107                     non-standard version
1108                     of split dwarf. Not DWARF5. */
1109                 int sres = 0;
1110                 if (version_stamp != DW_CU_VERSION4 ) {
1111                     /* Not supposed to happen. */
1112                     local_attrlist_dealloc(dbg,atcount,alist);
1113                     _dwarf_error(dbg,error,
1114                         DW_DLE_IMPROPER_DWO_ID);
1115                     return DW_DLV_ERROR;
1116                 }
1117                 memset(&signature, 0, sizeof(signature));
1118                 sres = dwarf_formsig8_const(attr,
1119                     &signature,error);
1120                 if(sres == DW_DLV_OK) {
1121                     if (!cucon->cc_signature_present) {
1122                         cucon->cc_signature = signature;
1123                         cucon->cc_signature_present = TRUE;
1124                     } else {
1125                         /*  Something wrong. Two styles ?
1126                             Do what? verify the same sig?
1127                             FIXME */
1128                     }
1129                 } else {
1130                     /* Something is badly wrong. */
1131                     local_attrlist_dealloc(dbg,atcount,alist);
1132                     return sres;
1133                 }
1134                 break;
1135             }
1136             /*  If, in .debug_rnglists for a CU the
1137                 applicable range has no base address
1138                 this attribute provides a base address.
1139                 If this is indexed doing this now would
1140                 lead to an infinite recursion.
1141                 So wait till all the other fields seen.
1142             */
1143             case DW_AT_low_pc: {
1144                 low_pc_attrnum = i;
1145                 break;
1146             }
1147 
1148             /*  The offset is of the first offset in
1149                 .debug_str_offsets that is the string table
1150                 for this CU. */
1151             case DW_AT_str_offsets_base:{
1152                 int udres = 0;
1153                 udres = dwarf_global_formref(attr,
1154                     &cucon->cc_str_offsets_base,
1155                     error);
1156                 if(udres == DW_DLV_OK) {
1157                     cucon->cc_str_offsets_base_present = TRUE;
1158                 } else {
1159                     local_attrlist_dealloc(dbg,atcount,alist);
1160                     /* Something is badly wrong. */
1161                     return udres;
1162                 }
1163                 break;
1164             }
1165             /*  offset in .debug_loclists  of the offsets table
1166                 applicable to this CU. */
1167             case DW_AT_loclists_base: {
1168                 int udres = 0;
1169                 udres = dwarf_global_formref(attr,
1170                     &cucon->cc_loclists_base,
1171                     error);
1172                 if(udres == DW_DLV_OK) {
1173                     cucon->cc_loclists_base_present = TRUE;
1174                 } else {
1175                     local_attrlist_dealloc(dbg,atcount,alist);
1176                     /* Something is badly wrong. */
1177                     return udres;
1178                 }
1179                 break;
1180                 }
1181             /*  Base offset  in .debug_addr of the addr table
1182                 for this CU. DWARF5 (and possibly GNU DWARF4) */
1183             case DW_AT_addr_base:
1184             case DW_AT_GNU_addr_base: {
1185                 int udres = 0;
1186                 at_addr_base_attrnum = i;
1187                 udres = dwarf_global_formref(attr,
1188                     &cucon->cc_addr_base,
1189                     error);
1190                 if(udres == DW_DLV_OK) {
1191                     cucon->cc_addr_base_present = TRUE;
1192                 } else {
1193                     local_attrlist_dealloc(dbg,atcount,alist);
1194                     /* Something is badly wrong. */
1195                     return udres;
1196                 }
1197                 break;
1198             }
1199             case DW_AT_GNU_ranges_base:
1200             /*  The DW4 ranges base was never used in GNU
1201                 but did get emitted in skeletons.
1202                 http://llvm.1065342.n5.nabble.com/
1203                 DebugInfo-DW-AT-GNU-ranges-base-in-
1204                 non-fission-td64194.html
1205                 We therefore ignore it.  */
1206                 break;
1207             /*  offset in .debug_rnglists  of the offsets table
1208                 applicable to this CU. */
1209             case  DW_AT_rnglists_base: {
1210                 int udres = 0;
1211                 udres = dwarf_global_formref(attr,
1212                     &cucon->cc_rnglists_base,
1213                     error);
1214                 if(udres == DW_DLV_OK) {
1215                     cucon->cc_rnglists_base_present = TRUE;
1216                 } else {
1217                     local_attrlist_dealloc(dbg,atcount,alist);
1218                     /* Something is badly wrong. */
1219                     return udres;
1220                 }
1221                 break;
1222                 }
1223             /*  A signature, found in a DWARF5 skeleton
1224                 compilation unit. */
1225             case DW_AT_GNU_dwo_name:
1226             case DW_AT_dwo_name: {
1227                 int dnres = 0;
1228 
1229                 dnres = dwarf_formstring(attr,
1230                     &cucon->cc_dwo_name,error);
1231                 if (dnres != DW_DLV_OK) {
1232                     local_attrlist_dealloc(dbg,atcount,alist);
1233                     return dnres;
1234                 }
1235                 cucon->cc_dwo_name_present = TRUE;
1236                 break;
1237                 }
1238             default: /* do nothing, not an attribute
1239                 we need to deal with here. */
1240                 break;
1241             }
1242         }
1243     }
1244     if (low_pc_attrnum >= 0 ){
1245         int lres = 0;
1246         Dwarf_Attribute attr = alist[low_pc_attrnum];
1247         Dwarf_Half form = 0;
1248 
1249         /* If the form is indexed, we better have
1250             seen DW_AT_addr_base.! */
1251         lres = dwarf_whatform(attr,&form,error);
1252         if (lres != DW_DLV_OK) {
1253             local_attrlist_dealloc(dbg,atcount,alist);
1254             return lres;
1255         }
1256         if (dwarf_addr_form_is_indexed(form)) {
1257             if (at_addr_base_attrnum < 0) {
1258                 dwarfstring m;
1259 
1260                 dwarfstring_constructor(&m);
1261                 dwarfstring_append(&m,
1262                     "DW_DLE_ATTR_NO_CU_CONTEXT: "
1263                     "The DW_AT_low_pc  CU_DIE uses "
1264                     "an indexed attribute yet "
1265                     "DW_AT_addr_base is not in the CU DIE.");
1266                 _dwarf_error_string(dbg,error,
1267                     DW_DLE_ATTR_NO_CU_CONTEXT,
1268                     dwarfstring_string(&m));
1269                 dwarfstring_destructor(&m);
1270                 local_attrlist_dealloc(dbg,atcount,alist);
1271                 return DW_DLV_ERROR;
1272             }
1273         }
1274         lres = dwarf_formaddr(attr,
1275             &cucon->cc_low_pc,error);
1276         if(lres == DW_DLV_OK) {
1277             cucon->cc_low_pc_present = TRUE;
1278         } else {
1279             /* Something is badly wrong. */
1280             local_attrlist_dealloc(dbg,atcount,alist);
1281             return lres;
1282         }
1283     }
1284     local_attrlist_dealloc(dbg,atcount,alist);
1285     alist = 0;
1286     atcount = 0;
1287     {
1288         int chres = 0;
1289         Dwarf_Half flag = 0;
1290 
1291         /*  always winds up with cc_cu_die_has_children
1292             set intentionally...to something. */
1293         cucon->cc_cu_die_has_children = TRUE;
1294         chres = dwarf_die_abbrev_children_flag(cudie,&flag);
1295         /*  If chres is not DW_DLV_OK the assumption
1296             of children remains true. */
1297         if (chres == DW_DLV_OK) {
1298             cucon->cc_cu_die_has_children = flag;
1299         }
1300     }
1301     return DW_DLV_OK;
1302 }
1303 
1304 /*  Called only for DWARF4 */
1305 static void
assign_correct_unit_type(Dwarf_CU_Context cu_context)1306 assign_correct_unit_type(Dwarf_CU_Context cu_context)
1307 {
1308     Dwarf_Half tag = cu_context->cc_cu_die_tag;
1309     if(!cu_context->cc_cu_die_has_children) {
1310         if(cu_context->cc_signature_present) {
1311             if (tag == DW_TAG_compile_unit ||
1312                 tag == DW_TAG_type_unit ) {
1313                 cu_context->cc_unit_type = DW_UT_skeleton;
1314             }
1315         }
1316     } else {
1317         if(cu_context->cc_signature_present) {
1318             if (tag == DW_TAG_compile_unit) {
1319                 cu_context->cc_unit_type = DW_UT_split_compile;
1320             } else if (tag == DW_TAG_type_unit) {
1321                 cu_context->cc_unit_type = DW_UT_split_type;
1322             }
1323         }
1324     }
1325 }
1326 
1327 static int
finish_up_cu_context_from_cudie(Dwarf_Debug dbg,Dwarf_Unsigned offset,Dwarf_CU_Context cu_context,Dwarf_Error * error)1328 finish_up_cu_context_from_cudie(Dwarf_Debug dbg,
1329     Dwarf_Unsigned offset,
1330     Dwarf_CU_Context cu_context,
1331     Dwarf_Error *error)
1332 {
1333     int version = cu_context->cc_version_stamp;
1334     Dwarf_Sig8 signaturedata;
1335     int res = 0;
1336 
1337 
1338     memset(&signaturedata,0,sizeof(signaturedata));
1339     signaturedata = cu_context->cc_signature;
1340 
1341     /*  Loads and initializes the dwarf .debug_cu_index
1342         and .debug_tu_index split dwarf package
1343         file sections */
1344     res = fill_in_dwp_offsets_if_present(dbg,
1345         cu_context,
1346         &signaturedata,
1347         offset,
1348         error);
1349     if (res == DW_DLV_ERROR) {
1350         return res;
1351     }
1352     if (res != DW_DLV_OK) {
1353         return res;
1354     }
1355 
1356     if (cu_context->cc_dwp_offsets.pcu_type) {
1357         Dwarf_Unsigned absize = 0;
1358         Dwarf_Unsigned aboff = 0;
1359 
1360         aboff = _dwarf_get_dwp_extra_offset(
1361             &cu_context->cc_dwp_offsets,
1362             DW_SECT_ABBREV, &absize);
1363         cu_context->cc_abbrev_offset +=  aboff;
1364     }
1365 
1366     if (cu_context->cc_abbrev_offset >=
1367         dbg->de_debug_abbrev.dss_size) {
1368         _dwarf_error(dbg, error, DW_DLE_ABBREV_OFFSET_ERROR);
1369         return DW_DLV_ERROR;
1370     }
1371     /*  Now we can read the CU die and determine
1372         the correct DW_UT_ type for DWARF4 and some
1373         offset base fields for DW4-fission and DW5 */
1374     if (version == DW_CU_VERSION4 || version == DW_CU_VERSION5) {
1375         res = finish_cu_context_via_cudie_inner(dbg,
1376             cu_context,
1377             error);
1378         if(res == DW_DLV_ERROR) {
1379             return res;
1380         }
1381         if(res != DW_DLV_OK) {
1382             return res;
1383         }
1384         if (version == DW_CU_VERSION4) {
1385             assign_correct_unit_type(cu_context);
1386         }
1387     }
1388     return DW_DLV_OK;
1389 }
1390 /*
1391     CU_Contexts do not overlap.
1392     cu_context we see here is not in the list we
1393     are updating. See _dwarf_find_CU_Context()
1394 
1395     Invariant: cc_debug_offset in strictly
1396         ascending order in the list.
1397 */
1398 static void
insert_into_cu_context_list(Dwarf_Debug_InfoTypes dis,Dwarf_CU_Context icu_context)1399 insert_into_cu_context_list(Dwarf_Debug_InfoTypes dis,
1400     Dwarf_CU_Context icu_context)
1401 {
1402     Dwarf_Unsigned ioffset = icu_context->cc_debug_offset;
1403     Dwarf_Unsigned eoffset = 0;
1404     Dwarf_Unsigned hoffset = 0;
1405     Dwarf_Unsigned coffset = 0;
1406     Dwarf_CU_Context next = 0;
1407     Dwarf_CU_Context past = 0;
1408     Dwarf_CU_Context cur = 0;
1409 
1410     /*  Add the context into the section context list.
1411         This is the one and only place where it is
1412         saved for re-use and eventual dealloc. */
1413     if (!dis->de_cu_context_list) {
1414         /*  First cu encountered. */
1415         dis->de_cu_context_list = icu_context;
1416         dis->de_cu_context_list_end = icu_context;
1417         return;
1418     }
1419     eoffset = dis->de_cu_context_list_end->cc_debug_offset;
1420     if (eoffset < ioffset) {
1421         /* Normal case, add at end. */
1422         dis->de_cu_context_list_end->cc_next = icu_context;
1423         dis->de_cu_context_list_end = icu_context;
1424         return;
1425     }
1426     hoffset = dis->de_cu_context_list->cc_debug_offset;
1427     if (hoffset > ioffset) {
1428         /* insert as new head. Unusual. */
1429         next =  dis->de_cu_context_list;
1430         dis->de_cu_context_list = icu_context;
1431         dis->de_cu_context_list->cc_next = next;
1432         /*  No need to touch de_cu_context_list_end */
1433         return;
1434     }
1435     cur = dis->de_cu_context_list;
1436     past = 0;
1437     /*  Insert in middle somewhere. Neither at
1438         start nor end.
1439         ASSERT: cur non-null
1440         ASSERT: past non-null */
1441     past = cur;
1442     cur = cur->cc_next;
1443     for ( ; cur ; cur = next) {
1444         next = cur->cc_next;
1445         coffset = cur->cc_debug_offset;
1446         if (coffset  >  ioffset) {
1447             /*  Insert before cur, using past.
1448                 ASSERT: past non-null  */
1449             past->cc_next = icu_context;
1450             icu_context->cc_next = cur;
1451             return;
1452         }
1453         past = cur;
1454     }
1455     /*  Impossible, for end, coffset (ie, eoffset) > ioffset  */
1456     /* NOTREACHED */
1457     return;
1458 }
1459 
1460 int
_dwarf_next_cu_header_internal(Dwarf_Debug dbg,Dwarf_Bool is_info,Dwarf_Unsigned * cu_header_length,Dwarf_Half * version_stamp,Dwarf_Unsigned * abbrev_offset,Dwarf_Half * address_size,Dwarf_Half * offset_size,Dwarf_Half * extension_size,Dwarf_Sig8 * signature_out,Dwarf_Bool * has_signature,Dwarf_Unsigned * typeoffset,Dwarf_Unsigned * next_cu_offset,Dwarf_Half * header_type,Dwarf_Error * error)1461 _dwarf_next_cu_header_internal(Dwarf_Debug dbg,
1462     Dwarf_Bool is_info,
1463     Dwarf_Unsigned * cu_header_length,
1464     Dwarf_Half * version_stamp,
1465     Dwarf_Unsigned * abbrev_offset,
1466     Dwarf_Half * address_size,
1467     Dwarf_Half * offset_size,
1468     Dwarf_Half * extension_size,
1469     Dwarf_Sig8 * signature_out,
1470     Dwarf_Bool * has_signature,
1471     Dwarf_Unsigned *typeoffset,
1472     Dwarf_Unsigned * next_cu_offset,
1473 
1474     /*  header_type: DW_UT_compile, DW_UT_partial,
1475         DW_UT_type, returned through the pointer.
1476         A new item in DWARF5, synthesized for earlier DWARF
1477         CUs (& TUs). */
1478     Dwarf_Half * header_type,
1479     Dwarf_Error * error)
1480 {
1481     /* Offset for current and new CU. */
1482     Dwarf_Unsigned new_offset = 0;
1483 
1484     /* CU Context for current CU. */
1485     Dwarf_CU_Context cu_context = 0;
1486     Dwarf_Debug_InfoTypes dis = 0;
1487     Dwarf_Unsigned section_size =  0;
1488     int res = 0;
1489 
1490     /* ***** BEGIN CODE ***** */
1491 
1492     if (dbg == NULL) {
1493         _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
1494         return (DW_DLV_ERROR);
1495     }
1496     dis = is_info? &dbg->de_info_reading: &dbg->de_types_reading;
1497     /*  Get offset into .debug_info of next CU.
1498         If dbg has no context,
1499         this has to be the first one.  */
1500     if (!dis->de_cu_context) {
1501         Dwarf_Small *dataptr = is_info? dbg->de_debug_info.dss_data:
1502             dbg->de_debug_types.dss_data;
1503         new_offset = 0;
1504         if (!dataptr) {
1505             Dwarf_Error err2= 0;
1506             int resd = is_info?_dwarf_load_debug_info(dbg, &err2):
1507                 _dwarf_load_debug_types(dbg,&err2);
1508 
1509             if (resd != DW_DLV_OK) {
1510                 if (reloc_incomplete(resd,err2)) {
1511                     /*  We will assume all is ok, though it is not.
1512                         Relocation errors need not be fatal. */
1513                     char msg_buf[300];
1514                     char *dwerrmsg = 0;
1515                     char *msgprefix =
1516                         "Relocations did not complete successfully, "
1517                         "but we are " " ignoring error: ";
1518                     size_t totallen = 0;
1519                     size_t prefixlen = 0;
1520 
1521                     dwerrmsg = dwarf_errmsg(err2);
1522                     prefixlen = strlen(msgprefix);
1523                     totallen = prefixlen + strlen(dwerrmsg);
1524                     if( totallen >= sizeof(msg_buf)) {
1525                         /*  Impossible unless something corrupted.
1526                             Provide a shorter dwerrmsg*/
1527                         strcpy(msg_buf,"Error:corrupted dwarf message table!");
1528                     } else {
1529                         strcpy(msg_buf,msgprefix);
1530                         strcpy(msg_buf+prefixlen,dwerrmsg);
1531                     }
1532                     dwarf_insert_harmless_error(dbg,msg_buf);
1533                     /*  Fall thru to use the newly loaded section.
1534                         even though it might not be adequately
1535                         relocated. */
1536                     if (resd == DW_DLV_ERROR) {
1537                         dwarf_dealloc(dbg,err2,DW_DLA_ERROR);
1538                         err2 = 0;
1539                     }
1540                 } else {
1541                     if (error) {
1542                         *error = err2;
1543                         err2 = 0;
1544                     }
1545                     /*  There is nothing here, or
1546                         what is here is damaged. */
1547                     return resd;
1548                 }
1549 
1550             }
1551         }
1552         /*  We are leaving new_offset zero. We are at the
1553             start of a section. */
1554     } else {
1555         /* We already have is_info  cu_context. */
1556 
1557         new_offset = dis->de_cu_context->cc_debug_offset +
1558             dis->de_cu_context->cc_length +
1559             dis->de_cu_context->cc_length_size +
1560             dis->de_cu_context->cc_extension_size;
1561     }
1562 
1563     /*  Check that there is room in .debug_info beyond
1564         the new offset for at least a new cu header.
1565         If not, return -1 (DW_DLV_NO_ENTRY) to indicate end
1566         of debug_info section, and reset
1567         de_cu_debug_info_offset to
1568         enable looping back through the cu's. */
1569     section_size = is_info? dbg->de_debug_info.dss_size:
1570         dbg->de_debug_types.dss_size;
1571     if ((new_offset + _dwarf_length_of_cu_header_simple(dbg,is_info)) >=
1572         section_size) {
1573         dis->de_cu_context = NULL;
1574         return DW_DLV_NO_ENTRY;
1575     }
1576 
1577     /* Check if this CU has been read before. */
1578     cu_context = _dwarf_find_CU_Context(dbg, new_offset,is_info);
1579 
1580     /* If not, make CU Context for it. */
1581     if (!cu_context) {
1582         res = _dwarf_make_CU_Context(dbg, new_offset,is_info,
1583             &cu_context,error);
1584         if (res == DW_DLV_ERROR) {
1585             return res;
1586         }
1587         if (res == DW_DLV_NO_ENTRY) {
1588             return res;
1589         }
1590         res = finish_up_cu_context_from_cudie(dbg,new_offset,
1591             cu_context,error);
1592         if (res == DW_DLV_ERROR) {
1593             local_dealloc_cu_context(dbg,cu_context);
1594             return res;
1595         }
1596         if (res == DW_DLV_NO_ENTRY) {
1597             local_dealloc_cu_context(dbg,cu_context);
1598             return res;
1599         }
1600 
1601         dis->de_cu_context = cu_context;
1602         insert_into_cu_context_list(dis,cu_context);
1603     } else {
1604         dis->de_cu_context = cu_context;
1605     }
1606 
1607     if (cu_header_length) {
1608         *cu_header_length = cu_context->cc_length;
1609     }
1610 
1611     if (version_stamp) {
1612         *version_stamp = cu_context->cc_version_stamp;
1613     }
1614     if (abbrev_offset) {
1615         *abbrev_offset = cu_context->cc_abbrev_offset;
1616     }
1617     if (address_size) {
1618         *address_size = cu_context->cc_address_size;
1619     }
1620     if (offset_size) {
1621         *offset_size = cu_context->cc_length_size;
1622     }
1623     if (extension_size) {
1624         *extension_size = cu_context->cc_extension_size;
1625     }
1626     if (header_type) {
1627         *header_type = cu_context->cc_unit_type;
1628     }
1629     if (typeoffset) {
1630         *typeoffset = cu_context->cc_signature_offset;
1631     }
1632     if (signature_out) {
1633         *signature_out = cu_context->cc_signature;
1634     }
1635     if (has_signature) {
1636         *has_signature = cu_context->cc_signature_present;
1637     }
1638     /*  Determine the offset of the next CU. */
1639     new_offset = new_offset + cu_context->cc_length +
1640         cu_context->cc_length_size + cu_context->cc_extension_size;
1641     /*  Allowing null argument starting 22 April 2019. */
1642     if (next_cu_offset) {
1643         *next_cu_offset = new_offset;
1644     }
1645     return DW_DLV_OK;
1646 }
1647 
1648 /*  This involves data in a split dwarf or package file.
1649 
1650     Given hash signature, return the CU_die of the applicable CU.
1651     The hash is assumed to be from 'somewhere'.
1652     For DWARF 4:
1653         From a skeleton DIE DW_AT_GNU_dwo_id  ("cu" case) or
1654         From a DW_FORM_ref_sig8 ("tu" case).
1655     For DWARF5:
1656         From  dwo_id in a skeleton CU header (DW_UT_skeleton).
1657         From a DW_FORM_ref_sig8 ("tu" case).
1658 
1659 
1660     If "tu" request,  the CU_die
1661     of of the type unit.
1662     Works on either a dwp package file or a dwo object.
1663 
1664     If "cu" request,  the CU_die
1665     of the compilation unit.
1666     Works on either a dwp package file or a dwo object.
1667 
1668     If the hash passed is not present, returns DW_DLV_NO_ENTRY
1669     (but read the next two paragraphs for more detail).
1670 
1671     If a dwp package file with the hash signature
1672     is present in the applicable index but no matching
1673     compilation unit can be found, it returns DW_DLV_ERROR.
1674 
1675     If a .dwo object there is no index and we look at the
1676     compilation units (possibly all of them). If not present
1677     then we return DW_DLV_NO_ENTRY.
1678 
1679     The returned_die is a CU DIE if the sig_type is "cu".
1680     The returned_die is a type DIE if the sig_type is "tu".
1681     Perhaps both should return CU die.
1682 
1683     New 27 April, 2015
1684 */
1685 int
dwarf_die_from_hash_signature(Dwarf_Debug dbg,Dwarf_Sig8 * hash_sig,const char * sig_type,Dwarf_Die * returned_die,Dwarf_Error * error)1686 dwarf_die_from_hash_signature(Dwarf_Debug dbg,
1687     Dwarf_Sig8 *     hash_sig,
1688     const char *     sig_type  /* "tu" or "cu"*/,
1689     Dwarf_Die  *     returned_die,
1690     Dwarf_Error*     error)
1691 {
1692     Dwarf_Bool is_type_unit = FALSE;
1693     int sres = 0;
1694 
1695     sres = _dwarf_load_debug_info(dbg,error);
1696     if (sres == DW_DLV_ERROR) {
1697         return sres;
1698     }
1699     sres = _dwarf_load_debug_types(dbg,error);
1700     if (sres == DW_DLV_ERROR) {
1701         return sres;
1702     }
1703 
1704     if (!strcmp(sig_type,"tu")) {
1705         is_type_unit = TRUE;
1706     } else if (!strcmp(sig_type,"cu")) {
1707         is_type_unit = FALSE;
1708     } else {
1709         _dwarf_error(dbg,error,DW_DLE_SIG_TYPE_WRONG_STRING);
1710         return DW_DLV_ERROR;
1711     }
1712 
1713     if (_dwarf_file_has_debug_fission_index(dbg)) {
1714         /* This is a dwp package file. */
1715         int fisres = 0;
1716         Dwarf_Bool is_info2 = 0;
1717         Dwarf_Off cu_header_off = 0;
1718         Dwarf_Off cu_size = 0;
1719         Dwarf_Off cu_die_off = 0;
1720         Dwarf_Off typeoffset = 0;
1721         Dwarf_Die cudie = 0;
1722         Dwarf_Die typedie = 0;
1723         Dwarf_CU_Context context = 0;
1724         Dwarf_Debug_Fission_Per_CU fiss;
1725 
1726         memset(&fiss,0,sizeof(fiss));
1727         fisres = dwarf_get_debugfission_for_key(dbg,hash_sig,
1728             sig_type,&fiss,error);
1729         if (fisres != DW_DLV_OK) {
1730             return fisres;
1731         }
1732         /* Found it */
1733         if(is_type_unit) {
1734             /*  DW4 has debug_types, so look in .debug_types
1735                 Else look in .debug_info.  */
1736             is_info2 = dbg->de_debug_types.dss_size?FALSE:TRUE;
1737         } else {
1738             is_info2 = TRUE;
1739         }
1740 
1741         cu_header_off = _dwarf_get_dwp_extra_offset(&fiss,
1742             is_info2?DW_SECT_INFO:DW_SECT_TYPES,
1743             &cu_size);
1744 
1745         fisres = dwarf_get_cu_die_offset_given_cu_header_offset_b(
1746             dbg,cu_header_off,
1747             is_info2,
1748             &cu_die_off,error);
1749         if (fisres != DW_DLV_OK) {
1750             return fisres;
1751         }
1752         fisres = dwarf_offdie_b(dbg,cu_die_off,is_info2,
1753             &cudie,error);
1754         if (fisres != DW_DLV_OK) {
1755             return fisres;
1756         }
1757         if (!is_type_unit) {
1758             *returned_die = cudie;
1759             return DW_DLV_OK;
1760         }
1761         context = cudie->di_cu_context;
1762         typeoffset = context->cc_signature_offset;
1763         typeoffset += cu_header_off;
1764         fisres = dwarf_offdie_b(dbg,typeoffset,is_info2,
1765             &typedie,error);
1766         if (fisres != DW_DLV_OK) {
1767             dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
1768             return fisres;
1769         }
1770         *returned_die = typedie;
1771         dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
1772         return DW_DLV_OK;
1773     }
1774     /*  Look thru all the CUs, there is no DWP tu/cu index.
1775         There will be COMDAT sections for  the type TUs
1776             (DW_UT_type).
1777         A single non-comdat for the DW_UT_compile. */
1778     /*  FIXME: DW_DLE_DEBUG_FISSION_INCOMPLETE  */
1779     _dwarf_error(dbg,error,DW_DLE_DEBUG_FISSION_INCOMPLETE);
1780     return DW_DLV_ERROR;
1781 }
1782 
1783 static int
dwarf_ptr_CU_offset(Dwarf_CU_Context cu_context,Dwarf_Byte_Ptr di_ptr,Dwarf_Bool is_info,Dwarf_Off * cu_off)1784 dwarf_ptr_CU_offset(Dwarf_CU_Context cu_context,
1785     Dwarf_Byte_Ptr di_ptr,
1786     Dwarf_Bool is_info,
1787     Dwarf_Off * cu_off)
1788 {
1789     Dwarf_Debug dbg = cu_context->cc_dbg;
1790     Dwarf_Small *dataptr = is_info? dbg->de_debug_info.dss_data:
1791         dbg->de_debug_types.dss_data;
1792     *cu_off = (di_ptr - dataptr);
1793     return DW_DLV_OK;
1794 }
1795 #if 0 /* FOR DEBUGGING */
1796 /* Just for debug purposes */
1797 void print_sib_offset(Dwarf_Die sibling)
1798 {
1799     Dwarf_Off sib_off;
1800     Dwarf_Error error;
1801     dwarf_dieoffset(sibling,&sib_off,&error);
1802     fprintf(stderr," SIB OFF = 0x%" DW_PR_XZEROS DW_PR_DUx,sib_off);
1803 }
1804 void print_ptr_offset(Dwarf_CU_Context cu_context,Dwarf_Byte_Ptr di_ptr)
1805 {
1806     Dwarf_Off ptr_off;
1807     dwarf_ptr_CU_offset(cu_context,di_ptr,&ptr_off);
1808     fprintf(stderr," PTR OFF = 0x%" DW_PR_XZEROS DW_PR_DUx,ptr_off);
1809 }
1810 #endif
1811 
1812 
1813 /*  Validate the sibling DIE. This only makes sense to call
1814     if the sibling's DIEs have been travsersed and
1815     dwarf_child() called on each,
1816     so that the last DIE dwarf_child saw was the last.
1817     Essentially ensuring that (after such traversal) that we
1818     are in the same place a sibling attribute would identify.
1819     In case we return DW_DLV_ERROR, the global offset of the last
1820     DIE traversed by dwarf_child is returned through *offset
1821 
1822     It is essentially guaranteed that  dbg->de_last_die
1823     is a stale DIE pointer of a deallocated DIE when we get here.
1824     It must not be used as a DIE pointer here,
1825     just as a sort of anonymous pointer that we just check against
1826     NULL.
1827 
1828     There is a (subtle?) dependence on the fact that when we call this
1829     the last dwarf_child() call would have been for this sibling.
1830     Meaning that this works in a depth-first traversal even though there
1831     is no stack of 'de_last_die' values.
1832 
1833     The check for dbg->de_last_die just ensures sanity.
1834 
1835     If one is switching between normal debug_frame and eh_frame
1836     (traversing them in tandem, let us say) in a single
1837     Dwarf_Debug this validator makes no sense.
1838     It works if one processes a .debug_frame (entirely) and
1839     then an eh_frame (or vice versa) though.
1840     Use caution.
1841 */
1842 int
dwarf_validate_die_sibling(Dwarf_Die sibling,Dwarf_Off * offset)1843 dwarf_validate_die_sibling(Dwarf_Die sibling,Dwarf_Off *offset)
1844 {
1845     Dwarf_Debug dbg = 0;
1846     Dwarf_Error *error = 0;
1847     Dwarf_Debug_InfoTypes dis = 0;
1848     CHECK_DIE(sibling, DW_DLV_ERROR);
1849     dbg = sibling->di_cu_context->cc_dbg;
1850 
1851     dis = sibling->di_is_info? &dbg->de_info_reading: &dbg->de_types_reading;
1852 
1853     *offset = 0;
1854     if (dis->de_last_die && dis->de_last_di_ptr) {
1855         if (sibling->di_debug_ptr == dis->de_last_di_ptr) {
1856             return (DW_DLV_OK);
1857         }
1858     }
1859     /* Calculate global offset used for error reporting */
1860     dwarf_ptr_CU_offset(sibling->di_cu_context,
1861         dis->de_last_di_ptr,sibling->di_is_info,offset);
1862     return (DW_DLV_ERROR);
1863 }
1864 
1865 /*  This function does two slightly different things
1866     depending on the input flag want_AT_sibling.  If
1867     this flag is true, it checks if the input die has
1868     a DW_AT_sibling attribute.  If it does it returns
1869     a pointer to the start of the sibling die in the
1870     .debug_info section.  Otherwise it behaves the
1871     same as the want_AT_sibling false case.
1872 
1873     If the want_AT_sibling flag is false, it returns
1874     a pointer to the immediately adjacent die in the
1875     .debug_info section.
1876 
1877     Die_info_end points to the end of the .debug_info
1878     portion for the cu the die belongs to.  It is used
1879     to check that the search for the next die does not
1880     cross the end of the current cu.  Cu_info_start points
1881     to the start of the .debug_info portion for the
1882     current cu, and is used to add to the offset for
1883     DW_AT_sibling attributes.  Finally, has_die_child
1884     is a pointer to a Dwarf_Bool that is set true if
1885     the present die has children, false otherwise.
1886     However, in case want_AT_child is true and the die
1887     has a DW_AT_sibling attribute *has_die_child is set
1888     false to indicate that the children are being skipped.
1889 
1890     die_info_end  points to the last byte+1 of the cu.  */
1891 static int
_dwarf_next_die_info_ptr(Dwarf_Byte_Ptr die_info_ptr,Dwarf_CU_Context cu_context,Dwarf_Byte_Ptr die_info_end,Dwarf_Byte_Ptr cu_info_start,Dwarf_Bool want_AT_sibling,Dwarf_Bool * has_die_child,Dwarf_Byte_Ptr * next_die_ptr_out,Dwarf_Error * error)1892 _dwarf_next_die_info_ptr(Dwarf_Byte_Ptr die_info_ptr,
1893     Dwarf_CU_Context cu_context,
1894     Dwarf_Byte_Ptr die_info_end,
1895     Dwarf_Byte_Ptr cu_info_start,
1896     Dwarf_Bool want_AT_sibling,
1897     Dwarf_Bool * has_die_child,
1898     Dwarf_Byte_Ptr *next_die_ptr_out,
1899     Dwarf_Error *error)
1900 {
1901     Dwarf_Byte_Ptr info_ptr = 0;
1902     Dwarf_Byte_Ptr abbrev_ptr = 0;
1903     Dwarf_Unsigned abbrev_code = 0;
1904     Dwarf_Abbrev_List abbrev_list = 0;
1905     Dwarf_Half attr = 0;
1906     Dwarf_Half attr_form = 0;
1907     Dwarf_Unsigned offset = 0;
1908     Dwarf_Unsigned utmp = 0;
1909     Dwarf_Debug dbg = 0;
1910     Dwarf_Byte_Ptr abbrev_end = 0;
1911     int lres = 0;
1912 
1913     info_ptr = die_info_ptr;
1914     DECODE_LEB128_UWORD_CK(info_ptr, utmp,dbg,error,die_info_end);
1915     abbrev_code = (Dwarf_Unsigned) utmp;
1916     if (abbrev_code == 0) {
1917         /*  Should never happen. Tested before we got here. */
1918         _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_PTR_NULL);
1919         return DW_DLV_ERROR;
1920     }
1921 
1922 
1923     lres = _dwarf_get_abbrev_for_code(cu_context, abbrev_code,
1924         &abbrev_list,error);
1925     if (lres == DW_DLV_ERROR) {
1926         return lres;
1927     }
1928     if (lres == DW_DLV_NO_ENTRY) {
1929         _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_NO_ABBREV_LIST);
1930         return DW_DLV_ERROR;
1931     }
1932     dbg = cu_context->cc_dbg;
1933 
1934     *has_die_child = abbrev_list->abl_has_child;
1935 
1936     abbrev_ptr = abbrev_list->abl_abbrev_ptr;
1937     abbrev_end = _dwarf_calculate_abbrev_section_end_ptr(cu_context);
1938 
1939     do {
1940         Dwarf_Unsigned utmp2;
1941 
1942         DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp2,dbg,error,
1943             abbrev_end);
1944         if (utmp2 > DW_AT_hi_user) {
1945             _dwarf_error(dbg, error, DW_DLE_ATTR_CORRUPT);
1946             return DW_DLV_ERROR;
1947         }
1948         attr = (Dwarf_Half) utmp2;
1949         DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp2,dbg,error,
1950             abbrev_end);
1951         if (!_dwarf_valid_form_we_know(utmp2,attr)) {
1952             _dwarf_error(dbg, error, DW_DLE_UNKNOWN_FORM);
1953             return DW_DLV_ERROR;
1954         }
1955         attr_form = (Dwarf_Half) utmp2;
1956         if (attr_form == DW_FORM_indirect) {
1957             Dwarf_Unsigned utmp6;
1958 
1959             /* DECODE_LEB128_UWORD updates info_ptr */
1960             DECODE_LEB128_UWORD_CK(info_ptr, utmp6,dbg,error,
1961                 die_info_end);
1962             attr_form = (Dwarf_Half) utmp6;
1963         }
1964         if (attr_form == DW_FORM_implicit_const) {
1965             UNUSEDARG Dwarf_Signed cval = 0;
1966 
1967             DECODE_LEB128_SWORD_CK(abbrev_ptr, cval,dbg,error,
1968                 abbrev_end);
1969         }
1970 
1971         if (want_AT_sibling && attr == DW_AT_sibling) {
1972             switch (attr_form) {
1973             case DW_FORM_ref1:
1974                 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
1975                     info_ptr, sizeof(Dwarf_Small),
1976                     error,die_info_end);
1977                 break;
1978             case DW_FORM_ref2:
1979                 /* READ_UNALIGNED does not update info_ptr */
1980                 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
1981                     info_ptr,DWARF_HALF_SIZE,
1982                     error,die_info_end);
1983                 break;
1984             case DW_FORM_ref4:
1985                 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
1986                     info_ptr, DWARF_32BIT_SIZE,
1987                     error,die_info_end);
1988                 break;
1989             case DW_FORM_ref8:
1990                 READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
1991                     info_ptr, DWARF_64BIT_SIZE,
1992                     error,die_info_end);
1993                 break;
1994             case DW_FORM_ref_udata:
1995                 DECODE_LEB128_UWORD_CK(info_ptr, offset,
1996                     dbg,error,die_info_end);
1997                 break;
1998             case DW_FORM_ref_addr:
1999                 /*  Very unusual.  The FORM is intended to refer to
2000                     a different CU, but a different CU cannot
2001                     be a sibling, can it?
2002                     We could ignore this and treat as if no
2003                     DW_AT_sibling
2004                     present.   Or derive the offset from it and if
2005                     it is in the same CU use it directly.
2006                     The offset here is *supposed* to be a
2007                     global offset,
2008                     so adding cu_info_start is wrong  to any offset
2009                     we find here unless cu_info_start
2010                     is zero! Lets pretend there is no DW_AT_sibling
2011                     attribute.  */
2012                 goto no_sibling_attr;
2013             default:
2014                 _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_WRONG_FORM);
2015                 return DW_DLV_ERROR;
2016             }
2017 
2018             /*  Reset *has_die_child to indicate children skipped.  */
2019             *has_die_child = false;
2020 
2021             /*  A value beyond die_info_end indicates an error. Exactly
2022                 at die_info_end means 1-past-cu-end and simply means we
2023                 are at the end, do not return error. Higher level
2024                 will detect that we are at the end. */
2025             {   /*  Care required here. Offset can be garbage. */
2026                 ptrdiff_t plen = die_info_end - cu_info_start;
2027                 ptrdiff_t signdoffset = (ptrdiff_t)offset;
2028                 if (signdoffset > plen || signdoffset < 0) {
2029                     /* Error case, bad DWARF. */
2030                     _dwarf_error(dbg, error,DW_DLE_SIBLING_OFFSET_WRONG);
2031                     return DW_DLV_ERROR;
2032                 }
2033             }
2034             /* At or before end-of-cu */
2035             *next_die_ptr_out = cu_info_start + offset;
2036             return DW_DLV_OK;
2037         }
2038 
2039         no_sibling_attr:
2040         if (attr_form != 0 && attr_form != DW_FORM_implicit_const) {
2041             int res = 0;
2042             Dwarf_Unsigned sizeofval = 0;
2043             ptrdiff_t  sizeb = 0;
2044 
2045             res = _dwarf_get_size_of_val(cu_context->cc_dbg,
2046                 attr_form,
2047                 cu_context->cc_version_stamp,
2048                 cu_context->cc_address_size,
2049                 info_ptr,
2050                 cu_context->cc_length_size,
2051                 &sizeofval,
2052                 die_info_end,
2053                 error);
2054             if(res != DW_DLV_OK) {
2055                 return res;
2056             }
2057             /*  It is ok for info_ptr == die_info_end, as we will test
2058                 later before using a too-large info_ptr */
2059             sizeb = (ptrdiff_t)sizeofval;
2060             if (sizeb > (die_info_end - info_ptr) ||
2061                 sizeb < 0) {
2062                 _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_PAST_END);
2063                 return DW_DLV_ERROR;
2064             }
2065             info_ptr += sizeofval;
2066             if (info_ptr > die_info_end) {
2067                 /*  More than one-past-end indicates a bug somewhere,
2068                     likely bad dwarf generation. */
2069                 _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_PAST_END);
2070                 return DW_DLV_ERROR;
2071             }
2072         }
2073     } while (attr != 0 || attr_form != 0);
2074     *next_die_ptr_out = info_ptr;
2075     return DW_DLV_OK;
2076 }
2077 
2078 /*  Multiple TAGs are in fact compile units.
2079     Allow them all.
2080     Return non-zero if a CU tag.
2081     Else return 0.
2082 */
2083 static int
is_cu_tag(int t)2084 is_cu_tag(int t)
2085 {
2086     if (t == DW_TAG_compile_unit  ||
2087         t == DW_TAG_partial_unit  ||
2088         t == DW_TAG_skeleton_unit ||
2089         t == DW_TAG_type_unit) {
2090         return 1;
2091     }
2092     return 0;
2093 }
2094 
2095 /*  Given a Dwarf_Debug dbg, and a Dwarf_Die die, it returns
2096     a Dwarf_Die for the sibling of die.  In case die is NULL,
2097     it returns (thru ptr) a Dwarf_Die for the first die in the current
2098     cu in dbg.  Returns DW_DLV_ERROR on error.
2099 
2100     It is assumed that every sibling chain including those with
2101     only one element is terminated with a NULL die, except a
2102     chain with only a NULL die.
2103 
2104     The algorithm moves from one die to the adjacent one.  It
2105     returns when the depth of children it sees equals the number
2106     of sibling chain terminations.  A single count, child_depth
2107     is used to track the depth of children and sibling terminations
2108     encountered.  Child_depth is incremented when a die has the
2109     Has-Child flag set unless the child happens to be a NULL die.
2110     Child_depth is decremented when a die has Has-Child false,
2111     and the adjacent die is NULL.  Algorithm returns when
2112     child_depth is 0.
2113 
2114     **NOTE: Do not modify input die, since it is used at the end.  */
2115 int
dwarf_siblingof(Dwarf_Debug dbg,Dwarf_Die die,Dwarf_Die * caller_ret_die,Dwarf_Error * error)2116 dwarf_siblingof(Dwarf_Debug dbg,
2117     Dwarf_Die die,
2118     Dwarf_Die * caller_ret_die, Dwarf_Error * error)
2119 {
2120     int res = 0;
2121     Dwarf_Bool is_info = TRUE;
2122     Dwarf_Debug_InfoTypes dis = 0;
2123 
2124     dis = &dbg->de_info_reading;
2125     res = _dwarf_siblingof_internal(dbg,die,
2126         die?die->di_cu_context:dis->de_cu_context,
2127         is_info,caller_ret_die,error);
2128     return res;
2129 }
2130 /*  This is the new form, October 2011.  On calling with 'die' NULL,
2131     we cannot tell if this is debug_info or debug_types, so
2132     we must be informed!. */
2133 int
dwarf_siblingof_b(Dwarf_Debug dbg,Dwarf_Die die,Dwarf_Bool is_info,Dwarf_Die * caller_ret_die,Dwarf_Error * error)2134 dwarf_siblingof_b(Dwarf_Debug dbg,
2135     Dwarf_Die die,
2136     Dwarf_Bool is_info,
2137     Dwarf_Die * caller_ret_die, Dwarf_Error * error)
2138 {
2139     int res;
2140     Dwarf_Debug_InfoTypes dis = 0;
2141 
2142     dis = is_info? &dbg->de_info_reading:
2143         &dbg->de_types_reading;
2144 
2145     res = _dwarf_siblingof_internal(dbg,die,
2146         die?die->di_cu_context:dis->de_cu_context,
2147         is_info,caller_ret_die,error);
2148     return res;
2149 }
2150 
2151 static int
_dwarf_siblingof_internal(Dwarf_Debug dbg,Dwarf_Die die,Dwarf_CU_Context context,Dwarf_Bool is_info,Dwarf_Die * caller_ret_die,Dwarf_Error * error)2152 _dwarf_siblingof_internal(Dwarf_Debug dbg,
2153     Dwarf_Die die,
2154     Dwarf_CU_Context context,
2155     Dwarf_Bool is_info,
2156     Dwarf_Die * caller_ret_die, Dwarf_Error * error)
2157 {
2158     Dwarf_Die ret_die = 0;
2159     Dwarf_Byte_Ptr die_info_ptr = 0;
2160     Dwarf_Byte_Ptr cu_info_start = 0;
2161 
2162     /* die_info_end points 1-past end of die (once set) */
2163     Dwarf_Byte_Ptr die_info_end = 0;
2164     Dwarf_Unsigned abbrev_code = 0;
2165     Dwarf_Unsigned utmp = 0;
2166     int lres = 0;
2167     int dieres = 0;
2168     /* Since die may be NULL, we rely on the input argument. */
2169     Dwarf_Small *dataptr =  0;
2170 
2171     if (dbg == NULL) {
2172         _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
2173         return (DW_DLV_ERROR);
2174     }
2175     dataptr = is_info? dbg->de_debug_info.dss_data:
2176         dbg->de_debug_types.dss_data;
2177     if (die == NULL) {
2178         /*  Find root die of cu */
2179         /*  die_info_end is untouched here, need not be set in this
2180             branch. */
2181         Dwarf_Off off2 = 0;
2182         Dwarf_Unsigned headerlen = 0;
2183         int cres = 0;
2184 
2185         /*  If we've not loaded debug_info
2186             context will be NULL. */
2187         if (!context) {
2188             local_dealloc_cu_context(dbg,context);
2189             return (DW_DLV_ERROR);
2190         }
2191         off2 = context->cc_debug_offset;
2192         cu_info_start = dataptr + off2;
2193         cres = _dwarf_length_of_cu_header(dbg, off2,is_info,
2194             &headerlen,error);
2195         if (cres != DW_DLV_OK) {
2196             return cres;
2197         }
2198         die_info_ptr = cu_info_start + headerlen;
2199         die_info_end = _dwarf_calculate_info_section_end_ptr(context);
2200 
2201         /*  Recording the CU die pointer so we can later access
2202             for special FORMs relating to .debug_str_offsets
2203             and .debug_addr  */
2204         context->cc_cu_die_offset_present = TRUE;
2205         context->cc_cu_die_global_sec_offset = off2 + headerlen;
2206     } else {
2207         /* Find sibling die. */
2208         Dwarf_Bool has_child = false;
2209         Dwarf_Signed child_depth = 0;
2210 
2211         /*  We cannot have a legal die unless debug_info
2212             was loaded, so
2213             no need to load debug_info here. */
2214         CHECK_DIE(die, DW_DLV_ERROR);
2215 
2216         die_info_ptr = die->di_debug_ptr;
2217         if (*die_info_ptr == 0) {
2218             return (DW_DLV_NO_ENTRY);
2219         }
2220         context = die->di_cu_context;
2221         cu_info_start = dataptr+ context->cc_debug_offset;
2222         die_info_end = _dwarf_calculate_info_section_end_ptr(context);
2223 
2224         if ((*die_info_ptr) == 0) {
2225             return (DW_DLV_NO_ENTRY);
2226         }
2227         child_depth = 0;
2228         do {
2229             int res2 = 0;
2230             Dwarf_Byte_Ptr die_info_ptr2 = 0;
2231 
2232             res2 = _dwarf_next_die_info_ptr(die_info_ptr,
2233                 context, die_info_end,
2234                 cu_info_start, true, &has_child,
2235                 &die_info_ptr2,
2236                 error);
2237             if(res2 != DW_DLV_OK) {
2238                 return res2;
2239             }
2240             if (die_info_ptr2 < die_info_ptr) {
2241                 /*  There is something very wrong, our die value
2242                     decreased.  Bad DWARF. */
2243                 _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_LOW_ERROR);
2244                 return (DW_DLV_ERROR);
2245             }
2246             if (die_info_ptr2 > die_info_end) {
2247                 _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_PAST_END);
2248                 return (DW_DLV_ERROR);
2249             }
2250             die_info_ptr = die_info_ptr2;
2251 
2252             /*  die_info_end is one past end. Do not read it!
2253                 A test for '!= die_info_end'  would work as well,
2254                 but perhaps < reads more like the meaning. */
2255             if (die_info_ptr < die_info_end) {
2256                 if ((*die_info_ptr) == 0 && has_child) {
2257                     die_info_ptr++;
2258                     has_child = false;
2259                 }
2260             }
2261 
2262             /*  die_info_ptr can be one-past-end.  */
2263             if ((die_info_ptr == die_info_end) ||
2264                 ((*die_info_ptr) == 0)) {
2265                 /* We are at the end of a sibling list.
2266                     get back to the next containing
2267                     sibling list (looking for a libling
2268                     list with more on it).
2269                     */
2270                 for (;;) {
2271                     if (child_depth == 0) {
2272                         /*  Meaning there is no outer list,
2273                             so stop. */
2274                         break;
2275                     }
2276                     if (die_info_ptr == die_info_end) {
2277                         /*  September 2016: do not deref
2278                             if we are past end.
2279                             If we are at end at this point
2280                             it means the sibling list
2281                             inside this CU is not properly
2282                             terminated.
2283                             August 2019:
2284                             We used to declare an error,
2285                             DW_DLE_SIBLING_LIST_IMPROPER but
2286                             now we just silently
2287                             declare this is the end of the list.
2288                             Each level of a sibling nest should
2289                             have a single NUL byte, but here
2290                             things are wrong, the DWARF
2291                             is corrupt.  */
2292                         return DW_DLV_NO_ENTRY;
2293                     }
2294                     if (*die_info_ptr) {
2295                         /* We have a real sibling. */
2296                         break;
2297                     }
2298                     /*  Move out one DIE level.
2299                         Move past NUL byte marking end of
2300                         this sibling list. */
2301                     child_depth--;
2302                     die_info_ptr++;
2303                 }
2304             } else {
2305                 child_depth = has_child ? child_depth + 1 : child_depth;
2306             }
2307         } while (child_depth != 0);
2308     }
2309 
2310     /*  die_info_ptr > die_info_end is really a bug (possibly in dwarf
2311         generation)(but we are past end, no more DIEs here), whereas
2312         die_info_ptr == die_info_end means 'one past end, no more DIEs
2313         here'. */
2314     if (die_info_ptr >= die_info_end) {
2315         return (DW_DLV_NO_ENTRY);
2316     }
2317     if ((*die_info_ptr) == 0) {
2318         return (DW_DLV_NO_ENTRY);
2319     }
2320 
2321     ret_die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
2322     if (ret_die == NULL) {
2323         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
2324         return (DW_DLV_ERROR);
2325     }
2326 
2327     ret_die->di_is_info = is_info;
2328     ret_die->di_debug_ptr = die_info_ptr;
2329     ret_die->di_cu_context =
2330         die == NULL ? context : die->di_cu_context;
2331 
2332 #if 0
2333     DECODE_LEB128_UWORD_CK(die_info_ptr, utmp,dbg,error,die_info_end);
2334 #endif
2335     dieres = _dwarf_leb128_uword_wrapper(dbg,
2336         &die_info_ptr,die_info_end,&utmp,error);
2337     if (dieres == DW_DLV_ERROR) {
2338         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2339         return dieres;
2340     }
2341     if (die_info_ptr > die_info_end) {
2342         /*  We managed to go past the end of the CU!.
2343             Something is badly wrong. */
2344         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2345         _dwarf_error(dbg, error, DW_DLE_ABBREV_DECODE_ERROR);
2346         return (DW_DLV_ERROR);
2347     }
2348     abbrev_code = (Dwarf_Unsigned) utmp;
2349     if (abbrev_code == 0) {
2350         /* Zero means a null DIE */
2351         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2352         return (DW_DLV_NO_ENTRY);
2353     }
2354     ret_die->di_abbrev_code = abbrev_code;
2355     lres = _dwarf_get_abbrev_for_code(ret_die->di_cu_context,
2356         abbrev_code,
2357         &ret_die->di_abbrev_list,error);
2358     if (lres == DW_DLV_ERROR) {
2359         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2360         return lres;
2361     }
2362     if (lres == DW_DLV_NO_ENTRY) {
2363         dwarfstring m;
2364         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2365         dwarfstring_constructor(&m);
2366         dwarfstring_append_printf_u(&m,
2367             "There is no abbrev present for code 0x%x .",
2368             abbrev_code);
2369         _dwarf_error_string(dbg, error,
2370             DW_DLE_DIE_ABBREV_LIST_NULL,dwarfstring_string(&m));
2371         dwarfstring_destructor(&m);
2372         return DW_DLV_ERROR;
2373     }
2374     if (die == NULL && !is_cu_tag(ret_die->di_abbrev_list->abl_tag)) {
2375         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2376         _dwarf_error(dbg, error, DW_DLE_FIRST_DIE_NOT_CU);
2377         return DW_DLV_ERROR;
2378     }
2379 
2380     *caller_ret_die = ret_die;
2381     return (DW_DLV_OK);
2382 }
2383 
2384 
2385 int
dwarf_child(Dwarf_Die die,Dwarf_Die * caller_ret_die,Dwarf_Error * error)2386 dwarf_child(Dwarf_Die die,
2387     Dwarf_Die * caller_ret_die,
2388     Dwarf_Error * error)
2389 {
2390     Dwarf_Byte_Ptr die_info_ptr = 0;
2391     Dwarf_Byte_Ptr die_info_ptr2 = 0;
2392 
2393     /* die_info_end points one-past-end of die area. */
2394     Dwarf_Byte_Ptr die_info_end = 0;
2395     Dwarf_Die ret_die = 0;
2396     Dwarf_Bool has_die_child = 0;
2397     Dwarf_Debug dbg;
2398     Dwarf_Unsigned abbrev_code = 0;
2399     Dwarf_Unsigned utmp = 0;
2400     Dwarf_Debug_InfoTypes dis = 0;
2401     int res = 0;
2402     Dwarf_CU_Context context = 0;
2403     int lres = 0;
2404 
2405     CHECK_DIE(die, DW_DLV_ERROR);
2406     dbg = die->di_cu_context->cc_dbg;
2407     dis = die->di_is_info? &dbg->de_info_reading:
2408         &dbg->de_types_reading;
2409     die_info_ptr = die->di_debug_ptr;
2410 
2411     /*  We are saving a DIE pointer here, but the pointer
2412         will not be presumed live later, when it is tested. */
2413     dis->de_last_die = die;
2414     dis->de_last_di_ptr = die_info_ptr;
2415 
2416     /* NULL die has no child. */
2417     if ((*die_info_ptr) == 0) {
2418         return DW_DLV_NO_ENTRY;
2419     }
2420     context = die->di_cu_context;
2421     die_info_end = _dwarf_calculate_info_section_end_ptr(context);
2422 
2423     res = _dwarf_next_die_info_ptr(die_info_ptr,
2424         die->di_cu_context,
2425         die_info_end,
2426         NULL, false,
2427         &has_die_child,
2428         &die_info_ptr2,
2429         error);
2430     if(res != DW_DLV_OK) {
2431         return res;
2432     }
2433     if (die_info_ptr == die_info_end) {
2434         return DW_DLV_NO_ENTRY;
2435     }
2436     die_info_ptr = die_info_ptr2;
2437 
2438     dis->de_last_di_ptr = die_info_ptr;
2439 
2440     if (!has_die_child) {
2441         /* Look for end of sibling chain. */
2442         while (dis->de_last_di_ptr < die_info_end) {
2443             if (*dis->de_last_di_ptr) {
2444                 break;
2445             }
2446             ++dis->de_last_di_ptr;
2447         }
2448         return DW_DLV_NO_ENTRY;
2449     }
2450 
2451     ret_die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
2452     if (ret_die == NULL) {
2453         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
2454         return DW_DLV_ERROR;
2455     }
2456     ret_die->di_debug_ptr = die_info_ptr;
2457     ret_die->di_cu_context = die->di_cu_context;
2458     ret_die->di_is_info = die->di_is_info;
2459 
2460     DECODE_LEB128_UWORD_CK(die_info_ptr, utmp,
2461         dbg,error,die_info_end);
2462     abbrev_code = (Dwarf_Unsigned) utmp;
2463 
2464     dis->de_last_di_ptr = die_info_ptr;
2465 
2466     if (abbrev_code == 0) {
2467         /* Look for end of sibling chain */
2468         while (dis->de_last_di_ptr < die_info_end) {
2469             if (*dis->de_last_di_ptr) {
2470                 break;
2471             }
2472             ++dis->de_last_di_ptr;
2473         }
2474 
2475         /*  We have arrived at a null DIE,
2476             at the end of a CU or the end
2477             of a list of siblings. */
2478         *caller_ret_die = 0;
2479         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2480         ret_die = 0;
2481         return DW_DLV_NO_ENTRY;
2482     }
2483     ret_die->di_abbrev_code = abbrev_code;
2484     lres = _dwarf_get_abbrev_for_code(die->di_cu_context, abbrev_code,
2485         &ret_die->di_abbrev_list,error);
2486     if (lres == DW_DLV_ERROR) {
2487         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2488         ret_die = 0;
2489         return lres;
2490     }
2491     if (lres == DW_DLV_NO_ENTRY) {
2492         dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2493         ret_die = 0;
2494         _dwarf_error(dbg, error, DW_DLE_ABBREV_MISSING);
2495         return DW_DLV_ERROR;
2496     }
2497     *caller_ret_die = ret_die;
2498     return (DW_DLV_OK);
2499 }
2500 
2501 /*  Given a (global, not cu_relative) die offset, this returns
2502     a pointer to a DIE thru *new_die.
2503     It is up to the caller to do a
2504     dwarf_dealloc(dbg,*new_die,DW_DLE_DIE);
2505     The old form only works with debug_info.
2506     The new _b form works with debug_info or debug_types.
2507     */
2508 int
dwarf_offdie(Dwarf_Debug dbg,Dwarf_Off offset,Dwarf_Die * new_die,Dwarf_Error * error)2509 dwarf_offdie(Dwarf_Debug dbg,
2510     Dwarf_Off offset, Dwarf_Die * new_die, Dwarf_Error * error)
2511 {
2512     Dwarf_Bool is_info = true;
2513     return dwarf_offdie_b(dbg,offset,is_info,new_die,error);
2514 }
2515 
2516 int
dwarf_offdie_b(Dwarf_Debug dbg,Dwarf_Off offset,Dwarf_Bool is_info,Dwarf_Die * new_die,Dwarf_Error * error)2517 dwarf_offdie_b(Dwarf_Debug dbg,
2518     Dwarf_Off offset, Dwarf_Bool is_info,
2519     Dwarf_Die * new_die, Dwarf_Error * error)
2520 {
2521     Dwarf_CU_Context cu_context = 0;
2522     Dwarf_Off new_cu_offset = 0;
2523     Dwarf_Die die = 0;
2524     Dwarf_Byte_Ptr info_ptr = 0;
2525     Dwarf_Unsigned abbrev_code = 0;
2526     Dwarf_Unsigned utmp = 0;
2527     int lres = 0;
2528     Dwarf_Debug_InfoTypes dis = 0;
2529     Dwarf_Byte_Ptr die_info_end = 0;
2530 
2531     if (dbg == NULL) {
2532         _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
2533         return (DW_DLV_ERROR);
2534     }
2535     dis = is_info? &dbg->de_info_reading:
2536         &dbg->de_types_reading;
2537 
2538     cu_context = _dwarf_find_CU_Context(dbg, offset,is_info);
2539     if (cu_context == NULL) {
2540         Dwarf_Unsigned section_size = is_info?
2541             dbg->de_debug_info.dss_size:
2542             dbg->de_debug_types.dss_size;
2543         int res = is_info?_dwarf_load_debug_info(dbg, error):
2544             _dwarf_load_debug_types(dbg,error);
2545 
2546         if (res != DW_DLV_OK) {
2547             return res;
2548         }
2549 
2550         if (dis->de_cu_context_list_end != NULL) {
2551             Dwarf_CU_Context lcu_context =
2552                 dis->de_cu_context_list_end;
2553             new_cu_offset =
2554                 lcu_context->cc_debug_offset +
2555                 lcu_context->cc_length +
2556                 lcu_context->cc_length_size +
2557                 lcu_context->cc_extension_size;
2558         }
2559 
2560         do {
2561             if ((new_cu_offset +
2562                 _dwarf_length_of_cu_header_simple(dbg,is_info)) >=
2563                 section_size) {
2564                 _dwarf_error(dbg, error, DW_DLE_OFFSET_BAD);
2565                 return (DW_DLV_ERROR);
2566             }
2567             res = _dwarf_make_CU_Context(dbg, new_cu_offset,is_info,
2568                 &cu_context,error);
2569             if (res != DW_DLV_OK) {
2570                 local_dealloc_cu_context(dbg,cu_context);
2571                 return res;
2572             }
2573 
2574             res = finish_up_cu_context_from_cudie(dbg,new_cu_offset,
2575                 cu_context,error);
2576             if (res == DW_DLV_ERROR) {
2577                 local_dealloc_cu_context(dbg,cu_context);
2578                 return res;
2579             }
2580             if (res == DW_DLV_NO_ENTRY) {
2581                 local_dealloc_cu_context(dbg,cu_context);
2582                 return res;
2583             }
2584             /*  Add the new cu_context to a list of contexts */
2585             insert_into_cu_context_list(dis,cu_context);
2586             new_cu_offset = new_cu_offset + cu_context->cc_length +
2587                 cu_context->cc_length_size +
2588                 cu_context->cc_extension_size;
2589         } while (offset >= new_cu_offset);
2590     }
2591 
2592     die_info_end = _dwarf_calculate_info_section_end_ptr(cu_context);
2593     die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
2594     if (!die) {
2595         _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
2596         return (DW_DLV_ERROR);
2597     }
2598     die->di_cu_context = cu_context;
2599     die->di_is_info = is_info;
2600 
2601     {
2602         Dwarf_Small *dataptr = is_info? dbg->de_debug_info.dss_data:
2603             dbg->de_debug_types.dss_data;
2604         info_ptr = dataptr + offset;
2605     }
2606     die->di_debug_ptr = info_ptr;
2607 #if 0
2608     DECODE_LEB128_UWORD_CK(info_ptr, utmp,dbg,error,die_info_end);
2609 #endif
2610     lres = _dwarf_leb128_uword_wrapper(dbg,&info_ptr,die_info_end,
2611         &utmp,error);
2612     if (lres != DW_DLV_OK) {
2613         dwarf_dealloc(dbg, die, DW_DLA_DIE);
2614         return lres;
2615     }
2616     abbrev_code = utmp;
2617     if (abbrev_code == 0) {
2618         /* we are at a null DIE (or there is a bug). */
2619         *new_die = 0;
2620         dwarf_dealloc(dbg, die, DW_DLA_DIE);
2621         return DW_DLV_NO_ENTRY;
2622     }
2623     die->di_abbrev_code = abbrev_code;
2624     lres = _dwarf_get_abbrev_for_code(cu_context, abbrev_code,
2625         &die->di_abbrev_list,error);
2626     if (lres == DW_DLV_ERROR) {
2627         dwarf_dealloc(dbg, die, DW_DLA_DIE);
2628         return lres;
2629     }
2630     if (lres == DW_DLV_NO_ENTRY) {
2631         dwarfstring m;
2632 
2633         dwarf_dealloc(dbg,die, DW_DLA_DIE);
2634         dwarfstring_constructor(&m);
2635         dwarfstring_append_printf_u(&m,
2636             "There is no abbrev present for code 0x%x .",
2637             abbrev_code);
2638         _dwarf_error_string(dbg, error,
2639             DW_DLE_DIE_ABBREV_LIST_NULL,dwarfstring_string(&m));
2640         dwarfstring_destructor(&m);
2641         return DW_DLV_ERROR;
2642     }
2643     *new_die = die;
2644     return DW_DLV_OK;
2645 }
2646 
2647 /*  New March 2016.
2648     Lets one cross check the abbreviations section and
2649     the DIE information presented  by dwarfdump -i -G -v. */
2650 int
dwarf_die_abbrev_global_offset(Dwarf_Die die,Dwarf_Off * abbrev_goffset,Dwarf_Unsigned * abbrev_count,Dwarf_Error * error)2651 dwarf_die_abbrev_global_offset(Dwarf_Die die,
2652     Dwarf_Off       * abbrev_goffset,
2653     Dwarf_Unsigned  * abbrev_count,
2654     Dwarf_Error*      error)
2655 {
2656     Dwarf_Abbrev_List dal = 0;
2657     Dwarf_Debug dbg = 0;
2658 
2659     CHECK_DIE(die, DW_DLV_ERROR);
2660     dbg = die->di_cu_context->cc_dbg;
2661     dal = die->di_abbrev_list;
2662     if(!dal) {
2663         _dwarf_error(dbg,error,DW_DLE_DWARF_ABBREV_NULL);
2664         return DW_DLV_ERROR;
2665     }
2666     *abbrev_goffset = dal->abl_goffset;
2667     *abbrev_count = dal->abl_count;
2668     return DW_DLV_OK;
2669 }
2670 
2671 
2672 /*  New August 2018.
2673     Because some real compressed sections
2674     have .zdebug instead
2675     of .debug as the leading characters.
2676     actual_sec_name_out points to a static
2677     string so so not free it. */
2678 int
dwarf_get_real_section_name(Dwarf_Debug dbg,const char * std_section_name,const char ** actual_sec_name_out,Dwarf_Small * marked_zcompressed,Dwarf_Small * marked_zlib_compressed,Dwarf_Small * marked_shf_compressed,Dwarf_Unsigned * compressed_length,Dwarf_Unsigned * uncompressed_length,Dwarf_Error * error)2679 dwarf_get_real_section_name(Dwarf_Debug dbg,
2680     const char  *std_section_name,
2681     const char **actual_sec_name_out,
2682     Dwarf_Small *marked_zcompressed, /* zdebug */
2683     Dwarf_Small *marked_zlib_compressed, /* ZLIB string */
2684     Dwarf_Small *marked_shf_compressed, /* SHF_COMPRESSED */
2685     Dwarf_Unsigned *compressed_length,
2686     Dwarf_Unsigned *uncompressed_length,
2687     Dwarf_Error *error)
2688 {
2689     unsigned i = 0;
2690     char tbuf[50];
2691     unsigned std_sec_name_len = strlen(std_section_name);
2692 
2693     tbuf[0] = 0;
2694     /*  std_section_name never has the .dwo on the end,
2695         so allow for that and allow one (arbitrarily) more. */
2696     if ((std_sec_name_len + 5) < sizeof(tbuf)) {
2697         strcpy(tbuf,std_section_name);
2698         strcpy(tbuf+std_sec_name_len,".dwo");
2699     }
2700     if (dbg == NULL) {
2701         _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
2702         return (DW_DLV_ERROR);
2703     }
2704     for (i=0; i < dbg->de_debug_sections_total_entries; i++) {
2705         struct Dwarf_dbg_sect_s *sdata = &dbg->de_debug_sections[i];
2706         struct Dwarf_Section_s *section = sdata->ds_secdata;
2707         const char *std = section->dss_standard_name;
2708 
2709         if (!strcmp(std,std_section_name) ||
2710             !strcmp(std,tbuf)) {
2711             const char *used = section->dss_name;
2712             *actual_sec_name_out = used;
2713             if (sdata->ds_have_zdebug) {
2714                 *marked_zcompressed = TRUE;
2715             }
2716             if (section->dss_ZLIB_compressed) {
2717                 *marked_zlib_compressed = TRUE;
2718                 if (uncompressed_length) {
2719                     *uncompressed_length =
2720                         section->dss_uncompressed_length;
2721                 }
2722                 if (compressed_length) {
2723                     *compressed_length =
2724                         section->dss_compressed_length;
2725                 }
2726             }
2727             if (section->dss_shf_compressed) {
2728                 *marked_shf_compressed = TRUE;
2729                 if (uncompressed_length) {
2730                     *uncompressed_length =
2731                         section->dss_uncompressed_length;
2732                 }
2733                 if (compressed_length) {
2734                     *compressed_length =
2735                         section->dss_compressed_length;
2736                 }
2737             }
2738             return DW_DLV_OK;
2739         }
2740     }
2741     return DW_DLV_NO_ENTRY;
2742 }
2743 /*  This is useful when printing DIE data.
2744     The string pointer returned must not be freed.
2745     With non-elf objects it is possible the
2746     string returned might be empty or NULL,
2747     so callers should be prepared for that kind
2748     of return. */
2749 int
dwarf_get_die_section_name(Dwarf_Debug dbg,Dwarf_Bool is_info,const char ** sec_name,Dwarf_Error * error)2750 dwarf_get_die_section_name(Dwarf_Debug dbg,
2751     Dwarf_Bool    is_info,
2752     const char ** sec_name,
2753     Dwarf_Error * error)
2754 {
2755     struct Dwarf_Section_s *sec = 0;
2756 
2757     if (dbg == NULL) {
2758         _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
2759         return (DW_DLV_ERROR);
2760     }
2761     if (is_info) {
2762         sec = &dbg->de_debug_info;
2763     } else {
2764         sec = &dbg->de_debug_types;
2765     }
2766     if (sec->dss_size == 0) {
2767         /* We don't have such a  section at all. */
2768         return DW_DLV_NO_ENTRY;
2769     }
2770     *sec_name = sec->dss_name;
2771     return DW_DLV_OK;
2772 }
2773 
2774 /* This one assumes is_info not known to caller but a DIE is known. */
2775 int
dwarf_get_die_section_name_b(Dwarf_Die die,const char ** sec_name,Dwarf_Error * error)2776 dwarf_get_die_section_name_b(Dwarf_Die die,
2777     const char ** sec_name,
2778     Dwarf_Error * error)
2779 {
2780     Dwarf_CU_Context context = 0;
2781     Dwarf_Bool is_info = 0;
2782     Dwarf_Debug dbg = 0;
2783 
2784     CHECK_DIE(die, DW_DLV_ERROR);
2785     context = die->di_cu_context;
2786     dbg = context->cc_dbg;
2787     is_info = context->cc_is_info;
2788     return dwarf_get_die_section_name(dbg,is_info,sec_name,error);
2789 }
2790