xref: /aosp_15_r20/external/elfutils/libdw/dwarf_getabbrev.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Get abbreviation at given offset.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2003, 2004, 2005, 2006, 2014, 2017 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker    Written by Ulrich Drepper <[email protected]>, 2003.
5*7304104dSAndroid Build Coastguard Worker 
6*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
7*7304104dSAndroid Build Coastguard Worker    it under the terms of either
8*7304104dSAndroid Build Coastguard Worker 
9*7304104dSAndroid Build Coastguard Worker      * the GNU Lesser General Public License as published by the Free
10*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 3 of the License, or (at
11*7304104dSAndroid Build Coastguard Worker        your option) any later version
12*7304104dSAndroid Build Coastguard Worker 
13*7304104dSAndroid Build Coastguard Worker    or
14*7304104dSAndroid Build Coastguard Worker 
15*7304104dSAndroid Build Coastguard Worker      * the GNU General Public License as published by the Free
16*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 2 of the License, or (at
17*7304104dSAndroid Build Coastguard Worker        your option) any later version
18*7304104dSAndroid Build Coastguard Worker 
19*7304104dSAndroid Build Coastguard Worker    or both in parallel, as here.
20*7304104dSAndroid Build Coastguard Worker 
21*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
22*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
23*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24*7304104dSAndroid Build Coastguard Worker    General Public License for more details.
25*7304104dSAndroid Build Coastguard Worker 
26*7304104dSAndroid Build Coastguard Worker    You should have received copies of the GNU General Public License and
27*7304104dSAndroid Build Coastguard Worker    the GNU Lesser General Public License along with this program.  If
28*7304104dSAndroid Build Coastguard Worker    not, see <http://www.gnu.org/licenses/>.  */
29*7304104dSAndroid Build Coastguard Worker 
30*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
31*7304104dSAndroid Build Coastguard Worker # include <config.h>
32*7304104dSAndroid Build Coastguard Worker #endif
33*7304104dSAndroid Build Coastguard Worker 
34*7304104dSAndroid Build Coastguard Worker #include <dwarf.h>
35*7304104dSAndroid Build Coastguard Worker #include "libdwP.h"
36*7304104dSAndroid Build Coastguard Worker 
37*7304104dSAndroid Build Coastguard Worker 
38*7304104dSAndroid Build Coastguard Worker Dwarf_Abbrev *
39*7304104dSAndroid Build Coastguard Worker internal_function
__libdw_getabbrev(Dwarf * dbg,struct Dwarf_CU * cu,Dwarf_Off offset,size_t * lengthp,Dwarf_Abbrev * result)40*7304104dSAndroid Build Coastguard Worker __libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu, Dwarf_Off offset,
41*7304104dSAndroid Build Coastguard Worker 		   size_t *lengthp, Dwarf_Abbrev *result)
42*7304104dSAndroid Build Coastguard Worker {
43*7304104dSAndroid Build Coastguard Worker   /* Don't fail if there is not .debug_abbrev section.  */
44*7304104dSAndroid Build Coastguard Worker   if (dbg->sectiondata[IDX_debug_abbrev] == NULL)
45*7304104dSAndroid Build Coastguard Worker     return NULL;
46*7304104dSAndroid Build Coastguard Worker 
47*7304104dSAndroid Build Coastguard Worker   if (offset >= dbg->sectiondata[IDX_debug_abbrev]->d_size)
48*7304104dSAndroid Build Coastguard Worker     {
49*7304104dSAndroid Build Coastguard Worker       __libdw_seterrno (DWARF_E_INVALID_OFFSET);
50*7304104dSAndroid Build Coastguard Worker       return NULL;
51*7304104dSAndroid Build Coastguard Worker     }
52*7304104dSAndroid Build Coastguard Worker 
53*7304104dSAndroid Build Coastguard Worker   const unsigned char *abbrevp
54*7304104dSAndroid Build Coastguard Worker     = (unsigned char *) dbg->sectiondata[IDX_debug_abbrev]->d_buf + offset;
55*7304104dSAndroid Build Coastguard Worker 
56*7304104dSAndroid Build Coastguard Worker   if (*abbrevp == '\0')
57*7304104dSAndroid Build Coastguard Worker     /* We are past the last entry.  */
58*7304104dSAndroid Build Coastguard Worker     return DWARF_END_ABBREV;
59*7304104dSAndroid Build Coastguard Worker 
60*7304104dSAndroid Build Coastguard Worker   /* 7.5.3 Abbreviations Tables
61*7304104dSAndroid Build Coastguard Worker 
62*7304104dSAndroid Build Coastguard Worker      [...] Each declaration begins with an unsigned LEB128 number
63*7304104dSAndroid Build Coastguard Worker      representing the abbreviation code itself.  [...]  The
64*7304104dSAndroid Build Coastguard Worker      abbreviation code is followed by another unsigned LEB128
65*7304104dSAndroid Build Coastguard Worker      number that encodes the entry's tag.  [...]
66*7304104dSAndroid Build Coastguard Worker 
67*7304104dSAndroid Build Coastguard Worker      [...] Following the tag encoding is a 1-byte value that
68*7304104dSAndroid Build Coastguard Worker      determines whether a debugging information entry using this
69*7304104dSAndroid Build Coastguard Worker      abbreviation has child entries or not. [...]
70*7304104dSAndroid Build Coastguard Worker 
71*7304104dSAndroid Build Coastguard Worker      [...] Finally, the child encoding is followed by a series of
72*7304104dSAndroid Build Coastguard Worker      attribute specifications. Each attribute specification
73*7304104dSAndroid Build Coastguard Worker      consists of two parts. The first part is an unsigned LEB128
74*7304104dSAndroid Build Coastguard Worker      number representing the attribute's name. The second part is
75*7304104dSAndroid Build Coastguard Worker      an unsigned LEB128 number representing the attribute's form.  */
76*7304104dSAndroid Build Coastguard Worker   const unsigned char *end = (dbg->sectiondata[IDX_debug_abbrev]->d_buf
77*7304104dSAndroid Build Coastguard Worker 			      + dbg->sectiondata[IDX_debug_abbrev]->d_size);
78*7304104dSAndroid Build Coastguard Worker   const unsigned char *start_abbrevp = abbrevp;
79*7304104dSAndroid Build Coastguard Worker   unsigned int code;
80*7304104dSAndroid Build Coastguard Worker   // We start off with abbrevp at offset, which is checked above.
81*7304104dSAndroid Build Coastguard Worker   get_uleb128 (code, abbrevp, end);
82*7304104dSAndroid Build Coastguard Worker 
83*7304104dSAndroid Build Coastguard Worker   /* Check whether this code is already in the hash table.  */
84*7304104dSAndroid Build Coastguard Worker   bool foundit = false;
85*7304104dSAndroid Build Coastguard Worker   Dwarf_Abbrev *abb = NULL;
86*7304104dSAndroid Build Coastguard Worker   if (cu == NULL
87*7304104dSAndroid Build Coastguard Worker       || (abb = Dwarf_Abbrev_Hash_find (&cu->abbrev_hash, code)) == NULL)
88*7304104dSAndroid Build Coastguard Worker     {
89*7304104dSAndroid Build Coastguard Worker       if (result == NULL)
90*7304104dSAndroid Build Coastguard Worker 	abb = libdw_typed_alloc (dbg, Dwarf_Abbrev);
91*7304104dSAndroid Build Coastguard Worker       else
92*7304104dSAndroid Build Coastguard Worker 	abb = result;
93*7304104dSAndroid Build Coastguard Worker     }
94*7304104dSAndroid Build Coastguard Worker   else
95*7304104dSAndroid Build Coastguard Worker     {
96*7304104dSAndroid Build Coastguard Worker       foundit = true;
97*7304104dSAndroid Build Coastguard Worker 
98*7304104dSAndroid Build Coastguard Worker       if (unlikely (abb->offset != offset))
99*7304104dSAndroid Build Coastguard Worker 	{
100*7304104dSAndroid Build Coastguard Worker 	  /* A duplicate abbrev code at a different offset,
101*7304104dSAndroid Build Coastguard Worker 	     that should never happen.  */
102*7304104dSAndroid Build Coastguard Worker 	invalid:
103*7304104dSAndroid Build Coastguard Worker 	  if (! foundit)
104*7304104dSAndroid Build Coastguard Worker 	    libdw_typed_unalloc (dbg, Dwarf_Abbrev);
105*7304104dSAndroid Build Coastguard Worker 	  __libdw_seterrno (DWARF_E_INVALID_DWARF);
106*7304104dSAndroid Build Coastguard Worker 	  return NULL;
107*7304104dSAndroid Build Coastguard Worker 	}
108*7304104dSAndroid Build Coastguard Worker 
109*7304104dSAndroid Build Coastguard Worker       /* If the caller doesn't need the length we are done.  */
110*7304104dSAndroid Build Coastguard Worker       if (lengthp == NULL)
111*7304104dSAndroid Build Coastguard Worker 	goto out;
112*7304104dSAndroid Build Coastguard Worker     }
113*7304104dSAndroid Build Coastguard Worker 
114*7304104dSAndroid Build Coastguard Worker   /* If there is already a value in the hash table we are going to
115*7304104dSAndroid Build Coastguard Worker      overwrite its content.  This must not be a problem, since the
116*7304104dSAndroid Build Coastguard Worker      content better be the same.  */
117*7304104dSAndroid Build Coastguard Worker   abb->code = code;
118*7304104dSAndroid Build Coastguard Worker   if (abbrevp >= end)
119*7304104dSAndroid Build Coastguard Worker     goto invalid;
120*7304104dSAndroid Build Coastguard Worker   get_uleb128 (abb->tag, abbrevp, end);
121*7304104dSAndroid Build Coastguard Worker   if (abbrevp + 1 >= end)
122*7304104dSAndroid Build Coastguard Worker     goto invalid;
123*7304104dSAndroid Build Coastguard Worker   abb->has_children = *abbrevp++ == DW_CHILDREN_yes;
124*7304104dSAndroid Build Coastguard Worker   abb->attrp = (unsigned char *) abbrevp;
125*7304104dSAndroid Build Coastguard Worker   abb->offset = offset;
126*7304104dSAndroid Build Coastguard Worker 
127*7304104dSAndroid Build Coastguard Worker   /* Skip over all the attributes and check rest of the abbrev is valid.  */
128*7304104dSAndroid Build Coastguard Worker   unsigned int attrname;
129*7304104dSAndroid Build Coastguard Worker   unsigned int attrform;
130*7304104dSAndroid Build Coastguard Worker   do
131*7304104dSAndroid Build Coastguard Worker     {
132*7304104dSAndroid Build Coastguard Worker       if (abbrevp >= end)
133*7304104dSAndroid Build Coastguard Worker 	goto invalid;
134*7304104dSAndroid Build Coastguard Worker       get_uleb128 (attrname, abbrevp, end);
135*7304104dSAndroid Build Coastguard Worker       if (abbrevp >= end)
136*7304104dSAndroid Build Coastguard Worker 	goto invalid;
137*7304104dSAndroid Build Coastguard Worker       get_uleb128 (attrform, abbrevp, end);
138*7304104dSAndroid Build Coastguard Worker       if (attrform == DW_FORM_implicit_const)
139*7304104dSAndroid Build Coastguard Worker 	{
140*7304104dSAndroid Build Coastguard Worker 	  int64_t formval __attribute__((__unused__));
141*7304104dSAndroid Build Coastguard Worker 	  if (abbrevp >= end)
142*7304104dSAndroid Build Coastguard Worker 	    goto invalid;
143*7304104dSAndroid Build Coastguard Worker 	  get_sleb128 (formval, abbrevp, end);
144*7304104dSAndroid Build Coastguard Worker 	}
145*7304104dSAndroid Build Coastguard Worker     }
146*7304104dSAndroid Build Coastguard Worker   while (attrname != 0 || attrform != 0);
147*7304104dSAndroid Build Coastguard Worker 
148*7304104dSAndroid Build Coastguard Worker   /* Return the length to the caller if she asked for it.  */
149*7304104dSAndroid Build Coastguard Worker   if (lengthp != NULL)
150*7304104dSAndroid Build Coastguard Worker     *lengthp = abbrevp - start_abbrevp;
151*7304104dSAndroid Build Coastguard Worker 
152*7304104dSAndroid Build Coastguard Worker   /* Add the entry to the hash table.  */
153*7304104dSAndroid Build Coastguard Worker   if (cu != NULL && ! foundit)
154*7304104dSAndroid Build Coastguard Worker     if (Dwarf_Abbrev_Hash_insert (&cu->abbrev_hash, abb->code, abb) == -1)
155*7304104dSAndroid Build Coastguard Worker       {
156*7304104dSAndroid Build Coastguard Worker 	/* The entry was already in the table, remove the one we just
157*7304104dSAndroid Build Coastguard Worker 	   created and get the one already inserted.  */
158*7304104dSAndroid Build Coastguard Worker 	libdw_typed_unalloc (dbg, Dwarf_Abbrev);
159*7304104dSAndroid Build Coastguard Worker 	abb = Dwarf_Abbrev_Hash_find (&cu->abbrev_hash, code);
160*7304104dSAndroid Build Coastguard Worker       }
161*7304104dSAndroid Build Coastguard Worker 
162*7304104dSAndroid Build Coastguard Worker  out:
163*7304104dSAndroid Build Coastguard Worker   return abb;
164*7304104dSAndroid Build Coastguard Worker }
165*7304104dSAndroid Build Coastguard Worker 
166*7304104dSAndroid Build Coastguard Worker 
167*7304104dSAndroid Build Coastguard Worker Dwarf_Abbrev *
dwarf_getabbrev(Dwarf_Die * die,Dwarf_Off offset,size_t * lengthp)168*7304104dSAndroid Build Coastguard Worker dwarf_getabbrev (Dwarf_Die *die, Dwarf_Off offset, size_t *lengthp)
169*7304104dSAndroid Build Coastguard Worker {
170*7304104dSAndroid Build Coastguard Worker   if (die == NULL || die->cu == NULL)
171*7304104dSAndroid Build Coastguard Worker     return NULL;
172*7304104dSAndroid Build Coastguard Worker 
173*7304104dSAndroid Build Coastguard Worker   Dwarf_CU *cu = die->cu;
174*7304104dSAndroid Build Coastguard Worker   Dwarf *dbg = cu->dbg;
175*7304104dSAndroid Build Coastguard Worker   Dwarf_Off abbrev_offset = cu->orig_abbrev_offset;
176*7304104dSAndroid Build Coastguard Worker   Elf_Data *data = dbg->sectiondata[IDX_debug_abbrev];
177*7304104dSAndroid Build Coastguard Worker   if (data == NULL)
178*7304104dSAndroid Build Coastguard Worker     return NULL;
179*7304104dSAndroid Build Coastguard Worker 
180*7304104dSAndroid Build Coastguard Worker   if (offset >= data->d_size - abbrev_offset)
181*7304104dSAndroid Build Coastguard Worker     {
182*7304104dSAndroid Build Coastguard Worker       __libdw_seterrno (DWARF_E_INVALID_OFFSET);
183*7304104dSAndroid Build Coastguard Worker       return NULL;
184*7304104dSAndroid Build Coastguard Worker     }
185*7304104dSAndroid Build Coastguard Worker 
186*7304104dSAndroid Build Coastguard Worker   return __libdw_getabbrev (dbg, cu, abbrev_offset + offset, lengthp, NULL);
187*7304104dSAndroid Build Coastguard Worker }
188