1*e04c7b34Sjkoshy /* $NetBSD: libdwarf_die.c,v 1.4 2022/05/01 17:20:47 jkoshy Exp $ */
24bd933feSchristos
33ae86eb5Schristos /*-
43ae86eb5Schristos * Copyright (c) 2007 John Birrell (jb@freebsd.org)
53ae86eb5Schristos * Copyright (c) 2009-2011 Kai Wang
63ae86eb5Schristos * All rights reserved.
73ae86eb5Schristos *
83ae86eb5Schristos * Redistribution and use in source and binary forms, with or without
93ae86eb5Schristos * modification, are permitted provided that the following conditions
103ae86eb5Schristos * are met:
113ae86eb5Schristos * 1. Redistributions of source code must retain the above copyright
123ae86eb5Schristos * notice, this list of conditions and the following disclaimer.
133ae86eb5Schristos * 2. Redistributions in binary form must reproduce the above copyright
143ae86eb5Schristos * notice, this list of conditions and the following disclaimer in the
153ae86eb5Schristos * documentation and/or other materials provided with the distribution.
163ae86eb5Schristos *
173ae86eb5Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183ae86eb5Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193ae86eb5Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203ae86eb5Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213ae86eb5Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223ae86eb5Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233ae86eb5Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243ae86eb5Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253ae86eb5Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263ae86eb5Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273ae86eb5Schristos * SUCH DAMAGE.
283ae86eb5Schristos */
293ae86eb5Schristos
303ae86eb5Schristos #include "_libdwarf.h"
313ae86eb5Schristos
32*e04c7b34Sjkoshy __RCSID("$NetBSD: libdwarf_die.c,v 1.4 2022/05/01 17:20:47 jkoshy Exp $");
339262d0f3Schristos ELFTC_VCSID("Id: libdwarf_die.c 3039 2014-05-18 15:10:56Z kaiwang27");
343ae86eb5Schristos
353ae86eb5Schristos int
_dwarf_die_alloc(Dwarf_Debug dbg,Dwarf_Die * ret_die,Dwarf_Error * error)363ae86eb5Schristos _dwarf_die_alloc(Dwarf_Debug dbg, Dwarf_Die *ret_die, Dwarf_Error *error)
373ae86eb5Schristos {
383ae86eb5Schristos Dwarf_Die die;
393ae86eb5Schristos
403ae86eb5Schristos assert(ret_die != NULL);
413ae86eb5Schristos
423ae86eb5Schristos if ((die = calloc(1, sizeof(struct _Dwarf_Die))) == NULL) {
433ae86eb5Schristos DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
443ae86eb5Schristos return (DW_DLE_MEMORY);
453ae86eb5Schristos }
463ae86eb5Schristos
473ae86eb5Schristos STAILQ_INIT(&die->die_attr);
483ae86eb5Schristos
493ae86eb5Schristos *ret_die = die;
503ae86eb5Schristos
513ae86eb5Schristos return (DW_DLE_NONE);
523ae86eb5Schristos }
533ae86eb5Schristos
543ae86eb5Schristos static int
_dwarf_die_add(Dwarf_CU cu,uint64_t offset,uint64_t abnum,Dwarf_Abbrev ab,Dwarf_Die * diep,Dwarf_Error * error)553ae86eb5Schristos _dwarf_die_add(Dwarf_CU cu, uint64_t offset, uint64_t abnum, Dwarf_Abbrev ab,
563ae86eb5Schristos Dwarf_Die *diep, Dwarf_Error *error)
573ae86eb5Schristos {
583ae86eb5Schristos Dwarf_Debug dbg;
593ae86eb5Schristos Dwarf_Die die;
603ae86eb5Schristos int ret;
613ae86eb5Schristos
623ae86eb5Schristos assert(cu != NULL);
633ae86eb5Schristos assert(ab != NULL);
643ae86eb5Schristos
653ae86eb5Schristos dbg = cu->cu_dbg;
663ae86eb5Schristos
673ae86eb5Schristos if ((ret = _dwarf_die_alloc(dbg, &die, error)) != DW_DLE_NONE)
683ae86eb5Schristos return (ret);
693ae86eb5Schristos
703ae86eb5Schristos die->die_offset = offset;
713ae86eb5Schristos die->die_abnum = abnum;
723ae86eb5Schristos die->die_ab = ab;
733ae86eb5Schristos die->die_cu = cu;
743ae86eb5Schristos die->die_dbg = cu->cu_dbg;
753ae86eb5Schristos
763ae86eb5Schristos if (diep != NULL)
773ae86eb5Schristos *diep = die;
783ae86eb5Schristos
793ae86eb5Schristos return (DW_DLE_NONE);
803ae86eb5Schristos }
813ae86eb5Schristos
823ae86eb5Schristos /* Find die at offset 'off' within the same CU. */
833ae86eb5Schristos Dwarf_Die
_dwarf_die_find(Dwarf_Die die,Dwarf_Unsigned off)843ae86eb5Schristos _dwarf_die_find(Dwarf_Die die, Dwarf_Unsigned off)
853ae86eb5Schristos {
863ae86eb5Schristos Dwarf_Debug dbg;
879262d0f3Schristos Dwarf_Section *ds;
883ae86eb5Schristos Dwarf_CU cu;
893ae86eb5Schristos Dwarf_Die die1;
903ae86eb5Schristos Dwarf_Error de;
913ae86eb5Schristos int ret;
923ae86eb5Schristos
933ae86eb5Schristos cu = die->die_cu;
943ae86eb5Schristos dbg = die->die_dbg;
959262d0f3Schristos ds = cu->cu_is_info ? dbg->dbg_info_sec : dbg->dbg_types_sec;
963ae86eb5Schristos
979262d0f3Schristos ret = _dwarf_die_parse(dbg, ds, cu, cu->cu_dwarf_size, off,
989262d0f3Schristos cu->cu_next_offset, &die1, 0, &de);
993ae86eb5Schristos
1003ae86eb5Schristos if (ret == DW_DLE_NONE)
1013ae86eb5Schristos return (die1);
1023ae86eb5Schristos else
1033ae86eb5Schristos return (NULL);
1043ae86eb5Schristos }
1053ae86eb5Schristos
1063ae86eb5Schristos int
_dwarf_die_parse(Dwarf_Debug dbg,Dwarf_Section * ds,Dwarf_CU cu,int dwarf_size,uint64_t offset,uint64_t next_offset,Dwarf_Die * ret_die,int search_sibling,Dwarf_Error * error)1073ae86eb5Schristos _dwarf_die_parse(Dwarf_Debug dbg, Dwarf_Section *ds, Dwarf_CU cu,
1083ae86eb5Schristos int dwarf_size, uint64_t offset, uint64_t next_offset, Dwarf_Die *ret_die,
1093ae86eb5Schristos int search_sibling, Dwarf_Error *error)
1103ae86eb5Schristos {
1113ae86eb5Schristos Dwarf_Abbrev ab;
1123ae86eb5Schristos Dwarf_AttrDef ad;
1133ae86eb5Schristos Dwarf_Die die;
1143ae86eb5Schristos uint64_t abnum;
1153ae86eb5Schristos uint64_t die_offset;
1163ae86eb5Schristos int ret, level;
1173ae86eb5Schristos
1183ae86eb5Schristos assert(cu != NULL);
1193ae86eb5Schristos
1203ae86eb5Schristos level = 1;
1213ae86eb5Schristos die = NULL;
1223ae86eb5Schristos
1233ae86eb5Schristos while (offset < next_offset && offset < ds->ds_size) {
1243ae86eb5Schristos
1253ae86eb5Schristos die_offset = offset;
1263ae86eb5Schristos
1273ae86eb5Schristos abnum = _dwarf_read_uleb128(ds->ds_data, &offset);
1283ae86eb5Schristos
1293ae86eb5Schristos if (abnum == 0) {
1303ae86eb5Schristos if (level == 0 || !search_sibling)
1313ae86eb5Schristos return (DW_DLE_NO_ENTRY);
1323ae86eb5Schristos
1333ae86eb5Schristos /*
1343ae86eb5Schristos * Return to previous DIE level.
1353ae86eb5Schristos */
1363ae86eb5Schristos level--;
1373ae86eb5Schristos continue;
1383ae86eb5Schristos }
1393ae86eb5Schristos
1403ae86eb5Schristos if ((ret = _dwarf_abbrev_find(cu, abnum, &ab, error)) !=
1413ae86eb5Schristos DW_DLE_NONE)
1423ae86eb5Schristos return (ret);
1433ae86eb5Schristos
1443ae86eb5Schristos if ((ret = _dwarf_die_add(cu, die_offset, abnum, ab, &die,
1453ae86eb5Schristos error)) != DW_DLE_NONE)
1463ae86eb5Schristos return (ret);
1473ae86eb5Schristos
1483ae86eb5Schristos STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) {
1493ae86eb5Schristos if ((ret = _dwarf_attr_init(dbg, ds, &offset,
1503ae86eb5Schristos dwarf_size, cu, die, ad, ad->ad_form, 0,
1513ae86eb5Schristos error)) != DW_DLE_NONE)
1523ae86eb5Schristos return (ret);
1533ae86eb5Schristos }
1543ae86eb5Schristos
1553ae86eb5Schristos die->die_next_off = offset;
1563ae86eb5Schristos if (search_sibling && level > 0) {
1573ae86eb5Schristos dwarf_dealloc(dbg, die, DW_DLA_DIE);
1583ae86eb5Schristos if (ab->ab_children == DW_CHILDREN_yes) {
1593ae86eb5Schristos /* Advance to next DIE level. */
1603ae86eb5Schristos level++;
1613ae86eb5Schristos }
1623ae86eb5Schristos } else {
1633ae86eb5Schristos *ret_die = die;
1643ae86eb5Schristos return (DW_DLE_NONE);
1653ae86eb5Schristos }
1663ae86eb5Schristos }
1673ae86eb5Schristos
1683ae86eb5Schristos return (DW_DLE_NO_ENTRY);
1693ae86eb5Schristos }
1703ae86eb5Schristos
1713ae86eb5Schristos void
_dwarf_die_link(Dwarf_P_Die die,Dwarf_P_Die parent,Dwarf_P_Die child,Dwarf_P_Die left_sibling,Dwarf_P_Die right_sibling)1723ae86eb5Schristos _dwarf_die_link(Dwarf_P_Die die, Dwarf_P_Die parent, Dwarf_P_Die child,
1733ae86eb5Schristos Dwarf_P_Die left_sibling, Dwarf_P_Die right_sibling)
1743ae86eb5Schristos {
1753ae86eb5Schristos Dwarf_P_Die last_child;
1763ae86eb5Schristos
1773ae86eb5Schristos assert(die != NULL);
1783ae86eb5Schristos
1793ae86eb5Schristos if (parent) {
1803ae86eb5Schristos
1813ae86eb5Schristos /* Disconnect from old parent. */
1823ae86eb5Schristos if (die->die_parent) {
1833ae86eb5Schristos if (die->die_parent != parent) {
1843ae86eb5Schristos if (die->die_parent->die_child == die)
1853ae86eb5Schristos die->die_parent->die_child = NULL;
1863ae86eb5Schristos die->die_parent = NULL;
1873ae86eb5Schristos }
1883ae86eb5Schristos }
1893ae86eb5Schristos
1903ae86eb5Schristos /* Find the last child of this parent. */
1913ae86eb5Schristos last_child = parent->die_child;
1923ae86eb5Schristos if (last_child) {
1933ae86eb5Schristos while (last_child->die_right != NULL)
1943ae86eb5Schristos last_child = last_child->die_right;
1953ae86eb5Schristos }
1963ae86eb5Schristos
1973ae86eb5Schristos /* Connect to new parent. */
1983ae86eb5Schristos die->die_parent = parent;
1993ae86eb5Schristos
2003ae86eb5Schristos /*
2013ae86eb5Schristos * Attach this DIE to the end of sibling list. If new
2023ae86eb5Schristos * parent doesn't have any child, set this DIE as the
2033ae86eb5Schristos * first child.
2043ae86eb5Schristos */
2053ae86eb5Schristos if (last_child) {
2063ae86eb5Schristos assert(last_child->die_right == NULL);
2073ae86eb5Schristos last_child->die_right = die;
2083ae86eb5Schristos die->die_left = last_child;
2093ae86eb5Schristos } else
2103ae86eb5Schristos parent->die_child = die;
2113ae86eb5Schristos }
2123ae86eb5Schristos
2133ae86eb5Schristos if (child) {
2143ae86eb5Schristos
2153ae86eb5Schristos /* Disconnect from old child. */
2163ae86eb5Schristos if (die->die_child) {
2173ae86eb5Schristos if (die->die_child != child) {
2183ae86eb5Schristos die->die_child->die_parent = NULL;
2193ae86eb5Schristos die->die_child = NULL;
2203ae86eb5Schristos }
2213ae86eb5Schristos }
2223ae86eb5Schristos
2233ae86eb5Schristos /* Connect to new child. */
2243ae86eb5Schristos die->die_child = child;
2253ae86eb5Schristos child->die_parent = die;
2263ae86eb5Schristos }
2273ae86eb5Schristos
2283ae86eb5Schristos if (left_sibling) {
2293ae86eb5Schristos
2303ae86eb5Schristos /* Disconnect from old left sibling. */
2313ae86eb5Schristos if (die->die_left) {
2323ae86eb5Schristos if (die->die_left != left_sibling) {
2333ae86eb5Schristos die->die_left->die_right = NULL;
2343ae86eb5Schristos die->die_left = NULL;
2353ae86eb5Schristos }
2363ae86eb5Schristos }
2373ae86eb5Schristos
2383ae86eb5Schristos /* Connect to new right sibling. */
2393ae86eb5Schristos die->die_left = left_sibling;
2403ae86eb5Schristos left_sibling->die_right = die;
2413ae86eb5Schristos }
2423ae86eb5Schristos
2433ae86eb5Schristos if (right_sibling) {
2443ae86eb5Schristos
2453ae86eb5Schristos /* Disconnect from old right sibling. */
2463ae86eb5Schristos if (die->die_right) {
2473ae86eb5Schristos if (die->die_right != right_sibling) {
2483ae86eb5Schristos die->die_right->die_left = NULL;
2493ae86eb5Schristos die->die_right = NULL;
2503ae86eb5Schristos }
2513ae86eb5Schristos }
2523ae86eb5Schristos
2533ae86eb5Schristos /* Connect to new right sibling. */
2543ae86eb5Schristos die->die_right = right_sibling;
2553ae86eb5Schristos right_sibling->die_left = die;
2563ae86eb5Schristos }
2573ae86eb5Schristos }
2583ae86eb5Schristos
2593ae86eb5Schristos int
_dwarf_die_count_links(Dwarf_P_Die parent,Dwarf_P_Die child,Dwarf_P_Die left_sibling,Dwarf_P_Die right_sibling)2603ae86eb5Schristos _dwarf_die_count_links(Dwarf_P_Die parent, Dwarf_P_Die child,
2613ae86eb5Schristos Dwarf_P_Die left_sibling, Dwarf_P_Die right_sibling)
2623ae86eb5Schristos {
2633ae86eb5Schristos int count;
2643ae86eb5Schristos
2653ae86eb5Schristos count = 0;
2663ae86eb5Schristos
2673ae86eb5Schristos if (parent)
2683ae86eb5Schristos count++;
2693ae86eb5Schristos if (child)
2703ae86eb5Schristos count++;
2713ae86eb5Schristos if (left_sibling)
2723ae86eb5Schristos count++;
2733ae86eb5Schristos if (right_sibling)
2743ae86eb5Schristos count++;
2753ae86eb5Schristos
2763ae86eb5Schristos return (count);
2773ae86eb5Schristos }
2783ae86eb5Schristos
2793ae86eb5Schristos static int
_dwarf_die_gen_recursive(Dwarf_P_Debug dbg,Dwarf_CU cu,Dwarf_Rel_Section drs,Dwarf_P_Die die,int pass2,Dwarf_Error * error)2803ae86eb5Schristos _dwarf_die_gen_recursive(Dwarf_P_Debug dbg, Dwarf_CU cu, Dwarf_Rel_Section drs,
2813ae86eb5Schristos Dwarf_P_Die die, int pass2, Dwarf_Error *error)
2823ae86eb5Schristos {
2833ae86eb5Schristos Dwarf_P_Section ds;
2843ae86eb5Schristos Dwarf_Abbrev ab;
2853ae86eb5Schristos Dwarf_Attribute at;
2863ae86eb5Schristos Dwarf_AttrDef ad;
2873ae86eb5Schristos int match, ret;
2883ae86eb5Schristos
2893ae86eb5Schristos ds = dbg->dbgp_info;
2903ae86eb5Schristos assert(ds != NULL);
2913ae86eb5Schristos
2923ae86eb5Schristos if (pass2)
2933ae86eb5Schristos goto attr_gen;
2943ae86eb5Schristos
2953ae86eb5Schristos /*
2963ae86eb5Schristos * Add DW_AT_sibling attribute for DIEs with children, so consumers
2973ae86eb5Schristos * can quickly scan chains of siblings, while ignoring the children
2983ae86eb5Schristos * of individual siblings.
2993ae86eb5Schristos */
3003ae86eb5Schristos if (die->die_child && die->die_right) {
3013ae86eb5Schristos if (_dwarf_attr_find(die, DW_AT_sibling) == NULL)
3023ae86eb5Schristos (void) dwarf_add_AT_reference(dbg, die, DW_AT_sibling,
3033ae86eb5Schristos die->die_right, error);
3043ae86eb5Schristos }
3053ae86eb5Schristos
3063ae86eb5Schristos /*
3073ae86eb5Schristos * Search abbrev list to find a matching entry.
3083ae86eb5Schristos */
3093ae86eb5Schristos die->die_ab = NULL;
3103ae86eb5Schristos for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) {
3113ae86eb5Schristos if (die->die_tag != ab->ab_tag)
3123ae86eb5Schristos continue;
3133ae86eb5Schristos if (ab->ab_children == DW_CHILDREN_no && die->die_child != NULL)
3143ae86eb5Schristos continue;
3153ae86eb5Schristos if (ab->ab_children == DW_CHILDREN_yes &&
3163ae86eb5Schristos die->die_child == NULL)
3173ae86eb5Schristos continue;
3183ae86eb5Schristos at = STAILQ_FIRST(&die->die_attr);
3193ae86eb5Schristos ad = STAILQ_FIRST(&ab->ab_attrdef);
3203ae86eb5Schristos match = 1;
3213ae86eb5Schristos while (at != NULL && ad != NULL) {
3223ae86eb5Schristos if (at->at_attrib != ad->ad_attrib ||
3233ae86eb5Schristos at->at_form != ad->ad_form) {
3243ae86eb5Schristos match = 0;
3253ae86eb5Schristos break;
3263ae86eb5Schristos }
3273ae86eb5Schristos at = STAILQ_NEXT(at, at_next);
3283ae86eb5Schristos ad = STAILQ_NEXT(ad, ad_next);
3293ae86eb5Schristos }
3303ae86eb5Schristos if ((at == NULL && ad != NULL) || (at != NULL && ad == NULL))
3313ae86eb5Schristos match = 0;
3323ae86eb5Schristos if (match) {
3333ae86eb5Schristos die->die_ab = ab;
3343ae86eb5Schristos break;
3353ae86eb5Schristos }
3363ae86eb5Schristos }
3373ae86eb5Schristos
3383ae86eb5Schristos /*
3393ae86eb5Schristos * Create a new abbrev entry if we can not reuse any existing one.
3403ae86eb5Schristos */
3413ae86eb5Schristos if (die->die_ab == NULL) {
3423ae86eb5Schristos ret = _dwarf_abbrev_add(cu, ++cu->cu_abbrev_cnt, die->die_tag,
3433ae86eb5Schristos die->die_child != NULL ? DW_CHILDREN_yes : DW_CHILDREN_no,
3443ae86eb5Schristos 0, &ab, error);
3453ae86eb5Schristos if (ret != DW_DLE_NONE)
3463ae86eb5Schristos return (ret);
3473ae86eb5Schristos STAILQ_FOREACH(at, &die->die_attr, at_next) {
3483ae86eb5Schristos ret = _dwarf_attrdef_add(dbg, ab, at->at_attrib,
3493ae86eb5Schristos at->at_form, 0, NULL, error);
3503ae86eb5Schristos if (ret != DW_DLE_NONE)
3513ae86eb5Schristos return (ret);
3523ae86eb5Schristos }
3533ae86eb5Schristos die->die_ab = ab;
3543ae86eb5Schristos }
3553ae86eb5Schristos
3563ae86eb5Schristos die->die_offset = ds->ds_size;
3573ae86eb5Schristos
3583ae86eb5Schristos /*
3593ae86eb5Schristos * Transform the DIE to bytes stream.
3603ae86eb5Schristos */
3613ae86eb5Schristos ret = _dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,
3623ae86eb5Schristos &ds->ds_size, die->die_ab->ab_entry, error);
3633ae86eb5Schristos if (ret != DW_DLE_NONE)
3643ae86eb5Schristos return (ret);
3653ae86eb5Schristos
3663ae86eb5Schristos attr_gen:
3673ae86eb5Schristos
3683ae86eb5Schristos /* Transform the attributes of this DIE. */
3693ae86eb5Schristos ret = _dwarf_attr_gen(dbg, ds, drs, cu, die, pass2, error);
3703ae86eb5Schristos if (ret != DW_DLE_NONE)
3713ae86eb5Schristos return (ret);
3723ae86eb5Schristos
3733ae86eb5Schristos /* Proceed to child DIE. */
3743ae86eb5Schristos if (die->die_child != NULL) {
3753ae86eb5Schristos ret = _dwarf_die_gen_recursive(dbg, cu, drs, die->die_child,
3763ae86eb5Schristos pass2, error);
3773ae86eb5Schristos if (ret != DW_DLE_NONE)
3783ae86eb5Schristos return (ret);
3793ae86eb5Schristos }
3803ae86eb5Schristos
3813ae86eb5Schristos /* Proceed to sibling DIE. */
3823ae86eb5Schristos if (die->die_right != NULL) {
3833ae86eb5Schristos ret = _dwarf_die_gen_recursive(dbg, cu, drs, die->die_right,
3843ae86eb5Schristos pass2, error);
3853ae86eb5Schristos if (ret != DW_DLE_NONE)
3863ae86eb5Schristos return (ret);
3873ae86eb5Schristos }
3883ae86eb5Schristos
3893ae86eb5Schristos /* Write a null DIE indicating the end of current level. */
3903ae86eb5Schristos if (die->die_right == NULL) {
3913ae86eb5Schristos ret = _dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,
3923ae86eb5Schristos &ds->ds_size, 0, error);
3933ae86eb5Schristos if (ret != DW_DLE_NONE)
3943ae86eb5Schristos return (ret);
3953ae86eb5Schristos }
3963ae86eb5Schristos
3973ae86eb5Schristos return (DW_DLE_NONE);
3983ae86eb5Schristos }
3993ae86eb5Schristos
4003ae86eb5Schristos int
_dwarf_die_gen(Dwarf_P_Debug dbg,Dwarf_CU cu,Dwarf_Rel_Section drs,Dwarf_Error * error)4013ae86eb5Schristos _dwarf_die_gen(Dwarf_P_Debug dbg, Dwarf_CU cu, Dwarf_Rel_Section drs,
4023ae86eb5Schristos Dwarf_Error *error)
4033ae86eb5Schristos {
4043ae86eb5Schristos Dwarf_Abbrev ab, tab;
4053ae86eb5Schristos Dwarf_AttrDef ad, tad;
4063ae86eb5Schristos Dwarf_Die die;
4073ae86eb5Schristos int ret;
4083ae86eb5Schristos
4093ae86eb5Schristos assert(dbg != NULL && cu != NULL);
4103ae86eb5Schristos assert(dbg->dbgp_root_die != NULL);
4113ae86eb5Schristos
4123ae86eb5Schristos die = dbg->dbgp_root_die;
4133ae86eb5Schristos
4143ae86eb5Schristos /*
4153ae86eb5Schristos * Insert a DW_AT_stmt_list attribute into root DIE, if there are
4163ae86eb5Schristos * line number information.
4173ae86eb5Schristos */
4183ae86eb5Schristos if (!STAILQ_EMPTY(&dbg->dbgp_lineinfo->li_lnlist))
4193ae86eb5Schristos RCHECK(_dwarf_add_AT_dataref(dbg, die, DW_AT_stmt_list, 0, 0,
4203ae86eb5Schristos ".debug_line", NULL, error));
4213ae86eb5Schristos
4223ae86eb5Schristos RCHECK(_dwarf_die_gen_recursive(dbg, cu, drs, die, 0, error));
4233ae86eb5Schristos
4243ae86eb5Schristos if (cu->cu_pass2)
4253ae86eb5Schristos RCHECK(_dwarf_die_gen_recursive(dbg, cu, drs, die, 1, error));
4263ae86eb5Schristos
4273ae86eb5Schristos return (DW_DLE_NONE);
4283ae86eb5Schristos
4293ae86eb5Schristos gen_fail:
4303ae86eb5Schristos
4313ae86eb5Schristos HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) {
4323ae86eb5Schristos HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab);
4333ae86eb5Schristos STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) {
4343ae86eb5Schristos STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef,
4353ae86eb5Schristos ad_next);
4363ae86eb5Schristos free(ad);
4373ae86eb5Schristos }
4383ae86eb5Schristos free(ab);
4393ae86eb5Schristos }
4403ae86eb5Schristos
4413ae86eb5Schristos return (ret);
4423ae86eb5Schristos }
4433ae86eb5Schristos
4443ae86eb5Schristos void
_dwarf_die_pro_cleanup(Dwarf_P_Debug dbg)4453ae86eb5Schristos _dwarf_die_pro_cleanup(Dwarf_P_Debug dbg)
4463ae86eb5Schristos {
4473ae86eb5Schristos Dwarf_P_Die die, tdie;
4483ae86eb5Schristos Dwarf_P_Attribute at, tat;
4493ae86eb5Schristos
4503ae86eb5Schristos assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
4513ae86eb5Schristos
4523ae86eb5Schristos STAILQ_FOREACH_SAFE(die, &dbg->dbgp_dielist, die_pro_next, tdie) {
4533ae86eb5Schristos STAILQ_FOREACH_SAFE(at, &die->die_attr, at_next, tat) {
4543ae86eb5Schristos STAILQ_REMOVE(&die->die_attr, at, _Dwarf_Attribute,
4553ae86eb5Schristos at_next);
4563ae86eb5Schristos free(at);
4573ae86eb5Schristos }
4583ae86eb5Schristos free(die);
4593ae86eb5Schristos }
4603ae86eb5Schristos }
461