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 (at) redhat.com>, 2000. 5 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. 9 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. 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, 17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 18 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>. */ 50 51 #ifdef HAVE_CONFIG_H 52 # include <config.h> 53 #endif 54 55 #include <stdlib.h> 56 #include <assert.h> 57 #include "libdwP.h" 58 #include <dwarf.h> 59 60 struct 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. */ 67 static int 68 compare_aranges (const void *a, const void *b) 69 { 70 struct arangelist *const *p1 = a, *const *p2 = b; 71 struct arangelist *l1 = *p1, *l2 = *p2; 72 return l1->arange.addr - l2->arange.addr; 73 } 74 75 int 76 dwarf_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 92 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 101 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 unsigned char *readp = dbg->sectiondata[IDX_debug_aranges]->d_buf; 108 const unsigned char *readendp 109 = readp + dbg->sectiondata[IDX_debug_aranges]->d_size; 110 111 while (readp < readendp) 112 { 113 const unsigned 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; 134 if (length == DWARF3_LENGTH_64_BIT) 135 { 136 length = read_8ubyte_unaligned_inc (dbg, readp); 137 length_bytes = 8; 138 } 139 else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE 140 && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE)) 141 goto invalid; 142 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 fail: 149 while (arangelist != NULL) 150 { 151 struct arangelist *next = arangelist->next; 152 free (arangelist); 153 arangelist = next; 154 } 155 return -1; 156 } 157 158 Dwarf_Word offset; 159 if (__libdw_read_offset_inc (dbg, 160 IDX_debug_aranges, &readp, 161 length_bytes, &offset, IDX_debug_info, 4)) 162 goto fail; 163 164 unsigned int address_size = *readp++; 165 if (address_size != 4 && address_size != 8) 166 goto invalid; 167 168 /* Ignore the segment size value. */ 169 // XXX Really? 170 (void) *readp++; 171 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)); 175 176 while (1) 177 { 178 Dwarf_Word range_address; 179 Dwarf_Word range_length; 180 181 if (__libdw_read_address_inc (dbg, IDX_debug_aranges, &readp, 182 address_size, &range_address)) 183 goto fail; 184 185 if (address_size == 4) 186 range_length = read_4ubyte_unaligned_inc (dbg, readp); 187 else 188 range_length = read_8ubyte_unaligned_inc (dbg, readp); 189 190 /* Two zero values mark the end. */ 191 if (range_address == 0 && range_length == 0) 192 break; 193 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)) 198 { 199 __libdw_seterrno (DWARF_E_NOMEM); 200 goto fail; 201 } 202 203 new_arange->arange.addr = range_address; 204 new_arange->arange.length = range_length; 205 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 208 + offset); 209 unsigned int offset_size; 210 if (read_4ubyte_unaligned_noncvt (cu_header) == DWARF3_LENGTH_64_BIT) 211 offset_size = 8; 212 else 213 offset_size = 4; 214 new_arange->arange.offset = DIE_OFFSET_FROM_CU_OFFSET (offset, 215 offset_size, 216 false); 217 218 new_arange->next = arangelist; 219 arangelist = new_arange; 220 ++narangelist; 221 222 /* Sanity-check the data. */ 223 if (unlikely (new_arange->arange.offset 224 >= dbg->sectiondata[IDX_debug_info]->d_size)) 225 goto invalid; 226 } 227 } 228 229 if (narangelist == 0) 230 { 231 assert (arangelist == NULL); 232 if (naranges != NULL) 233 *naranges = 0; 234 *aranges = NULL; 235 return 0; 236 } 237 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); 242 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)); 250 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; 255 while (i-- > 0) 256 { 257 sortaranges[i] = arangelist; 258 arangelist = arangelist->next; 259 } 260 assert (arangelist == NULL); 261 262 /* Sort by ascending address. */ 263 qsort (sortaranges, narangelist, sizeof sortaranges[0], &compare_aranges); 264 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. */ 268 *aranges = buf; 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) 275 { 276 struct arangelist *elt = sortaranges[i]; 277 (*aranges)->info[i] = elt->arange; 278 free (elt); 279 } 280 281 return 0; 282 } 283 INTDEF(dwarf_getaranges) 284