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_die.c 3039 2014-05-18 15:10:56Z kaiwang27 $");
33 _dwarf_die_alloc(Dwarf_Debug dbg, Dwarf_Die *ret_die, Dwarf_Error *error)
37 assert(ret_die != NULL);
39 if ((die = calloc(1, sizeof(struct _Dwarf_Die))) == NULL) {
40 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
41 return (DW_DLE_MEMORY);
44 STAILQ_INIT(&die->die_attr);
52 _dwarf_die_add(Dwarf_CU cu, uint64_t offset, uint64_t abnum, Dwarf_Abbrev ab,
53 Dwarf_Die *diep, Dwarf_Error *error)
64 if ((ret = _dwarf_die_alloc(dbg, &die, error)) != DW_DLE_NONE)
67 die->die_offset = offset;
68 die->die_abnum = abnum;
71 die->die_dbg = cu->cu_dbg;
79 /* Find die at offset 'off' within the same CU. */
81 _dwarf_die_find(Dwarf_Die die, Dwarf_Unsigned off)
92 ds = cu->cu_is_info ? dbg->dbg_info_sec : dbg->dbg_types_sec;
94 ret = _dwarf_die_parse(dbg, ds, cu, cu->cu_dwarf_size, off,
95 cu->cu_next_offset, &die1, 0, &de);
97 if (ret == DW_DLE_NONE)
104 _dwarf_die_parse(Dwarf_Debug dbg, Dwarf_Section *ds, Dwarf_CU cu,
105 int dwarf_size, uint64_t offset, uint64_t next_offset, Dwarf_Die *ret_die,
106 int search_sibling, Dwarf_Error *error)
120 while (offset < next_offset && offset < ds->ds_size) {
124 abnum = _dwarf_read_uleb128(ds->ds_data, &offset);
127 if (level == 0 || !search_sibling)
128 return (DW_DLE_NO_ENTRY);
131 * Return to previous DIE level.
137 if ((ret = _dwarf_abbrev_find(cu, abnum, &ab, error)) !=
141 if ((ret = _dwarf_die_add(cu, die_offset, abnum, ab, &die,
142 error)) != DW_DLE_NONE)
145 STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) {
146 if ((ret = _dwarf_attr_init(dbg, ds, &offset,
147 dwarf_size, cu, die, ad, ad->ad_form, 0,
148 error)) != DW_DLE_NONE)
152 die->die_next_off = offset;
153 if (search_sibling && level > 0) {
154 dwarf_dealloc(dbg, die, DW_DLA_DIE);
155 if (ab->ab_children == DW_CHILDREN_yes) {
156 /* Advance to next DIE level. */
161 return (DW_DLE_NONE);
165 return (DW_DLE_NO_ENTRY);
169 _dwarf_die_link(Dwarf_P_Die die, Dwarf_P_Die parent, Dwarf_P_Die child,
170 Dwarf_P_Die left_sibling, Dwarf_P_Die right_sibling)
172 Dwarf_P_Die last_child;
178 /* Disconnect from old parent. */
179 if (die->die_parent) {
180 if (die->die_parent != parent) {
181 if (die->die_parent->die_child == die)
182 die->die_parent->die_child = NULL;
183 die->die_parent = NULL;
187 /* Find the last child of this parent. */
188 last_child = parent->die_child;
190 while (last_child->die_right != NULL)
191 last_child = last_child->die_right;
194 /* Connect to new parent. */
195 die->die_parent = parent;
198 * Attach this DIE to the end of sibling list. If new
199 * parent doesn't have any child, set this DIE as the
203 assert(last_child->die_right == NULL);
204 last_child->die_right = die;
205 die->die_left = last_child;
207 parent->die_child = die;
212 /* Disconnect from old child. */
213 if (die->die_child) {
214 if (die->die_child != child) {
215 die->die_child->die_parent = NULL;
216 die->die_child = NULL;
220 /* Connect to new child. */
221 die->die_child = child;
222 child->die_parent = die;
227 /* Disconnect from old left sibling. */
229 if (die->die_left != left_sibling) {
230 die->die_left->die_right = NULL;
231 die->die_left = NULL;
235 /* Connect to new right sibling. */
236 die->die_left = left_sibling;
237 left_sibling->die_right = die;
242 /* Disconnect from old right sibling. */
243 if (die->die_right) {
244 if (die->die_right != right_sibling) {
245 die->die_right->die_left = NULL;
246 die->die_right = NULL;
250 /* Connect to new right sibling. */
251 die->die_right = right_sibling;
252 right_sibling->die_left = die;
257 _dwarf_die_count_links(Dwarf_P_Die parent, Dwarf_P_Die child,
258 Dwarf_P_Die left_sibling, Dwarf_P_Die right_sibling)
277 _dwarf_die_gen_recursive(Dwarf_P_Debug dbg, Dwarf_CU cu, Dwarf_Rel_Section drs,
278 Dwarf_P_Die die, int pass2, Dwarf_Error *error)
293 * Add DW_AT_sibling attribute for DIEs with children, so consumers
294 * can quickly scan chains of siblings, while ignoring the children
295 * of individual siblings.
297 if (die->die_child && die->die_right) {
298 if (_dwarf_attr_find(die, DW_AT_sibling) == NULL)
299 (void) dwarf_add_AT_reference(dbg, die, DW_AT_sibling,
300 die->die_right, error);
304 * Search abbrev list to find a matching entry.
307 for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) {
308 if (die->die_tag != ab->ab_tag)
310 if (ab->ab_children == DW_CHILDREN_no && die->die_child != NULL)
312 if (ab->ab_children == DW_CHILDREN_yes &&
313 die->die_child == NULL)
315 at = STAILQ_FIRST(&die->die_attr);
316 ad = STAILQ_FIRST(&ab->ab_attrdef);
318 while (at != NULL && ad != NULL) {
319 if (at->at_attrib != ad->ad_attrib ||
320 at->at_form != ad->ad_form) {
324 at = STAILQ_NEXT(at, at_next);
325 ad = STAILQ_NEXT(ad, ad_next);
327 if ((at == NULL && ad != NULL) || (at != NULL && ad == NULL))
336 * Create a new abbrev entry if we can not reuse any existing one.
338 if (die->die_ab == NULL) {
339 ret = _dwarf_abbrev_add(cu, ++cu->cu_abbrev_cnt, die->die_tag,
340 die->die_child != NULL ? DW_CHILDREN_yes : DW_CHILDREN_no,
342 if (ret != DW_DLE_NONE)
344 STAILQ_FOREACH(at, &die->die_attr, at_next) {
345 ret = _dwarf_attrdef_add(dbg, ab, at->at_attrib,
346 at->at_form, 0, NULL, error);
347 if (ret != DW_DLE_NONE)
353 die->die_offset = ds->ds_size;
356 * Transform the DIE to bytes stream.
358 ret = _dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,
359 &ds->ds_size, die->die_ab->ab_entry, error);
360 if (ret != DW_DLE_NONE)
365 /* Transform the attributes of this DIE. */
366 ret = _dwarf_attr_gen(dbg, ds, drs, cu, die, pass2, error);
367 if (ret != DW_DLE_NONE)
370 /* Proceed to child DIE. */
371 if (die->die_child != NULL) {
372 ret = _dwarf_die_gen_recursive(dbg, cu, drs, die->die_child,
374 if (ret != DW_DLE_NONE)
378 /* Proceed to sibling DIE. */
379 if (die->die_right != NULL) {
380 ret = _dwarf_die_gen_recursive(dbg, cu, drs, die->die_right,
382 if (ret != DW_DLE_NONE)
386 /* Write a null DIE indicating the end of current level. */
387 if (die->die_right == NULL) {
388 ret = _dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,
389 &ds->ds_size, 0, error);
390 if (ret != DW_DLE_NONE)
394 return (DW_DLE_NONE);
398 _dwarf_die_gen(Dwarf_P_Debug dbg, Dwarf_CU cu, Dwarf_Rel_Section drs,
401 Dwarf_Abbrev ab, tab;
402 Dwarf_AttrDef ad, tad;
406 assert(dbg != NULL && cu != NULL);
407 assert(dbg->dbgp_root_die != NULL);
409 die = dbg->dbgp_root_die;
412 * Insert a DW_AT_stmt_list attribute into root DIE, if there are
413 * line number information.
415 if (!STAILQ_EMPTY(&dbg->dbgp_lineinfo->li_lnlist))
416 RCHECK(_dwarf_add_AT_dataref(dbg, die, DW_AT_stmt_list, 0, 0,
417 ".debug_line", NULL, error));
419 RCHECK(_dwarf_die_gen_recursive(dbg, cu, drs, die, 0, error));
422 RCHECK(_dwarf_die_gen_recursive(dbg, cu, drs, die, 1, error));
424 return (DW_DLE_NONE);
428 HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) {
429 HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab);
430 STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) {
431 STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef,
442 _dwarf_die_pro_cleanup(Dwarf_P_Debug dbg)
444 Dwarf_P_Die die, tdie;
445 Dwarf_P_Attribute at, tat;
447 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
449 STAILQ_FOREACH_SAFE(die, &dbg->dbgp_dielist, die_pro_next, tdie) {
450 STAILQ_FOREACH_SAFE(at, &die->die_attr, at_next, tat) {
451 STAILQ_REMOVE(&die->die_attr, at, _Dwarf_Attribute,