1 /* Enumerate the PC ranges covered by a DIE.
2 Copyright (C) 2005, 2007, 2009 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
37 /* Read up begin/end pair and increment read pointer.
38 - If it's normal range record, set up `*beginp' and `*endp' and return 0.
39 - If it's base address selection record, set up `*basep' and return 1.
40 - If it's end of rangelist, don't set anything and return 2
41 - If an error occurs, don't set anything and return -1. */
43 __libdw_read_begin_end_pair_inc (Dwarf *dbg, int sec_index,
44 unsigned char **addrp, int width,
45 Dwarf_Addr *beginp, Dwarf_Addr *endp,
48 Dwarf_Addr escape = (width == 8 ? (Elf64_Addr) -1
49 : (Elf64_Addr) (Elf32_Addr) -1);
53 unsigned char *addr = *addrp;
54 bool begin_relocated = READ_AND_RELOCATE (__libdw_relocate_address, begin);
55 bool end_relocated = READ_AND_RELOCATE (__libdw_relocate_address, end);
58 /* Unrelocated escape for begin means base address selection. */
59 if (begin == escape && !begin_relocated)
61 if (unlikely (end == escape))
63 __libdw_seterrno (DWARF_E_INVALID_DWARF);
72 /* Unrelocated pair of zeroes means end of range list. */
73 if (begin == 0 && end == 0 && !begin_relocated && !end_relocated)
76 /* Don't check for begin_relocated == end_relocated. Serve the data
77 to the client even though it may be buggy. */
85 dwarf_ranges (Dwarf_Die *die, ptrdiff_t offset, Dwarf_Addr *basep,
86 Dwarf_Addr *startp, Dwarf_Addr *endp)
92 /* Usually there is a single contiguous range. */
93 && INTUSE(dwarf_highpc) (die, endp) == 0
94 && INTUSE(dwarf_lowpc) (die, startp) == 0)
95 /* A offset into .debug_ranges will never be 1, it must be at least a
96 multiple of 4. So we can return 1 as a special case value to mark
97 there are no ranges to look for on the next call. */
103 /* We have to look for a noncontiguous range. */
105 const Elf_Data *d = die->cu->dbg->sectiondata[IDX_debug_ranges];
106 if (d == NULL && offset != 0)
108 __libdw_seterrno (DWARF_E_NO_DEBUG_RANGES);
112 unsigned char *readp;
113 unsigned char *readendp;
116 Dwarf_Attribute attr_mem;
117 Dwarf_Attribute *attr = INTUSE(dwarf_attr) (die, DW_AT_ranges,
120 /* No PC attributes in this DIE at all, so an empty range list. */
123 Dwarf_Word start_offset;
124 if ((readp = __libdw_formptr (attr, IDX_debug_ranges,
125 DWARF_E_NO_DEBUG_RANGES,
126 &readendp, &start_offset)) == NULL)
129 offset = start_offset;
130 assert ((Dwarf_Word) offset == start_offset);
132 /* Fetch the CU's base address. */
133 Dwarf_Die cudie = CUDIE (attr->cu);
135 /* Find the base address of the compilation unit. It will
136 normally be specified by DW_AT_low_pc. In DWARF-3 draft 4,
137 the base address could be overridden by DW_AT_entry_pc. It's
138 been removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc
139 for compilation units with discontinuous ranges. */
140 if (unlikely (INTUSE(dwarf_lowpc) (&cudie, basep) != 0)
141 && INTUSE(dwarf_formaddr) (INTUSE(dwarf_attr) (&cudie,
145 *basep = (Dwarf_Addr) -1;
149 if (__libdw_offset_in_section (die->cu->dbg,
150 IDX_debug_ranges, offset, 1))
153 readp = d->d_buf + offset;
154 readendp = d->d_buf + d->d_size;
158 if (readendp - readp < die->cu->address_size * 2)
164 switch (__libdw_read_begin_end_pair_inc (die->cu->dbg, IDX_debug_ranges,
165 &readp, die->cu->address_size,
166 &begin, &end, basep))
178 /* We have an address range entry. Check that we have a base. */
179 if (*basep == (Dwarf_Addr) -1)
181 if (INTUSE(dwarf_errno) () == 0)
184 __libdw_seterrno (DWARF_E_INVALID_DWARF);
189 *startp = *basep + begin;
190 *endp = *basep + end;
191 return readp - (unsigned char *) d->d_buf;
193 INTDEF (dwarf_ranges)