1 /* $NetBSD: libdwarf_ranges.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2009 Kai Wang
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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 ");
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
;
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
);
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
;
56 rg
[i
].dwr_type
= DW_RANGES_ENTRY
;
61 if (start
== 0 && end
== 0)
72 _dwarf_ranges_find(Dwarf_Debug dbg
, uint64_t off
, Dwarf_Rangelist
*ret_rl
)
76 STAILQ_FOREACH(rl
, &dbg
->dbg_rllist
, rl_next
)
77 if (rl
->rl_offset
== off
)
81 return (DW_DLE_NO_ENTRY
);
90 _dwarf_ranges_cleanup(Dwarf_Debug dbg
)
92 Dwarf_Rangelist rl
, trl
;
94 if (STAILQ_EMPTY(&dbg
->dbg_rllist
))
97 STAILQ_FOREACH_SAFE(rl
, &dbg
->dbg_rllist
, rl_next
, trl
) {
98 STAILQ_REMOVE(&dbg
->dbg_rllist
, rl
, _Dwarf_Rangelist
, rl_next
);
100 free(rl
->rl_rgarray
);
106 _dwarf_ranges_add(Dwarf_Debug dbg
, Dwarf_CU cu
, uint64_t off
,
107 Dwarf_Rangelist
*ret_rl
, Dwarf_Error
*error
)
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
);
126 ret
= _dwarf_ranges_parse(dbg
, cu
, ds
, off
, NULL
, &cnt
);
127 if (ret
!= DW_DLE_NONE
) {
134 if ((rl
->rl_rgarray
= calloc(cnt
, sizeof(Dwarf_Ranges
))) ==
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
,
143 if (ret
!= DW_DLE_NONE
) {
144 free(rl
->rl_rgarray
);
149 rl
->rl_rgarray
= NULL
;
151 STAILQ_INSERT_TAIL(&dbg
->dbg_rllist
, rl
, rl_next
);
154 return (DW_DLE_NONE
);