xref: /freebsd/contrib/elftoolchain/libdwarf/libdwarf_ranges.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2009 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "_libdwarf.h"
28 
29 ELFTC_VCSID("$Id: libdwarf_ranges.c 2972 2013-12-23 06:46:04Z kaiwang27 $");
30 
31 static int
32 _dwarf_ranges_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds,
33     uint64_t off, Dwarf_Ranges *rg, Dwarf_Unsigned *cnt)
34 {
35 	Dwarf_Unsigned start, end;
36 	int i;
37 
38 	i = 0;
39 	while (off < ds->ds_size) {
40 
41 		start = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
42 		end = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
43 
44 		if (rg != NULL) {
45 			rg[i].dwr_addr1 = start;
46 			rg[i].dwr_addr2 = end;
47 			if (start == 0 && end == 0)
48 				rg[i].dwr_type = DW_RANGES_END;
49 			else if ((start == ~0U && cu->cu_pointer_size == 4) ||
50 			    (start == ~0ULL && cu->cu_pointer_size == 8))
51 				rg[i].dwr_type = DW_RANGES_ADDRESS_SELECTION;
52 			else
53 				rg[i].dwr_type = DW_RANGES_ENTRY;
54 		}
55 
56 		i++;
57 
58 		if (start == 0 && end == 0)
59 			break;
60 	}
61 
62 	if (cnt != NULL)
63 		*cnt = i;
64 
65 	return (DW_DLE_NONE);
66 }
67 
68 int
69 _dwarf_ranges_find(Dwarf_Debug dbg, uint64_t off, Dwarf_Rangelist *ret_rl)
70 {
71 	Dwarf_Rangelist rl;
72 
73 	STAILQ_FOREACH(rl, &dbg->dbg_rllist, rl_next)
74 		if (rl->rl_offset == off)
75 			break;
76 
77 	if (rl == NULL)
78 		return (DW_DLE_NO_ENTRY);
79 
80 	if (ret_rl != NULL)
81 		*ret_rl = rl;
82 
83 	return (DW_DLE_NONE);
84 }
85 
86 void
87 _dwarf_ranges_cleanup(Dwarf_Debug dbg)
88 {
89 	Dwarf_Rangelist rl, trl;
90 
91 	if (STAILQ_EMPTY(&dbg->dbg_rllist))
92 		return;
93 
94 	STAILQ_FOREACH_SAFE(rl, &dbg->dbg_rllist, rl_next, trl) {
95 		STAILQ_REMOVE(&dbg->dbg_rllist, rl, _Dwarf_Rangelist, rl_next);
96 		if (rl->rl_rgarray)
97 			free(rl->rl_rgarray);
98 		free(rl);
99 	}
100 }
101 
102 int
103 _dwarf_ranges_add(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t off,
104     Dwarf_Rangelist *ret_rl, Dwarf_Error *error)
105 {
106 	Dwarf_Section *ds;
107 	Dwarf_Rangelist rl;
108 	Dwarf_Unsigned cnt;
109 	int ret;
110 
111 	if ((ds = _dwarf_find_section(dbg, ".debug_ranges")) == NULL) {
112 		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
113 		return (DW_DLE_NO_ENTRY);
114 	}
115 
116 	if ((rl = malloc(sizeof(struct _Dwarf_Rangelist))) == NULL) {
117 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
118 		return (DW_DLE_MEMORY);
119 	}
120 
121 	rl->rl_offset = off;
122 
123 	ret = _dwarf_ranges_parse(dbg, cu, ds, off, NULL, &cnt);
124 	if (ret != DW_DLE_NONE) {
125 		free(rl);
126 		return (ret);
127 	}
128 
129 	rl->rl_rglen = cnt;
130 	if (cnt != 0) {
131 		if ((rl->rl_rgarray = calloc(cnt, sizeof(Dwarf_Ranges))) ==
132 		    NULL) {
133 			free(rl);
134 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
135 			return (DW_DLE_MEMORY);
136 		}
137 
138 		ret = _dwarf_ranges_parse(dbg, cu, ds, off, rl->rl_rgarray,
139 		    NULL);
140 		if (ret != DW_DLE_NONE) {
141 			free(rl->rl_rgarray);
142 			free(rl);
143 			return (ret);
144 		}
145 	} else
146 		rl->rl_rgarray = NULL;
147 
148 	STAILQ_INSERT_TAIL(&dbg->dbg_rllist, rl, rl_next);
149 	*ret_rl = rl;
150 
151 	return (DW_DLE_NONE);
152 }
153