2 * Copyright (c) 2009 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include "_libdwarf.h"
29 ELFTC_VCSID("$Id: libdwarf_ranges.c 2972 2013-12-23 06:46:04Z kaiwang27 $");
32 _dwarf_ranges_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds,
33 uint64_t off, Dwarf_Ranges *rg, Dwarf_Unsigned *cnt)
35 Dwarf_Unsigned start, end;
39 while (off < ds->ds_size) {
41 start = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
42 end = dbg->read(ds->ds_data, &off, cu->cu_pointer_size);
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;
53 rg[i].dwr_type = DW_RANGES_ENTRY;
58 if (start == 0 && end == 0)
69 _dwarf_ranges_find(Dwarf_Debug dbg, uint64_t off, Dwarf_Rangelist *ret_rl)
73 STAILQ_FOREACH(rl, &dbg->dbg_rllist, rl_next)
74 if (rl->rl_offset == off)
78 return (DW_DLE_NO_ENTRY);
87 _dwarf_ranges_cleanup(Dwarf_Debug dbg)
89 Dwarf_Rangelist rl, trl;
91 if (STAILQ_EMPTY(&dbg->dbg_rllist))
94 STAILQ_FOREACH_SAFE(rl, &dbg->dbg_rllist, rl_next, trl) {
95 STAILQ_REMOVE(&dbg->dbg_rllist, rl, _Dwarf_Rangelist, rl_next);
103 _dwarf_ranges_add(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t off,
104 Dwarf_Rangelist *ret_rl, Dwarf_Error *error)
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);
116 if ((rl = malloc(sizeof(struct _Dwarf_Rangelist))) == NULL) {
117 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
118 return (DW_DLE_MEMORY);
123 ret = _dwarf_ranges_parse(dbg, cu, ds, off, NULL, &cnt);
124 if (ret != DW_DLE_NONE) {
131 if ((rl->rl_rgarray = calloc(cnt, sizeof(Dwarf_Ranges))) ==
134 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
135 return (DW_DLE_MEMORY);
138 ret = _dwarf_ranges_parse(dbg, cu, ds, off, rl->rl_rgarray,
140 if (ret != DW_DLE_NONE) {
141 free(rl->rl_rgarray);
146 rl->rl_rgarray = NULL;
148 STAILQ_INSERT_TAIL(&dbg->dbg_rllist, rl, rl_next);
151 return (DW_DLE_NONE);