Remove building with NOCRYPTO option
[minix3.git] / external / bsd / elftoolchain / dist / libdwarf / libdwarf_ranges.c
blob1656fd08c978163e929a5f72b29c14d5e9912fa2
1 /* $NetBSD: libdwarf_ranges.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
3 /*-
4 * Copyright (c) 2009 Kai Wang
5 * All rights reserved.
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.
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.
29 #include "_libdwarf.h"
31 __RCSID("$NetBSD: libdwarf_ranges.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32 ELFTC_VCSID("Id: libdwarf_ranges.c 2972 2013-12-23 06:46:04Z kaiwang27 ");
34 static int
35 _dwarf_ranges_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds,
36 uint64_t off, Dwarf_Ranges *rg, Dwarf_Unsigned *cnt)
38 Dwarf_Unsigned start, end;
39 int i;
41 i = 0;
42 while (off < ds->ds_size) {
44 start = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
45 end = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
47 if (rg != NULL) {
48 rg[i].dwr_addr1 = start;
49 rg[i].dwr_addr2 = end;
50 if (start == 0 && end == 0)
51 rg[i].dwr_type = DW_RANGES_END;
52 else if ((start == ~0U && cu->cu_pointer_size == 4) ||
53 (start == ~0ULL && cu->cu_pointer_size == 8))
54 rg[i].dwr_type = DW_RANGES_ADDRESS_SELECTION;
55 else
56 rg[i].dwr_type = DW_RANGES_ENTRY;
59 i++;
61 if (start == 0 && end == 0)
62 break;
65 if (cnt != NULL)
66 *cnt = i;
68 return (DW_DLE_NONE);
71 int
72 _dwarf_ranges_find(Dwarf_Debug dbg, uint64_t off, Dwarf_Rangelist *ret_rl)
74 Dwarf_Rangelist rl;
76 STAILQ_FOREACH(rl, &dbg->dbg_rllist, rl_next)
77 if (rl->rl_offset == off)
78 break;
80 if (rl == NULL)
81 return (DW_DLE_NO_ENTRY);
83 if (ret_rl != NULL)
84 *ret_rl = rl;
86 return (DW_DLE_NONE);
89 void
90 _dwarf_ranges_cleanup(Dwarf_Debug dbg)
92 Dwarf_Rangelist rl, trl;
94 if (STAILQ_EMPTY(&dbg->dbg_rllist))
95 return;
97 STAILQ_FOREACH_SAFE(rl, &dbg->dbg_rllist, rl_next, trl) {
98 STAILQ_REMOVE(&dbg->dbg_rllist, rl, _Dwarf_Rangelist, rl_next);
99 if (rl->rl_rgarray)
100 free(rl->rl_rgarray);
101 free(rl);
106 _dwarf_ranges_add(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t off,
107 Dwarf_Rangelist *ret_rl, Dwarf_Error *error)
109 Dwarf_Section *ds;
110 Dwarf_Rangelist rl;
111 Dwarf_Unsigned cnt;
112 int ret;
114 if ((ds = _dwarf_find_section(dbg, ".debug_ranges")) == NULL) {
115 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
116 return (DW_DLE_NO_ENTRY);
119 if ((rl = malloc(sizeof(struct _Dwarf_Rangelist))) == NULL) {
120 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
121 return (DW_DLE_MEMORY);
124 rl->rl_offset = off;
126 ret = _dwarf_ranges_parse(dbg, cu, ds, off, NULL, &cnt);
127 if (ret != DW_DLE_NONE) {
128 free(rl);
129 return (ret);
132 rl->rl_rglen = cnt;
133 if (cnt != 0) {
134 if ((rl->rl_rgarray = calloc(cnt, sizeof(Dwarf_Ranges))) ==
135 NULL) {
136 free(rl);
137 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
138 return (DW_DLE_MEMORY);
141 ret = _dwarf_ranges_parse(dbg, cu, ds, off, rl->rl_rgarray,
142 NULL);
143 if (ret != DW_DLE_NONE) {
144 free(rl->rl_rgarray);
145 free(rl);
146 return (ret);
148 } else
149 rl->rl_rgarray = NULL;
151 STAILQ_INSERT_TAIL(&dbg->dbg_rllist, rl, rl_next);
152 *ret_rl = rl;
154 return (DW_DLE_NONE);