1 /*	$NetBSD: dwarf_arange.c,v 1.2 2014/03/09 16:58:03 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009,2011 Kai Wang
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "_libdwarf.h"
30 
31 __RCSID("$NetBSD: dwarf_arange.c,v 1.2 2014/03/09 16:58:03 christos Exp $");
32 ELFTC_VCSID("Id: dwarf_arange.c 2072 2011-10-27 03:26:49Z jkoshy ");
33 
34 int
dwarf_get_aranges(Dwarf_Debug dbg,Dwarf_Arange ** arlist,Dwarf_Signed * ret_arange_cnt,Dwarf_Error * error)35 dwarf_get_aranges(Dwarf_Debug dbg, Dwarf_Arange **arlist,
36     Dwarf_Signed *ret_arange_cnt, Dwarf_Error *error)
37 {
38 
39 	if (dbg == NULL || arlist == NULL || ret_arange_cnt == NULL) {
40 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
41 		return (DW_DLV_ERROR);
42 	}
43 
44 	if (dbg->dbg_arange_cnt == 0) {
45 		if (_dwarf_arange_init(dbg, error) != DW_DLE_NONE)
46 			return (DW_DLV_ERROR);
47 		if (dbg->dbg_arange_cnt == 0) {
48 			DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
49 			return (DW_DLV_NO_ENTRY);
50 		}
51 	}
52 
53 	assert(dbg->dbg_arange_array != NULL);
54 
55 	*arlist = dbg->dbg_arange_array;
56 	*ret_arange_cnt = dbg->dbg_arange_cnt;
57 
58 	return (DW_DLV_OK);
59 }
60 
61 int
dwarf_get_arange(Dwarf_Arange * arlist,Dwarf_Unsigned arange_cnt,Dwarf_Addr addr,Dwarf_Arange * ret_arange,Dwarf_Error * error)62 dwarf_get_arange(Dwarf_Arange *arlist, Dwarf_Unsigned arange_cnt,
63     Dwarf_Addr addr, Dwarf_Arange *ret_arange, Dwarf_Error *error)
64 {
65 	Dwarf_Arange ar;
66 	Dwarf_Debug dbg;
67 	int i;
68 
69 	if (arlist == NULL) {
70 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
71 		return (DW_DLV_ERROR);
72 	}
73 
74 	dbg = (*arlist)->ar_as->as_cu->cu_dbg;
75 
76 	if (ret_arange == NULL || arange_cnt == 0) {
77 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
78 		return (DW_DLV_ERROR);
79 	}
80 
81 	for (i = 0; (Dwarf_Unsigned)i < arange_cnt; i++) {
82 		ar = arlist[i];
83 		if (addr >= ar->ar_address && addr < ar->ar_address +
84 		    ar->ar_range) {
85 			*ret_arange = ar;
86 			return (DW_DLV_OK);
87 		}
88 	}
89 
90 	DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
91 
92 	return (DW_DLV_NO_ENTRY);
93 }
94 
95 int
dwarf_get_cu_die_offset(Dwarf_Arange ar,Dwarf_Off * ret_offset,Dwarf_Error * error)96 dwarf_get_cu_die_offset(Dwarf_Arange ar, Dwarf_Off *ret_offset,
97     Dwarf_Error *error)
98 {
99 	Dwarf_CU cu;
100 	Dwarf_ArangeSet as;
101 
102 	if (ar == NULL) {
103 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
104 		return (DW_DLV_ERROR);
105 	}
106 
107 	as = ar->ar_as;
108 	assert(as != NULL);
109 	cu = as->as_cu;
110 	assert(cu != NULL);
111 
112 	if (ret_offset == NULL) {
113 		DWARF_SET_ERROR(cu->cu_dbg, error, DW_DLE_ARGUMENT);
114 		return (DW_DLV_ERROR);
115 	}
116 
117 	*ret_offset = cu->cu_1st_offset;
118 
119 	return (DW_DLV_OK);
120 }
121 
122 int
dwarf_get_arange_cu_header_offset(Dwarf_Arange ar,Dwarf_Off * ret_offset,Dwarf_Error * error)123 dwarf_get_arange_cu_header_offset(Dwarf_Arange ar, Dwarf_Off *ret_offset,
124     Dwarf_Error *error)
125 {
126 	Dwarf_ArangeSet as;
127 
128 	if (ar == NULL) {
129 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
130 		return (DW_DLV_ERROR);
131 	}
132 
133 	as = ar->ar_as;
134 	assert(as != NULL);
135 
136 	if (ret_offset == NULL) {
137 		DWARF_SET_ERROR(as->as_cu->cu_dbg, error, DW_DLE_ARGUMENT);
138 		return (DW_DLV_ERROR);
139 	}
140 
141 	*ret_offset = as->as_cu_offset;
142 
143 	return (DW_DLV_OK);
144 }
145 
146 int
dwarf_get_arange_info(Dwarf_Arange ar,Dwarf_Addr * start,Dwarf_Unsigned * length,Dwarf_Off * cu_die_offset,Dwarf_Error * error)147 dwarf_get_arange_info(Dwarf_Arange ar, Dwarf_Addr *start,
148     Dwarf_Unsigned *length, Dwarf_Off *cu_die_offset, Dwarf_Error *error)
149 {
150 	Dwarf_CU cu;
151 	Dwarf_ArangeSet as;
152 
153 	if (ar == NULL) {
154 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
155 		return (DW_DLV_ERROR);
156 	}
157 
158 	as = ar->ar_as;
159 	assert(as != NULL);
160 	cu = as->as_cu;
161 	assert(cu != NULL);
162 
163 	if (start == NULL || length == NULL ||
164 	    cu_die_offset == NULL) {
165 		DWARF_SET_ERROR(cu->cu_dbg, error, DW_DLE_ARGUMENT);
166 		return (DW_DLV_ERROR);
167 	}
168 
169 	*start = ar->ar_address;
170 	*length = ar->ar_range;
171 	*cu_die_offset = cu->cu_1st_offset;
172 
173 	return (DW_DLV_OK);
174 }
175