xref: /minix3/external/bsd/elftoolchain/dist/libdwarf/libdwarf_loclist.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: libdwarf_loclist.c,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2009,2011 Kai Wang
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc  * are met:
10*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  *
16*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc  */
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libdwarf_loclist.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libdwarf_loclist.c 2972 2013-12-23 06:46:04Z kaiwang27 ");
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc static int
_dwarf_loclist_add_locdesc(Dwarf_Debug dbg,Dwarf_CU cu,Dwarf_Section * ds,uint64_t * off,Dwarf_Locdesc ** ld,uint64_t * ldlen,Dwarf_Unsigned * total_len,Dwarf_Error * error)35*0a6a1f1dSLionel Sambuc _dwarf_loclist_add_locdesc(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds,
36*0a6a1f1dSLionel Sambuc     uint64_t *off, Dwarf_Locdesc **ld, uint64_t *ldlen,
37*0a6a1f1dSLionel Sambuc     Dwarf_Unsigned *total_len, Dwarf_Error *error)
38*0a6a1f1dSLionel Sambuc {
39*0a6a1f1dSLionel Sambuc 	uint64_t start, end;
40*0a6a1f1dSLionel Sambuc 	int i, len, ret;
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc 	if (total_len != NULL)
43*0a6a1f1dSLionel Sambuc 		*total_len = 0;
44*0a6a1f1dSLionel Sambuc 
45*0a6a1f1dSLionel Sambuc 	for (i = 0; *off < ds->ds_size; i++) {
46*0a6a1f1dSLionel Sambuc 		start = dbg->read(ds->ds_data, off, cu->cu_pointer_size);
47*0a6a1f1dSLionel Sambuc 		end = dbg->read(ds->ds_data, off, cu->cu_pointer_size);
48*0a6a1f1dSLionel Sambuc 		if (ld != NULL) {
49*0a6a1f1dSLionel Sambuc 			ld[i]->ld_lopc = start;
50*0a6a1f1dSLionel Sambuc 			ld[i]->ld_hipc = end;
51*0a6a1f1dSLionel Sambuc 		}
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc 		if (total_len != NULL)
54*0a6a1f1dSLionel Sambuc 			*total_len += 2 * cu->cu_pointer_size;
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc 		/* Check if it is the end entry. */
57*0a6a1f1dSLionel Sambuc 		if (start == 0 && end ==0) {
58*0a6a1f1dSLionel Sambuc 			i++;
59*0a6a1f1dSLionel Sambuc 			break;
60*0a6a1f1dSLionel Sambuc 		}
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc 		/* Check if it is base-select entry. */
63*0a6a1f1dSLionel Sambuc 		if ((cu->cu_pointer_size == 4 && start == ~0U) ||
64*0a6a1f1dSLionel Sambuc 		    (cu->cu_pointer_size == 8 && start == ~0ULL))
65*0a6a1f1dSLionel Sambuc 			continue;
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc 		/* Otherwise it's normal entry. */
68*0a6a1f1dSLionel Sambuc 		len = dbg->read(ds->ds_data, off, 2);
69*0a6a1f1dSLionel Sambuc 		if (*off + len > ds->ds_size) {
70*0a6a1f1dSLionel Sambuc 			DWARF_SET_ERROR(dbg, error,
71*0a6a1f1dSLionel Sambuc 			    DW_DLE_DEBUG_LOC_SECTION_SHORT);
72*0a6a1f1dSLionel Sambuc 			return (DW_DLE_DEBUG_LOC_SECTION_SHORT);
73*0a6a1f1dSLionel Sambuc 		}
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc 		if (total_len != NULL)
76*0a6a1f1dSLionel Sambuc 			*total_len += len;
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc 		if (ld != NULL) {
79*0a6a1f1dSLionel Sambuc 			ret = _dwarf_loc_fill_locdesc(dbg, ld[i],
80*0a6a1f1dSLionel Sambuc 			    ds->ds_data + *off, len, cu->cu_pointer_size,
81*0a6a1f1dSLionel Sambuc 			    error);
82*0a6a1f1dSLionel Sambuc 			if (ret != DW_DLE_NONE)
83*0a6a1f1dSLionel Sambuc 				return (ret);
84*0a6a1f1dSLionel Sambuc 		}
85*0a6a1f1dSLionel Sambuc 
86*0a6a1f1dSLionel Sambuc 		*off += len;
87*0a6a1f1dSLionel Sambuc 	}
88*0a6a1f1dSLionel Sambuc 
89*0a6a1f1dSLionel Sambuc 	if (ldlen != NULL)
90*0a6a1f1dSLionel Sambuc 		*ldlen = i;
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
93*0a6a1f1dSLionel Sambuc }
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc int
_dwarf_loclist_find(Dwarf_Debug dbg,Dwarf_CU cu,uint64_t lloff,Dwarf_Loclist * ret_ll,Dwarf_Error * error)96*0a6a1f1dSLionel Sambuc _dwarf_loclist_find(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t lloff,
97*0a6a1f1dSLionel Sambuc     Dwarf_Loclist *ret_ll, Dwarf_Error *error)
98*0a6a1f1dSLionel Sambuc {
99*0a6a1f1dSLionel Sambuc 	Dwarf_Loclist ll;
100*0a6a1f1dSLionel Sambuc 	int ret;
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc 	assert(ret_ll != NULL);
103*0a6a1f1dSLionel Sambuc 	ret = DW_DLE_NONE;
104*0a6a1f1dSLionel Sambuc 
105*0a6a1f1dSLionel Sambuc 	TAILQ_FOREACH(ll, &dbg->dbg_loclist, ll_next)
106*0a6a1f1dSLionel Sambuc 		if (ll->ll_offset == lloff)
107*0a6a1f1dSLionel Sambuc 			break;
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc 	if (ll == NULL)
110*0a6a1f1dSLionel Sambuc 		ret = _dwarf_loclist_add(dbg, cu, lloff, ret_ll, error);
111*0a6a1f1dSLionel Sambuc 	else
112*0a6a1f1dSLionel Sambuc 		*ret_ll = ll;
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc 	return (ret);
115*0a6a1f1dSLionel Sambuc }
116*0a6a1f1dSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc int
_dwarf_loclist_add(Dwarf_Debug dbg,Dwarf_CU cu,uint64_t lloff,Dwarf_Loclist * ret_ll,Dwarf_Error * error)118*0a6a1f1dSLionel Sambuc _dwarf_loclist_add(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t lloff,
119*0a6a1f1dSLionel Sambuc     Dwarf_Loclist *ret_ll, Dwarf_Error *error)
120*0a6a1f1dSLionel Sambuc {
121*0a6a1f1dSLionel Sambuc 	Dwarf_Section *ds;
122*0a6a1f1dSLionel Sambuc 	Dwarf_Loclist ll, tll;
123*0a6a1f1dSLionel Sambuc 	uint64_t ldlen;
124*0a6a1f1dSLionel Sambuc 	int i, ret;
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc 	ret = DW_DLE_NONE;
127*0a6a1f1dSLionel Sambuc 
128*0a6a1f1dSLionel Sambuc 	if ((ds = _dwarf_find_section(dbg, ".debug_loc")) == NULL) {
129*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
130*0a6a1f1dSLionel Sambuc 		return (DW_DLE_NO_ENTRY);
131*0a6a1f1dSLionel Sambuc 	}
132*0a6a1f1dSLionel Sambuc 
133*0a6a1f1dSLionel Sambuc 	if (lloff >= ds->ds_size) {
134*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
135*0a6a1f1dSLionel Sambuc 		return (DW_DLE_NO_ENTRY);
136*0a6a1f1dSLionel Sambuc 	}
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc 	if ((ll = malloc(sizeof(struct _Dwarf_Loclist))) == NULL) {
139*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
140*0a6a1f1dSLionel Sambuc 		return (DW_DLE_MEMORY);
141*0a6a1f1dSLionel Sambuc 	}
142*0a6a1f1dSLionel Sambuc 
143*0a6a1f1dSLionel Sambuc 	ll->ll_offset = lloff;
144*0a6a1f1dSLionel Sambuc 
145*0a6a1f1dSLionel Sambuc 	/* Get the number of locdesc the first round. */
146*0a6a1f1dSLionel Sambuc 	ret = _dwarf_loclist_add_locdesc(dbg, cu, ds, &lloff, NULL, &ldlen,
147*0a6a1f1dSLionel Sambuc 	    NULL, error);
148*0a6a1f1dSLionel Sambuc 	if (ret != DW_DLE_NONE)
149*0a6a1f1dSLionel Sambuc 		goto fail_cleanup;
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc 	/*
152*0a6a1f1dSLionel Sambuc 	 * Dwarf_Locdesc list memory is allocated in this way (one more level
153*0a6a1f1dSLionel Sambuc 	 * of indirect) to make the loclist API be compatible with SGI libdwarf.
154*0a6a1f1dSLionel Sambuc 	 */
155*0a6a1f1dSLionel Sambuc 	ll->ll_ldlen = ldlen;
156*0a6a1f1dSLionel Sambuc 	if (ldlen != 0) {
157*0a6a1f1dSLionel Sambuc 		if ((ll->ll_ldlist = calloc(ldlen, sizeof(Dwarf_Locdesc *))) ==
158*0a6a1f1dSLionel Sambuc 		    NULL) {
159*0a6a1f1dSLionel Sambuc 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
160*0a6a1f1dSLionel Sambuc 			ret = DW_DLE_MEMORY;
161*0a6a1f1dSLionel Sambuc 			goto fail_cleanup;
162*0a6a1f1dSLionel Sambuc 		}
163*0a6a1f1dSLionel Sambuc 		for (i = 0; (uint64_t) i < ldlen; i++) {
164*0a6a1f1dSLionel Sambuc 			if ((ll->ll_ldlist[i] =
165*0a6a1f1dSLionel Sambuc 			    calloc(1, sizeof(Dwarf_Locdesc))) == NULL) {
166*0a6a1f1dSLionel Sambuc 				DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
167*0a6a1f1dSLionel Sambuc 				ret = DW_DLE_MEMORY;
168*0a6a1f1dSLionel Sambuc 				goto fail_cleanup;
169*0a6a1f1dSLionel Sambuc 			}
170*0a6a1f1dSLionel Sambuc 		}
171*0a6a1f1dSLionel Sambuc 	} else
172*0a6a1f1dSLionel Sambuc 		ll->ll_ldlist = NULL;
173*0a6a1f1dSLionel Sambuc 
174*0a6a1f1dSLionel Sambuc 	lloff = ll->ll_offset;
175*0a6a1f1dSLionel Sambuc 
176*0a6a1f1dSLionel Sambuc 	/* Fill in locdesc. */
177*0a6a1f1dSLionel Sambuc 	ret = _dwarf_loclist_add_locdesc(dbg, cu, ds, &lloff, ll->ll_ldlist,
178*0a6a1f1dSLionel Sambuc 	    NULL, &ll->ll_length, error);
179*0a6a1f1dSLionel Sambuc 	if (ret != DW_DLE_NONE)
180*0a6a1f1dSLionel Sambuc 		goto fail_cleanup;
181*0a6a1f1dSLionel Sambuc 
182*0a6a1f1dSLionel Sambuc 	/* Insert to the queue. Sort by offset. */
183*0a6a1f1dSLionel Sambuc 	TAILQ_FOREACH(tll, &dbg->dbg_loclist, ll_next)
184*0a6a1f1dSLionel Sambuc 		if (tll->ll_offset > ll->ll_offset) {
185*0a6a1f1dSLionel Sambuc 			TAILQ_INSERT_BEFORE(tll, ll, ll_next);
186*0a6a1f1dSLionel Sambuc 			break;
187*0a6a1f1dSLionel Sambuc 		}
188*0a6a1f1dSLionel Sambuc 
189*0a6a1f1dSLionel Sambuc 	if (tll == NULL)
190*0a6a1f1dSLionel Sambuc 		TAILQ_INSERT_TAIL(&dbg->dbg_loclist, ll, ll_next);
191*0a6a1f1dSLionel Sambuc 
192*0a6a1f1dSLionel Sambuc 	*ret_ll = ll;
193*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
194*0a6a1f1dSLionel Sambuc 
195*0a6a1f1dSLionel Sambuc fail_cleanup:
196*0a6a1f1dSLionel Sambuc 
197*0a6a1f1dSLionel Sambuc 	_dwarf_loclist_free(ll);
198*0a6a1f1dSLionel Sambuc 
199*0a6a1f1dSLionel Sambuc 	return (ret);
200*0a6a1f1dSLionel Sambuc }
201*0a6a1f1dSLionel Sambuc 
202*0a6a1f1dSLionel Sambuc void
_dwarf_loclist_free(Dwarf_Loclist ll)203*0a6a1f1dSLionel Sambuc _dwarf_loclist_free(Dwarf_Loclist ll)
204*0a6a1f1dSLionel Sambuc {
205*0a6a1f1dSLionel Sambuc 	int i;
206*0a6a1f1dSLionel Sambuc 
207*0a6a1f1dSLionel Sambuc 	if (ll == NULL)
208*0a6a1f1dSLionel Sambuc 		return;
209*0a6a1f1dSLionel Sambuc 
210*0a6a1f1dSLionel Sambuc 	if (ll->ll_ldlist != NULL) {
211*0a6a1f1dSLionel Sambuc 		for (i = 0; i < ll->ll_ldlen; i++) {
212*0a6a1f1dSLionel Sambuc 			if (ll->ll_ldlist[i]->ld_s)
213*0a6a1f1dSLionel Sambuc 				free(ll->ll_ldlist[i]->ld_s);
214*0a6a1f1dSLionel Sambuc 			free(ll->ll_ldlist[i]);
215*0a6a1f1dSLionel Sambuc 		}
216*0a6a1f1dSLionel Sambuc 		free(ll->ll_ldlist);
217*0a6a1f1dSLionel Sambuc 	}
218*0a6a1f1dSLionel Sambuc 	free(ll);
219*0a6a1f1dSLionel Sambuc }
220*0a6a1f1dSLionel Sambuc 
221*0a6a1f1dSLionel Sambuc void
_dwarf_loclist_cleanup(Dwarf_Debug dbg)222*0a6a1f1dSLionel Sambuc _dwarf_loclist_cleanup(Dwarf_Debug dbg)
223*0a6a1f1dSLionel Sambuc {
224*0a6a1f1dSLionel Sambuc 	Dwarf_Loclist ll, tll;
225*0a6a1f1dSLionel Sambuc 
226*0a6a1f1dSLionel Sambuc 	assert(dbg != NULL && dbg->dbg_mode == DW_DLC_READ);
227*0a6a1f1dSLionel Sambuc 
228*0a6a1f1dSLionel Sambuc 	TAILQ_FOREACH_SAFE(ll, &dbg->dbg_loclist, ll_next, tll) {
229*0a6a1f1dSLionel Sambuc 		TAILQ_REMOVE(&dbg->dbg_loclist, ll, ll_next);
230*0a6a1f1dSLionel Sambuc 		_dwarf_loclist_free(ll);
231*0a6a1f1dSLionel Sambuc 	}
232*0a6a1f1dSLionel Sambuc }
233