1 /* $NetBSD: libdwarf_abbrev.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5 * Copyright (c) 2009-2011 Kai Wang
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "_libdwarf.h"
32 __RCSID("$NetBSD: libdwarf_abbrev.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
33 ELFTC_VCSID("Id: libdwarf_abbrev.c 2070 2011-10-27 03:05:32Z jkoshy ");
36 _dwarf_abbrev_add(Dwarf_CU cu
, uint64_t entry
, uint64_t tag
, uint8_t children
,
37 uint64_t aboff
, Dwarf_Abbrev
*abp
, Dwarf_Error
*error
)
42 dbg
= cu
!= NULL
? cu
->cu_dbg
: NULL
;
44 if ((ab
= malloc(sizeof(struct _Dwarf_Abbrev
))) == NULL
) {
45 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
46 return (DW_DLE_MEMORY
);
49 /* Initialise the abbrev structure. */
52 ab
->ab_children
= children
;
53 ab
->ab_offset
= aboff
;
54 ab
->ab_length
= 0; /* fill in later. */
55 ab
->ab_atnum
= 0; /* fill in later. */
57 /* Initialise the list of attribute definitions. */
58 STAILQ_INIT(&ab
->ab_attrdef
);
60 /* Add the abbrev to the hash table of the compilation unit. */
62 HASH_ADD(ab_hh
, cu
->cu_abbrev_hash
, ab_entry
,
63 sizeof(ab
->ab_entry
), ab
);
72 _dwarf_attrdef_add(Dwarf_Debug dbg
, Dwarf_Abbrev ab
, uint64_t attr
,
73 uint64_t form
, uint64_t adoff
, Dwarf_AttrDef
*adp
, Dwarf_Error
*error
)
78 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
79 return (DW_DLE_ARGUMENT
);
82 if ((ad
= malloc(sizeof(struct _Dwarf_AttrDef
))) == NULL
) {
83 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
84 return (DW_DLE_MEMORY
);
87 /* Initialise the attribute definition structure. */
90 ad
->ad_offset
= adoff
;
92 /* Add the attribute definition to the list in the abbrev. */
93 STAILQ_INSERT_TAIL(&ab
->ab_attrdef
, ad
, ad_next
);
95 /* Increase number of attribute counter. */
101 return (DW_DLE_NONE
);
105 _dwarf_abbrev_parse(Dwarf_Debug dbg
, Dwarf_CU cu
, Dwarf_Unsigned
*offset
,
106 Dwarf_Abbrev
*abp
, Dwarf_Error
*error
)
120 ds
= _dwarf_find_section(dbg
, ".debug_abbrev");
123 if (*offset
>= ds
->ds_size
)
124 return (DW_DLE_NO_ENTRY
);
128 entry
= _dwarf_read_uleb128(ds
->ds_data
, offset
);
131 ret
= _dwarf_abbrev_add(cu
, entry
, 0, 0, aboff
, abp
,
133 if (ret
== DW_DLE_NONE
) {
134 (*abp
)->ab_length
= 1;
139 tag
= _dwarf_read_uleb128(ds
->ds_data
, offset
);
140 children
= dbg
->read(ds
->ds_data
, offset
, 1);
141 if ((ret
= _dwarf_abbrev_add(cu
, entry
, tag
, children
, aboff
,
142 abp
, error
)) != DW_DLE_NONE
)
145 /* Parse attribute definitions. */
148 attr
= _dwarf_read_uleb128(ds
->ds_data
, offset
);
149 form
= _dwarf_read_uleb128(ds
->ds_data
, offset
);
151 if ((ret
= _dwarf_attrdef_add(dbg
, *abp
, attr
,
152 form
, adoff
, NULL
, error
)) != DW_DLE_NONE
)
156 (*abp
)->ab_length
= *offset
- aboff
;
162 _dwarf_abbrev_find(Dwarf_CU cu
, uint64_t entry
, Dwarf_Abbrev
*abp
,
167 Dwarf_Unsigned offset
;
171 return (DW_DLE_NO_ENTRY
);
173 /* Check if the desired abbrev entry is already in the hash table. */
174 HASH_FIND(ab_hh
, cu
->cu_abbrev_hash
, &entry
, sizeof(entry
), ab
);
177 return (DW_DLE_NONE
);
180 if (cu
->cu_abbrev_loaded
) {
181 return (DW_DLE_NO_ENTRY
);
184 /* Load and search the abbrev table. */
185 ds
= _dwarf_find_section(cu
->cu_dbg
, ".debug_abbrev");
187 offset
= cu
->cu_abbrev_offset_cur
;
188 while (offset
< ds
->ds_size
) {
189 ret
= _dwarf_abbrev_parse(cu
->cu_dbg
, cu
, &offset
, &ab
, error
);
190 if (ret
!= DW_DLE_NONE
)
192 if (ab
->ab_entry
== entry
) {
193 cu
->cu_abbrev_offset_cur
= offset
;
195 return (DW_DLE_NONE
);
197 if (ab
->ab_entry
== 0) {
198 cu
->cu_abbrev_offset_cur
= offset
;
199 cu
->cu_abbrev_loaded
= 1;
204 return (DW_DLE_NO_ENTRY
);
208 _dwarf_abbrev_cleanup(Dwarf_CU cu
)
210 Dwarf_Abbrev ab
, tab
;
211 Dwarf_AttrDef ad
, tad
;
215 HASH_ITER(ab_hh
, cu
->cu_abbrev_hash
, ab
, tab
) {
216 HASH_DELETE(ab_hh
, cu
->cu_abbrev_hash
, ab
);
217 STAILQ_FOREACH_SAFE(ad
, &ab
->ab_attrdef
, ad_next
, tad
) {
218 STAILQ_REMOVE(&ab
->ab_attrdef
, ad
, _Dwarf_AttrDef
,
227 _dwarf_abbrev_gen(Dwarf_P_Debug dbg
, Dwarf_Error
*error
)
235 cu
= STAILQ_FIRST(&dbg
->dbg_cu
);
237 return (DW_DLE_NONE
);
239 /* Create .debug_abbrev section. */
240 if ((ret
= _dwarf_section_init(dbg
, &ds
, ".debug_abbrev", 0, error
)) !=
244 for (ab
= cu
->cu_abbrev_hash
; ab
!= NULL
; ab
= ab
->ab_hh
.next
) {
245 RCHECK(WRITE_ULEB128(ab
->ab_entry
));
246 RCHECK(WRITE_ULEB128(ab
->ab_tag
));
247 RCHECK(WRITE_VALUE(ab
->ab_children
, 1));
248 STAILQ_FOREACH(ad
, &ab
->ab_attrdef
, ad_next
) {
249 RCHECK(WRITE_ULEB128(ad
->ad_attrib
));
250 RCHECK(WRITE_ULEB128(ad
->ad_form
));
252 /* Signal end of attribute spec list. */
253 RCHECK(WRITE_ULEB128(0));
254 RCHECK(WRITE_ULEB128(0));
256 /* End of abbreviation for this CU. */
257 RCHECK(WRITE_ULEB128(0));
259 /* Notify the creation of .debug_abbrev ELF section. */
260 RCHECK(_dwarf_section_callback(dbg
, ds
, SHT_PROGBITS
, 0, 0, 0, error
));
262 return (DW_DLE_NONE
);
266 _dwarf_section_free(dbg
, &ds
);