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);
67 _dwarf_attrdef_add(Dwarf_Debug dbg, Dwarf_Abbrev ab, uint64_t attr,
68 uint64_t form, uint64_t adoff, Dwarf_AttrDef *adp, Dwarf_Error *error)
73 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
74 return (DW_DLE_ARGUMENT);
77 if ((ad = malloc(sizeof(struct _Dwarf_AttrDef))) == NULL) {
78 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
79 return (DW_DLE_MEMORY);
82 /* Initialise the attribute definition structure. */
85 ad->ad_offset = adoff;
87 /* Add the attribute definition to the list in the abbrev. */
88 STAILQ_INSERT_TAIL(&ab->ab_attrdef, ad, ad_next);
90 /* Increase number of attribute counter. */
100 _dwarf_abbrev_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Unsigned *offset,
101 Dwarf_Abbrev *abp, Dwarf_Error *error)
115 ds = _dwarf_find_section(dbg, ".debug_abbrev");
116 if (ds == NULL || *offset >= ds->ds_size)
117 return (DW_DLE_NO_ENTRY);
121 entry = _dwarf_read_uleb128(ds->ds_data, offset);
124 ret = _dwarf_abbrev_add(cu, entry, 0, 0, aboff, abp,
126 if (ret == DW_DLE_NONE) {
127 (*abp)->ab_length = 1;
132 tag = _dwarf_read_uleb128(ds->ds_data, offset);
133 children = dbg->read(ds->ds_data, offset, 1);
134 if ((ret = _dwarf_abbrev_add(cu, entry, tag, children, aboff,
135 abp, error)) != DW_DLE_NONE)
138 /* Parse attribute definitions. */
141 attr = _dwarf_read_uleb128(ds->ds_data, offset);
142 form = _dwarf_read_uleb128(ds->ds_data, offset);
144 if ((ret = _dwarf_attrdef_add(dbg, *abp, attr,
145 form, adoff, NULL, error)) != DW_DLE_NONE)
149 (*abp)->ab_length = *offset - aboff;
155 _dwarf_abbrev_find(Dwarf_CU cu, uint64_t entry, Dwarf_Abbrev *abp,
160 Dwarf_Unsigned offset;
164 return (DW_DLE_NO_ENTRY);
166 /* Check if the desired abbrev entry is already in the hash table. */
167 HASH_FIND(ab_hh, cu->cu_abbrev_hash, &entry, sizeof(entry), ab);
170 return (DW_DLE_NONE);
173 if (cu->cu_abbrev_loaded) {
174 return (DW_DLE_NO_ENTRY);
177 /* Load and search the abbrev table. */
178 ds = _dwarf_find_section(cu->cu_dbg, ".debug_abbrev");
180 return (DW_DLE_NO_ENTRY);
182 offset = cu->cu_abbrev_offset_cur;
183 while (offset < ds->ds_size) {
184 ret = _dwarf_abbrev_parse(cu->cu_dbg, cu, &offset, &ab, error);
185 if (ret != DW_DLE_NONE)
187 if (ab->ab_entry == entry) {
188 cu->cu_abbrev_offset_cur = offset;
190 return (DW_DLE_NONE);
192 if (ab->ab_entry == 0) {
193 cu->cu_abbrev_offset_cur = offset;
194 cu->cu_abbrev_loaded = 1;
199 return (DW_DLE_NO_ENTRY);
203 _dwarf_abbrev_cleanup(Dwarf_CU cu)
205 Dwarf_Abbrev ab, tab;
206 Dwarf_AttrDef ad, tad;
210 HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) {
211 HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab);
212 STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) {
213 STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef,
222 _dwarf_abbrev_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
230 cu = STAILQ_FIRST(&dbg->dbg_cu);
232 return (DW_DLE_NONE);
234 /* Create .debug_abbrev section. */
235 if ((ret = _dwarf_section_init(dbg, &ds, ".debug_abbrev", 0, error)) !=
239 for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) {
240 RCHECK(WRITE_ULEB128(ab->ab_entry));
241 RCHECK(WRITE_ULEB128(ab->ab_tag));
242 RCHECK(WRITE_VALUE(ab->ab_children, 1));
243 STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) {
244 RCHECK(WRITE_ULEB128(ad->ad_attrib));
245 RCHECK(WRITE_ULEB128(ad->ad_form));
247 /* Signal end of attribute spec list. */
248 RCHECK(WRITE_ULEB128(0));
249 RCHECK(WRITE_ULEB128(0));
251 /* End of abbreviation for this CU. */
252 RCHECK(WRITE_ULEB128(0));
254 /* Notify the creation of .debug_abbrev ELF section. */
255 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
257 return (DW_DLE_NONE);
261 _dwarf_section_free(dbg, &ds);