1 /* Return list address ranges.
2 Copyright (C) 2000-2010 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19 In addition, as a special exception, Red Hat, Inc. gives You the
20 additional right to link the code of Red Hat elfutils with code licensed
21 under any Open Source Initiative certified open source license
22 (http://www.opensource.org/licenses/index.php) which requires the
23 distribution of source code with any binary distribution and to
24 distribute linked combinations of the two. Non-GPL Code permitted under
25 this exception must only link to the code of Red Hat elfutils through
26 those well defined interfaces identified in the file named EXCEPTION
27 found in the source code files (the "Approved Interfaces"). The files
28 of Non-GPL Code may instantiate templates or use macros or inline
29 functions from the Approved Interfaces without causing the resulting
30 work to be covered by the GNU General Public License. Only Red Hat,
31 Inc. may make changes or additions to the list of Approved Interfaces.
32 Red Hat's grant of this exception is conditioned upon your not adding
33 any new exceptions. If you wish to add a new Approved Interface or
34 exception, please contact Red Hat. You must obey the GNU General Public
35 License in all respects for all of the Red Hat elfutils code and other
36 code used in conjunction with Red Hat elfutils except the Non-GPL Code
37 covered by this exception. If you modify this file, you may extend this
38 exception to your version of the file, but you are not obligated to do
39 so. If you do not wish to provide this exception without modification,
40 you must delete this exception statement from your version and license
41 this file solely under the GPL without exception.
43 Red Hat elfutils is an included package of the Open Invention Network.
44 An included package of the Open Invention Network is a package for which
45 Open Invention Network licensees cross-license their patents. No patent
46 license is granted, either expressly or impliedly, by designation as an
47 included package. Should you wish to participate in the Open Invention
48 Network licensing program, please visit www.openinventionnetwork.com
49 <http://www.openinventionnetwork.com>. */
63 struct arangelist *next;
66 /* Compare by Dwarf_Arange.addr, given pointers into an array of pointeers. */
68 compare_aranges (const void *a, const void *b)
70 struct arangelist *const *p1 = a, *const *p2 = b;
71 struct arangelist *l1 = *p1, *l2 = *p2;
72 return l1->arange.addr - l2->arange.addr;
76 dwarf_getaranges (dbg, aranges, naranges)
78 Dwarf_Aranges **aranges;
84 if (dbg->aranges != NULL)
86 *aranges = dbg->aranges;
88 *naranges = dbg->aranges->naranges;
92 if (dbg->sectiondata[IDX_debug_aranges] == NULL)
94 /* No such section. */
101 if (dbg->sectiondata[IDX_debug_aranges]->d_buf == NULL)
104 struct arangelist *arangelist = NULL;
105 unsigned int narangelist = 0;
107 const unsigned char *readp = dbg->sectiondata[IDX_debug_aranges]->d_buf;
108 const unsigned char *readendp
109 = readp + dbg->sectiondata[IDX_debug_aranges]->d_size;
111 while (readp < readendp)
113 const unsigned char *hdrstart = readp;
115 /* Each entry starts with a header:
117 1. A 4-byte or 12-byte length containing the length of the
118 set of entries for this compilation unit, not including the
119 length field itself. [...]
121 2. A 2-byte version identifier containing the value 2 for
124 3. A 4-byte or 8-byte offset into the .debug_info section. [...]
126 4. A 1-byte unsigned integer containing the size in bytes of
127 an address (or the offset portion of an address for segmented
128 addressing) on the target system.
130 5. A 1-byte unsigned integer containing the size in bytes of
131 a segment descriptor on the target system. */
132 Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
133 unsigned int length_bytes = 4;
134 if (length == DWARF3_LENGTH_64_BIT)
136 length = read_8ubyte_unaligned_inc (dbg, readp);
139 else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
140 && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
143 unsigned int version = read_2ubyte_unaligned_inc (dbg, readp);
147 __libdw_seterrno (DWARF_E_INVALID_DWARF);
149 while (arangelist != NULL)
151 struct arangelist *next = arangelist->next;
159 if (__libdw_read_offset_inc (dbg,
160 IDX_debug_aranges, &readp,
161 length_bytes, &offset, IDX_debug_info, 4))
164 unsigned int address_size = *readp++;
165 if (address_size != 4 && address_size != 8)
168 /* Ignore the segment size value. */
172 /* Round the address to the next multiple of 2*address_size. */
173 readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
174 % (2 * address_size));
178 Dwarf_Word range_address;
179 Dwarf_Word range_length;
181 if (__libdw_read_address_inc (dbg, IDX_debug_aranges, &readp,
182 address_size, &range_address))
185 if (address_size == 4)
186 range_length = read_4ubyte_unaligned_inc (dbg, readp);
188 range_length = read_8ubyte_unaligned_inc (dbg, readp);
190 /* Two zero values mark the end. */
191 if (range_address == 0 && range_length == 0)
194 /* We don't use alloca for these temporary structures because
195 the total number of them can be quite large. */
196 struct arangelist *new_arange = malloc (sizeof *new_arange);
197 if (unlikely (new_arange == NULL))
199 __libdw_seterrno (DWARF_E_NOMEM);
203 new_arange->arange.addr = range_address;
204 new_arange->arange.length = range_length;
206 /* We store the actual CU DIE offset, not the CU header offset. */
207 const char *cu_header = (dbg->sectiondata[IDX_debug_info]->d_buf
209 unsigned int offset_size;
210 if (read_4ubyte_unaligned_noncvt (cu_header) == DWARF3_LENGTH_64_BIT)
214 new_arange->arange.offset = DIE_OFFSET_FROM_CU_OFFSET (offset,
218 new_arange->next = arangelist;
219 arangelist = new_arange;
222 /* Sanity-check the data. */
223 if (unlikely (new_arange->arange.offset
224 >= dbg->sectiondata[IDX_debug_info]->d_size))
229 if (narangelist == 0)
231 assert (arangelist == NULL);
232 if (naranges != NULL)
238 /* Allocate the array for the result. */
239 void *buf = libdw_alloc (dbg, Dwarf_Aranges,
240 sizeof (Dwarf_Aranges)
241 + narangelist * sizeof (Dwarf_Arange), 1);
243 /* First use the buffer for the pointers, and sort the entries.
244 We'll write the pointers in the end of the buffer, and then
245 copy into the buffer from the beginning so the overlap works. */
246 assert (sizeof (Dwarf_Arange) >= sizeof (Dwarf_Arange *));
247 struct arangelist **sortaranges
248 = (buf + sizeof (Dwarf_Aranges)
249 + ((sizeof (Dwarf_Arange) - sizeof sortaranges[0]) * narangelist));
251 /* The list is in LIFO order and usually they come in clumps with
252 ascending addresses. So fill from the back to probably start with
253 runs already in order before we sort. */
254 unsigned int i = narangelist;
257 sortaranges[i] = arangelist;
258 arangelist = arangelist->next;
260 assert (arangelist == NULL);
262 /* Sort by ascending address. */
263 qsort (sortaranges, narangelist, sizeof sortaranges[0], &compare_aranges);
265 /* Now that they are sorted, put them in the final array.
266 The buffers overlap, so we've clobbered the early elements
267 of SORTARANGES by the time we're reading the later ones. */
269 (*aranges)->dbg = dbg;
270 (*aranges)->naranges = narangelist;
271 dbg->aranges = *aranges;
272 if (naranges != NULL)
273 *naranges = narangelist;
274 for (i = 0; i < narangelist; ++i)
276 struct arangelist *elt = sortaranges[i];
277 (*aranges)->info[i] = elt->arange;
283 INTDEF(dwarf_getaranges)