xref: /minix3/external/bsd/elftoolchain/dist/libdwarf/libdwarf_die.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: libdwarf_die.c,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5*0a6a1f1dSLionel Sambuc  * Copyright (c) 2009-2011 Kai Wang
6*0a6a1f1dSLionel Sambuc  * All rights reserved.
7*0a6a1f1dSLionel Sambuc  *
8*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
10*0a6a1f1dSLionel Sambuc  * are met:
11*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
14*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
15*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
16*0a6a1f1dSLionel Sambuc  *
17*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*0a6a1f1dSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*0a6a1f1dSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*0a6a1f1dSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc  */
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libdwarf_die.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
33*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libdwarf_die.c 2948 2013-05-30 21:25:52Z kaiwang27 ");
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc int
_dwarf_die_alloc(Dwarf_Debug dbg,Dwarf_Die * ret_die,Dwarf_Error * error)36*0a6a1f1dSLionel Sambuc _dwarf_die_alloc(Dwarf_Debug dbg, Dwarf_Die *ret_die, Dwarf_Error *error)
37*0a6a1f1dSLionel Sambuc {
38*0a6a1f1dSLionel Sambuc 	Dwarf_Die die;
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc 	assert(ret_die != NULL);
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc 	if ((die = calloc(1, sizeof(struct _Dwarf_Die))) == NULL) {
43*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
44*0a6a1f1dSLionel Sambuc 		return (DW_DLE_MEMORY);
45*0a6a1f1dSLionel Sambuc 	}
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc 	STAILQ_INIT(&die->die_attr);
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc 	*ret_die = die;
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
52*0a6a1f1dSLionel Sambuc }
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc static int
_dwarf_die_add(Dwarf_CU cu,uint64_t offset,uint64_t abnum,Dwarf_Abbrev ab,Dwarf_Die * diep,Dwarf_Error * error)55*0a6a1f1dSLionel Sambuc _dwarf_die_add(Dwarf_CU cu, uint64_t offset, uint64_t abnum, Dwarf_Abbrev ab,
56*0a6a1f1dSLionel Sambuc     Dwarf_Die *diep, Dwarf_Error *error)
57*0a6a1f1dSLionel Sambuc {
58*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
59*0a6a1f1dSLionel Sambuc 	Dwarf_Die die;
60*0a6a1f1dSLionel Sambuc 	int ret;
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc 	assert(cu != NULL);
63*0a6a1f1dSLionel Sambuc 	assert(ab != NULL);
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc 	dbg = cu->cu_dbg;
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc 	if ((ret = _dwarf_die_alloc(dbg, &die, error)) != DW_DLE_NONE)
68*0a6a1f1dSLionel Sambuc 		return (ret);
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc 	die->die_offset	= offset;
71*0a6a1f1dSLionel Sambuc 	die->die_abnum	= abnum;
72*0a6a1f1dSLionel Sambuc 	die->die_ab	= ab;
73*0a6a1f1dSLionel Sambuc 	die->die_cu	= cu;
74*0a6a1f1dSLionel Sambuc 	die->die_dbg	= cu->cu_dbg;
75*0a6a1f1dSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc 	if (diep != NULL)
77*0a6a1f1dSLionel Sambuc 		*diep = die;
78*0a6a1f1dSLionel Sambuc 
79*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
80*0a6a1f1dSLionel Sambuc }
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc /* Find die at offset 'off' within the same CU. */
83*0a6a1f1dSLionel Sambuc Dwarf_Die
_dwarf_die_find(Dwarf_Die die,Dwarf_Unsigned off)84*0a6a1f1dSLionel Sambuc _dwarf_die_find(Dwarf_Die die, Dwarf_Unsigned off)
85*0a6a1f1dSLionel Sambuc {
86*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
87*0a6a1f1dSLionel Sambuc 	Dwarf_CU cu;
88*0a6a1f1dSLionel Sambuc 	Dwarf_Die die1;
89*0a6a1f1dSLionel Sambuc 	Dwarf_Error de;
90*0a6a1f1dSLionel Sambuc 	int ret;
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc 	cu = die->die_cu;
93*0a6a1f1dSLionel Sambuc 	dbg = die->die_dbg;
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc 	ret = _dwarf_die_parse(dbg, dbg->dbg_info_sec, cu, cu->cu_dwarf_size,
96*0a6a1f1dSLionel Sambuc 	    off, cu->cu_next_offset, &die1, 0, &de);
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc 	if (ret == DW_DLE_NONE)
99*0a6a1f1dSLionel Sambuc 		return (die1);
100*0a6a1f1dSLionel Sambuc 	else
101*0a6a1f1dSLionel Sambuc 		return (NULL);
102*0a6a1f1dSLionel Sambuc }
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc 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)105*0a6a1f1dSLionel Sambuc _dwarf_die_parse(Dwarf_Debug dbg, Dwarf_Section *ds, Dwarf_CU cu,
106*0a6a1f1dSLionel Sambuc     int dwarf_size, uint64_t offset, uint64_t next_offset, Dwarf_Die *ret_die,
107*0a6a1f1dSLionel Sambuc     int search_sibling, Dwarf_Error *error)
108*0a6a1f1dSLionel Sambuc {
109*0a6a1f1dSLionel Sambuc 	Dwarf_Abbrev ab;
110*0a6a1f1dSLionel Sambuc 	Dwarf_AttrDef ad;
111*0a6a1f1dSLionel Sambuc 	Dwarf_Die die;
112*0a6a1f1dSLionel Sambuc 	uint64_t abnum;
113*0a6a1f1dSLionel Sambuc 	uint64_t die_offset;
114*0a6a1f1dSLionel Sambuc 	int ret, level;
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc 	assert(cu != NULL);
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc 	level = 1;
119*0a6a1f1dSLionel Sambuc 	die = NULL;
120*0a6a1f1dSLionel Sambuc 
121*0a6a1f1dSLionel Sambuc 	while (offset < next_offset && offset < ds->ds_size) {
122*0a6a1f1dSLionel Sambuc 
123*0a6a1f1dSLionel Sambuc 		die_offset = offset;
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc 		abnum = _dwarf_read_uleb128(ds->ds_data, &offset);
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc 		if (abnum == 0) {
128*0a6a1f1dSLionel Sambuc 			if (level == 0 || !search_sibling)
129*0a6a1f1dSLionel Sambuc 				return (DW_DLE_NO_ENTRY);
130*0a6a1f1dSLionel Sambuc 
131*0a6a1f1dSLionel Sambuc 			/*
132*0a6a1f1dSLionel Sambuc 			 * Return to previous DIE level.
133*0a6a1f1dSLionel Sambuc 			 */
134*0a6a1f1dSLionel Sambuc 			level--;
135*0a6a1f1dSLionel Sambuc 			continue;
136*0a6a1f1dSLionel Sambuc 		}
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc 		if ((ret = _dwarf_abbrev_find(cu, abnum, &ab, error)) !=
139*0a6a1f1dSLionel Sambuc 		    DW_DLE_NONE)
140*0a6a1f1dSLionel Sambuc 			return (ret);
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc 		if ((ret = _dwarf_die_add(cu, die_offset, abnum, ab, &die,
143*0a6a1f1dSLionel Sambuc 		    error)) != DW_DLE_NONE)
144*0a6a1f1dSLionel Sambuc 			return (ret);
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc 		STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) {
147*0a6a1f1dSLionel Sambuc 			if ((ret = _dwarf_attr_init(dbg, ds, &offset,
148*0a6a1f1dSLionel Sambuc 			    dwarf_size, cu, die, ad, ad->ad_form, 0,
149*0a6a1f1dSLionel Sambuc 			    error)) != DW_DLE_NONE)
150*0a6a1f1dSLionel Sambuc 				return (ret);
151*0a6a1f1dSLionel Sambuc 		}
152*0a6a1f1dSLionel Sambuc 
153*0a6a1f1dSLionel Sambuc 		die->die_next_off = offset;
154*0a6a1f1dSLionel Sambuc 		if (search_sibling && level > 0) {
155*0a6a1f1dSLionel Sambuc 			dwarf_dealloc(dbg, die, DW_DLA_DIE);
156*0a6a1f1dSLionel Sambuc 			if (ab->ab_children == DW_CHILDREN_yes) {
157*0a6a1f1dSLionel Sambuc 				/* Advance to next DIE level. */
158*0a6a1f1dSLionel Sambuc 				level++;
159*0a6a1f1dSLionel Sambuc 			}
160*0a6a1f1dSLionel Sambuc 		} else {
161*0a6a1f1dSLionel Sambuc 			*ret_die = die;
162*0a6a1f1dSLionel Sambuc 			return (DW_DLE_NONE);
163*0a6a1f1dSLionel Sambuc 		}
164*0a6a1f1dSLionel Sambuc 	}
165*0a6a1f1dSLionel Sambuc 
166*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NO_ENTRY);
167*0a6a1f1dSLionel Sambuc }
168*0a6a1f1dSLionel Sambuc 
169*0a6a1f1dSLionel Sambuc 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)170*0a6a1f1dSLionel Sambuc _dwarf_die_link(Dwarf_P_Die die, Dwarf_P_Die parent, Dwarf_P_Die child,
171*0a6a1f1dSLionel Sambuc     Dwarf_P_Die left_sibling, Dwarf_P_Die right_sibling)
172*0a6a1f1dSLionel Sambuc {
173*0a6a1f1dSLionel Sambuc 	Dwarf_P_Die last_child;
174*0a6a1f1dSLionel Sambuc 
175*0a6a1f1dSLionel Sambuc 	assert(die != NULL);
176*0a6a1f1dSLionel Sambuc 
177*0a6a1f1dSLionel Sambuc 	if (parent) {
178*0a6a1f1dSLionel Sambuc 
179*0a6a1f1dSLionel Sambuc 		/* Disconnect from old parent. */
180*0a6a1f1dSLionel Sambuc 		if (die->die_parent) {
181*0a6a1f1dSLionel Sambuc 			if (die->die_parent != parent) {
182*0a6a1f1dSLionel Sambuc 				if (die->die_parent->die_child == die)
183*0a6a1f1dSLionel Sambuc 					die->die_parent->die_child = NULL;
184*0a6a1f1dSLionel Sambuc 				die->die_parent = NULL;
185*0a6a1f1dSLionel Sambuc                      }
186*0a6a1f1dSLionel Sambuc 		}
187*0a6a1f1dSLionel Sambuc 
188*0a6a1f1dSLionel Sambuc 		/* Find the last child of this parent. */
189*0a6a1f1dSLionel Sambuc 		last_child = parent->die_child;
190*0a6a1f1dSLionel Sambuc 		if (last_child) {
191*0a6a1f1dSLionel Sambuc 			while (last_child->die_right != NULL)
192*0a6a1f1dSLionel Sambuc 				last_child = last_child->die_right;
193*0a6a1f1dSLionel Sambuc 		}
194*0a6a1f1dSLionel Sambuc 
195*0a6a1f1dSLionel Sambuc 		/* Connect to new parent. */
196*0a6a1f1dSLionel Sambuc 		die->die_parent = parent;
197*0a6a1f1dSLionel Sambuc 
198*0a6a1f1dSLionel Sambuc 		/*
199*0a6a1f1dSLionel Sambuc 		 * Attach this DIE to the end of sibling list. If new
200*0a6a1f1dSLionel Sambuc 		 * parent doesn't have any child, set this DIE as the
201*0a6a1f1dSLionel Sambuc 		 * first child.
202*0a6a1f1dSLionel Sambuc 		 */
203*0a6a1f1dSLionel Sambuc 		if (last_child) {
204*0a6a1f1dSLionel Sambuc 			assert(last_child->die_right == NULL);
205*0a6a1f1dSLionel Sambuc 			last_child->die_right = die;
206*0a6a1f1dSLionel Sambuc 			die->die_left = last_child;
207*0a6a1f1dSLionel Sambuc 		} else
208*0a6a1f1dSLionel Sambuc 			parent->die_child = die;
209*0a6a1f1dSLionel Sambuc 	}
210*0a6a1f1dSLionel Sambuc 
211*0a6a1f1dSLionel Sambuc 	if (child) {
212*0a6a1f1dSLionel Sambuc 
213*0a6a1f1dSLionel Sambuc 		/* Disconnect from old child. */
214*0a6a1f1dSLionel Sambuc 		if (die->die_child) {
215*0a6a1f1dSLionel Sambuc 			if (die->die_child != child) {
216*0a6a1f1dSLionel Sambuc 				die->die_child->die_parent = NULL;
217*0a6a1f1dSLionel Sambuc 				die->die_child = NULL;
218*0a6a1f1dSLionel Sambuc 			}
219*0a6a1f1dSLionel Sambuc 		}
220*0a6a1f1dSLionel Sambuc 
221*0a6a1f1dSLionel Sambuc 		/* Connect to new child. */
222*0a6a1f1dSLionel Sambuc 		die->die_child = child;
223*0a6a1f1dSLionel Sambuc 		child->die_parent = die;
224*0a6a1f1dSLionel Sambuc 	}
225*0a6a1f1dSLionel Sambuc 
226*0a6a1f1dSLionel Sambuc 	if (left_sibling) {
227*0a6a1f1dSLionel Sambuc 
228*0a6a1f1dSLionel Sambuc 		/* Disconnect from old left sibling. */
229*0a6a1f1dSLionel Sambuc 		if (die->die_left) {
230*0a6a1f1dSLionel Sambuc 			if (die->die_left != left_sibling) {
231*0a6a1f1dSLionel Sambuc 				die->die_left->die_right = NULL;
232*0a6a1f1dSLionel Sambuc 				die->die_left = NULL;
233*0a6a1f1dSLionel Sambuc 			}
234*0a6a1f1dSLionel Sambuc 		}
235*0a6a1f1dSLionel Sambuc 
236*0a6a1f1dSLionel Sambuc 		/* Connect to new right sibling. */
237*0a6a1f1dSLionel Sambuc 		die->die_left = left_sibling;
238*0a6a1f1dSLionel Sambuc 		left_sibling->die_right = die;
239*0a6a1f1dSLionel Sambuc 	}
240*0a6a1f1dSLionel Sambuc 
241*0a6a1f1dSLionel Sambuc 	if (right_sibling) {
242*0a6a1f1dSLionel Sambuc 
243*0a6a1f1dSLionel Sambuc 		/* Disconnect from old right sibling. */
244*0a6a1f1dSLionel Sambuc 		if (die->die_right) {
245*0a6a1f1dSLionel Sambuc 			if (die->die_right != right_sibling) {
246*0a6a1f1dSLionel Sambuc 				die->die_right->die_left = NULL;
247*0a6a1f1dSLionel Sambuc 				die->die_right = NULL;
248*0a6a1f1dSLionel Sambuc 			}
249*0a6a1f1dSLionel Sambuc 		}
250*0a6a1f1dSLionel Sambuc 
251*0a6a1f1dSLionel Sambuc 		/* Connect to new right sibling. */
252*0a6a1f1dSLionel Sambuc 		die->die_right = right_sibling;
253*0a6a1f1dSLionel Sambuc 		right_sibling->die_left = die;
254*0a6a1f1dSLionel Sambuc 	}
255*0a6a1f1dSLionel Sambuc }
256*0a6a1f1dSLionel Sambuc 
257*0a6a1f1dSLionel Sambuc int
_dwarf_die_count_links(Dwarf_P_Die parent,Dwarf_P_Die child,Dwarf_P_Die left_sibling,Dwarf_P_Die right_sibling)258*0a6a1f1dSLionel Sambuc _dwarf_die_count_links(Dwarf_P_Die parent, Dwarf_P_Die child,
259*0a6a1f1dSLionel Sambuc     Dwarf_P_Die left_sibling, Dwarf_P_Die right_sibling)
260*0a6a1f1dSLionel Sambuc {
261*0a6a1f1dSLionel Sambuc 	int count;
262*0a6a1f1dSLionel Sambuc 
263*0a6a1f1dSLionel Sambuc 	count = 0;
264*0a6a1f1dSLionel Sambuc 
265*0a6a1f1dSLionel Sambuc 	if (parent)
266*0a6a1f1dSLionel Sambuc 		count++;
267*0a6a1f1dSLionel Sambuc 	if (child)
268*0a6a1f1dSLionel Sambuc 		count++;
269*0a6a1f1dSLionel Sambuc 	if (left_sibling)
270*0a6a1f1dSLionel Sambuc 		count++;
271*0a6a1f1dSLionel Sambuc 	if (right_sibling)
272*0a6a1f1dSLionel Sambuc 		count++;
273*0a6a1f1dSLionel Sambuc 
274*0a6a1f1dSLionel Sambuc 	return (count);
275*0a6a1f1dSLionel Sambuc }
276*0a6a1f1dSLionel Sambuc 
277*0a6a1f1dSLionel Sambuc 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)278*0a6a1f1dSLionel Sambuc _dwarf_die_gen_recursive(Dwarf_P_Debug dbg, Dwarf_CU cu, Dwarf_Rel_Section drs,
279*0a6a1f1dSLionel Sambuc     Dwarf_P_Die die, int pass2, Dwarf_Error *error)
280*0a6a1f1dSLionel Sambuc {
281*0a6a1f1dSLionel Sambuc 	Dwarf_P_Section ds;
282*0a6a1f1dSLionel Sambuc 	Dwarf_Abbrev ab;
283*0a6a1f1dSLionel Sambuc 	Dwarf_Attribute at;
284*0a6a1f1dSLionel Sambuc 	Dwarf_AttrDef ad;
285*0a6a1f1dSLionel Sambuc 	int match, ret;
286*0a6a1f1dSLionel Sambuc 
287*0a6a1f1dSLionel Sambuc 	ds = dbg->dbgp_info;
288*0a6a1f1dSLionel Sambuc 	assert(ds != NULL);
289*0a6a1f1dSLionel Sambuc 
290*0a6a1f1dSLionel Sambuc 	if (pass2)
291*0a6a1f1dSLionel Sambuc 		goto attr_gen;
292*0a6a1f1dSLionel Sambuc 
293*0a6a1f1dSLionel Sambuc 	/*
294*0a6a1f1dSLionel Sambuc 	 * Add DW_AT_sibling attribute for DIEs with children, so consumers
295*0a6a1f1dSLionel Sambuc 	 * can quickly scan chains of siblings, while ignoring the children
296*0a6a1f1dSLionel Sambuc 	 * of individual siblings.
297*0a6a1f1dSLionel Sambuc 	 */
298*0a6a1f1dSLionel Sambuc 	if (die->die_child && die->die_right) {
299*0a6a1f1dSLionel Sambuc 		if (_dwarf_attr_find(die, DW_AT_sibling) == NULL)
300*0a6a1f1dSLionel Sambuc 			(void) dwarf_add_AT_reference(dbg, die, DW_AT_sibling,
301*0a6a1f1dSLionel Sambuc 			    die->die_right, error);
302*0a6a1f1dSLionel Sambuc 	}
303*0a6a1f1dSLionel Sambuc 
304*0a6a1f1dSLionel Sambuc 	/*
305*0a6a1f1dSLionel Sambuc 	 * Search abbrev list to find a matching entry.
306*0a6a1f1dSLionel Sambuc 	 */
307*0a6a1f1dSLionel Sambuc 	die->die_ab = NULL;
308*0a6a1f1dSLionel Sambuc 	for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) {
309*0a6a1f1dSLionel Sambuc 		if (die->die_tag != ab->ab_tag)
310*0a6a1f1dSLionel Sambuc 			continue;
311*0a6a1f1dSLionel Sambuc 		if (ab->ab_children == DW_CHILDREN_no && die->die_child != NULL)
312*0a6a1f1dSLionel Sambuc 			continue;
313*0a6a1f1dSLionel Sambuc 		if (ab->ab_children == DW_CHILDREN_yes &&
314*0a6a1f1dSLionel Sambuc 		    die->die_child == NULL)
315*0a6a1f1dSLionel Sambuc 			continue;
316*0a6a1f1dSLionel Sambuc 		at = STAILQ_FIRST(&die->die_attr);
317*0a6a1f1dSLionel Sambuc 		ad = STAILQ_FIRST(&ab->ab_attrdef);
318*0a6a1f1dSLionel Sambuc 		match = 1;
319*0a6a1f1dSLionel Sambuc 		while (at != NULL && ad != NULL) {
320*0a6a1f1dSLionel Sambuc 			if (at->at_attrib != ad->ad_attrib ||
321*0a6a1f1dSLionel Sambuc 			    at->at_form != ad->ad_form) {
322*0a6a1f1dSLionel Sambuc 				match = 0;
323*0a6a1f1dSLionel Sambuc 				break;
324*0a6a1f1dSLionel Sambuc 			}
325*0a6a1f1dSLionel Sambuc 			at = STAILQ_NEXT(at, at_next);
326*0a6a1f1dSLionel Sambuc 			ad = STAILQ_NEXT(ad, ad_next);
327*0a6a1f1dSLionel Sambuc 		}
328*0a6a1f1dSLionel Sambuc 		if ((at == NULL && ad != NULL) || (at != NULL && ad == NULL))
329*0a6a1f1dSLionel Sambuc 			match = 0;
330*0a6a1f1dSLionel Sambuc 		if (match) {
331*0a6a1f1dSLionel Sambuc 			die->die_ab = ab;
332*0a6a1f1dSLionel Sambuc 			break;
333*0a6a1f1dSLionel Sambuc 		}
334*0a6a1f1dSLionel Sambuc 	}
335*0a6a1f1dSLionel Sambuc 
336*0a6a1f1dSLionel Sambuc 	/*
337*0a6a1f1dSLionel Sambuc 	 * Create a new abbrev entry if we can not reuse any existing one.
338*0a6a1f1dSLionel Sambuc 	 */
339*0a6a1f1dSLionel Sambuc 	if (die->die_ab == NULL) {
340*0a6a1f1dSLionel Sambuc 		ret = _dwarf_abbrev_add(cu, ++cu->cu_abbrev_cnt, die->die_tag,
341*0a6a1f1dSLionel Sambuc 		    die->die_child != NULL ? DW_CHILDREN_yes : DW_CHILDREN_no,
342*0a6a1f1dSLionel Sambuc 		    0, &ab, error);
343*0a6a1f1dSLionel Sambuc 		if (ret != DW_DLE_NONE)
344*0a6a1f1dSLionel Sambuc 			return (ret);
345*0a6a1f1dSLionel Sambuc 		STAILQ_FOREACH(at, &die->die_attr, at_next) {
346*0a6a1f1dSLionel Sambuc 			ret = _dwarf_attrdef_add(dbg, ab, at->at_attrib,
347*0a6a1f1dSLionel Sambuc 			    at->at_form, 0, NULL, error);
348*0a6a1f1dSLionel Sambuc 			if (ret != DW_DLE_NONE)
349*0a6a1f1dSLionel Sambuc 				return (ret);
350*0a6a1f1dSLionel Sambuc 		}
351*0a6a1f1dSLionel Sambuc 		die->die_ab = ab;
352*0a6a1f1dSLionel Sambuc 	}
353*0a6a1f1dSLionel Sambuc 
354*0a6a1f1dSLionel Sambuc 	die->die_offset = ds->ds_size;
355*0a6a1f1dSLionel Sambuc 
356*0a6a1f1dSLionel Sambuc 	/*
357*0a6a1f1dSLionel Sambuc 	 * Transform the DIE to bytes stream.
358*0a6a1f1dSLionel Sambuc 	 */
359*0a6a1f1dSLionel Sambuc 	ret = _dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,
360*0a6a1f1dSLionel Sambuc 	    &ds->ds_size, die->die_ab->ab_entry, error);
361*0a6a1f1dSLionel Sambuc 	if (ret != DW_DLE_NONE)
362*0a6a1f1dSLionel Sambuc 		return (ret);
363*0a6a1f1dSLionel Sambuc 
364*0a6a1f1dSLionel Sambuc attr_gen:
365*0a6a1f1dSLionel Sambuc 
366*0a6a1f1dSLionel Sambuc 	/* Transform the attributes of this DIE. */
367*0a6a1f1dSLionel Sambuc 	ret = _dwarf_attr_gen(dbg, ds, drs, cu, die, pass2, error);
368*0a6a1f1dSLionel Sambuc 	if (ret != DW_DLE_NONE)
369*0a6a1f1dSLionel Sambuc 		return (ret);
370*0a6a1f1dSLionel Sambuc 
371*0a6a1f1dSLionel Sambuc 	/* Proceed to child DIE. */
372*0a6a1f1dSLionel Sambuc 	if (die->die_child != NULL) {
373*0a6a1f1dSLionel Sambuc 		ret = _dwarf_die_gen_recursive(dbg, cu, drs, die->die_child,
374*0a6a1f1dSLionel Sambuc 		    pass2, error);
375*0a6a1f1dSLionel Sambuc 		if (ret != DW_DLE_NONE)
376*0a6a1f1dSLionel Sambuc 			return (ret);
377*0a6a1f1dSLionel Sambuc 	}
378*0a6a1f1dSLionel Sambuc 
379*0a6a1f1dSLionel Sambuc 	/* Proceed to sibling DIE. */
380*0a6a1f1dSLionel Sambuc 	if (die->die_right != NULL) {
381*0a6a1f1dSLionel Sambuc 		ret = _dwarf_die_gen_recursive(dbg, cu, drs, die->die_right,
382*0a6a1f1dSLionel Sambuc 		    pass2, error);
383*0a6a1f1dSLionel Sambuc 		if (ret != DW_DLE_NONE)
384*0a6a1f1dSLionel Sambuc 			return (ret);
385*0a6a1f1dSLionel Sambuc 	}
386*0a6a1f1dSLionel Sambuc 
387*0a6a1f1dSLionel Sambuc 	/* Write a null DIE indicating the end of current level. */
388*0a6a1f1dSLionel Sambuc 	if (die->die_right == NULL) {
389*0a6a1f1dSLionel Sambuc 		ret = _dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,
390*0a6a1f1dSLionel Sambuc 		    &ds->ds_size, 0, error);
391*0a6a1f1dSLionel Sambuc 		if (ret != DW_DLE_NONE)
392*0a6a1f1dSLionel Sambuc 			return (ret);
393*0a6a1f1dSLionel Sambuc 	}
394*0a6a1f1dSLionel Sambuc 
395*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
396*0a6a1f1dSLionel Sambuc }
397*0a6a1f1dSLionel Sambuc 
398*0a6a1f1dSLionel Sambuc int
_dwarf_die_gen(Dwarf_P_Debug dbg,Dwarf_CU cu,Dwarf_Rel_Section drs,Dwarf_Error * error)399*0a6a1f1dSLionel Sambuc _dwarf_die_gen(Dwarf_P_Debug dbg, Dwarf_CU cu, Dwarf_Rel_Section drs,
400*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
401*0a6a1f1dSLionel Sambuc {
402*0a6a1f1dSLionel Sambuc 	Dwarf_Abbrev ab, tab;
403*0a6a1f1dSLionel Sambuc 	Dwarf_AttrDef ad, tad;
404*0a6a1f1dSLionel Sambuc 	Dwarf_Die die;
405*0a6a1f1dSLionel Sambuc 	int ret;
406*0a6a1f1dSLionel Sambuc 
407*0a6a1f1dSLionel Sambuc 	assert(dbg != NULL && cu != NULL);
408*0a6a1f1dSLionel Sambuc 	assert(dbg->dbgp_root_die != NULL);
409*0a6a1f1dSLionel Sambuc 
410*0a6a1f1dSLionel Sambuc 	die = dbg->dbgp_root_die;
411*0a6a1f1dSLionel Sambuc 
412*0a6a1f1dSLionel Sambuc 	/*
413*0a6a1f1dSLionel Sambuc 	 * Insert a DW_AT_stmt_list attribute into root DIE, if there are
414*0a6a1f1dSLionel Sambuc 	 * line number information.
415*0a6a1f1dSLionel Sambuc 	 */
416*0a6a1f1dSLionel Sambuc 	if (!STAILQ_EMPTY(&dbg->dbgp_lineinfo->li_lnlist))
417*0a6a1f1dSLionel Sambuc 		RCHECK(_dwarf_add_AT_dataref(dbg, die, DW_AT_stmt_list, 0, 0,
418*0a6a1f1dSLionel Sambuc 		    ".debug_line", NULL, error));
419*0a6a1f1dSLionel Sambuc 
420*0a6a1f1dSLionel Sambuc 	RCHECK(_dwarf_die_gen_recursive(dbg, cu, drs, die, 0, error));
421*0a6a1f1dSLionel Sambuc 
422*0a6a1f1dSLionel Sambuc 	if (cu->cu_pass2)
423*0a6a1f1dSLionel Sambuc 		RCHECK(_dwarf_die_gen_recursive(dbg, cu, drs, die, 1, error));
424*0a6a1f1dSLionel Sambuc 
425*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
426*0a6a1f1dSLionel Sambuc 
427*0a6a1f1dSLionel Sambuc gen_fail:
428*0a6a1f1dSLionel Sambuc 
429*0a6a1f1dSLionel Sambuc 	HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) {
430*0a6a1f1dSLionel Sambuc 		HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab);
431*0a6a1f1dSLionel Sambuc 		STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) {
432*0a6a1f1dSLionel Sambuc 			STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef,
433*0a6a1f1dSLionel Sambuc 			    ad_next);
434*0a6a1f1dSLionel Sambuc 			free(ad);
435*0a6a1f1dSLionel Sambuc 		}
436*0a6a1f1dSLionel Sambuc 		free(ab);
437*0a6a1f1dSLionel Sambuc 	}
438*0a6a1f1dSLionel Sambuc 
439*0a6a1f1dSLionel Sambuc 	return (ret);
440*0a6a1f1dSLionel Sambuc }
441*0a6a1f1dSLionel Sambuc 
442*0a6a1f1dSLionel Sambuc void
_dwarf_die_pro_cleanup(Dwarf_P_Debug dbg)443*0a6a1f1dSLionel Sambuc _dwarf_die_pro_cleanup(Dwarf_P_Debug dbg)
444*0a6a1f1dSLionel Sambuc {
445*0a6a1f1dSLionel Sambuc 	Dwarf_P_Die die, tdie;
446*0a6a1f1dSLionel Sambuc 	Dwarf_P_Attribute at, tat;
447*0a6a1f1dSLionel Sambuc 
448*0a6a1f1dSLionel Sambuc 	assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
449*0a6a1f1dSLionel Sambuc 
450*0a6a1f1dSLionel Sambuc 	STAILQ_FOREACH_SAFE(die, &dbg->dbgp_dielist, die_pro_next, tdie) {
451*0a6a1f1dSLionel Sambuc 		STAILQ_FOREACH_SAFE(at, &die->die_attr, at_next, tat) {
452*0a6a1f1dSLionel Sambuc 			STAILQ_REMOVE(&die->die_attr, at, _Dwarf_Attribute,
453*0a6a1f1dSLionel Sambuc 			    at_next);
454*0a6a1f1dSLionel Sambuc 			free(at);
455*0a6a1f1dSLionel Sambuc 		}
456*0a6a1f1dSLionel Sambuc 		free(die);
457*0a6a1f1dSLionel Sambuc 	}
458*0a6a1f1dSLionel Sambuc }
459