blob: b6c194255b8734b8ac95d1815204f6147009e833 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Return list address ranges.
Ulrich Dreppere7a73172006-05-22 18:16:45 +00002 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006 Red Hat, Inc.
Ulrich Drepper361df7d2006-04-04 21:38:57 +00003 This file is part of Red Hat elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
Ulrich Drepper361df7d2006-04-04 21:38:57 +00006 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Ulrich Drepper361df7d2006-04-04 21:38:57 +000010 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.
14
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,
Ulrich Drepper1e9ef502006-04-04 22:29:06 +000017 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000018
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.
42
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>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000050
51#ifdef HAVE_CONFIG_H
52# include <config.h>
53#endif
54
55#include <stdlib.h>
56#include <assert.h>
57#include "libdwP.h"
Ulrich Drepper35f08c42008-01-18 19:59:08 +000058#include <dwarf.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000059
60struct arangelist
61{
62 Dwarf_Arange arange;
63 struct arangelist *next;
64};
65
66/* Compare by Dwarf_Arange.addr, given pointers into an array of pointeers. */
67static int
68compare_aranges (const void *a, const void *b)
69{
70 Dwarf_Arange *const *p1 = a, *const *p2 = b;
71 Dwarf_Arange *l1 = *p1, *l2 = *p2;
72 return l1->addr - l2->addr;
73}
74
75int
76dwarf_getaranges (dbg, aranges, naranges)
77 Dwarf *dbg;
78 Dwarf_Aranges **aranges;
79 size_t *naranges;
80{
81 if (dbg == NULL)
82 return -1;
83
84 if (dbg->aranges != NULL)
85 {
86 *aranges = dbg->aranges;
87 if (naranges != NULL)
88 *naranges = dbg->aranges->naranges;
89 return 0;
90 }
91
Ulrich Dreppere7a73172006-05-22 18:16:45 +000092 if (dbg->sectiondata[IDX_debug_aranges] == NULL)
93 {
94 /* No such section. */
95 *aranges = NULL;
96 if (naranges != NULL)
97 *naranges = 0;
98 return 0;
99 }
100
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000101 if (dbg->sectiondata[IDX_debug_aranges]->d_buf == NULL)
102 return -1;
103
104 struct arangelist *arangelist = NULL;
105 unsigned int narangelist = 0;
106
107 const char *readp
108 = (const char *) dbg->sectiondata[IDX_debug_aranges]->d_buf;
109 const char *readendp = readp + dbg->sectiondata[IDX_debug_aranges]->d_size;
110
111 while (readp < readendp)
112 {
113 const char *hdrstart = readp;
114
115 /* Each entry starts with a header:
116
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. [...]
120
121 2. A 2-byte version identifier containing the value 2 for
122 DWARF Version 2.1.
123
124 3. A 4-byte or 8-byte offset into the .debug_info section. [...]
125
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.
129
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;
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000134 if (length == DWARF3_LENGTH_64_BIT)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000135 {
136 length = read_8ubyte_unaligned_inc (dbg, readp);
137 length_bytes = 8;
138 }
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000139 else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
140 && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
141 goto invalid;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000142
143 unsigned int version = read_2ubyte_unaligned_inc (dbg, readp);
144 if (version != 2)
145 {
146 invalid:
147 __libdw_seterrno (DWARF_E_INVALID_DWARF);
148 return -1;
149 }
150
151 Dwarf_Word offset;
152 if (length_bytes == 4)
153 offset = read_4ubyte_unaligned_inc (dbg, readp);
154 else
155 offset = read_8ubyte_unaligned_inc (dbg, readp);
156
Roland McGratha5a89682005-08-02 01:24:01 +0000157 /* Sanity-check the offset. */
158 if (offset + 4 > dbg->sectiondata[IDX_debug_info]->d_size)
159 goto invalid;
160
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000161 unsigned int address_size = *readp++;
162 if (address_size != 4 && address_size != 8)
163 goto invalid;
164
165 /* Ignore the segment size value. */
166 // XXX Really?
167 (void) *readp++;
168
169 /* Round the address to the next multiple of 2*address_size. */
170 readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
171 % (2 * address_size));
172
173 while (1)
174 {
175 Dwarf_Word range_address;
176 Dwarf_Word range_length;
177
178 if (address_size == 4)
179 {
180 range_address = read_4ubyte_unaligned_inc (dbg, readp);
181 range_length = read_4ubyte_unaligned_inc (dbg, readp);
182 }
183 else
184 {
185 range_address = read_8ubyte_unaligned_inc (dbg, readp);
186 range_length = read_8ubyte_unaligned_inc (dbg, readp);
187 }
188
189 /* Two zero values mark the end. */
190 if (range_address == 0 && range_length == 0)
191 break;
192
193 struct arangelist *new_arange =
194 (struct arangelist *) alloca (sizeof (struct arangelist));
195
196 new_arange->arange.addr = range_address;
197 new_arange->arange.length = range_length;
198
199 /* We store the actual CU DIE offset, not the CU header offset. */
200 const char *cu_header = (dbg->sectiondata[IDX_debug_info]->d_buf
201 + offset);
202 unsigned int offset_size;
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000203 if (read_4ubyte_unaligned_noncvt (cu_header) == DWARF3_LENGTH_64_BIT)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000204 offset_size = 8;
205 else
206 offset_size = 4;
Ulrich Drepper35f08c42008-01-18 19:59:08 +0000207 new_arange->arange.offset = DIE_OFFSET_FROM_CU_OFFSET (offset, offset_size);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000208
Roland McGratha5a89682005-08-02 01:24:01 +0000209 /* Sanity-check the data. */
210 if (new_arange->arange.offset
211 >= dbg->sectiondata[IDX_debug_info]->d_size)
212 goto invalid;
213
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000214 new_arange->next = arangelist;
215 arangelist = new_arange;
216 ++narangelist;
217 }
218 }
219
220 if (narangelist == 0)
221 {
222 if (naranges != NULL)
223 *naranges = 0;
224 *aranges = NULL;
225 return 0;
226 }
227
228 /* Allocate the array for the result. */
229 void *buf = libdw_alloc (dbg, Dwarf_Aranges,
230 sizeof (Dwarf_Aranges)
231 + narangelist * sizeof (Dwarf_Arange), 1);
232
233 /* First use the buffer for the pointers, and sort the entries.
234 We'll write the pointers in the end of the buffer, and then
235 copy into the buffer from the beginning so the overlap works. */
236 assert (sizeof (Dwarf_Arange) >= sizeof (Dwarf_Arange *));
237 Dwarf_Arange **sortaranges = (buf + sizeof (Dwarf_Aranges)
238 + ((sizeof (Dwarf_Arange)
239 - sizeof (Dwarf_Arange *)) * narangelist));
240
241 /* The list is in LIFO order and usually they come in clumps with
242 ascending addresses. So fill from the back to probably start with
243 runs already in order before we sort. */
244 unsigned int i = narangelist;
245 while (i-- > 0)
246 {
247 sortaranges[i] = &arangelist->arange;
248 arangelist = arangelist->next;
249 }
250 assert (arangelist == NULL);
251
252 /* Sort by ascending address. */
253 qsort (sortaranges, narangelist, sizeof sortaranges[0], &compare_aranges);
254
255 /* Now that they are sorted, put them in the final array.
256 The buffers overlap, so we've clobbered the early elements
257 of SORTARANGES by the time we're reading the later ones. */
258 *aranges = buf;
259 (*aranges)->dbg = dbg;
260 (*aranges)->naranges = narangelist;
261 dbg->aranges = *aranges;
262 if (naranges != NULL)
263 *naranges = narangelist;
264 for (i = 0; i < narangelist; ++i)
265 (*aranges)->info[i] = *sortaranges[i];
266
267 return 0;
268}
269INTDEF(dwarf_getaranges)