xref: /illumos-gate/usr/src/lib/libdwarf/common/dwarf_util.c (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
1 /*
2   Copyright (C) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
3   Portions Copyright (C) 2007-2020 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 and/or modify it
7   under the terms of version 2.1 of the GNU Lesser General Public License
8   as published by the Free Software Foundation.
9 
10   This program is distributed in the hope that it would be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 
14   Further, this software is distributed without any warranty that it is
15   free of the rightful claim of any third person regarding infringement
16   or the like.  Any license provided herein, whether implied or
17   otherwise, applies only to this software file.  Patent licenses, if
18   any, provided herein do not apply to combinations of this program with
19   other software, or any other product whatsoever.
20 
21   You should have received a copy of the GNU Lesser General Public
22   License along with this program; if not, write the Free Software
23   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
24   USA.
25 
26 */
27 
28 #include "config.h"
29 #include <stdio.h>
30 #include <stdarg.h>
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h> /* For free() and emergency abort() */
33 #endif /* HAVE_STDLIB_H */
34 #ifdef HAVE_MALLOC_H
35 /* Useful include for some Windows compilers. */
36 #include <malloc.h>
37 #endif /* HAVE_MALLOC_H */
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #elif defined(_WIN32) && defined(_MSC_VER)
41 #include <io.h>
42 #endif /* HAVE_UNISTD_H */
43 #include <sys/types.h> /* for open() */
44 #include <sys/stat.h> /* for open() */
45 #include <fcntl.h> /* for open() */
46 #include "dwarf_incl.h"
47 #include "dwarf_alloc.h"
48 #include "dwarf_error.h"
49 #include "dwarf_util.h"
50 #include "dwarf_abbrev.h"
51 #include "memcpy_swap.h"
52 #include "dwarf_die_deliv.h"
53 #include "dwarfstring.h"
54 #include "pro_encode_nm.h"
55 
56 #ifndef O_BINARY
57 #define O_BINARY 0
58 #endif /* O_BINARY */
59 
60 
61 
62 #define MINBUFLEN 1000
63 #define TRUE  1
64 #define FALSE 0
65 
66 #if _WIN32
67 #define NULL_DEVICE_NAME "NUL"
68 #else
69 #define NULL_DEVICE_NAME "/dev/null"
70 #endif /* _WIN32 */
71 
72 /*  The function returned allows dwarfdump and other callers to
73     do an endian-sensitive copy-word with a chosen
74     source-length.  */
75 typedef void (*endian_funcp_type)(void *, const void *,unsigned long);
76 
77 const char *
78 dwarf_package_version(void)
79 {
80     return PACKAGE_VERSION;
81 }
82 
83 #if 0
84 static void
85 dump_bytes(char * msg,Dwarf_Small * start, long len)
86 {
87     Dwarf_Small *end = start + len;
88     Dwarf_Small *cur = start;
89 
90     printf("%s ",msg);
91     for (; cur < end; cur++) {
92         printf("%02x ", *cur);
93     }
94     printf("\n");
95 }
96 #endif
97 
98 endian_funcp_type
99 dwarf_get_endian_copy_function(Dwarf_Debug dbg)
100 {
101     if (dbg) {
102         return dbg->de_copy_word;
103     }
104     return 0;
105 }
106 
107 
108 Dwarf_Bool
109 _dwarf_file_has_debug_fission_cu_index(Dwarf_Debug dbg)
110 {
111     if(!dbg) {
112         return FALSE;
113     }
114     if (dbg->de_cu_hashindex_data) {
115         return TRUE;
116     }
117     return FALSE;
118 }
119 Dwarf_Bool
120 _dwarf_file_has_debug_fission_tu_index(Dwarf_Debug dbg)
121 {
122     if(!dbg) {
123         return FALSE;
124     }
125     if (dbg->de_tu_hashindex_data ) {
126         return TRUE;
127     }
128     return FALSE;
129 }
130 
131 
132 Dwarf_Bool
133 _dwarf_file_has_debug_fission_index(Dwarf_Debug dbg)
134 {
135     if(!dbg) {
136         return FALSE;
137     }
138     if (dbg->de_cu_hashindex_data ||
139         dbg->de_tu_hashindex_data) {
140         return 1;
141     }
142     return FALSE;
143 }
144 
145 int
146 _dwarf_internal_get_die_comp_dir(Dwarf_Die die, const char **compdir_out,
147     const char **compname_out,
148     Dwarf_Error *error)
149 {
150     Dwarf_Attribute comp_dir_attr = 0;
151     Dwarf_Attribute comp_name_attr = 0;
152     int resattr = 0;
153     Dwarf_Debug dbg = 0;
154 
155     dbg = die->di_cu_context->cc_dbg;
156     resattr = dwarf_attr(die, DW_AT_name, &comp_name_attr, error);
157     if (resattr == DW_DLV_ERROR) {
158         return resattr;
159     }
160     if (resattr == DW_DLV_OK) {
161         int cres = DW_DLV_ERROR;
162         char *name = 0;
163 
164         cres = dwarf_formstring(comp_name_attr, &name, error);
165         if (cres == DW_DLV_ERROR) {
166             dwarf_dealloc(dbg, comp_name_attr, DW_DLA_ATTR);
167             return cres;
168         } else if (cres == DW_DLV_OK) {
169             *compname_out = (const char *)name;
170         } else {
171             /* FALL thru */
172         }
173     }
174     if (resattr == DW_DLV_OK) {
175         dwarf_dealloc(dbg, comp_name_attr, DW_DLA_ATTR);
176     }
177     resattr = dwarf_attr(die, DW_AT_comp_dir, &comp_dir_attr, error);
178     if (resattr == DW_DLV_ERROR) {
179         return resattr;
180     }
181     if (resattr == DW_DLV_OK) {
182         int cres = DW_DLV_ERROR;
183         char *cdir = 0;
184 
185         cres = dwarf_formstring(comp_dir_attr, &cdir, error);
186         if (cres == DW_DLV_ERROR) {
187             dwarf_dealloc(dbg, comp_dir_attr, DW_DLA_ATTR);
188             return cres;
189         } else if (cres == DW_DLV_OK) {
190             *compdir_out = (const char *) cdir;
191         } else {
192             /* FALL thru */
193         }
194     }
195     if (resattr == DW_DLV_OK) {
196         dwarf_dealloc(dbg, comp_dir_attr, DW_DLA_ATTR);
197     }
198     return resattr;
199 }
200 
201 
202 /*  Given a form, and a pointer to the bytes encoding
203     a value of that form, val_ptr, this function returns
204     the length, in bytes, of a value of that form.
205     When using this function, check for a return of 0
206     a recursive DW_FORM_INDIRECT value.  */
207 int
208 _dwarf_get_size_of_val(Dwarf_Debug dbg,
209     Dwarf_Unsigned form,
210     Dwarf_Half cu_version,
211     Dwarf_Half address_size,
212     Dwarf_Small * val_ptr,
213     int v_length_size,
214     Dwarf_Unsigned *size_out,
215     Dwarf_Small *section_end_ptr,
216     Dwarf_Error*error)
217 {
218     Dwarf_Unsigned length = 0;
219     Dwarf_Unsigned leb128_length = 0;
220     Dwarf_Unsigned form_indirect = 0;
221     Dwarf_Unsigned ret_value = 0;
222 
223     switch (form) {
224 
225     /*  When we encounter a FORM here that
226         we know about but forgot to enter here,
227         we had better not just continue.
228         Usually means we forgot to update this function
229         when implementing form handling of a new FORM.
230         Disaster results from using a bogus value,
231         so generate error. */
232     default:
233         _dwarf_error(dbg,error,DW_DLE_DEBUG_FORM_HANDLING_INCOMPLETE);
234         return DW_DLV_ERROR;
235 
236 
237     case 0:  return DW_DLV_OK;
238 
239     case DW_FORM_GNU_ref_alt:
240     case DW_FORM_GNU_strp_alt:
241     case DW_FORM_strp_sup:
242         *size_out = v_length_size;
243         return DW_DLV_OK;
244 
245     case DW_FORM_addr:
246         if (address_size) {
247             *size_out = address_size;
248         } else {
249             /* This should never happen, address_size should be set. */
250             *size_out = dbg->de_pointer_size;
251         }
252         return DW_DLV_OK;
253     case DW_FORM_ref_sig8:
254         *size_out = 8;
255         /* sizeof Dwarf_Sig8 */
256         return DW_DLV_OK;
257 
258     /*  DWARF2 was wrong on the size of the attribute for
259         DW_FORM_ref_addr.  We assume compilers are using the
260         corrected DWARF3 text (for 32bit pointer target objects pointer and
261         offsets are the same size anyway).
262         It is clear (as of 2014) that for 64bit folks used
263         the V2 spec in the way V2 was
264         written, so the ref_addr has to account for that.*/
265     case DW_FORM_ref_addr:
266         if (cu_version == DW_CU_VERSION2) {
267             *size_out = address_size;
268         } else {
269             *size_out = v_length_size;
270         }
271         return DW_DLV_OK;
272 
273     case DW_FORM_block1: {
274         ptrdiff_t sizeasptrdiff = 0;
275 
276         if (val_ptr >= section_end_ptr) {
277             _dwarf_error_string(dbg,error,
278                 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
279                 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: DW_FORM_block1"
280                 " itself is off the end of the section."
281                 " Corrupt Dwarf.");
282             return DW_DLV_ERROR;
283         }
284         ret_value =  *(Dwarf_Small *) val_ptr;
285         sizeasptrdiff = (ptrdiff_t)ret_value;
286         if (sizeasptrdiff > (section_end_ptr - val_ptr) ||
287             sizeasptrdiff < 0) {
288             _dwarf_error_string(dbg,error,
289                 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
290                 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: DW_FORM_block1"
291                 " runs off the end of the section."
292                 " Corrupt Dwarf.");
293             return DW_DLV_ERROR;
294         }
295         *size_out = ret_value +1;
296         }
297         return DW_DLV_OK;
298 
299     case DW_FORM_block2: {
300         ptrdiff_t sizeasptrdiff = 0;
301 
302         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
303             val_ptr, DWARF_HALF_SIZE,error,section_end_ptr);
304         sizeasptrdiff = (ptrdiff_t)ret_value;
305         if (sizeasptrdiff > (section_end_ptr - val_ptr) ||
306             sizeasptrdiff < 0) {
307             _dwarf_error_string(dbg,error,
308                 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
309                 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: DW_FORM_block2"
310                 " runs off the end of the section."
311                 " Corrupt Dwarf.");
312             return DW_DLV_ERROR;
313         }
314         *size_out = ret_value + DWARF_HALF_SIZE;
315         }
316         return DW_DLV_OK;
317 
318     case DW_FORM_block4: {
319         ptrdiff_t sizeasptrdiff = 0;
320 
321         READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
322             val_ptr, DWARF_32BIT_SIZE,
323             error,section_end_ptr);
324         sizeasptrdiff = (ptrdiff_t)ret_value;
325         if (sizeasptrdiff > (section_end_ptr - val_ptr) ||
326             sizeasptrdiff < 0) {
327             _dwarf_error_string(dbg,error,
328                 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
329                 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: DW_FORM_block4"
330                 " runs off the end of the section."
331                 " Corrupt Dwarf.");
332             return DW_DLV_ERROR;
333         }
334         *size_out = ret_value + DWARF_32BIT_SIZE;
335         }
336         return DW_DLV_OK;
337 
338     case DW_FORM_data1:
339         *size_out = 1;
340         return DW_DLV_OK;
341 
342     case DW_FORM_data2:
343         *size_out = 2;
344         return DW_DLV_OK;
345 
346     case DW_FORM_data4:
347         *size_out = 4;
348         return DW_DLV_OK;
349 
350     case DW_FORM_data8:
351         *size_out = 8;
352         return DW_DLV_OK;
353 
354     case DW_FORM_data16:
355         *size_out = 16;
356         return DW_DLV_OK;
357 
358     case DW_FORM_string: {
359         int res = 0;
360         res = _dwarf_check_string_valid(dbg,val_ptr,
361             val_ptr,
362             section_end_ptr,
363             DW_DLE_FORM_STRING_BAD_STRING,
364             error);
365         if ( res != DW_DLV_OK) {
366             return res;
367         }
368         }
369         *size_out = strlen((char *) val_ptr) + 1;
370         return DW_DLV_OK;
371 
372     case DW_FORM_block:
373     case DW_FORM_exprloc: {
374         DECODE_LEB128_UWORD_LEN_CK(val_ptr,length,leb128_length,
375             dbg,error,section_end_ptr);
376         *size_out = length + leb128_length;
377         return DW_DLV_OK;;
378     }
379 
380     case DW_FORM_flag_present:
381         *size_out = 0;
382         return DW_DLV_OK;
383 
384     case DW_FORM_flag:
385         *size_out = 1;
386         return DW_DLV_OK;
387 
388     case DW_FORM_sec_offset:
389         /* If 32bit dwarf, is 4. Else is 64bit dwarf and is 8. */
390         *size_out = v_length_size;
391         return DW_DLV_OK;
392 
393     case DW_FORM_ref_udata: {
394         UNUSEDARG Dwarf_Unsigned v = 0;
395 
396         /*  Discard the decoded value, we just want the length
397             of the value. */
398         DECODE_LEB128_UWORD_LEN_CK(val_ptr,v,leb128_length,
399             dbg,error,section_end_ptr);
400         *size_out = leb128_length;
401         return DW_DLV_OK;;
402     }
403 
404     case DW_FORM_indirect:
405         {
406             Dwarf_Unsigned indir_len = 0;
407             int res = 0;
408             Dwarf_Unsigned info_data_len = 0;
409 
410             DECODE_LEB128_UWORD_LEN_CK(val_ptr,form_indirect,indir_len,
411                 dbg,error,section_end_ptr);
412             if (form_indirect == DW_FORM_indirect) {
413                 /* We are in big trouble: The true form
414                     of DW_FORM_indirect is
415                     DW_FORM_indirect? Nonsense. Should
416                     never happen. */
417                 _dwarf_error(dbg,error,DW_DLE_NESTED_FORM_INDIRECT_ERROR);
418                 return DW_DLV_ERROR;
419             }
420             /*  If form_indirect  is DW_FORM_implicit_const then
421                 the following call will set info_data_len 0 */
422             res = _dwarf_get_size_of_val(dbg,
423                 form_indirect,
424                 cu_version,
425                 address_size,
426                 val_ptr + indir_len,
427                 v_length_size,
428                 &info_data_len,
429                 section_end_ptr,
430                 error);
431             if(res != DW_DLV_OK) {
432                 return res;
433             }
434             *size_out = indir_len + info_data_len;
435             return DW_DLV_OK;
436         }
437 
438     case DW_FORM_ref1:
439         *size_out = 1;
440         return DW_DLV_OK;
441 
442     case DW_FORM_ref2:
443         *size_out = 2;
444         return DW_DLV_OK;
445 
446     case DW_FORM_ref4:
447         *size_out = 4;
448         return DW_DLV_OK;
449 
450     case DW_FORM_ref8:
451         *size_out = 8;
452         return DW_DLV_OK;
453 
454     /*  DW_FORM_implicit_const  is a value in the
455         abbreviations, not in the DIEs and this
456         functions measures DIE size. */
457     case DW_FORM_implicit_const:
458         *size_out = 0;
459         return DW_DLV_OK;
460 
461     case DW_FORM_sdata: {
462         /*  Discard the decoded value, we just want the length
463             of the value. */
464         UNUSEDARG Dwarf_Signed v = 0;
465 
466         /*  Discard the decoded value, we just want the length
467             of the value. */
468         DECODE_LEB128_SWORD_LEN_CK(val_ptr,v,leb128_length,
469             dbg,error,section_end_ptr);
470         *size_out = leb128_length;
471         return DW_DLV_OK;
472     }
473     case DW_FORM_ref_sup4:
474         *size_out = 4;
475         return DW_DLV_OK;
476     case DW_FORM_ref_sup8:
477         *size_out = 8;
478         return DW_DLV_OK;
479     case DW_FORM_addrx1:
480         *size_out = 1;
481         return DW_DLV_OK;
482     case DW_FORM_addrx2:
483         *size_out = 2;
484         return DW_DLV_OK;
485     case DW_FORM_addrx3:
486         *size_out = 4;
487         return DW_DLV_OK;
488     case DW_FORM_addrx4:
489         *size_out = 4;
490         return DW_DLV_OK;
491     case DW_FORM_strx1:
492         *size_out = 1;
493         return DW_DLV_OK;
494     case DW_FORM_strx2:
495         *size_out = 2;
496         return DW_DLV_OK;
497     case DW_FORM_strx3:
498         *size_out = 4;
499         return DW_DLV_OK;
500     case DW_FORM_strx4:
501         *size_out = 4;
502         return DW_DLV_OK;
503 
504     case DW_FORM_loclistx:
505     case DW_FORM_rnglistx:
506     case DW_FORM_addrx:
507     case DW_FORM_GNU_addr_index:
508     case DW_FORM_strx:
509     case DW_FORM_GNU_str_index: {
510         UNUSEDARG Dwarf_Unsigned v = 0;
511 
512         DECODE_LEB128_UWORD_LEN_CK(val_ptr,v,leb128_length,
513             dbg,error,section_end_ptr);
514         *size_out = leb128_length;
515         return DW_DLV_OK;
516     }
517 
518     case DW_FORM_line_strp:
519     case DW_FORM_strp:
520         *size_out = v_length_size;
521         return DW_DLV_OK;
522 
523     case DW_FORM_udata: {
524         /*  Discard the decoded value, we just want the length
525             of the value. */
526         UNUSEDARG Dwarf_Unsigned v = 0;
527 
528         DECODE_LEB128_UWORD_LEN_CK(val_ptr,v,leb128_length,
529             dbg,error,section_end_ptr);
530         *size_out = leb128_length;
531         return DW_DLV_OK;
532     }
533     }
534 }
535 
536 /*  We allow an arbitrary number of HT_MULTIPLE entries
537     before resizing.  It seems up to 20 or 30
538     would work nearly as well.
539     We could have a different resize multiple than 'resize now'
540     test multiple, but for now we don't do that.  */
541 #define HT_MULTIPLE 8
542 
543 /*  Copy the old entries, updating each to be in
544     a new list.  Don't delete anything. Leave the
545     htin with stale data. */
546 static void
547 copy_abbrev_table_to_new_table(Dwarf_Hash_Table htin,
548   Dwarf_Hash_Table htout)
549 {
550     Dwarf_Hash_Table_Entry entry_in = htin->tb_entries;
551     unsigned entry_in_count = htin->tb_table_entry_count;
552     Dwarf_Hash_Table_Entry entry_out = htout->tb_entries;
553     unsigned entry_out_count = htout->tb_table_entry_count;
554     unsigned k = 0;
555     for (; k < entry_in_count; ++k,++entry_in) {
556         Dwarf_Abbrev_List listent = entry_in->at_head;
557         Dwarf_Abbrev_List nextlistent = 0;
558 
559         for (; listent ; listent = nextlistent) {
560             unsigned newtmp = listent->abl_code;
561             unsigned newhash = newtmp%entry_out_count;
562             Dwarf_Hash_Table_Entry e;
563             nextlistent = listent->abl_next;
564             e = entry_out+newhash;
565             /*  Move_entry_to_new_hash. This reverses the
566                 order of the entries, effectively, but
567                 that does not seem significant. */
568             listent->abl_next = e->at_head;
569             e->at_head = listent;
570 
571             htout->tb_total_abbrev_count++;
572         }
573     }
574 }
575 
576 /*  We allow zero form here, end of list. */
577 int
578 _dwarf_valid_form_we_know(Dwarf_Unsigned at_form,
579     Dwarf_Unsigned at_name)
580 {
581     if(at_form == 0 && at_name == 0) {
582         return TRUE;
583     }
584     if (at_name == 0) {
585         return FALSE;
586     }
587     if (at_form <= DW_FORM_addrx4 ) {
588         return TRUE;
589     }
590     if (at_form == DW_FORM_GNU_addr_index ||
591         at_form == DW_FORM_GNU_str_index  ||
592         at_form == DW_FORM_GNU_ref_alt ||
593         at_form == DW_FORM_GNU_strp_alt) {
594         return TRUE;
595     }
596     return FALSE;
597 }
598 
599 int
600 _dwarf_format_TAG_err_msg(Dwarf_Debug dbg,
601     Dwarf_Unsigned tag,
602     const char *m,
603     Dwarf_Error *errp)
604 {
605     dwarfstring v;
606 
607     dwarfstring_constructor(&v);
608     dwarfstring_append(&v,(char *)m);
609     dwarfstring_append(&v," The value ");
610     dwarfstring_append_printf_u(&v,"0x%" DW_PR_DUx
611         " is outside the valid TAG range.",tag);
612     dwarfstring_append(&v," Corrupt DWARF.");
613     _dwarf_error_string(dbg, errp,DW_DLE_TAG_CORRUPT,
614         dwarfstring_string(&v));
615     dwarfstring_destructor(&v);
616     return DW_DLV_ERROR;
617 }
618 
619 /*  This function returns a pointer to a Dwarf_Abbrev_List_s
620     struct for the abbrev with the given code.  It puts the
621     struct on the appropriate hash table.  It also adds all
622     the abbrev between the last abbrev added and this one to
623     the hash table.  In other words, the .debug_abbrev section
624     is scanned sequentially from the top for an abbrev with
625     the given code.  All intervening abbrevs are also put
626     into the hash table.
627 
628     This function hashes the given code, and checks the chain
629     at that hash table entry to see if a Dwarf_Abbrev_List_s
630     with the given code exists.  If yes, it returns a pointer
631     to that struct.  Otherwise, it scans the .debug_abbrev
632     section from the last byte scanned for that CU till either
633     an abbrev with the given code is found, or an abbrev code
634     of 0 is read.  It puts Dwarf_Abbrev_List_s entries for all
635     abbrev's read till that point into the hash table.  The
636     hash table contains both a head pointer and a tail pointer
637     for each entry.
638 
639     While the lists can move and entries can be moved between
640     lists on reallocation, any given Dwarf_Abbrev_list entry
641     never moves once allocated, so the pointer is safe to return.
642 
643     See also dwarf_get_abbrev() in dwarf_abbrev.c.
644 
645     Returns DW_DLV_ERROR on error.  */
646 int
647 _dwarf_get_abbrev_for_code(Dwarf_CU_Context cu_context,
648     Dwarf_Unsigned code,
649     Dwarf_Abbrev_List *list_out,
650     Dwarf_Error *error)
651 {
652     Dwarf_Debug dbg = cu_context->cc_dbg;
653     Dwarf_Hash_Table hash_table_base =
654         cu_context->cc_abbrev_hash_table;
655     Dwarf_Hash_Table_Entry entry_base = 0;
656     Dwarf_Hash_Table_Entry entry_cur = 0;
657     Dwarf_Unsigned hash_num = 0;
658     Dwarf_Unsigned abbrev_code = 0;
659     Dwarf_Unsigned abbrev_tag  = 0;
660     Dwarf_Abbrev_List hash_abbrev_entry = 0;
661     Dwarf_Abbrev_List inner_list_entry = 0;
662     Dwarf_Hash_Table_Entry inner_hash_entry = 0;
663 
664     Dwarf_Byte_Ptr abbrev_ptr = 0;
665     Dwarf_Byte_Ptr end_abbrev_ptr = 0;
666     unsigned hashable_val = 0;
667 
668     if (!hash_table_base->tb_entries) {
669         hash_table_base->tb_table_entry_count =  HT_MULTIPLE;
670         hash_table_base->tb_total_abbrev_count= 0;
671         hash_table_base->tb_entries =
672             (struct  Dwarf_Hash_Table_Entry_s *)_dwarf_get_alloc(dbg,
673             DW_DLA_HASH_TABLE_ENTRY,
674             hash_table_base->tb_table_entry_count);
675         if (!hash_table_base->tb_entries) {
676             return DW_DLV_NO_ENTRY;
677         }
678     } else if (hash_table_base->tb_total_abbrev_count >
679         (hash_table_base->tb_table_entry_count * HT_MULTIPLE)) {
680         struct Dwarf_Hash_Table_s newht;
681 
682         memset(&newht,0,sizeof(newht));
683         /* Effectively multiplies by >= HT_MULTIPLE */
684         newht.tb_table_entry_count =
685             hash_table_base->tb_total_abbrev_count;
686         newht.tb_total_abbrev_count = 0;
687         newht.tb_entries =
688             (struct  Dwarf_Hash_Table_Entry_s *)
689             _dwarf_get_alloc(dbg, DW_DLA_HASH_TABLE_ENTRY,
690             newht.tb_table_entry_count);
691         if (!newht.tb_entries) {
692             return DW_DLV_NO_ENTRY;
693         }
694         /*  Copy the existing entries to the new table,
695             rehashing each.  */
696         copy_abbrev_table_to_new_table(hash_table_base, &newht);
697         /*  Dealloc only the entries hash table array, not the lists
698             of things pointed to by a hash table entry array. */
699         dwarf_dealloc(dbg, hash_table_base->tb_entries,
700             DW_DLA_HASH_TABLE_ENTRY);
701         hash_table_base->tb_entries = 0;
702         /*  Now overwrite the existing table descriptor with
703             the new, newly valid, contents. */
704         *hash_table_base = newht;
705     } /* Else is ok as is, add entry */
706 
707     hashable_val = code;
708     hash_num = hashable_val %
709         hash_table_base->tb_table_entry_count;
710     entry_base = hash_table_base->tb_entries;
711     entry_cur  = entry_base + hash_num;
712 
713     /* Determine if the 'code' is the list of synonyms already. */
714     for (hash_abbrev_entry = entry_cur->at_head;
715         hash_abbrev_entry != NULL && hash_abbrev_entry->abl_code != code;
716         hash_abbrev_entry = hash_abbrev_entry->abl_next);
717     if (hash_abbrev_entry) {
718         /*  This returns a pointer to an abbrev
719             list entry, not the list itself. */
720         *list_out = hash_abbrev_entry;
721         return DW_DLV_OK;
722     }
723 
724     if (cu_context->cc_last_abbrev_ptr) {
725         abbrev_ptr = cu_context->cc_last_abbrev_ptr;
726         end_abbrev_ptr = cu_context->cc_last_abbrev_endptr;
727     } else {
728         /*  This is ok because cc_abbrev_offset includes DWP
729             offset if appropriate. */
730         abbrev_ptr = dbg->de_debug_abbrev.dss_data +
731             cu_context->cc_abbrev_offset;
732 
733         if (cu_context->cc_dwp_offsets.pcu_type)  {
734             /*  In a DWP the abbrevs
735                 for this context are known quite precisely. */
736             Dwarf_Unsigned size = 0;
737 
738             /*  Ignore the offset returned.
739                 Already in cc_abbrev_offset. */
740             _dwarf_get_dwp_extra_offset(
741                 &cu_context->cc_dwp_offsets,
742                 DW_SECT_ABBREV,&size);
743             /*  ASSERT: size != 0 */
744             end_abbrev_ptr = abbrev_ptr + size;
745         } else {
746             end_abbrev_ptr = dbg->de_debug_abbrev.dss_data +
747                 dbg->de_debug_abbrev.dss_size;
748         }
749     }
750 
751     /*  End of abbrev's as we are past the end entirely.
752         This can happen,though it seems wrong.
753         Or we are at the end of the data block,
754         which we also take as
755         meaning done with abbrevs for this CU.
756         An abbreviations table
757         is supposed to end with a zero byte.
758         Not ended by end of data block.
759         But we are allowing what is possibly a bit
760         more flexible end policy here. */
761     if (abbrev_ptr >= end_abbrev_ptr) {
762         return DW_DLV_NO_ENTRY;
763     }
764     /*  End of abbrev's for this cu, since abbrev code
765         is 0. */
766     if (*abbrev_ptr == 0) {
767         return DW_DLV_NO_ENTRY;
768     }
769 
770     do {
771         unsigned new_hashable_val = 0;
772         Dwarf_Off  abb_goff = 0;
773         Dwarf_Unsigned atcount = 0;
774         Dwarf_Byte_Ptr abbrev_ptr2 = 0;
775         int res = 0;
776 
777         abb_goff = abbrev_ptr - dbg->de_debug_abbrev.dss_data;
778         DECODE_LEB128_UWORD_CK(abbrev_ptr, abbrev_code,
779             dbg,error,end_abbrev_ptr);
780         DECODE_LEB128_UWORD_CK(abbrev_ptr, abbrev_tag,
781             dbg,error,end_abbrev_ptr);
782         if (abbrev_tag > DW_TAG_hi_user) {
783             return _dwarf_format_TAG_err_msg(dbg,
784                 abbrev_tag,"DW_DLE_TAG_CORRUPT",
785                 error);
786         }
787         if (abbrev_ptr >= end_abbrev_ptr) {
788             _dwarf_error(dbg, error, DW_DLE_ABBREV_OFF_END);
789             return DW_DLV_ERROR;
790         }
791         inner_list_entry = (Dwarf_Abbrev_List)
792             _dwarf_get_alloc(cu_context->cc_dbg,
793                 DW_DLA_ABBREV_LIST, 1);
794         if (inner_list_entry == NULL) {
795             _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
796             return DW_DLV_ERROR;
797         }
798 
799         new_hashable_val = abbrev_code;
800         hash_num = new_hashable_val %
801             hash_table_base->tb_table_entry_count;
802         inner_hash_entry = entry_base + hash_num;
803         /* Move_entry_to_new_hash */
804         inner_list_entry->abl_next = inner_hash_entry->at_head;
805         inner_hash_entry->at_head = inner_list_entry;
806 
807         hash_table_base->tb_total_abbrev_count++;
808 
809         inner_list_entry->abl_code = abbrev_code;
810         inner_list_entry->abl_tag = abbrev_tag;
811         inner_list_entry->abl_has_child = *(abbrev_ptr++);
812         inner_list_entry->abl_abbrev_ptr = abbrev_ptr;
813         inner_list_entry->abl_goffset =  abb_goff;
814         hash_table_base->tb_total_abbrev_count++;
815 
816         /*  Cycle thru the abbrev content,
817             ignoring the content except
818             to find the end of the content. */
819         res = _dwarf_count_abbrev_entries(dbg,abbrev_ptr,
820             end_abbrev_ptr,&atcount,&abbrev_ptr2,error);
821         if (res != DW_DLV_OK) {
822             return res;
823         }
824         abbrev_ptr = abbrev_ptr2;
825         inner_list_entry->abl_count = atcount;
826     } while ((abbrev_ptr < end_abbrev_ptr) &&
827         *abbrev_ptr != 0 && abbrev_code != code);
828 
829     cu_context->cc_last_abbrev_ptr = abbrev_ptr;
830     cu_context->cc_last_abbrev_endptr = end_abbrev_ptr;
831     if(abbrev_code == code) {
832         *list_out = inner_list_entry;
833         return DW_DLV_OK;
834     }
835     /*  We cannot find an abbrev_code  matching code.
836         ERROR will be declared eventually.
837         Might be better to declare
838         specific errors here? */
839     return DW_DLV_NO_ENTRY;
840 }
841 
842 
843 /*
844     We check that:
845         areaptr <= strptr.
846         a NUL byte (*p) exists at p < end.
847     and return DW_DLV_ERROR if a check fails.
848 
849     de_assume_string_in_bounds
850 */
851 int
852 _dwarf_check_string_valid(Dwarf_Debug dbg,void *areaptr,
853     void *strptr, void *areaendptr,
854     int suggested_error,
855     Dwarf_Error*error)
856 {
857     Dwarf_Small *start = areaptr;
858     Dwarf_Small *p = strptr;
859     Dwarf_Small *end = areaendptr;
860 
861     if (p < start) {
862         _dwarf_error(dbg,error,suggested_error);
863         return DW_DLV_ERROR;
864     }
865     if (p >= end) {
866         _dwarf_error(dbg,error,suggested_error);
867         return DW_DLV_ERROR;
868     }
869     if (dbg->de_assume_string_in_bounds) {
870         /* This NOT the default. But folks can choose
871             to live dangerously and just assume strings ok. */
872         return DW_DLV_OK;
873     }
874     while (p < end) {
875         if (*p == 0) {
876             return DW_DLV_OK;
877         }
878         ++p;
879     }
880     _dwarf_error(dbg,error,DW_DLE_STRING_NOT_TERMINATED);
881     return DW_DLV_ERROR;
882 }
883 
884 
885 /*  Return non-zero if the start/end are not valid for the
886     die's section.
887     If pastend matches the dss_data+dss_size then
888     pastend is a pointer that cannot be dereferenced.
889     But we allow it as valid here, it is normal for
890     a pointer to point one-past-end in
891     various circumstances (one must
892     avoid dereferencing it, of course).
893     Return 0 if valid. Return 1 if invalid. */
894 int
895 _dwarf_reference_outside_section(Dwarf_Die die,
896     Dwarf_Small * startaddr,
897     Dwarf_Small * pastend)
898 {
899     Dwarf_Debug dbg = 0;
900     Dwarf_CU_Context contxt = 0;
901     struct Dwarf_Section_s *sec = 0;
902 
903     contxt = die->di_cu_context;
904     dbg = contxt->cc_dbg;
905     if (die->di_is_info) {
906         sec = &dbg->de_debug_info;
907     } else {
908         sec = &dbg->de_debug_types;
909     }
910     if (startaddr < sec->dss_data) {
911         return 1;
912     }
913     if (pastend > (sec->dss_data + sec->dss_size)) {
914         return 1;
915     }
916     return 0;
917 }
918 
919 
920 /*
921   A byte-swapping version of memcpy
922   for cross-endian use.
923   Only 2,4,8 should be lengths passed in.
924 */
925 void
926 _dwarf_memcpy_noswap_bytes(void *s1, const void *s2, unsigned long len)
927 {
928     memcpy(s1,s2,(size_t)len);
929     return;
930 }
931 void
932 _dwarf_memcpy_swap_bytes(void *s1, const void *s2, unsigned long len)
933 {
934     unsigned char *targ = (unsigned char *) s1;
935     const unsigned char *src = (const unsigned char *) s2;
936 
937     if (len == 4) {
938         targ[3] = src[0];
939         targ[2] = src[1];
940         targ[1] = src[2];
941         targ[0] = src[3];
942     } else if (len == 8) {
943         targ[7] = src[0];
944         targ[6] = src[1];
945         targ[5] = src[2];
946         targ[4] = src[3];
947         targ[3] = src[4];
948         targ[2] = src[5];
949         targ[1] = src[6];
950         targ[0] = src[7];
951     } else if (len == 2) {
952         targ[1] = src[0];
953         targ[0] = src[1];
954     }
955 /* should NOT get below here: is not the intended use */
956     else if (len == 1) {
957         targ[0] = src[0];
958     } else {
959         memcpy(s1, s2, (size_t)len);
960     }
961     return;
962 }
963 
964 
965 /*  This calculation used to be sprinkled all over.
966     Now brought to one place.
967 
968     We try to accurately compute the size of a cu header
969     given a known cu header location ( an offset in .debug_info
970     or debug_types).  */
971 /* ARGSUSED */
972 
973 int
974 _dwarf_length_of_cu_header(Dwarf_Debug dbg,
975     Dwarf_Unsigned offset,
976     Dwarf_Bool is_info,
977     Dwarf_Unsigned *area_length_out,
978     Dwarf_Error *error)
979 {
980     int local_length_size = 0;
981     int local_extension_size = 0;
982     Dwarf_Half version = 0;
983     Dwarf_Unsigned length = 0;
984     Dwarf_Unsigned final_size = 0;
985     Dwarf_Small *section_start =
986         is_info? dbg->de_debug_info.dss_data:
987             dbg->de_debug_types.dss_data;
988     Dwarf_Small *cuptr = section_start + offset;
989     Dwarf_Unsigned section_length =
990         is_info? dbg->de_debug_info.dss_size:
991             dbg->de_debug_types.dss_size;
992     Dwarf_Small * section_end_ptr =
993         section_start + section_length;
994 
995     READ_AREA_LENGTH_CK(dbg, length, Dwarf_Unsigned,
996         cuptr, local_length_size, local_extension_size,
997         error,section_length,section_end_ptr);
998 
999     READ_UNALIGNED_CK(dbg, version, Dwarf_Half,
1000         cuptr, DWARF_HALF_SIZE,error,section_end_ptr);
1001     cuptr += DWARF_HALF_SIZE;
1002     if (version == 5) {
1003         Dwarf_Ubyte unit_type = 0;
1004 
1005         READ_UNALIGNED_CK(dbg, unit_type, Dwarf_Ubyte,
1006             cuptr, sizeof(Dwarf_Ubyte),error,section_end_ptr);
1007         switch (unit_type) {
1008         case DW_UT_compile:
1009             final_size = local_extension_size +
1010                 local_length_size  + /* Size of cu length field. */
1011                 DWARF_HALF_SIZE + /* Size of version stamp field. */
1012                 sizeof(Dwarf_Small)+ /* Size of  unit type field. */
1013                 sizeof(Dwarf_Small)+ /* Size of address size field. */
1014                 local_length_size ;  /* Size of abbrev offset field. */
1015             break;
1016         case DW_UT_type:
1017         case DW_UT_partial:
1018         case DW_UT_skeleton:
1019         case DW_UT_split_compile:
1020         case DW_UT_split_type:
1021         default:
1022             _dwarf_error(dbg,error,DW_DLE_UNIT_TYPE_NOT_HANDLED);
1023             return DW_DLV_ERROR;
1024         }
1025     } else if (version == 4) {
1026         final_size = local_extension_size +
1027             local_length_size  +  /* Size of cu length field. */
1028             DWARF_HALF_SIZE +  /* Size of version stamp field. */
1029             local_length_size  +  /* Size of abbrev offset field. */
1030             sizeof(Dwarf_Small);  /* Size of address size field. */
1031         if (!is_info) {
1032             final_size +=
1033             /* type signature size */
1034             sizeof (Dwarf_Sig8) +
1035             /* type offset size */
1036             local_length_size;
1037         }
1038     } else if (version < 4) {
1039         final_size = local_extension_size +
1040             local_length_size  +
1041             DWARF_HALF_SIZE +
1042             local_length_size  +
1043             sizeof(Dwarf_Small);  /* Size of address size field. */
1044     }
1045 
1046     *area_length_out = final_size;
1047     return DW_DLV_OK;
1048 }
1049 
1050 /*  Pretend we know nothing about the CU
1051     and just roughly compute the result.  */
1052 Dwarf_Unsigned
1053 _dwarf_length_of_cu_header_simple(Dwarf_Debug dbg,
1054     Dwarf_Bool dinfo)
1055 {
1056     Dwarf_Unsigned finalsize = 0;
1057     finalsize =  dbg->de_length_size + /* Size of cu length field. */
1058         DWARF_HALF_SIZE +    /* Size of version stamp field. */
1059         dbg->de_length_size +   /* Size of abbrev offset field. */
1060         sizeof(Dwarf_Small);    /* Size of address size field. */
1061     if (!dinfo) {
1062         finalsize +=
1063             /* type signature size */
1064             sizeof (Dwarf_Sig8) +
1065             /* type offset size */
1066             dbg->de_length_size;
1067     }
1068     return finalsize;
1069 }
1070 
1071 /*  Now that we delay loading .debug_info, we need to do the
1072     load in more places. So putting the load
1073     code in one place now instead of replicating it in multiple
1074     places.  */
1075 int
1076 _dwarf_load_debug_info(Dwarf_Debug dbg, Dwarf_Error * error)
1077 {
1078     int res = DW_DLV_ERROR;
1079     if (dbg->de_debug_info.dss_data) {
1080         return DW_DLV_OK;
1081     }
1082     res = _dwarf_load_section(dbg, &dbg->de_debug_abbrev,error);
1083     if (res != DW_DLV_OK) {
1084         return res;
1085     }
1086     res = _dwarf_load_section(dbg, &dbg->de_debug_info, error);
1087     if (res != DW_DLV_OK) {
1088         return res;
1089     }
1090     /*  debug_info won't be meaningful without
1091         .debug_rnglists and .debug_rnglists if there
1092         is one or both such sections. */
1093     res = dwarf_load_rnglists(dbg,0,error);
1094     if (res == DW_DLV_ERROR) {
1095         return res;
1096     }
1097     res = dwarf_load_loclists(dbg,0,error);
1098     if (res == DW_DLV_ERROR) {
1099         return res;
1100     }
1101     return DW_DLV_OK;
1102 }
1103 int
1104 _dwarf_load_debug_types(Dwarf_Debug dbg, Dwarf_Error * error)
1105 {
1106     int res = DW_DLV_ERROR;
1107     if (dbg->de_debug_types.dss_data) {
1108         return DW_DLV_OK;
1109     }
1110     res = _dwarf_load_section(dbg, &dbg->de_debug_abbrev,error);
1111     if (res != DW_DLV_OK) {
1112         return res;
1113     }
1114     res = _dwarf_load_section(dbg, &dbg->de_debug_types, error);
1115     return res;
1116 }
1117 void
1118 _dwarf_free_abbrev_hash_table_contents(Dwarf_Debug dbg,
1119     Dwarf_Hash_Table hash_table)
1120 {
1121     /*  A Hash Table is an array with tb_table_entry_count struct
1122         Dwarf_Hash_Table_s entries in the array. */
1123     unsigned hashnum = 0;
1124     if(!hash_table) {
1125         /*  Not fully set up yet. There is nothing to do. */
1126         return;
1127     }
1128     if (!hash_table->tb_entries) {
1129         /*  Not fully set up yet. There is nothing to do. */
1130         return;
1131     }
1132     for (; hashnum < hash_table->tb_table_entry_count; ++hashnum) {
1133         struct Dwarf_Abbrev_List_s *abbrev = 0;
1134         struct Dwarf_Abbrev_List_s *nextabbrev = 0;
1135         struct  Dwarf_Hash_Table_Entry_s *tb =
1136             &hash_table->tb_entries[hashnum];
1137 
1138         abbrev = tb->at_head;
1139         for (; abbrev; abbrev = nextabbrev) {
1140             nextabbrev = abbrev->abl_next;
1141             abbrev->abl_next = 0;
1142             dwarf_dealloc(dbg, abbrev, DW_DLA_ABBREV_LIST);
1143         }
1144         tb->at_head = 0;
1145     }
1146     /* Frees all the entries at once: an array. */
1147     dwarf_dealloc(dbg,hash_table->tb_entries,DW_DLA_HASH_TABLE_ENTRY);
1148     hash_table->tb_entries = 0;
1149 }
1150 
1151 /*
1152     If no die provided the size value returned might be wrong.
1153     If different compilation units have different address sizes
1154     this may not give the correct value in all contexts if the die
1155     pointer is NULL.
1156     If the Elf offset size != address_size
1157     (for example if address_size = 4 but recorded in elf64 object)
1158     this may not give the correct value in all contexts if the die
1159     pointer is NULL.
1160     If the die pointer is non-NULL (in which case it must point to
1161     a valid DIE) this will return the correct size.
1162 */
1163 int
1164 _dwarf_get_address_size(Dwarf_Debug dbg, Dwarf_Die die)
1165 {
1166     Dwarf_CU_Context context = 0;
1167     Dwarf_Half addrsize = 0;
1168     if (!die) {
1169         return dbg->de_pointer_size;
1170     }
1171     context = die->di_cu_context;
1172     addrsize = context->cc_address_size;
1173     return addrsize;
1174 }
1175 
1176 /* Encode val as an unsigned LEB128. */
1177 int dwarf_encode_leb128(Dwarf_Unsigned val, int *nbytes,
1178     char *space, int splen)
1179 {
1180     /* Encode val as an unsigned LEB128. */
1181     return _dwarf_pro_encode_leb128_nm(val,nbytes,space,splen);
1182 }
1183 
1184 /* Encode val as a signed LEB128. */
1185 int dwarf_encode_signed_leb128(Dwarf_Signed val, int *nbytes,
1186     char *space, int splen)
1187 {
1188     /* Encode val as a signed LEB128. */
1189     return _dwarf_pro_encode_signed_leb128_nm(val,nbytes,space,splen);
1190 }
1191 
1192 
1193 struct  Dwarf_Printf_Callback_Info_s
1194 dwarf_register_printf_callback( Dwarf_Debug dbg,
1195     struct  Dwarf_Printf_Callback_Info_s * newvalues)
1196 {
1197     struct  Dwarf_Printf_Callback_Info_s oldval = dbg->de_printf_callback;
1198     if (!newvalues) {
1199         return oldval;
1200     }
1201     if( newvalues->dp_buffer_user_provided) {
1202         if( oldval.dp_buffer_user_provided) {
1203             /* User continues to control the buffer. */
1204             dbg->de_printf_callback = *newvalues;
1205         }else {
1206             /*  Switch from our control of buffer to user
1207                 control.  */
1208             free(oldval.dp_buffer);
1209             oldval.dp_buffer = 0;
1210             dbg->de_printf_callback = *newvalues;
1211         }
1212     } else if (oldval.dp_buffer_user_provided){
1213         /* Switch from user control to our control */
1214         dbg->de_printf_callback = *newvalues;
1215         dbg->de_printf_callback.dp_buffer_len = 0;
1216         dbg->de_printf_callback.dp_buffer= 0;
1217     } else {
1218         /* User does not control the buffer. */
1219         dbg->de_printf_callback = *newvalues;
1220         dbg->de_printf_callback.dp_buffer_len =
1221             oldval.dp_buffer_len;
1222         dbg->de_printf_callback.dp_buffer =
1223             oldval.dp_buffer;
1224     }
1225     return oldval;
1226 }
1227 
1228 
1229 
1230 /* No varargs required */
1231 int
1232 _dwarf_printf(Dwarf_Debug dbg,
1233     const char * data)
1234 {
1235     int nlen = 0;
1236     struct Dwarf_Printf_Callback_Info_s *bufdata =
1237         &dbg->de_printf_callback;
1238 
1239     dwarf_printf_callback_function_type func = bufdata->dp_fptr;
1240     if (!func) {
1241         return 0;
1242     }
1243     nlen =  strlen(data);
1244     func(bufdata->dp_user_pointer,data);
1245     return nlen;
1246 }
1247 
1248 /*  Often errs and errt point to the same Dwarf_Error,
1249     So exercise care.
1250     All the arguments MUST be non-null.*/
1251 void
1252 _dwarf_error_mv_s_to_t(Dwarf_Debug dbgs,Dwarf_Error *errs,
1253     Dwarf_Debug dbgt,Dwarf_Error *errt)
1254 {
1255     if (!errt || !errs) {
1256         return;
1257     }
1258     if (!dbgs || !dbgt) {
1259         return;
1260     }
1261     if(dbgs == dbgt) {
1262         if(errs != errt) {
1263             Dwarf_Error ers = *errs;
1264             *errs = 0;
1265             *errt = ers;
1266         }
1267     } else {
1268         /*  Do not stomp on the system errno
1269             variable if there is one! */
1270         int mydw_errno = dwarf_errno(*errs);
1271 
1272         dwarf_dealloc(dbgs,*errs, DW_DLA_ERROR);
1273         *errs = 0;
1274         _dwarf_error(dbgt,errt, mydw_errno);
1275     }
1276 }
1277 
1278 static int
1279 inthissection(struct Dwarf_Section_s *sec,Dwarf_Small *ptr)
1280 {
1281     if (!sec->dss_data) {
1282         return FALSE;
1283     }
1284     if (ptr < sec->dss_data ) {
1285         return FALSE;
1286     }
1287     if (ptr >= (sec->dss_data + sec->dss_size) ) {
1288         return FALSE;
1289     }
1290     return TRUE;
1291 }
1292 
1293 #define FINDSEC(m_s,m_p,n,st,l,e)    \
1294 do {                                 \
1295     if (inthissection((m_s),(m_p))) { \
1296         *(n) = (m_s)->dss_name;      \
1297         *(st)= (m_s)->dss_data;      \
1298         *(l) = (m_s)->dss_size;      \
1299         *(e) = (m_s)->dss_data + (m_s)->dss_size; \
1300         return DW_DLV_OK;            \
1301     }                                \
1302 } while (0)
1303 
1304 
1305 /* So we can know a section end even when we do not
1306     have the section info apriori  It's only
1307     needed for a subset of sections. */
1308 int
1309 _dwarf_what_section_are_we(Dwarf_Debug dbg,
1310     Dwarf_Small    *  our_pointer,
1311     const char     ** section_name_out,
1312     Dwarf_Small    ** sec_start_ptr_out,
1313     Dwarf_Unsigned *  sec_len_out,
1314     Dwarf_Small    ** sec_end_ptr_out,
1315     UNUSEDARG Dwarf_Error    *  error)
1316 {
1317     FINDSEC(&dbg->de_debug_info,
1318         our_pointer, section_name_out,
1319         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1320     FINDSEC(&dbg->de_debug_loc,
1321         our_pointer, section_name_out,
1322         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1323     FINDSEC(&dbg->de_debug_loclists,
1324         our_pointer, section_name_out,
1325         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1326     FINDSEC(&dbg->de_debug_rnglists,
1327         our_pointer, section_name_out,
1328         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1329     FINDSEC(&dbg->de_debug_addr,
1330         our_pointer, section_name_out,
1331         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1332     FINDSEC(&dbg->de_debug_line,
1333         our_pointer, section_name_out,
1334         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1335     FINDSEC(&dbg->de_debug_aranges,
1336         our_pointer, section_name_out,
1337         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1338     FINDSEC(&dbg->de_debug_macro,
1339         our_pointer, section_name_out,
1340         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1341     FINDSEC(&dbg->de_debug_ranges,
1342         our_pointer, section_name_out,
1343         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1344     FINDSEC(&dbg->de_debug_str_offsets,
1345         our_pointer, section_name_out,
1346         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1347     FINDSEC(&dbg->de_debug_addr,
1348         our_pointer, section_name_out,
1349         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1350     FINDSEC(&dbg->de_debug_pubtypes,
1351         our_pointer, section_name_out,
1352         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1353     FINDSEC(&dbg->de_debug_gdbindex,
1354         our_pointer, section_name_out,
1355         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1356     FINDSEC(&dbg->de_debug_abbrev,
1357         our_pointer, section_name_out,
1358         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1359     FINDSEC(&dbg->de_debug_cu_index,
1360         our_pointer, section_name_out,
1361         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1362     FINDSEC(&dbg->de_debug_tu_index,
1363         our_pointer, section_name_out,
1364         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1365     FINDSEC(&dbg->de_debug_line_str,
1366         our_pointer, section_name_out,
1367         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1368     FINDSEC(&dbg->de_debug_types,
1369         our_pointer, section_name_out,
1370         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1371     FINDSEC(&dbg->de_debug_sup,
1372         our_pointer, section_name_out,
1373         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1374     FINDSEC(&dbg->de_debug_frame,
1375         our_pointer, section_name_out,
1376         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1377     FINDSEC(&dbg->de_debug_frame_eh_gnu,
1378         our_pointer, section_name_out,
1379         sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1380     return DW_DLV_NO_ENTRY;
1381 }
1382 
1383 /* New September 2019. */
1384 int  dwarf_add_file_path(
1385     Dwarf_Debug dbg,
1386     const char *          file_name,
1387     Dwarf_Error* error)
1388 {
1389     if(!dbg || !file_name) {
1390         /*  Pretty much a disaster. Caller error. */
1391         _dwarf_error(dbg,error,DW_DLE_NULL_ARGS_DWARF_ADD_PATH);
1392         return DW_DLV_ERROR;
1393     }
1394     if (!dbg->de_path) {
1395         dbg->de_path = strdup(file_name);
1396     }
1397     return DW_DLV_OK;
1398 }
1399 
1400 /*  New late April 2020.
1401     All the crucial macros will surely
1402     need  to use wrapper code to ensure we do not leak
1403     memory at certain points.  */
1404 int
1405 _dwarf_read_unaligned_ck_wrapper(Dwarf_Debug dbg,
1406     Dwarf_Unsigned *out_value,
1407     Dwarf_Small *readfrom,
1408     int          readlength,
1409     Dwarf_Small *end_arange,
1410     Dwarf_Error *err)
1411 {
1412     Dwarf_Unsigned val = 0;
1413 
1414     READ_UNALIGNED_CK(dbg,val,Dwarf_Unsigned,
1415         readfrom,readlength,err,end_arange);
1416     *out_value = val;
1417     return DW_DLV_OK;
1418 }
1419 
1420 int
1421 _dwarf_read_area_length_ck_wrapper(Dwarf_Debug dbg,
1422     Dwarf_Unsigned *out_value,
1423     Dwarf_Small **readfrom,
1424     int    *  length_size_out,
1425     int    *  exten_size_out,
1426     Dwarf_Unsigned sectionlength,
1427     Dwarf_Small *endsection,
1428     Dwarf_Error *err)
1429 {
1430     Dwarf_Small *ptr = *readfrom;
1431     Dwarf_Unsigned val = 0;
1432     int length_size = 0;
1433     int exten_size = 0;
1434 
1435     READ_AREA_LENGTH_CK(dbg,val,Dwarf_Unsigned,
1436         ptr,length_size,exten_size,
1437         err,
1438         sectionlength,endsection);
1439     *readfrom = ptr;
1440     *out_value = val;
1441     *length_size_out = length_size;
1442     *exten_size_out = exten_size;
1443     return DW_DLV_OK;
1444 }
1445 /*  New March 2020 */
1446 /*  We need to increment startptr for the caller
1447     in these wrappers so the caller passes in
1448     wrappers return either DW_DLV_OK or DW_DLV_ERROR.
1449     Never DW_DLV_NO_ENTRY. */
1450 int
1451 _dwarf_leb128_uword_wrapper(Dwarf_Debug dbg,
1452     Dwarf_Small ** startptr,
1453     Dwarf_Small * endptr,
1454     Dwarf_Unsigned *out_value,
1455     Dwarf_Error * error)
1456 {
1457     Dwarf_Unsigned utmp2 = 0;
1458     Dwarf_Small * start = *startptr;
1459     DECODE_LEB128_UWORD_CK(start, utmp2,
1460         dbg,error,endptr);
1461     *out_value = utmp2;
1462     *startptr = start;
1463     return DW_DLV_OK;
1464 }
1465 int
1466 _dwarf_leb128_sword_wrapper(Dwarf_Debug dbg,
1467     Dwarf_Small ** startptr,
1468     Dwarf_Small * endptr,
1469     Dwarf_Signed *out_value,
1470     Dwarf_Error * error)
1471 {
1472     Dwarf_Small * start = *startptr;
1473     Dwarf_Signed stmp2 = 0;
1474     DECODE_LEB128_SWORD_CK(start, stmp2,
1475         dbg,error,endptr);
1476     *out_value = stmp2;
1477     *startptr = start;
1478     return DW_DLV_OK;
1479 }
1480