1 /* Return list address ranges. 2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2008 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 Dwarf_Arange *const *p1 = a, *const *p2 = b; 71 Dwarf_Arange *l1 = *p1, *l2 = *p2; 72 return l1->addr - l2->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 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; 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 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 157 /* Sanity-check the offset. */ 158 if (offset + 4 > dbg->sectiondata[IDX_debug_info]->d_size) 159 goto invalid; 160 161 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; 203 if (read_4ubyte_unaligned_noncvt (cu_header) == DWARF3_LENGTH_64_BIT) 204 offset_size = 8; 205 else 206 offset_size = 4; 207 new_arange->arange.offset = DIE_OFFSET_FROM_CU_OFFSET (offset, 208 offset_size); 209 210 /* Sanity-check the data. */ 211 if (new_arange->arange.offset 212 >= dbg->sectiondata[IDX_debug_info]->d_size) 213 goto invalid; 214 215 new_arange->next = arangelist; 216 arangelist = new_arange; 217 ++narangelist; 218 } 219 } 220 221 if (narangelist == 0) 222 { 223 if (naranges != NULL) 224 *naranges = 0; 225 *aranges = NULL; 226 return 0; 227 } 228 229 /* Allocate the array for the result. */ 230 void *buf = libdw_alloc (dbg, Dwarf_Aranges, 231 sizeof (Dwarf_Aranges) 232 + narangelist * sizeof (Dwarf_Arange), 1); 233 234 /* First use the buffer for the pointers, and sort the entries. 235 We'll write the pointers in the end of the buffer, and then 236 copy into the buffer from the beginning so the overlap works. */ 237 assert (sizeof (Dwarf_Arange) >= sizeof (Dwarf_Arange *)); 238 Dwarf_Arange **sortaranges = (buf + sizeof (Dwarf_Aranges) 239 + ((sizeof (Dwarf_Arange) 240 - sizeof (Dwarf_Arange *)) * narangelist)); 241 242 /* The list is in LIFO order and usually they come in clumps with 243 ascending addresses. So fill from the back to probably start with 244 runs already in order before we sort. */ 245 unsigned int i = narangelist; 246 while (i-- > 0) 247 { 248 sortaranges[i] = &arangelist->arange; 249 arangelist = arangelist->next; 250 } 251 assert (arangelist == NULL); 252 253 /* Sort by ascending address. */ 254 qsort (sortaranges, narangelist, sizeof sortaranges[0], &compare_aranges); 255 256 /* Now that they are sorted, put them in the final array. 257 The buffers overlap, so we've clobbered the early elements 258 of SORTARANGES by the time we're reading the later ones. */ 259 *aranges = buf; 260 (*aranges)->dbg = dbg; 261 (*aranges)->naranges = narangelist; 262 dbg->aranges = *aranges; 263 if (naranges != NULL) 264 *naranges = narangelist; 265 for (i = 0; i < narangelist; ++i) 266 (*aranges)->info[i] = *sortaranges[i]; 267 268 return 0; 269 } 270 INTDEF(dwarf_getaranges) 271