xref: /minix3/external/bsd/elftoolchain/dist/libdwarf/libdwarf_ranges.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: libdwarf_ranges.c,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2009 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_ranges.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libdwarf_ranges.c 2972 2013-12-23 06:46:04Z kaiwang27 ");
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc static int
_dwarf_ranges_parse(Dwarf_Debug dbg,Dwarf_CU cu,Dwarf_Section * ds,uint64_t off,Dwarf_Ranges * rg,Dwarf_Unsigned * cnt)35*0a6a1f1dSLionel Sambuc _dwarf_ranges_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds,
36*0a6a1f1dSLionel Sambuc     uint64_t off, Dwarf_Ranges *rg, Dwarf_Unsigned *cnt)
37*0a6a1f1dSLionel Sambuc {
38*0a6a1f1dSLionel Sambuc 	Dwarf_Unsigned start, end;
39*0a6a1f1dSLionel Sambuc 	int i;
40*0a6a1f1dSLionel Sambuc 
41*0a6a1f1dSLionel Sambuc 	i = 0;
42*0a6a1f1dSLionel Sambuc 	while (off < ds->ds_size) {
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc 		start = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
45*0a6a1f1dSLionel Sambuc 		end = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc 		if (rg != NULL) {
48*0a6a1f1dSLionel Sambuc 			rg[i].dwr_addr1 = start;
49*0a6a1f1dSLionel Sambuc 			rg[i].dwr_addr2 = end;
50*0a6a1f1dSLionel Sambuc 			if (start == 0 && end == 0)
51*0a6a1f1dSLionel Sambuc 				rg[i].dwr_type = DW_RANGES_END;
52*0a6a1f1dSLionel Sambuc 			else if ((start == ~0U && cu->cu_pointer_size == 4) ||
53*0a6a1f1dSLionel Sambuc 			    (start == ~0ULL && cu->cu_pointer_size == 8))
54*0a6a1f1dSLionel Sambuc 				rg[i].dwr_type = DW_RANGES_ADDRESS_SELECTION;
55*0a6a1f1dSLionel Sambuc 			else
56*0a6a1f1dSLionel Sambuc 				rg[i].dwr_type = DW_RANGES_ENTRY;
57*0a6a1f1dSLionel Sambuc 		}
58*0a6a1f1dSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc 		i++;
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc 		if (start == 0 && end == 0)
62*0a6a1f1dSLionel Sambuc 			break;
63*0a6a1f1dSLionel Sambuc 	}
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc 	if (cnt != NULL)
66*0a6a1f1dSLionel Sambuc 		*cnt = i;
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
69*0a6a1f1dSLionel Sambuc }
70*0a6a1f1dSLionel Sambuc 
71*0a6a1f1dSLionel Sambuc int
_dwarf_ranges_find(Dwarf_Debug dbg,uint64_t off,Dwarf_Rangelist * ret_rl)72*0a6a1f1dSLionel Sambuc _dwarf_ranges_find(Dwarf_Debug dbg, uint64_t off, Dwarf_Rangelist *ret_rl)
73*0a6a1f1dSLionel Sambuc {
74*0a6a1f1dSLionel Sambuc 	Dwarf_Rangelist rl;
75*0a6a1f1dSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc 	STAILQ_FOREACH(rl, &dbg->dbg_rllist, rl_next)
77*0a6a1f1dSLionel Sambuc 		if (rl->rl_offset == off)
78*0a6a1f1dSLionel Sambuc 			break;
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc 	if (rl == NULL)
81*0a6a1f1dSLionel Sambuc 		return (DW_DLE_NO_ENTRY);
82*0a6a1f1dSLionel Sambuc 
83*0a6a1f1dSLionel Sambuc 	if (ret_rl != NULL)
84*0a6a1f1dSLionel Sambuc 		*ret_rl = rl;
85*0a6a1f1dSLionel Sambuc 
86*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
87*0a6a1f1dSLionel Sambuc }
88*0a6a1f1dSLionel Sambuc 
89*0a6a1f1dSLionel Sambuc void
_dwarf_ranges_cleanup(Dwarf_Debug dbg)90*0a6a1f1dSLionel Sambuc _dwarf_ranges_cleanup(Dwarf_Debug dbg)
91*0a6a1f1dSLionel Sambuc {
92*0a6a1f1dSLionel Sambuc 	Dwarf_Rangelist rl, trl;
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc 	if (STAILQ_EMPTY(&dbg->dbg_rllist))
95*0a6a1f1dSLionel Sambuc 		return;
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc 	STAILQ_FOREACH_SAFE(rl, &dbg->dbg_rllist, rl_next, trl) {
98*0a6a1f1dSLionel Sambuc 		STAILQ_REMOVE(&dbg->dbg_rllist, rl, _Dwarf_Rangelist, rl_next);
99*0a6a1f1dSLionel Sambuc 		if (rl->rl_rgarray)
100*0a6a1f1dSLionel Sambuc 			free(rl->rl_rgarray);
101*0a6a1f1dSLionel Sambuc 		free(rl);
102*0a6a1f1dSLionel Sambuc 	}
103*0a6a1f1dSLionel Sambuc }
104*0a6a1f1dSLionel Sambuc 
105*0a6a1f1dSLionel Sambuc int
_dwarf_ranges_add(Dwarf_Debug dbg,Dwarf_CU cu,uint64_t off,Dwarf_Rangelist * ret_rl,Dwarf_Error * error)106*0a6a1f1dSLionel Sambuc _dwarf_ranges_add(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t off,
107*0a6a1f1dSLionel Sambuc     Dwarf_Rangelist *ret_rl, Dwarf_Error *error)
108*0a6a1f1dSLionel Sambuc {
109*0a6a1f1dSLionel Sambuc 	Dwarf_Section *ds;
110*0a6a1f1dSLionel Sambuc 	Dwarf_Rangelist rl;
111*0a6a1f1dSLionel Sambuc 	Dwarf_Unsigned cnt;
112*0a6a1f1dSLionel Sambuc 	int ret;
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc 	if ((ds = _dwarf_find_section(dbg, ".debug_ranges")) == NULL) {
115*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
116*0a6a1f1dSLionel Sambuc 		return (DW_DLE_NO_ENTRY);
117*0a6a1f1dSLionel Sambuc 	}
118*0a6a1f1dSLionel Sambuc 
119*0a6a1f1dSLionel Sambuc 	if ((rl = malloc(sizeof(struct _Dwarf_Rangelist))) == NULL) {
120*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
121*0a6a1f1dSLionel Sambuc 		return (DW_DLE_MEMORY);
122*0a6a1f1dSLionel Sambuc 	}
123*0a6a1f1dSLionel Sambuc 
124*0a6a1f1dSLionel Sambuc 	rl->rl_offset = off;
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc 	ret = _dwarf_ranges_parse(dbg, cu, ds, off, NULL, &cnt);
127*0a6a1f1dSLionel Sambuc 	if (ret != DW_DLE_NONE) {
128*0a6a1f1dSLionel Sambuc 		free(rl);
129*0a6a1f1dSLionel Sambuc 		return (ret);
130*0a6a1f1dSLionel Sambuc 	}
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc 	rl->rl_rglen = cnt;
133*0a6a1f1dSLionel Sambuc 	if (cnt != 0) {
134*0a6a1f1dSLionel Sambuc 		if ((rl->rl_rgarray = calloc(cnt, sizeof(Dwarf_Ranges))) ==
135*0a6a1f1dSLionel Sambuc 		    NULL) {
136*0a6a1f1dSLionel Sambuc 			free(rl);
137*0a6a1f1dSLionel Sambuc 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
138*0a6a1f1dSLionel Sambuc 			return (DW_DLE_MEMORY);
139*0a6a1f1dSLionel Sambuc 		}
140*0a6a1f1dSLionel Sambuc 
141*0a6a1f1dSLionel Sambuc 		ret = _dwarf_ranges_parse(dbg, cu, ds, off, rl->rl_rgarray,
142*0a6a1f1dSLionel Sambuc 		    NULL);
143*0a6a1f1dSLionel Sambuc 		if (ret != DW_DLE_NONE) {
144*0a6a1f1dSLionel Sambuc 			free(rl->rl_rgarray);
145*0a6a1f1dSLionel Sambuc 			free(rl);
146*0a6a1f1dSLionel Sambuc 			return (ret);
147*0a6a1f1dSLionel Sambuc 		}
148*0a6a1f1dSLionel Sambuc 	} else
149*0a6a1f1dSLionel Sambuc 		rl->rl_rgarray = NULL;
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc 	STAILQ_INSERT_TAIL(&dbg->dbg_rllist, rl, rl_next);
152*0a6a1f1dSLionel Sambuc 	*ret_rl = rl;
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc 	return (DW_DLE_NONE);
155*0a6a1f1dSLionel Sambuc }
156