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;
78 /* Find die at offset 'off' within the same CU. */
80 _dwarf_die_find(Dwarf_Die die, Dwarf_Unsigned off)
91 ds = cu->cu_is_info ? dbg->dbg_info_sec : dbg->dbg_types_sec;
93 ret = _dwarf_die_parse(dbg, ds, cu, cu->cu_dwarf_size, off,
94 cu->cu_next_offset, &die1, 0, &de);
96 if (ret == DW_DLE_NONE)
103 _dwarf_die_parse(Dwarf_Debug dbg, Dwarf_Section *ds, Dwarf_CU cu,
104 int dwarf_size, uint64_t offset, uint64_t next_offset, Dwarf_Die *ret_die,
105 int search_sibling, Dwarf_Error *error)
119 while (offset < next_offset && offset < ds->ds_size) {
123 abnum = _dwarf_read_uleb128(ds->ds_data, &offset);
126 if (level == 0 || !search_sibling)
127 return (DW_DLE_NO_ENTRY);
130 * Return to previous DIE level.
136 if ((ret = _dwarf_abbrev_find(cu, abnum, &ab, error)) !=
140 if ((ret = _dwarf_die_add(cu, die_offset, abnum, ab, &die,
141 error)) != DW_DLE_NONE)
144 STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) {
145 if ((ret = _dwarf_attr_init(dbg, ds, &offset,
146 dwarf_size, cu, die, ad, ad->ad_form, 0,
147 error)) != DW_DLE_NONE)
151 die->die_next_off = offset;
152 if (search_sibling && level > 0) {
153 dwarf_dealloc(dbg, die, DW_DLA_DIE);
154 if (ab->ab_children == DW_CHILDREN_yes) {
155 /* Advance to next DIE level. */
160 return (DW_DLE_NONE);
164 return (DW_DLE_NO_ENTRY);
168 _dwarf_die_link(Dwarf_P_Die die, Dwarf_P_Die parent, Dwarf_P_Die child,
169 Dwarf_P_Die left_sibling, Dwarf_P_Die right_sibling)
171 Dwarf_P_Die last_child;
177 /* Disconnect from old parent. */
178 if (die->die_parent) {
179 if (die->die_parent != parent) {
180 if (die->die_parent->die_child == die)
181 die->die_parent->die_child = NULL;
182 die->die_parent = NULL;
186 /* Find the last child of this parent. */
187 last_child = parent->die_child;
189 while (last_child->die_right != NULL)
190 last_child = last_child->die_right;
193 /* Connect to new parent. */
194 die->die_parent = parent;
197 * Attach this DIE to the end of sibling list. If new
198 * parent doesn't have any child, set this DIE as the
202 assert(last_child->die_right == NULL);
203 last_child->die_right = die;
204 die->die_left = last_child;
206 parent->die_child = die;
211 /* Disconnect from old child. */
212 if (die->die_child) {
213 if (die->die_child != child) {
214 die->die_child->die_parent = NULL;
215 die->die_child = NULL;
219 /* Connect to new child. */
220 die->die_child = child;
221 child->die_parent = die;
226 /* Disconnect from old left sibling. */
228 if (die->die_left != left_sibling) {
229 die->die_left->die_right = NULL;
230 die->die_left = NULL;
234 /* Connect to new right sibling. */
235 die->die_left = left_sibling;
236 left_sibling->die_right = die;
241 /* Disconnect from old right sibling. */
242 if (die->die_right) {
243 if (die->die_right != right_sibling) {
244 die->die_right->die_left = NULL;
245 die->die_right = NULL;
249 /* Connect to new right sibling. */
250 die->die_right = right_sibling;
251 right_sibling->die_left = die;
256 _dwarf_die_count_links(Dwarf_P_Die parent, Dwarf_P_Die child,
257 Dwarf_P_Die left_sibling, Dwarf_P_Die right_sibling)
276 _dwarf_die_gen_recursive(Dwarf_P_Debug dbg, Dwarf_CU cu, Dwarf_Rel_Section drs,
277 Dwarf_P_Die die, int pass2, Dwarf_Error *error)
292 * Add DW_AT_sibling attribute for DIEs with children, so consumers
293 * can quickly scan chains of siblings, while ignoring the children
294 * of individual siblings.
296 if (die->die_child && die->die_right) {
297 if (_dwarf_attr_find(die, DW_AT_sibling) == NULL)
298 (void) dwarf_add_AT_reference(dbg, die, DW_AT_sibling,
299 die->die_right, error);
303 * Search abbrev list to find a matching entry.
306 for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) {
307 if (die->die_tag != ab->ab_tag)
309 if (ab->ab_children == DW_CHILDREN_no && die->die_child != NULL)
311 if (ab->ab_children == DW_CHILDREN_yes &&
312 die->die_child == NULL)
314 at = STAILQ_FIRST(&die->die_attr);
315 ad = STAILQ_FIRST(&ab->ab_attrdef);
317 while (at != NULL && ad != NULL) {
318 if (at->at_attrib != ad->ad_attrib ||
319 at->at_form != ad->ad_form) {
323 at = STAILQ_NEXT(at, at_next);
324 ad = STAILQ_NEXT(ad, ad_next);
326 if ((at == NULL && ad != NULL) || (at != NULL && ad == NULL))
335 * Create a new abbrev entry if we can not reuse any existing one.
337 if (die->die_ab == NULL) {
338 ret = _dwarf_abbrev_add(cu, ++cu->cu_abbrev_cnt, die->die_tag,
339 die->die_child != NULL ? DW_CHILDREN_yes : DW_CHILDREN_no,
341 if (ret != DW_DLE_NONE)
343 STAILQ_FOREACH(at, &die->die_attr, at_next) {
344 ret = _dwarf_attrdef_add(dbg, ab, at->at_attrib,
345 at->at_form, 0, NULL, error);
346 if (ret != DW_DLE_NONE)
352 die->die_offset = ds->ds_size;
355 * Transform the DIE to bytes stream.
357 ret = _dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,
358 &ds->ds_size, die->die_ab->ab_entry, error);
359 if (ret != DW_DLE_NONE)
364 /* Transform the attributes of this DIE. */
365 ret = _dwarf_attr_gen(dbg, ds, drs, cu, die, pass2, error);
366 if (ret != DW_DLE_NONE)
369 /* Proceed to child DIE. */
370 if (die->die_child != NULL) {
371 ret = _dwarf_die_gen_recursive(dbg, cu, drs, die->die_child,
373 if (ret != DW_DLE_NONE)
377 /* Proceed to sibling DIE. */
378 if (die->die_right != NULL) {
379 ret = _dwarf_die_gen_recursive(dbg, cu, drs, die->die_right,
381 if (ret != DW_DLE_NONE)
385 /* Write a null DIE indicating the end of current level. */
386 if (die->die_right == NULL) {
387 ret = _dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,
388 &ds->ds_size, 0, error);
389 if (ret != DW_DLE_NONE)
393 return (DW_DLE_NONE);
397 _dwarf_die_gen(Dwarf_P_Debug dbg, Dwarf_CU cu, Dwarf_Rel_Section drs,
400 Dwarf_Abbrev ab, tab;
401 Dwarf_AttrDef ad, tad;
405 assert(dbg != NULL && cu != NULL);
406 assert(dbg->dbgp_root_die != NULL);
408 die = dbg->dbgp_root_die;
411 * Insert a DW_AT_stmt_list attribute into root DIE, if there are
412 * line number information.
414 if (!STAILQ_EMPTY(&dbg->dbgp_lineinfo->li_lnlist))
415 RCHECK(_dwarf_add_AT_dataref(dbg, die, DW_AT_stmt_list, 0, 0,
416 ".debug_line", NULL, error));
418 RCHECK(_dwarf_die_gen_recursive(dbg, cu, drs, die, 0, error));
421 RCHECK(_dwarf_die_gen_recursive(dbg, cu, drs, die, 1, error));
423 return (DW_DLE_NONE);
427 HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) {
428 HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab);
429 STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) {
430 STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef,
441 _dwarf_die_pro_cleanup(Dwarf_P_Debug dbg)
443 Dwarf_P_Die die, tdie;
444 Dwarf_P_Attribute at, tat;
446 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
448 STAILQ_FOREACH_SAFE(die, &dbg->dbgp_dielist, die_pro_next, tdie) {
449 STAILQ_FOREACH_SAFE(at, &die->die_attr, at_next, tat) {
450 STAILQ_REMOVE(&die->die_attr, at, _Dwarf_Attribute,