1 // Copyright (c) 2010 Google Inc. All Rights Reserved.
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 Inc. 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 #include <assert.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 
33 #include "common/dwarf/bytereader-inl.h"
34 #include "common/dwarf/bytereader.h"
35 
36 namespace dwarf2reader {
37 
ByteReader(enum Endianness endian)38 ByteReader::ByteReader(enum Endianness endian)
39     :offset_reader_(NULL), address_reader_(NULL), endian_(endian),
40      address_size_(0), offset_size_(0),
41      have_section_base_(), have_text_base_(), have_data_base_(),
42      have_function_base_() { }
43 
~ByteReader()44 ByteReader::~ByteReader() { }
45 
SetOffsetSize(uint8_t size)46 void ByteReader::SetOffsetSize(uint8_t size) {
47   offset_size_ = size;
48   assert(size == 4 || size == 8);
49   if (size == 4) {
50     this->offset_reader_ = &ByteReader::ReadFourBytes;
51   } else {
52     this->offset_reader_ = &ByteReader::ReadEightBytes;
53   }
54 }
55 
SetAddressSize(uint8_t size)56 void ByteReader::SetAddressSize(uint8_t size) {
57   address_size_ = size;
58   assert(size == 4 || size == 8);
59   if (size == 4) {
60     this->address_reader_ = &ByteReader::ReadFourBytes;
61   } else {
62     this->address_reader_ = &ByteReader::ReadEightBytes;
63   }
64 }
65 
ReadInitialLength(const uint8_t * start,size_t * len)66 uint64_t ByteReader::ReadInitialLength(const uint8_t *start, size_t* len) {
67   const uint64_t initial_length = ReadFourBytes(start);
68   start += 4;
69 
70   // In DWARF2/3, if the initial length is all 1 bits, then the offset
71   // size is 8 and we need to read the next 8 bytes for the real length.
72   if (initial_length == 0xffffffff) {
73     SetOffsetSize(8);
74     *len = 12;
75     return ReadOffset(start);
76   } else {
77     SetOffsetSize(4);
78     *len = 4;
79   }
80   return initial_length;
81 }
82 
ValidEncoding(DwarfPointerEncoding encoding) const83 bool ByteReader::ValidEncoding(DwarfPointerEncoding encoding) const {
84   if (encoding == DW_EH_PE_omit) return true;
85   if (encoding == DW_EH_PE_aligned) return true;
86   if ((encoding & 0x7) > DW_EH_PE_udata8)
87     return false;
88   if ((encoding & 0x70) > DW_EH_PE_funcrel)
89     return false;
90   return true;
91 }
92 
UsableEncoding(DwarfPointerEncoding encoding) const93 bool ByteReader::UsableEncoding(DwarfPointerEncoding encoding) const {
94   switch (encoding & 0x70) {
95     case DW_EH_PE_absptr:  return true;
96     case DW_EH_PE_pcrel:   return have_section_base_;
97     case DW_EH_PE_textrel: return have_text_base_;
98     case DW_EH_PE_datarel: return have_data_base_;
99     case DW_EH_PE_funcrel: return have_function_base_;
100     default:               return false;
101   }
102 }
103 
ReadEncodedPointer(const uint8_t * buffer,DwarfPointerEncoding encoding,size_t * len) const104 uint64_t ByteReader::ReadEncodedPointer(const uint8_t *buffer,
105                                       DwarfPointerEncoding encoding,
106                                       size_t *len) const {
107   // UsableEncoding doesn't approve of DW_EH_PE_omit, so we shouldn't
108   // see it here.
109   assert(encoding != DW_EH_PE_omit);
110 
111   // The Linux Standards Base 4.0 does not make this clear, but the
112   // GNU tools (gcc/unwind-pe.h; readelf/dwarf.c; gdb/dwarf2-frame.c)
113   // agree that aligned pointers are always absolute, machine-sized,
114   // machine-signed pointers.
115   if (encoding == DW_EH_PE_aligned) {
116     assert(have_section_base_);
117 
118     // We don't need to align BUFFER in *our* address space. Rather, we
119     // need to find the next position in our buffer that would be aligned
120     // when the .eh_frame section the buffer contains is loaded into the
121     // program's memory. So align assuming that buffer_base_ gets loaded at
122     // address section_base_, where section_base_ itself may or may not be
123     // aligned.
124 
125     // First, find the offset to START from the closest prior aligned
126     // address.
127     uint64_t skew = section_base_ & (AddressSize() - 1);
128     // Now find the offset from that aligned address to buffer.
129     uint64_t offset = skew + (buffer - buffer_base_);
130     // Round up to the next boundary.
131     uint64_t aligned = (offset + AddressSize() - 1) & -AddressSize();
132     // Convert back to a pointer.
133     const uint8_t *aligned_buffer = buffer_base_ + (aligned - skew);
134     // Finally, store the length and actually fetch the pointer.
135     *len = aligned_buffer - buffer + AddressSize();
136     return ReadAddress(aligned_buffer);
137   }
138 
139   // Extract the value first, ignoring whether it's a pointer or an
140   // offset relative to some base.
141   uint64_t offset;
142   switch (encoding & 0x0f) {
143     case DW_EH_PE_absptr:
144       // DW_EH_PE_absptr is weird, as it is used as a meaningful value for
145       // both the high and low nybble of encoding bytes. When it appears in
146       // the high nybble, it means that the pointer is absolute, not an
147       // offset from some base address. When it appears in the low nybble,
148       // as here, it means that the pointer is stored as a normal
149       // machine-sized and machine-signed address. A low nybble of
150       // DW_EH_PE_absptr does not imply that the pointer is absolute; it is
151       // correct for us to treat the value as an offset from a base address
152       // if the upper nybble is not DW_EH_PE_absptr.
153       offset = ReadAddress(buffer);
154       *len = AddressSize();
155       break;
156 
157     case DW_EH_PE_uleb128:
158       offset = ReadUnsignedLEB128(buffer, len);
159       break;
160 
161     case DW_EH_PE_udata2:
162       offset = ReadTwoBytes(buffer);
163       *len = 2;
164       break;
165 
166     case DW_EH_PE_udata4:
167       offset = ReadFourBytes(buffer);
168       *len = 4;
169       break;
170 
171     case DW_EH_PE_udata8:
172       offset = ReadEightBytes(buffer);
173       *len = 8;
174       break;
175 
176     case DW_EH_PE_sleb128:
177       offset = ReadSignedLEB128(buffer, len);
178       break;
179 
180     case DW_EH_PE_sdata2:
181       offset = ReadTwoBytes(buffer);
182       // Sign-extend from 16 bits.
183       offset = (offset ^ 0x8000) - 0x8000;
184       *len = 2;
185       break;
186 
187     case DW_EH_PE_sdata4:
188       offset = ReadFourBytes(buffer);
189       // Sign-extend from 32 bits.
190       offset = (offset ^ 0x80000000ULL) - 0x80000000ULL;
191       *len = 4;
192       break;
193 
194     case DW_EH_PE_sdata8:
195       // No need to sign-extend; this is the full width of our type.
196       offset = ReadEightBytes(buffer);
197       *len = 8;
198       break;
199 
200     default:
201       abort();
202   }
203 
204   // Find the appropriate base address.
205   uint64_t base;
206   switch (encoding & 0x70) {
207     case DW_EH_PE_absptr:
208       base = 0;
209       break;
210 
211     case DW_EH_PE_pcrel:
212       assert(have_section_base_);
213       base = section_base_ + (buffer - buffer_base_);
214       break;
215 
216     case DW_EH_PE_textrel:
217       assert(have_text_base_);
218       base = text_base_;
219       break;
220 
221     case DW_EH_PE_datarel:
222       assert(have_data_base_);
223       base = data_base_;
224       break;
225 
226     case DW_EH_PE_funcrel:
227       assert(have_function_base_);
228       base = function_base_;
229       break;
230 
231     default:
232       abort();
233   }
234 
235   uint64_t pointer = base + offset;
236 
237   // Remove inappropriate upper bits.
238   if (AddressSize() == 4)
239     pointer = pointer & 0xffffffff;
240   else
241     assert(AddressSize() == sizeof(uint64_t));
242 
243   return pointer;
244 }
245 
GetEndianness() const246 Endianness ByteReader::GetEndianness() const {
247   return endian_;
248 }
249 
250 }  // namespace dwarf2reader
251