2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3 * Copyright (c) 2009-2011 Kai Wang
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "_libdwarf.h"
30 ELFTC_VCSID("$Id: libdwarf_abbrev.c 3420 2016-02-27 02:14:05Z emaste $");
33 _dwarf_abbrev_add(Dwarf_CU cu, uint64_t entry, uint64_t tag, uint8_t children,
34 uint64_t aboff, Dwarf_Abbrev *abp, Dwarf_Error *error)
39 dbg = cu != NULL ? cu->cu_dbg : NULL;
41 if ((ab = malloc(sizeof(struct _Dwarf_Abbrev))) == NULL) {
42 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
43 return (DW_DLE_MEMORY);
46 /* Initialise the abbrev structure. */
49 ab->ab_children = children;
50 ab->ab_offset = aboff;
51 ab->ab_length = 0; /* fill in later. */
52 ab->ab_atnum = 0; /* fill in later. */
54 /* Initialise the list of attribute definitions. */
55 STAILQ_INIT(&ab->ab_attrdef);
57 /* Add the abbrev to the hash table of the compilation unit. */
59 HASH_ADD(ab_hh, cu->cu_abbrev_hash, ab_entry,
60 sizeof(ab->ab_entry), ab);
69 _dwarf_attrdef_add(Dwarf_Debug dbg, Dwarf_Abbrev ab, uint64_t attr,
70 uint64_t form, uint64_t adoff, Dwarf_AttrDef *adp, Dwarf_Error *error)
75 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
76 return (DW_DLE_ARGUMENT);
79 if ((ad = malloc(sizeof(struct _Dwarf_AttrDef))) == NULL) {
80 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
81 return (DW_DLE_MEMORY);
84 /* Initialise the attribute definition structure. */
87 ad->ad_offset = adoff;
89 /* Add the attribute definition to the list in the abbrev. */
90 STAILQ_INSERT_TAIL(&ab->ab_attrdef, ad, ad_next);
92 /* Increase number of attribute counter. */
102 _dwarf_abbrev_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Unsigned *offset,
103 Dwarf_Abbrev *abp, Dwarf_Error *error)
117 ds = _dwarf_find_section(dbg, ".debug_abbrev");
118 if (ds == NULL || *offset >= ds->ds_size)
119 return (DW_DLE_NO_ENTRY);
123 entry = _dwarf_read_uleb128(ds->ds_data, offset);
126 ret = _dwarf_abbrev_add(cu, entry, 0, 0, aboff, abp,
128 if (ret == DW_DLE_NONE) {
129 (*abp)->ab_length = 1;
134 tag = _dwarf_read_uleb128(ds->ds_data, offset);
135 children = dbg->read(ds->ds_data, offset, 1);
136 if ((ret = _dwarf_abbrev_add(cu, entry, tag, children, aboff,
137 abp, error)) != DW_DLE_NONE)
140 /* Parse attribute definitions. */
143 attr = _dwarf_read_uleb128(ds->ds_data, offset);
144 form = _dwarf_read_uleb128(ds->ds_data, offset);
146 if ((ret = _dwarf_attrdef_add(dbg, *abp, attr,
147 form, adoff, NULL, error)) != DW_DLE_NONE)
151 (*abp)->ab_length = *offset - aboff;
157 _dwarf_abbrev_find(Dwarf_CU cu, uint64_t entry, Dwarf_Abbrev *abp,
162 Dwarf_Unsigned offset;
166 return (DW_DLE_NO_ENTRY);
168 /* Check if the desired abbrev entry is already in the hash table. */
169 HASH_FIND(ab_hh, cu->cu_abbrev_hash, &entry, sizeof(entry), ab);
172 return (DW_DLE_NONE);
175 if (cu->cu_abbrev_loaded) {
176 return (DW_DLE_NO_ENTRY);
179 /* Load and search the abbrev table. */
180 ds = _dwarf_find_section(cu->cu_dbg, ".debug_abbrev");
182 return (DW_DLE_NO_ENTRY);
184 offset = cu->cu_abbrev_offset_cur;
185 while (offset < ds->ds_size) {
186 ret = _dwarf_abbrev_parse(cu->cu_dbg, cu, &offset, &ab, error);
187 if (ret != DW_DLE_NONE)
189 if (ab->ab_entry == entry) {
190 cu->cu_abbrev_offset_cur = offset;
192 return (DW_DLE_NONE);
194 if (ab->ab_entry == 0) {
195 cu->cu_abbrev_offset_cur = offset;
196 cu->cu_abbrev_loaded = 1;
201 return (DW_DLE_NO_ENTRY);
205 _dwarf_abbrev_cleanup(Dwarf_CU cu)
207 Dwarf_Abbrev ab, tab;
208 Dwarf_AttrDef ad, tad;
212 HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) {
213 HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab);
214 STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) {
215 STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef,
224 _dwarf_abbrev_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
232 cu = STAILQ_FIRST(&dbg->dbg_cu);
234 return (DW_DLE_NONE);
236 /* Create .debug_abbrev section. */
237 if ((ret = _dwarf_section_init(dbg, &ds, ".debug_abbrev", 0, error)) !=
241 for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) {
242 RCHECK(WRITE_ULEB128(ab->ab_entry));
243 RCHECK(WRITE_ULEB128(ab->ab_tag));
244 RCHECK(WRITE_VALUE(ab->ab_children, 1));
245 STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) {
246 RCHECK(WRITE_ULEB128(ad->ad_attrib));
247 RCHECK(WRITE_ULEB128(ad->ad_form));
249 /* Signal end of attribute spec list. */
250 RCHECK(WRITE_ULEB128(0));
251 RCHECK(WRITE_ULEB128(0));
253 /* End of abbreviation for this CU. */
254 RCHECK(WRITE_ULEB128(0));
256 /* Notify the creation of .debug_abbrev ELF section. */
257 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
259 return (DW_DLE_NONE);
263 _dwarf_section_free(dbg, &ds);