xref: /titanic_41/usr/src/tools/ctf/dwarf/common/dwarf_util.c (revision 07dc1947c362e187fb955d283b692f8769dd5def)
149d3bc91SRichard Lowe /*
2*07dc1947SRichard Lowe   Copyright (C) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
3*07dc1947SRichard Lowe   Portions Copyright (C) 2007-2010 David Anderson. All Rights Reserved.
449d3bc91SRichard Lowe 
549d3bc91SRichard Lowe   This program is free software; you can redistribute it and/or modify it
649d3bc91SRichard Lowe   under the terms of version 2.1 of the GNU Lesser General Public License
749d3bc91SRichard Lowe   as published by the Free Software Foundation.
849d3bc91SRichard Lowe 
949d3bc91SRichard Lowe   This program is distributed in the hope that it would be useful, but
1049d3bc91SRichard Lowe   WITHOUT ANY WARRANTY; without even the implied warranty of
1149d3bc91SRichard Lowe   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1249d3bc91SRichard Lowe 
1349d3bc91SRichard Lowe   Further, this software is distributed without any warranty that it is
1449d3bc91SRichard Lowe   free of the rightful claim of any third person regarding infringement
1549d3bc91SRichard Lowe   or the like.  Any license provided herein, whether implied or
1649d3bc91SRichard Lowe   otherwise, applies only to this software file.  Patent licenses, if
1749d3bc91SRichard Lowe   any, provided herein do not apply to combinations of this program with
1849d3bc91SRichard Lowe   other software, or any other product whatsoever.
1949d3bc91SRichard Lowe 
2049d3bc91SRichard Lowe   You should have received a copy of the GNU Lesser General Public
2149d3bc91SRichard Lowe   License along with this program; if not, write the Free Software
22*07dc1947SRichard Lowe   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
2349d3bc91SRichard Lowe   USA.
2449d3bc91SRichard Lowe 
25*07dc1947SRichard Lowe   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
2649d3bc91SRichard Lowe   Mountain View, CA 94043, or:
2749d3bc91SRichard Lowe 
2849d3bc91SRichard Lowe   http://www.sgi.com
2949d3bc91SRichard Lowe 
3049d3bc91SRichard Lowe   For further information regarding this notice, see:
3149d3bc91SRichard Lowe 
3249d3bc91SRichard Lowe   http://oss.sgi.com/projects/GenInfo/NoticeExplan
3349d3bc91SRichard Lowe 
3449d3bc91SRichard Lowe */
35*07dc1947SRichard Lowe /* The address of the Free Software Foundation is
36*07dc1947SRichard Lowe    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
37*07dc1947SRichard Lowe    Boston, MA 02110-1301, USA.
38*07dc1947SRichard Lowe    SGI has moved from the Crittenden Lane address.
39*07dc1947SRichard Lowe */
40*07dc1947SRichard Lowe 
41*07dc1947SRichard Lowe 
4249d3bc91SRichard Lowe 
4349d3bc91SRichard Lowe 
4449d3bc91SRichard Lowe 
4549d3bc91SRichard Lowe #include "config.h"
4649d3bc91SRichard Lowe #include "dwarf_incl.h"
4749d3bc91SRichard Lowe #include <stdio.h>
4849d3bc91SRichard Lowe #include "dwarf_die_deliv.h"
4949d3bc91SRichard Lowe 
5049d3bc91SRichard Lowe 
5149d3bc91SRichard Lowe 
5249d3bc91SRichard Lowe /*
5349d3bc91SRichard Lowe     Given a form, and a pointer to the bytes encoding
5449d3bc91SRichard Lowe     a value of that form, val_ptr, this function returns
5549d3bc91SRichard Lowe     the length, in bytes, of a value of that form.
5649d3bc91SRichard Lowe     When using this function, check for a return of 0
5749d3bc91SRichard Lowe     a recursive DW_FORM_INDIRECT value.
5849d3bc91SRichard Lowe */
5949d3bc91SRichard Lowe Dwarf_Unsigned
_dwarf_get_size_of_val(Dwarf_Debug dbg,Dwarf_Unsigned form,Dwarf_Half address_size,Dwarf_Small * val_ptr,int v_length_size)6049d3bc91SRichard Lowe _dwarf_get_size_of_val(Dwarf_Debug dbg,
6149d3bc91SRichard Lowe     Dwarf_Unsigned form,
62*07dc1947SRichard Lowe     Dwarf_Half address_size,
6349d3bc91SRichard Lowe     Dwarf_Small * val_ptr, int v_length_size)
6449d3bc91SRichard Lowe {
6549d3bc91SRichard Lowe     Dwarf_Unsigned length = 0;
6649d3bc91SRichard Lowe     Dwarf_Word leb128_length = 0;
6749d3bc91SRichard Lowe     Dwarf_Unsigned form_indirect = 0;
6849d3bc91SRichard Lowe     Dwarf_Unsigned ret_value = 0;
6949d3bc91SRichard Lowe 
7049d3bc91SRichard Lowe     switch (form) {
7149d3bc91SRichard Lowe 
7249d3bc91SRichard Lowe     default:                    /* Handles form = 0. */
7349d3bc91SRichard Lowe         return (form);
7449d3bc91SRichard Lowe 
7549d3bc91SRichard Lowe     case DW_FORM_addr:
76*07dc1947SRichard Lowe         if(address_size) {
77*07dc1947SRichard Lowe             return address_size;
78*07dc1947SRichard Lowe         }
79*07dc1947SRichard Lowe         /* This should never happen, address_size should be set. */
8049d3bc91SRichard Lowe         return (dbg->de_pointer_size);
8149d3bc91SRichard Lowe 
82*07dc1947SRichard Lowe     /* DWARF2 was wrong on the size of the attribute for
83*07dc1947SRichard Lowe        DW_FORM_ref_addr.  We assume compilers are using the
84*07dc1947SRichard Lowe        corrected DWARF3 text (for 32bit pointer target objects pointer and
85*07dc1947SRichard Lowe        offsets are the same size anyway). */
8649d3bc91SRichard Lowe     case DW_FORM_ref_addr:
8749d3bc91SRichard Lowe         return (v_length_size);
8849d3bc91SRichard Lowe 
8949d3bc91SRichard Lowe     case DW_FORM_block1:
9049d3bc91SRichard Lowe         return (*(Dwarf_Small *) val_ptr + 1);
9149d3bc91SRichard Lowe 
9249d3bc91SRichard Lowe     case DW_FORM_block2:
9349d3bc91SRichard Lowe         READ_UNALIGNED(dbg, ret_value, Dwarf_Unsigned,
9449d3bc91SRichard Lowe                        val_ptr, sizeof(Dwarf_Half));
9549d3bc91SRichard Lowe         return (ret_value + sizeof(Dwarf_Half));
9649d3bc91SRichard Lowe 
9749d3bc91SRichard Lowe     case DW_FORM_block4:
9849d3bc91SRichard Lowe         READ_UNALIGNED(dbg, ret_value, Dwarf_Unsigned,
9949d3bc91SRichard Lowe                        val_ptr, sizeof(Dwarf_ufixed));
10049d3bc91SRichard Lowe         return (ret_value + sizeof(Dwarf_ufixed));
10149d3bc91SRichard Lowe 
10249d3bc91SRichard Lowe 
10349d3bc91SRichard Lowe     case DW_FORM_data1:
10449d3bc91SRichard Lowe         return (1);
10549d3bc91SRichard Lowe 
10649d3bc91SRichard Lowe     case DW_FORM_data2:
10749d3bc91SRichard Lowe         return (2);
10849d3bc91SRichard Lowe 
10949d3bc91SRichard Lowe     case DW_FORM_data4:
11049d3bc91SRichard Lowe         return (4);
11149d3bc91SRichard Lowe 
11249d3bc91SRichard Lowe     case DW_FORM_data8:
11349d3bc91SRichard Lowe         return (8);
11449d3bc91SRichard Lowe 
11549d3bc91SRichard Lowe     case DW_FORM_string:
11649d3bc91SRichard Lowe         return (strlen((char *) val_ptr) + 1);
11749d3bc91SRichard Lowe 
11849d3bc91SRichard Lowe     case DW_FORM_block:
119*07dc1947SRichard Lowe     case DW_FORM_exprloc:
12049d3bc91SRichard Lowe         length = _dwarf_decode_u_leb128(val_ptr, &leb128_length);
12149d3bc91SRichard Lowe         return (length + leb128_length);
12249d3bc91SRichard Lowe 
123*07dc1947SRichard Lowe     case DW_FORM_flag_present:
124*07dc1947SRichard Lowe         return (0);
12549d3bc91SRichard Lowe     case DW_FORM_flag:
12649d3bc91SRichard Lowe         return (1);
12749d3bc91SRichard Lowe 
128*07dc1947SRichard Lowe     case DW_FORM_sec_offset:
129*07dc1947SRichard Lowe         /* If 32bit dwarf, is 4. Else is 64bit dwarf and is 8. */
130*07dc1947SRichard Lowe         return (v_length_size);
131*07dc1947SRichard Lowe 
13249d3bc91SRichard Lowe     case DW_FORM_ref_udata:
133*07dc1947SRichard Lowe         length = _dwarf_decode_u_leb128(val_ptr, &leb128_length);
13449d3bc91SRichard Lowe         return (leb128_length);
13549d3bc91SRichard Lowe 
13649d3bc91SRichard Lowe     case DW_FORM_indirect:
13749d3bc91SRichard Lowe         {
13849d3bc91SRichard Lowe             Dwarf_Word indir_len = 0;
13949d3bc91SRichard Lowe 
14049d3bc91SRichard Lowe             form_indirect = _dwarf_decode_u_leb128(val_ptr, &indir_len);
14149d3bc91SRichard Lowe             if (form_indirect == DW_FORM_indirect) {
14249d3bc91SRichard Lowe                 return (0);     /* We are in big trouble: The true form
14349d3bc91SRichard Lowe                                    of DW_FORM_indirect is
14449d3bc91SRichard Lowe                                    DW_FORM_indirect? Nonsense. Should
14549d3bc91SRichard Lowe                                    never happen. */
14649d3bc91SRichard Lowe             }
14749d3bc91SRichard Lowe             return (indir_len + _dwarf_get_size_of_val(dbg,
14849d3bc91SRichard Lowe                    form_indirect,
149*07dc1947SRichard Lowe                    address_size,
150*07dc1947SRichard Lowe                    val_ptr + indir_len,
15149d3bc91SRichard Lowe                    v_length_size));
15249d3bc91SRichard Lowe         }
15349d3bc91SRichard Lowe 
15449d3bc91SRichard Lowe     case DW_FORM_ref1:
15549d3bc91SRichard Lowe         return (1);
15649d3bc91SRichard Lowe 
15749d3bc91SRichard Lowe     case DW_FORM_ref2:
15849d3bc91SRichard Lowe         return (2);
15949d3bc91SRichard Lowe 
16049d3bc91SRichard Lowe     case DW_FORM_ref4:
16149d3bc91SRichard Lowe         return (4);
16249d3bc91SRichard Lowe 
16349d3bc91SRichard Lowe     case DW_FORM_ref8:
16449d3bc91SRichard Lowe         return (8);
16549d3bc91SRichard Lowe 
16649d3bc91SRichard Lowe     case DW_FORM_sdata:
16749d3bc91SRichard Lowe         _dwarf_decode_s_leb128(val_ptr, &leb128_length);
16849d3bc91SRichard Lowe         return (leb128_length);
16949d3bc91SRichard Lowe 
17049d3bc91SRichard Lowe     case DW_FORM_strp:
17149d3bc91SRichard Lowe         return (v_length_size);
17249d3bc91SRichard Lowe 
17349d3bc91SRichard Lowe     case DW_FORM_udata:
17449d3bc91SRichard Lowe         _dwarf_decode_u_leb128(val_ptr, &leb128_length);
17549d3bc91SRichard Lowe         return (leb128_length);
17649d3bc91SRichard Lowe     }
17749d3bc91SRichard Lowe }
17849d3bc91SRichard Lowe 
179*07dc1947SRichard Lowe /* We allow an arbitrary number of HT_MULTIPLE entries
180*07dc1947SRichard Lowe    before resizing.  It seems up to 20 or 30
181*07dc1947SRichard Lowe    would work nearly as well.
182*07dc1947SRichard Lowe    We could have a different resize multiple than 'resize now'
183*07dc1947SRichard Lowe    test multiple, but for now we don't do that.
184*07dc1947SRichard Lowe */
185*07dc1947SRichard Lowe #define HT_MULTIPLE 8
186*07dc1947SRichard Lowe 
187*07dc1947SRichard Lowe /* Copy the old entries, updating each to be in
188*07dc1947SRichard Lowe    a new list.  Don't delete anything. Leave the
189*07dc1947SRichard Lowe    htin with stale data. */
190*07dc1947SRichard Lowe static void
copy_abbrev_table_to_new_table(Dwarf_Hash_Table htin,Dwarf_Hash_Table htout)191*07dc1947SRichard Lowe copy_abbrev_table_to_new_table(Dwarf_Hash_Table htin,
192*07dc1947SRichard Lowe   Dwarf_Hash_Table htout)
193*07dc1947SRichard Lowe {
194*07dc1947SRichard Lowe     Dwarf_Hash_Table_Entry entry_in = htin->tb_entries;
195*07dc1947SRichard Lowe     unsigned entry_in_count = htin->tb_table_entry_count;
196*07dc1947SRichard Lowe     Dwarf_Hash_Table_Entry entry_out = htout->tb_entries;
197*07dc1947SRichard Lowe     unsigned entry_out_count = htout->tb_table_entry_count;
198*07dc1947SRichard Lowe     unsigned k = 0;
199*07dc1947SRichard Lowe     for ( ;  k < entry_in_count; ++k,++entry_in) {
200*07dc1947SRichard Lowe         Dwarf_Abbrev_List listent = entry_in->at_head;
201*07dc1947SRichard Lowe         Dwarf_Abbrev_List nextlistent = 0;
202*07dc1947SRichard Lowe 
203*07dc1947SRichard Lowe         for (  ; listent ; listent = nextlistent) {
204*07dc1947SRichard Lowe              unsigned newtmp = listent->ab_code;
205*07dc1947SRichard Lowe              unsigned newhash = newtmp%entry_out_count;
206*07dc1947SRichard Lowe              Dwarf_Hash_Table_Entry e;
207*07dc1947SRichard Lowe              nextlistent = listent->ab_next;
208*07dc1947SRichard Lowe              e = entry_out+newhash;
209*07dc1947SRichard Lowe              /* Move_entry_to_new_hash. This reverses the
210*07dc1947SRichard Lowe                 order of the entries, effectively, but
211*07dc1947SRichard Lowe                 that does not seem significant. */
212*07dc1947SRichard Lowe              listent->ab_next = e->at_head;
213*07dc1947SRichard Lowe              e->at_head = listent;
214*07dc1947SRichard Lowe 
215*07dc1947SRichard Lowe              htout->tb_total_abbrev_count++;
216*07dc1947SRichard Lowe         }
217*07dc1947SRichard Lowe     }
218*07dc1947SRichard Lowe }
21949d3bc91SRichard Lowe 
22049d3bc91SRichard Lowe /*
22149d3bc91SRichard Lowe     This function returns a pointer to a Dwarf_Abbrev_List_s
22249d3bc91SRichard Lowe     struct for the abbrev with the given code.  It puts the
22349d3bc91SRichard Lowe     struct on the appropriate hash table.  It also adds all
22449d3bc91SRichard Lowe     the abbrev between the last abbrev added and this one to
22549d3bc91SRichard Lowe     the hash table.  In other words, the .debug_abbrev section
22649d3bc91SRichard Lowe     is scanned sequentially from the top for an abbrev with
22749d3bc91SRichard Lowe     the given code.  All intervening abbrevs are also put
22849d3bc91SRichard Lowe     into the hash table.
22949d3bc91SRichard Lowe 
23049d3bc91SRichard Lowe     This function hashes the given code, and checks the chain
23149d3bc91SRichard Lowe     at that hash table entry to see if a Dwarf_Abbrev_List_s
23249d3bc91SRichard Lowe     with the given code exists.  If yes, it returns a pointer
23349d3bc91SRichard Lowe     to that struct.  Otherwise, it scans the .debug_abbrev
23449d3bc91SRichard Lowe     section from the last byte scanned for that CU till either
23549d3bc91SRichard Lowe     an abbrev with the given code is found, or an abbrev code
23649d3bc91SRichard Lowe     of 0 is read.  It puts Dwarf_Abbrev_List_s entries for all
23749d3bc91SRichard Lowe     abbrev's read till that point into the hash table.  The
23849d3bc91SRichard Lowe     hash table contains both a head pointer and a tail pointer
23949d3bc91SRichard Lowe     for each entry.
24049d3bc91SRichard Lowe 
241*07dc1947SRichard Lowe     While the lists can move and entries can be moved between
242*07dc1947SRichard Lowe     lists on reallocation, any given Dwarf_Abbrev_list entry
243*07dc1947SRichard Lowe     never moves once allocated, so the pointer is safe to return.
244*07dc1947SRichard Lowe 
24549d3bc91SRichard Lowe     Returns NULL on error.
24649d3bc91SRichard Lowe */
24749d3bc91SRichard Lowe Dwarf_Abbrev_List
_dwarf_get_abbrev_for_code(Dwarf_CU_Context cu_context,Dwarf_Unsigned code)248*07dc1947SRichard Lowe _dwarf_get_abbrev_for_code(Dwarf_CU_Context cu_context, Dwarf_Unsigned code)
24949d3bc91SRichard Lowe {
25049d3bc91SRichard Lowe     Dwarf_Debug dbg = cu_context->cc_dbg;
251*07dc1947SRichard Lowe     Dwarf_Hash_Table hash_table_base = cu_context->cc_abbrev_hash_table;
252*07dc1947SRichard Lowe     Dwarf_Hash_Table_Entry entry_base = 0;
253*07dc1947SRichard Lowe     Dwarf_Hash_Table_Entry entry_cur = 0;
254*07dc1947SRichard Lowe     Dwarf_Word hash_num = 0;
255*07dc1947SRichard Lowe     Dwarf_Unsigned abbrev_code = 0;
256*07dc1947SRichard Lowe     Dwarf_Unsigned abbrev_tag  = 0;
257*07dc1947SRichard Lowe     Dwarf_Unsigned attr_name = 0;
258*07dc1947SRichard Lowe     Dwarf_Unsigned attr_form = 0;
25949d3bc91SRichard Lowe 
260*07dc1947SRichard Lowe     Dwarf_Abbrev_List hash_abbrev_entry = 0;
261*07dc1947SRichard Lowe 
262*07dc1947SRichard Lowe     Dwarf_Abbrev_List inner_list_entry = 0;
263*07dc1947SRichard Lowe     Dwarf_Hash_Table_Entry inner_hash_entry = 0;
264*07dc1947SRichard Lowe 
265*07dc1947SRichard Lowe     Dwarf_Byte_Ptr abbrev_ptr = 0;
266*07dc1947SRichard Lowe     unsigned hashable_val;
267*07dc1947SRichard Lowe 
268*07dc1947SRichard Lowe     if ( !hash_table_base->tb_entries ) {
269*07dc1947SRichard Lowe          hash_table_base->tb_table_entry_count =  HT_MULTIPLE;
270*07dc1947SRichard Lowe          hash_table_base->tb_total_abbrev_count= 0;
271*07dc1947SRichard Lowe          hash_table_base->tb_entries =  _dwarf_get_alloc(dbg,
272*07dc1947SRichard Lowe             DW_DLA_HASH_TABLE_ENTRY,
273*07dc1947SRichard Lowe             hash_table_base->tb_table_entry_count);
274*07dc1947SRichard Lowe          if(! hash_table_base->tb_entries) {
275*07dc1947SRichard Lowe              return NULL;
276*07dc1947SRichard Lowe          }
277*07dc1947SRichard Lowe 
278*07dc1947SRichard Lowe     } else if (hash_table_base->tb_total_abbrev_count >
279*07dc1947SRichard Lowe           ( hash_table_base->tb_table_entry_count * HT_MULTIPLE) ) {
280*07dc1947SRichard Lowe         struct Dwarf_Hash_Table_s newht;
281*07dc1947SRichard Lowe         /* Effectively multiplies by >= HT_MULTIPLE */
282*07dc1947SRichard Lowe         newht.tb_table_entry_count =  hash_table_base->tb_total_abbrev_count;
283*07dc1947SRichard Lowe         newht.tb_total_abbrev_count = 0;
284*07dc1947SRichard Lowe         newht.tb_entries =  _dwarf_get_alloc(dbg,
285*07dc1947SRichard Lowe             DW_DLA_HASH_TABLE_ENTRY,
286*07dc1947SRichard Lowe             newht.tb_table_entry_count);
287*07dc1947SRichard Lowe 
288*07dc1947SRichard Lowe         if(! newht.tb_entries) {
289*07dc1947SRichard Lowe              return NULL;
290*07dc1947SRichard Lowe         }
291*07dc1947SRichard Lowe         /* Copy the existing entries to the new table,
292*07dc1947SRichard Lowe            rehashing each.
293*07dc1947SRichard Lowe         */
294*07dc1947SRichard Lowe         copy_abbrev_table_to_new_table(hash_table_base, &newht);
295*07dc1947SRichard Lowe         /* Dealloc only the entries hash table array, not the lists
296*07dc1947SRichard Lowe            of things pointed to by a hash table entry array. */
297*07dc1947SRichard Lowe         dwarf_dealloc(dbg, hash_table_base->tb_entries,DW_DLA_HASH_TABLE_ENTRY);
298*07dc1947SRichard Lowe         hash_table_base->tb_entries = 0;
299*07dc1947SRichard Lowe         /* Now overwrite the existing table descriptor with
300*07dc1947SRichard Lowe            the new, newly valid, contents. */
301*07dc1947SRichard Lowe         *hash_table_base = newht;
302*07dc1947SRichard Lowe     } /* Else is ok as is, add entry */
303*07dc1947SRichard Lowe 
304*07dc1947SRichard Lowe 
305*07dc1947SRichard Lowe     hashable_val = code;
306*07dc1947SRichard Lowe     hash_num = hashable_val %
307*07dc1947SRichard Lowe         hash_table_base->tb_table_entry_count;
308*07dc1947SRichard Lowe     entry_base = hash_table_base->tb_entries;
309*07dc1947SRichard Lowe     entry_cur  = entry_base + hash_num;
310*07dc1947SRichard Lowe 
311*07dc1947SRichard Lowe     /* Determine if the 'code' is the list of synonyms already. */
312*07dc1947SRichard Lowe     for (hash_abbrev_entry = entry_cur->at_head;
313*07dc1947SRichard Lowe          hash_abbrev_entry != NULL && hash_abbrev_entry->ab_code != code;
314*07dc1947SRichard Lowe          hash_abbrev_entry = hash_abbrev_entry->ab_next);
315*07dc1947SRichard Lowe     if (hash_abbrev_entry != NULL) {
316*07dc1947SRichard Lowe         /* This returns a pointer to an abbrev list entry, not
317*07dc1947SRichard Lowe            the list itself. */
318*07dc1947SRichard Lowe         return (hash_abbrev_entry);
319*07dc1947SRichard Lowe     }
32049d3bc91SRichard Lowe 
32149d3bc91SRichard Lowe     abbrev_ptr = cu_context->cc_last_abbrev_ptr != NULL ?
32249d3bc91SRichard Lowe         cu_context->cc_last_abbrev_ptr :
323*07dc1947SRichard Lowe         dbg->de_debug_abbrev.dss_data + cu_context->cc_abbrev_offset;
32449d3bc91SRichard Lowe 
32549d3bc91SRichard Lowe     /* End of abbrev's for this cu, since abbrev code is 0. */
32649d3bc91SRichard Lowe     if (*abbrev_ptr == 0) {
32749d3bc91SRichard Lowe         return (NULL);
32849d3bc91SRichard Lowe     }
32949d3bc91SRichard Lowe 
33049d3bc91SRichard Lowe     do {
331*07dc1947SRichard Lowe         unsigned new_hashable_val;
332*07dc1947SRichard Lowe         DECODE_LEB128_UWORD(abbrev_ptr, abbrev_code);
333*07dc1947SRichard Lowe         DECODE_LEB128_UWORD(abbrev_ptr, abbrev_tag);
33449d3bc91SRichard Lowe 
335*07dc1947SRichard Lowe         inner_list_entry = (Dwarf_Abbrev_List)
33649d3bc91SRichard Lowe             _dwarf_get_alloc(cu_context->cc_dbg, DW_DLA_ABBREV_LIST, 1);
337*07dc1947SRichard Lowe         if (inner_list_entry == NULL)
33849d3bc91SRichard Lowe             return (NULL);
33949d3bc91SRichard Lowe 
340*07dc1947SRichard Lowe         new_hashable_val = abbrev_code;
341*07dc1947SRichard Lowe         hash_num = new_hashable_val %
342*07dc1947SRichard Lowe             hash_table_base->tb_table_entry_count;
343*07dc1947SRichard Lowe         inner_hash_entry = entry_base + hash_num;
344*07dc1947SRichard Lowe         /* Move_entry_to_new_hash */
345*07dc1947SRichard Lowe         inner_list_entry->ab_next = inner_hash_entry->at_head;
346*07dc1947SRichard Lowe         inner_hash_entry->at_head = inner_list_entry;
34749d3bc91SRichard Lowe 
348*07dc1947SRichard Lowe         hash_table_base->tb_total_abbrev_count++;
34949d3bc91SRichard Lowe 
350*07dc1947SRichard Lowe         inner_list_entry->ab_code = abbrev_code;
351*07dc1947SRichard Lowe         inner_list_entry->ab_tag = abbrev_tag;
352*07dc1947SRichard Lowe         inner_list_entry->ab_has_child = *(abbrev_ptr++);
353*07dc1947SRichard Lowe         inner_list_entry->ab_abbrev_ptr = abbrev_ptr;
35449d3bc91SRichard Lowe 
355*07dc1947SRichard Lowe         /* Cycle thru the abbrev content, ignoring the content except
356*07dc1947SRichard Lowe            to find the end of the content. */
35749d3bc91SRichard Lowe         do {
358*07dc1947SRichard Lowe             DECODE_LEB128_UWORD(abbrev_ptr, attr_name);
359*07dc1947SRichard Lowe             DECODE_LEB128_UWORD(abbrev_ptr, attr_form);
36049d3bc91SRichard Lowe         } while (attr_name != 0 && attr_form != 0);
36149d3bc91SRichard Lowe 
36249d3bc91SRichard Lowe     } while (*abbrev_ptr != 0 && abbrev_code != code);
36349d3bc91SRichard Lowe 
36449d3bc91SRichard Lowe     cu_context->cc_last_abbrev_ptr = abbrev_ptr;
365*07dc1947SRichard Lowe     return (abbrev_code == code ? inner_list_entry : NULL);
36649d3bc91SRichard Lowe }
36749d3bc91SRichard Lowe 
36849d3bc91SRichard Lowe 
36949d3bc91SRichard Lowe /* return 1 if string ends before 'endptr' else
37049d3bc91SRichard Lowe ** return 0 meaning string is not properly terminated.
37149d3bc91SRichard Lowe ** Presumption is the 'endptr' pts to end of some dwarf section data.
37249d3bc91SRichard Lowe */
37349d3bc91SRichard Lowe int
_dwarf_string_valid(void * startptr,void * endptr)37449d3bc91SRichard Lowe _dwarf_string_valid(void *startptr, void *endptr)
37549d3bc91SRichard Lowe {
37649d3bc91SRichard Lowe 
37749d3bc91SRichard Lowe     char *start = startptr;
37849d3bc91SRichard Lowe     char *end = endptr;
37949d3bc91SRichard Lowe 
38049d3bc91SRichard Lowe     while (start < end) {
38149d3bc91SRichard Lowe         if (*start == 0) {
38249d3bc91SRichard Lowe             return 1;           /* OK! */
38349d3bc91SRichard Lowe         }
38449d3bc91SRichard Lowe         ++start;
38549d3bc91SRichard Lowe         ++end;
38649d3bc91SRichard Lowe     }
38749d3bc91SRichard Lowe     return 0;                   /* FAIL! bad string! */
38849d3bc91SRichard Lowe }
38949d3bc91SRichard Lowe 
39049d3bc91SRichard Lowe /*
39149d3bc91SRichard Lowe   A byte-swapping version of memcpy
39249d3bc91SRichard Lowe   for cross-endian use.
39349d3bc91SRichard Lowe   Only 2,4,8 should be lengths passed in.
39449d3bc91SRichard Lowe */
39549d3bc91SRichard Lowe void *
_dwarf_memcpy_swap_bytes(void * s1,const void * s2,size_t len)39649d3bc91SRichard Lowe _dwarf_memcpy_swap_bytes(void *s1, const void *s2, size_t len)
39749d3bc91SRichard Lowe {
39849d3bc91SRichard Lowe     void *orig_s1 = s1;
39949d3bc91SRichard Lowe     unsigned char *targ = (unsigned char *) s1;
40049d3bc91SRichard Lowe     unsigned char *src = (unsigned char *) s2;
40149d3bc91SRichard Lowe 
40249d3bc91SRichard Lowe     if (len == 4) {
40349d3bc91SRichard Lowe         targ[3] = src[0];
40449d3bc91SRichard Lowe         targ[2] = src[1];
40549d3bc91SRichard Lowe         targ[1] = src[2];
40649d3bc91SRichard Lowe         targ[0] = src[3];
40749d3bc91SRichard Lowe     } else if (len == 8) {
40849d3bc91SRichard Lowe         targ[7] = src[0];
40949d3bc91SRichard Lowe         targ[6] = src[1];
41049d3bc91SRichard Lowe         targ[5] = src[2];
41149d3bc91SRichard Lowe         targ[4] = src[3];
41249d3bc91SRichard Lowe         targ[3] = src[4];
41349d3bc91SRichard Lowe         targ[2] = src[5];
41449d3bc91SRichard Lowe         targ[1] = src[6];
41549d3bc91SRichard Lowe         targ[0] = src[7];
41649d3bc91SRichard Lowe     } else if (len == 2) {
41749d3bc91SRichard Lowe         targ[1] = src[0];
41849d3bc91SRichard Lowe         targ[0] = src[1];
41949d3bc91SRichard Lowe     }
42049d3bc91SRichard Lowe /* should NOT get below here: is not the intended use */
42149d3bc91SRichard Lowe     else if (len == 1) {
42249d3bc91SRichard Lowe         targ[0] = src[0];
42349d3bc91SRichard Lowe     } else {
42449d3bc91SRichard Lowe         memcpy(s1, s2, len);
42549d3bc91SRichard Lowe     }
42649d3bc91SRichard Lowe 
42749d3bc91SRichard Lowe     return orig_s1;
42849d3bc91SRichard Lowe }
42949d3bc91SRichard Lowe 
43049d3bc91SRichard Lowe 
43149d3bc91SRichard Lowe /*
43249d3bc91SRichard Lowe   This calculation used to be sprinkled all over.
43349d3bc91SRichard Lowe   Now brought to one place.
43449d3bc91SRichard Lowe 
43549d3bc91SRichard Lowe   We try to accurately compute the size of a cu header
43649d3bc91SRichard Lowe   given a known cu header location ( an offset in .debug_info).
43749d3bc91SRichard Lowe 
43849d3bc91SRichard Lowe */
43949d3bc91SRichard Lowe /* ARGSUSED */
44049d3bc91SRichard Lowe Dwarf_Unsigned
_dwarf_length_of_cu_header(Dwarf_Debug dbg,Dwarf_Unsigned offset)44149d3bc91SRichard Lowe _dwarf_length_of_cu_header(Dwarf_Debug dbg, Dwarf_Unsigned offset)
44249d3bc91SRichard Lowe {
44349d3bc91SRichard Lowe     int local_length_size = 0;
44449d3bc91SRichard Lowe     int local_extension_size = 0;
44549d3bc91SRichard Lowe     Dwarf_Unsigned length = 0;
446*07dc1947SRichard Lowe     Dwarf_Small *cuptr = dbg->de_debug_info.dss_data + offset;
44749d3bc91SRichard Lowe 
44849d3bc91SRichard Lowe     READ_AREA_LENGTH(dbg, length, Dwarf_Unsigned,
44949d3bc91SRichard Lowe                      cuptr, local_length_size, local_extension_size);
45049d3bc91SRichard Lowe 
45149d3bc91SRichard Lowe     return local_extension_size +       /* initial extesion, if present
45249d3bc91SRichard Lowe                                          */
45349d3bc91SRichard Lowe         local_length_size +     /* Size of cu length field. */
45449d3bc91SRichard Lowe         sizeof(Dwarf_Half) +    /* Size of version stamp field. */
45549d3bc91SRichard Lowe         local_length_size +     /* Size of abbrev offset field. */
45649d3bc91SRichard Lowe         sizeof(Dwarf_Small);    /* Size of address size field. */
45749d3bc91SRichard Lowe 
45849d3bc91SRichard Lowe }
45949d3bc91SRichard Lowe 
46049d3bc91SRichard Lowe /*
46149d3bc91SRichard Lowe         Pretend we know nothing about the CU
46249d3bc91SRichard Lowe         and just roughly compute the result.
46349d3bc91SRichard Lowe */
46449d3bc91SRichard Lowe Dwarf_Unsigned
_dwarf_length_of_cu_header_simple(Dwarf_Debug dbg)46549d3bc91SRichard Lowe _dwarf_length_of_cu_header_simple(Dwarf_Debug dbg)
46649d3bc91SRichard Lowe {
46749d3bc91SRichard Lowe     return dbg->de_length_size +        /* Size of cu length field. */
46849d3bc91SRichard Lowe         sizeof(Dwarf_Half) +    /* Size of version stamp field. */
46949d3bc91SRichard Lowe         dbg->de_length_size +   /* Size of abbrev offset field. */
47049d3bc91SRichard Lowe         sizeof(Dwarf_Small);    /* Size of address size field. */
47149d3bc91SRichard Lowe }
47249d3bc91SRichard Lowe 
47349d3bc91SRichard Lowe /* Now that we delay loading .debug_info, we need to do the
47449d3bc91SRichard Lowe    load in more places. So putting the load
47549d3bc91SRichard Lowe    code in one place now instead of replicating it in multiple
47649d3bc91SRichard Lowe    places.
47749d3bc91SRichard Lowe 
47849d3bc91SRichard Lowe */
47949d3bc91SRichard Lowe int
_dwarf_load_debug_info(Dwarf_Debug dbg,Dwarf_Error * error)48049d3bc91SRichard Lowe _dwarf_load_debug_info(Dwarf_Debug dbg, Dwarf_Error * error)
48149d3bc91SRichard Lowe {
482*07dc1947SRichard Lowe     int res = DW_DLV_ERROR;
48349d3bc91SRichard Lowe 
484*07dc1947SRichard Lowe     /* Testing de_debug_info.dss_data allows us to avoid testing
485*07dc1947SRichard Lowe        de_debug_abbrev.dss_data.
486*07dc1947SRichard Lowe        One test instead of 2. .debug_info is useless
48749d3bc91SRichard Lowe        without .debug_abbrev. */
488*07dc1947SRichard Lowe     if (dbg->de_debug_info.dss_data) {
48949d3bc91SRichard Lowe         return DW_DLV_OK;
49049d3bc91SRichard Lowe     }
49149d3bc91SRichard Lowe 
492*07dc1947SRichard Lowe     res = _dwarf_load_section(dbg, &dbg->de_debug_abbrev,error);
49349d3bc91SRichard Lowe     if (res != DW_DLV_OK) {
49449d3bc91SRichard Lowe         return res;
49549d3bc91SRichard Lowe     }
496*07dc1947SRichard Lowe     res = _dwarf_load_section(dbg, &dbg->de_debug_info, error);
49749d3bc91SRichard Lowe     return res;
49849d3bc91SRichard Lowe 
49949d3bc91SRichard Lowe }
500*07dc1947SRichard Lowe void
_dwarf_free_abbrev_hash_table_contents(Dwarf_Debug dbg,Dwarf_Hash_Table hash_table)501*07dc1947SRichard Lowe _dwarf_free_abbrev_hash_table_contents(Dwarf_Debug dbg,Dwarf_Hash_Table hash_table)
502*07dc1947SRichard Lowe {
503*07dc1947SRichard Lowe     /* A Hash Table is an array with tb_table_entry_count struct
504*07dc1947SRichard Lowe        Dwarf_Hash_Table_s entries in the array. */
505*07dc1947SRichard Lowe     int hashnum = 0;
506*07dc1947SRichard Lowe     for (; hashnum < hash_table->tb_table_entry_count; ++hashnum) {
507*07dc1947SRichard Lowe         struct Dwarf_Abbrev_List_s *abbrev = 0;
508*07dc1947SRichard Lowe         struct Dwarf_Abbrev_List_s *nextabbrev = 0;
509*07dc1947SRichard Lowe         struct  Dwarf_Hash_Table_Entry_s *tb =  &hash_table->tb_entries[hashnum];
510*07dc1947SRichard Lowe 
511*07dc1947SRichard Lowe         abbrev = tb->at_head;
512*07dc1947SRichard Lowe         for (; abbrev; abbrev = nextabbrev) {
513*07dc1947SRichard Lowe             nextabbrev = abbrev->ab_next;
514*07dc1947SRichard Lowe             dwarf_dealloc(dbg, abbrev, DW_DLA_ABBREV_LIST);
515*07dc1947SRichard Lowe         }
516*07dc1947SRichard Lowe     }
517*07dc1947SRichard Lowe     /* Frees all the entries at once: an array. */
518*07dc1947SRichard Lowe     dwarf_dealloc(dbg,hash_table->tb_entries,DW_DLA_HASH_TABLE_ENTRY);
519*07dc1947SRichard Lowe }
520*07dc1947SRichard Lowe 
521*07dc1947SRichard Lowe /*
522*07dc1947SRichard Lowe     If no die provided the size value returned might be wrong.
523*07dc1947SRichard Lowe     If different compilation units have different address sizes
524*07dc1947SRichard Lowe     this may not give the correct value in all contexts if the die
525*07dc1947SRichard Lowe     pointer is NULL.
526*07dc1947SRichard Lowe     If the Elf offset size != address_size
527*07dc1947SRichard Lowe     (for example if address_size = 4 but recorded in elf64 object)
528*07dc1947SRichard Lowe     this may not give the correct value in all contexts if the die
529*07dc1947SRichard Lowe     pointer is NULL.
530*07dc1947SRichard Lowe     If the die pointer is non-NULL (in which case it must point to
531*07dc1947SRichard Lowe     a valid DIE) this will return the correct size.
532*07dc1947SRichard Lowe */
533*07dc1947SRichard Lowe int
_dwarf_get_address_size(Dwarf_Debug dbg,Dwarf_Die die)534*07dc1947SRichard Lowe _dwarf_get_address_size(Dwarf_Debug dbg, Dwarf_Die die)
535*07dc1947SRichard Lowe {
536*07dc1947SRichard Lowe     Dwarf_CU_Context context = 0;
537*07dc1947SRichard Lowe     Dwarf_Half addrsize = 0;
538*07dc1947SRichard Lowe     if(!die) {
539*07dc1947SRichard Lowe         return dbg->de_pointer_size;
540*07dc1947SRichard Lowe     }
541*07dc1947SRichard Lowe     context = die->di_cu_context;
542*07dc1947SRichard Lowe     addrsize = context->cc_address_size;
543*07dc1947SRichard Lowe     return addrsize;
544*07dc1947SRichard Lowe }
545*07dc1947SRichard Lowe 
546*07dc1947SRichard Lowe 
547*07dc1947SRichard Lowe 
548