Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Advance to next CU header. |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 2 | Copyright (C) 2002-2010 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 5 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 8 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 9 | * the GNU Lesser General Public License as published by the Free |
| 10 | Software Foundation; either version 3 of the License, or (at |
| 11 | your option) any later version |
| 12 | |
| 13 | or |
| 14 | |
| 15 | * the GNU General Public License as published by the Free |
| 16 | Software Foundation; either version 2 of the License, or (at |
| 17 | your option) any later version |
| 18 | |
| 19 | or both in parallel, as here. |
| 20 | |
| 21 | elfutils is distributed in the hope that it will be useful, but |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 22 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | General Public License for more details. |
| 25 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 26 | You should have received copies of the GNU General Public License and |
| 27 | the GNU Lesser General Public License along with this program. If |
| 28 | not, see <http://www.gnu.org/licenses/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | # include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <libdwP.h> |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 35 | #include <dwarf.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | int |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 39 | dwarf_next_unit (dwarf, off, next_off, header_sizep, versionp, abbrev_offsetp, |
| 40 | address_sizep, offset_sizep, type_signaturep, type_offsetp) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 41 | Dwarf *dwarf; |
| 42 | Dwarf_Off off; |
| 43 | Dwarf_Off *next_off; |
| 44 | size_t *header_sizep; |
Roland McGrath | 2b1f095 | 2010-06-20 17:55:50 -0700 | [diff] [blame] | 45 | Dwarf_Half *versionp; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 46 | Dwarf_Off *abbrev_offsetp; |
| 47 | uint8_t *address_sizep; |
| 48 | uint8_t *offset_sizep; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 49 | uint64_t *type_signaturep; |
| 50 | Dwarf_Off *type_offsetp; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 51 | { |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 52 | const bool debug_types = type_signaturep != NULL; |
| 53 | const size_t sec_idx = debug_types ? IDX_debug_types : IDX_debug_info; |
| 54 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 55 | /* Maybe there has been an error before. */ |
| 56 | if (dwarf == NULL) |
| 57 | return -1; |
| 58 | |
| 59 | /* If we reached the end before don't do anything. */ |
| 60 | if (off == (Dwarf_Off) -1l |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 61 | || unlikely (dwarf->sectiondata[sec_idx] == NULL) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 62 | /* Make sure there is enough space in the .debug_info section |
| 63 | for at least the initial word. We cannot test the rest since |
| 64 | we don't know yet whether this is a 64-bit object or not. */ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 65 | || unlikely (off + 4 >= dwarf->sectiondata[sec_idx]->d_size)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 66 | { |
| 67 | *next_off = (Dwarf_Off) -1l; |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | /* This points into the .debug_info section to the beginning of the |
| 72 | CU entry. */ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 73 | const unsigned char *data = dwarf->sectiondata[sec_idx]->d_buf; |
Roland McGrath | 05c4e04 | 2009-06-18 13:56:02 -0700 | [diff] [blame] | 74 | const unsigned char *bytes = data + off; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 75 | |
| 76 | /* The format of the CU header is described in dwarf2p1 7.5.1: |
| 77 | |
| 78 | 1. A 4-byte or 12-byte unsigned integer representing the length |
| 79 | of the .debug_info contribution for that compilation unit, not |
| 80 | including the length field itself. In the 32-bit DWARF format, |
| 81 | this is a 4-byte unsigned integer (which must be less than |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 82 | 0xfffffff0); in the 64-bit DWARF format, this consists of the |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 83 | 4-byte value 0xffffffff followed by an 8-byte unsigned integer |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 84 | that gives the actual length (see Section 7.2.2). |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 85 | |
| 86 | 2. A 2-byte unsigned integer representing the version of the |
| 87 | DWARF information for that compilation unit. For DWARF Version |
| 88 | 2.1, the value in this field is 2. |
| 89 | |
| 90 | 3. A 4-byte or 8-byte unsigned offset into the .debug_abbrev |
| 91 | section. This offset associates the compilation unit with a |
| 92 | particular set of debugging information entry abbreviations. In |
| 93 | the 32-bit DWARF format, this is a 4-byte unsigned length; in |
| 94 | the 64-bit DWARF format, this is an 8-byte unsigned length (see |
| 95 | Section 7.4). |
| 96 | |
| 97 | 4. A 1-byte unsigned integer representing the size in bytes of |
| 98 | an address on the target architecture. If the system uses |
| 99 | segmented addressing, this value represents the size of the |
| 100 | offset portion of an address. */ |
| 101 | uint64_t length = read_4ubyte_unaligned_inc (dwarf, bytes); |
| 102 | size_t offset_size = 4; |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 103 | /* Lengths of 0xfffffff0 - 0xffffffff are escape codes. Oxffffffff is |
| 104 | used to indicate that 64-bit dwarf information is being used, the |
| 105 | other values are currently reserved. */ |
| 106 | if (length == DWARF3_LENGTH_64_BIT) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 107 | offset_size = 8; |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 108 | else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE |
| 109 | && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE)) |
| 110 | { |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 111 | invalid: |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 112 | __libdw_seterrno (DWARF_E_INVALID_DWARF); |
| 113 | return -1; |
| 114 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 115 | |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 116 | /* Now we know how large the header is. */ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 117 | if (unlikely (DIE_OFFSET_FROM_CU_OFFSET (off, offset_size, debug_types) |
| 118 | >= dwarf->sectiondata[sec_idx]->d_size)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 119 | { |
| 120 | *next_off = -1; |
| 121 | return 1; |
| 122 | } |
| 123 | |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 124 | if (length == DWARF3_LENGTH_64_BIT) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 125 | /* This is a 64-bit DWARF format. */ |
| 126 | length = read_8ubyte_unaligned_inc (dwarf, bytes); |
| 127 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 128 | /* Read the version stamp. Always a 16-bit value. */ |
| 129 | uint_fast16_t version = read_2ubyte_unaligned_inc (dwarf, bytes); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 130 | |
| 131 | /* Get offset in .debug_abbrev. Note that the size of the entry |
| 132 | depends on whether this is a 32-bit or 64-bit DWARF definition. */ |
| 133 | uint64_t abbrev_offset; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 134 | if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size, |
Ulrich Drepper | 99d2372 | 2009-06-14 20:19:45 -0700 | [diff] [blame] | 135 | &abbrev_offset, IDX_debug_abbrev, 0)) |
| 136 | return -1; |
| 137 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 138 | /* The address size. Always an 8-bit value. */ |
| 139 | uint8_t address_size = *bytes++; |
| 140 | |
| 141 | if (debug_types) |
| 142 | { |
| 143 | uint64_t type_sig8 = read_8ubyte_unaligned_inc (dwarf, bytes); |
| 144 | |
| 145 | Dwarf_Off type_offset; |
| 146 | if (__libdw_read_offset_inc (dwarf, sec_idx, &bytes, offset_size, |
| 147 | &type_offset, sec_idx, 0)) |
| 148 | return -1; |
| 149 | |
| 150 | /* Validate that the TYPE_OFFSET points past the header. */ |
| 151 | if (unlikely (type_offset < (size_t) (bytes - (data + off)))) |
| 152 | goto invalid; |
| 153 | |
| 154 | *type_signaturep = type_sig8; |
| 155 | if (type_offsetp != NULL) |
| 156 | *type_offsetp = type_offset; |
| 157 | } |
| 158 | |
| 159 | /* Store the header length. */ |
| 160 | if (header_sizep != NULL) |
| 161 | *header_sizep = bytes - (data + off); |
| 162 | |
| 163 | if (versionp != NULL) |
| 164 | *versionp = version; |
| 165 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 166 | if (abbrev_offsetp != NULL) |
| 167 | *abbrev_offsetp = abbrev_offset; |
| 168 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 169 | if (address_sizep != NULL) |
| 170 | *address_sizep = address_size; |
| 171 | |
| 172 | /* Store the offset size. */ |
| 173 | if (offset_sizep != NULL) |
| 174 | *offset_sizep = offset_size; |
| 175 | |
Ulrich Drepper | 35f08c4 | 2008-01-18 19:59:08 +0000 | [diff] [blame] | 176 | /* See definition of DIE_OFFSET_FROM_CU_OFFSET macro |
| 177 | for an explanation of the trick in this expression. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 178 | *next_off = off + 2 * offset_size - 4 + length; |
| 179 | |
| 180 | return 0; |
| 181 | } |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 182 | INTDEF(dwarf_next_unit) |
| 183 | |
| 184 | int |
| 185 | dwarf_nextcu (dwarf, off, next_off, header_sizep, abbrev_offsetp, |
| 186 | address_sizep, offset_sizep) |
| 187 | Dwarf *dwarf; |
| 188 | Dwarf_Off off; |
| 189 | Dwarf_Off *next_off; |
| 190 | size_t *header_sizep; |
| 191 | Dwarf_Off *abbrev_offsetp; |
| 192 | uint8_t *address_sizep; |
| 193 | uint8_t *offset_sizep; |
| 194 | { |
| 195 | return INTUSE(dwarf_next_unit) (dwarf, off, next_off, header_sizep, NULL, |
| 196 | abbrev_offsetp, address_sizep, offset_sizep, |
| 197 | NULL, NULL); |
| 198 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 199 | INTDEF(dwarf_nextcu) |