1 /* Enumerate the PC ranges covered by a DIE.
2 Copyright (C) 2005, 2007, 2009 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18 In addition, as a special exception, Red Hat, Inc. gives You the
19 additional right to link the code of Red Hat elfutils with code licensed
20 under any Open Source Initiative certified open source license
21 (http://www.opensource.org/licenses/index.php) which requires the
22 distribution of source code with any binary distribution and to
23 distribute linked combinations of the two. Non-GPL Code permitted under
24 this exception must only link to the code of Red Hat elfutils through
25 those well defined interfaces identified in the file named EXCEPTION
26 found in the source code files (the "Approved Interfaces"). The files
27 of Non-GPL Code may instantiate templates or use macros or inline
28 functions from the Approved Interfaces without causing the resulting
29 work to be covered by the GNU General Public License. Only Red Hat,
30 Inc. may make changes or additions to the list of Approved Interfaces.
31 Red Hat's grant of this exception is conditioned upon your not adding
32 any new exceptions. If you wish to add a new Approved Interface or
33 exception, please contact Red Hat. You must obey the GNU General Public
34 License in all respects for all of the Red Hat elfutils code and other
35 code used in conjunction with Red Hat elfutils except the Non-GPL Code
36 covered by this exception. If you modify this file, you may extend this
37 exception to your version of the file, but you are not obligated to do
38 so. If you do not wish to provide this exception without modification,
39 you must delete this exception statement from your version and license
40 this file solely under the GPL without exception.
42 Red Hat elfutils is an included package of the Open Invention Network.
43 An included package of the Open Invention Network is a package for which
44 Open Invention Network licensees cross-license their patents. No patent
45 license is granted, either expressly or impliedly, by designation as an
46 included package. Should you wish to participate in the Open Invention
47 Network licensing program, please visit www.openinventionnetwork.com
48 <http://www.openinventionnetwork.com>. */
58 /* Read up begin/end pair and increment read pointer.
59 - If it's normal range record, set up `*beginp' and `*endp' and return 0.
60 - If it's base address selection record, set up `*basep' and return 1.
61 - If it's end of rangelist, don't set anything and return 2
62 - If an error occurs, don't set anything and return -1. */
64 __libdw_read_begin_end_pair_inc (Dwarf *dbg, int sec_index,
65 unsigned char **addrp, int width,
66 Dwarf_Addr *beginp, Dwarf_Addr *endp,
69 Dwarf_Addr escape = (width == 8 ? (Elf64_Addr) -1
70 : (Elf64_Addr) (Elf32_Addr) -1);
74 unsigned char *addr = *addrp;
75 bool begin_relocated = READ_AND_RELOCATE (__libdw_relocate_address, begin);
76 bool end_relocated = READ_AND_RELOCATE (__libdw_relocate_address, end);
79 /* Unrelocated escape for begin means base address selection. */
80 if (begin == escape && !begin_relocated)
82 if (unlikely (end == escape))
84 __libdw_seterrno (DWARF_E_INVALID_DWARF);
93 /* Unrelocated pair of zeroes means end of range list. */
94 if (begin == 0 && end == 0 && !begin_relocated && !end_relocated)
97 /* Don't check for begin_relocated == end_relocated. Serve the data
98 to the client even though it may be buggy. */
106 dwarf_ranges (Dwarf_Die *die, ptrdiff_t offset, Dwarf_Addr *basep,
107 Dwarf_Addr *startp, Dwarf_Addr *endp)
113 /* Usually there is a single contiguous range. */
114 && INTUSE(dwarf_highpc) (die, endp) == 0
115 && INTUSE(dwarf_lowpc) (die, startp) == 0)
116 /* A offset into .debug_ranges will never be 1, it must be at least a
117 multiple of 4. So we can return 1 as a special case value to mark
118 there are no ranges to look for on the next call. */
124 /* We have to look for a noncontiguous range. */
126 const Elf_Data *d = die->cu->dbg->sectiondata[IDX_debug_ranges];
127 if (d == NULL && offset != 0)
129 __libdw_seterrno (DWARF_E_NO_DEBUG_RANGES);
133 unsigned char *readp;
134 unsigned char *readendp;
137 Dwarf_Attribute attr_mem;
138 Dwarf_Attribute *attr = INTUSE(dwarf_attr) (die, DW_AT_ranges,
141 /* No PC attributes in this DIE at all, so an empty range list. */
144 Dwarf_Word start_offset;
145 if ((readp = __libdw_formptr (attr, IDX_debug_ranges,
146 DWARF_E_NO_DEBUG_RANGES,
147 &readendp, &start_offset)) == NULL)
150 offset = start_offset;
151 assert ((Dwarf_Word) offset == start_offset);
153 /* Fetch the CU's base address. */
154 Dwarf_Die cudie = CUDIE (attr->cu);
156 /* Find the base address of the compilation unit. It will
157 normally be specified by DW_AT_low_pc. In DWARF-3 draft 4,
158 the base address could be overridden by DW_AT_entry_pc. It's
159 been removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc
160 for compilation units with discontinuous ranges. */
161 if (unlikely (INTUSE(dwarf_lowpc) (&cudie, basep) != 0)
162 && INTUSE(dwarf_formaddr) (INTUSE(dwarf_attr) (&cudie,
167 if (INTUSE(dwarf_errno) () == 0)
170 __libdw_seterrno (DWARF_E_INVALID_DWARF);
177 if (__libdw_offset_in_section (die->cu->dbg,
178 IDX_debug_ranges, offset, 1))
181 readp = d->d_buf + offset;
182 readendp = d->d_buf + d->d_size;
186 if (readendp - readp < die->cu->address_size * 2)
192 switch (__libdw_read_begin_end_pair_inc (die->cu->dbg, IDX_debug_ranges,
193 &readp, die->cu->address_size,
194 &begin, &end, basep))
206 /* We have an address range entry. */
207 *startp = *basep + begin;
208 *endp = *basep + end;
209 return readp - (unsigned char *) d->d_buf;
211 INTDEF (dwarf_ranges)