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