1 // Copyright 2010 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h> // Must come first
31 #endif
32
33 #include <assert.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36
37 #include "common/dwarf/bytereader-inl.h"
38 #include "common/dwarf/bytereader.h"
39
40 namespace google_breakpad {
41
ByteReader(enum Endianness endian)42 ByteReader::ByteReader(enum Endianness endian)
43 :offset_reader_(NULL), address_reader_(NULL), endian_(endian),
44 address_size_(0), offset_size_(0),
45 have_section_base_(), have_text_base_(), have_data_base_(),
46 have_function_base_() { }
47
~ByteReader()48 ByteReader::~ByteReader() { }
49
SetOffsetSize(uint8_t size)50 void ByteReader::SetOffsetSize(uint8_t size) {
51 offset_size_ = size;
52 assert(size == 4 || size == 8);
53 if (size == 4) {
54 this->offset_reader_ = &ByteReader::ReadFourBytes;
55 } else {
56 this->offset_reader_ = &ByteReader::ReadEightBytes;
57 }
58 }
59
SetAddressSize(uint8_t size)60 void ByteReader::SetAddressSize(uint8_t size) {
61 address_size_ = size;
62 assert(size == 4 || size == 8);
63 if (size == 4) {
64 this->address_reader_ = &ByteReader::ReadFourBytes;
65 } else {
66 this->address_reader_ = &ByteReader::ReadEightBytes;
67 }
68 }
69
ReadInitialLength(const uint8_t * start,size_t * len)70 uint64_t ByteReader::ReadInitialLength(const uint8_t* start, size_t* len) {
71 const uint64_t initial_length = ReadFourBytes(start);
72 start += 4;
73
74 // In DWARF2/3, if the initial length is all 1 bits, then the offset
75 // size is 8 and we need to read the next 8 bytes for the real length.
76 if (initial_length == 0xffffffff) {
77 SetOffsetSize(8);
78 *len = 12;
79 return ReadOffset(start);
80 } else {
81 SetOffsetSize(4);
82 *len = 4;
83 }
84 return initial_length;
85 }
86
ValidEncoding(DwarfPointerEncoding encoding) const87 bool ByteReader::ValidEncoding(DwarfPointerEncoding encoding) const {
88 if (encoding == DW_EH_PE_omit) return true;
89 if (encoding == DW_EH_PE_aligned) return true;
90 if ((encoding & 0x7) > DW_EH_PE_udata8)
91 return false;
92 if ((encoding & 0x70) > DW_EH_PE_funcrel)
93 return false;
94 return true;
95 }
96
UsableEncoding(DwarfPointerEncoding encoding) const97 bool ByteReader::UsableEncoding(DwarfPointerEncoding encoding) const {
98 switch (encoding & 0x70) {
99 case DW_EH_PE_absptr: return true;
100 case DW_EH_PE_pcrel: return have_section_base_;
101 case DW_EH_PE_textrel: return have_text_base_;
102 case DW_EH_PE_datarel: return have_data_base_;
103 case DW_EH_PE_funcrel: return have_function_base_;
104 default: return false;
105 }
106 }
107
ReadEncodedPointer(const uint8_t * buffer,DwarfPointerEncoding encoding,size_t * len) const108 uint64_t ByteReader::ReadEncodedPointer(const uint8_t* buffer,
109 DwarfPointerEncoding encoding,
110 size_t* len) const {
111 // UsableEncoding doesn't approve of DW_EH_PE_omit, so we shouldn't
112 // see it here.
113 assert(encoding != DW_EH_PE_omit);
114
115 // The Linux Standards Base 4.0 does not make this clear, but the
116 // GNU tools (gcc/unwind-pe.h; readelf/dwarf.c; gdb/dwarf2-frame.c)
117 // agree that aligned pointers are always absolute, machine-sized,
118 // machine-signed pointers.
119 if (encoding == DW_EH_PE_aligned) {
120 assert(have_section_base_);
121
122 // We don't need to align BUFFER in *our* address space. Rather, we
123 // need to find the next position in our buffer that would be aligned
124 // when the .eh_frame section the buffer contains is loaded into the
125 // program's memory. So align assuming that buffer_base_ gets loaded at
126 // address section_base_, where section_base_ itself may or may not be
127 // aligned.
128
129 // First, find the offset to START from the closest prior aligned
130 // address.
131 uint64_t skew = section_base_ & (AddressSize() - 1);
132 // Now find the offset from that aligned address to buffer.
133 uint64_t offset = skew + (buffer - buffer_base_);
134 // Round up to the next boundary.
135 uint64_t aligned = (offset + AddressSize() - 1) & -AddressSize();
136 // Convert back to a pointer.
137 const uint8_t* aligned_buffer = buffer_base_ + (aligned - skew);
138 // Finally, store the length and actually fetch the pointer.
139 *len = aligned_buffer - buffer + AddressSize();
140 return ReadAddress(aligned_buffer);
141 }
142
143 // Extract the value first, ignoring whether it's a pointer or an
144 // offset relative to some base.
145 uint64_t offset;
146 switch (encoding & 0x0f) {
147 case DW_EH_PE_absptr:
148 // DW_EH_PE_absptr is weird, as it is used as a meaningful value for
149 // both the high and low nybble of encoding bytes. When it appears in
150 // the high nybble, it means that the pointer is absolute, not an
151 // offset from some base address. When it appears in the low nybble,
152 // as here, it means that the pointer is stored as a normal
153 // machine-sized and machine-signed address. A low nybble of
154 // DW_EH_PE_absptr does not imply that the pointer is absolute; it is
155 // correct for us to treat the value as an offset from a base address
156 // if the upper nybble is not DW_EH_PE_absptr.
157 offset = ReadAddress(buffer);
158 *len = AddressSize();
159 break;
160
161 case DW_EH_PE_uleb128:
162 offset = ReadUnsignedLEB128(buffer, len);
163 break;
164
165 case DW_EH_PE_udata2:
166 offset = ReadTwoBytes(buffer);
167 *len = 2;
168 break;
169
170 case DW_EH_PE_udata4:
171 offset = ReadFourBytes(buffer);
172 *len = 4;
173 break;
174
175 case DW_EH_PE_udata8:
176 offset = ReadEightBytes(buffer);
177 *len = 8;
178 break;
179
180 case DW_EH_PE_sleb128:
181 offset = ReadSignedLEB128(buffer, len);
182 break;
183
184 case DW_EH_PE_sdata2:
185 offset = ReadTwoBytes(buffer);
186 // Sign-extend from 16 bits.
187 offset = (offset ^ 0x8000) - 0x8000;
188 *len = 2;
189 break;
190
191 case DW_EH_PE_sdata4:
192 offset = ReadFourBytes(buffer);
193 // Sign-extend from 32 bits.
194 offset = (offset ^ 0x80000000ULL) - 0x80000000ULL;
195 *len = 4;
196 break;
197
198 case DW_EH_PE_sdata8:
199 // No need to sign-extend; this is the full width of our type.
200 offset = ReadEightBytes(buffer);
201 *len = 8;
202 break;
203
204 default:
205 abort();
206 }
207
208 // Find the appropriate base address.
209 uint64_t base;
210 switch (encoding & 0x70) {
211 case DW_EH_PE_absptr:
212 base = 0;
213 break;
214
215 case DW_EH_PE_pcrel:
216 assert(have_section_base_);
217 base = section_base_ + (buffer - buffer_base_);
218 break;
219
220 case DW_EH_PE_textrel:
221 assert(have_text_base_);
222 base = text_base_;
223 break;
224
225 case DW_EH_PE_datarel:
226 assert(have_data_base_);
227 base = data_base_;
228 break;
229
230 case DW_EH_PE_funcrel:
231 assert(have_function_base_);
232 base = function_base_;
233 break;
234
235 default:
236 abort();
237 }
238
239 uint64_t pointer = base + offset;
240
241 // Remove inappropriate upper bits.
242 if (AddressSize() == 4)
243 pointer = pointer & 0xffffffff;
244 else
245 assert(AddressSize() == sizeof(uint64_t));
246
247 return pointer;
248 }
249
GetEndianness() const250 Endianness ByteReader::GetEndianness() const {
251 return endian_;
252 }
253
254 } // namespace google_breakpad
255