1 // Copyright 2006 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 #ifndef UTIL_DEBUGINFO_BYTEREADER_INL_H__
30 #define UTIL_DEBUGINFO_BYTEREADER_INL_H__
31
32 #include "common/dwarf/bytereader.h"
33
34 #include <assert.h>
35 #include <stdint.h>
36
37 namespace dwarf2reader {
38
ReadOneByte(const uint8_t * buffer)39 inline uint8_t ByteReader::ReadOneByte(const uint8_t *buffer) const {
40 return buffer[0];
41 }
42
ReadTwoBytes(const uint8_t * buffer)43 inline uint16_t ByteReader::ReadTwoBytes(const uint8_t *buffer) const {
44 const uint16_t buffer0 = buffer[0];
45 const uint16_t buffer1 = buffer[1];
46 if (endian_ == ENDIANNESS_LITTLE) {
47 return buffer0 | buffer1 << 8;
48 } else {
49 return buffer1 | buffer0 << 8;
50 }
51 }
52
ReadFourBytes(const uint8_t * buffer)53 inline uint64_t ByteReader::ReadFourBytes(const uint8_t *buffer) const {
54 const uint32_t buffer0 = buffer[0];
55 const uint32_t buffer1 = buffer[1];
56 const uint32_t buffer2 = buffer[2];
57 const uint32_t buffer3 = buffer[3];
58 if (endian_ == ENDIANNESS_LITTLE) {
59 return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24;
60 } else {
61 return buffer3 | buffer2 << 8 | buffer1 << 16 | buffer0 << 24;
62 }
63 }
64
ReadEightBytes(const uint8_t * buffer)65 inline uint64_t ByteReader::ReadEightBytes(const uint8_t *buffer) const {
66 const uint64_t buffer0 = buffer[0];
67 const uint64_t buffer1 = buffer[1];
68 const uint64_t buffer2 = buffer[2];
69 const uint64_t buffer3 = buffer[3];
70 const uint64_t buffer4 = buffer[4];
71 const uint64_t buffer5 = buffer[5];
72 const uint64_t buffer6 = buffer[6];
73 const uint64_t buffer7 = buffer[7];
74 if (endian_ == ENDIANNESS_LITTLE) {
75 return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24 |
76 buffer4 << 32 | buffer5 << 40 | buffer6 << 48 | buffer7 << 56;
77 } else {
78 return buffer7 | buffer6 << 8 | buffer5 << 16 | buffer4 << 24 |
79 buffer3 << 32 | buffer2 << 40 | buffer1 << 48 | buffer0 << 56;
80 }
81 }
82
83 // Read an unsigned LEB128 number. Each byte contains 7 bits of
84 // information, plus one bit saying whether the number continues or
85 // not.
86
ReadUnsignedLEB128(const uint8_t * buffer,size_t * len)87 inline uint64_t ByteReader::ReadUnsignedLEB128(const uint8_t *buffer,
88 size_t* len) const {
89 uint64_t result = 0;
90 size_t num_read = 0;
91 unsigned int shift = 0;
92 uint8_t byte;
93
94 do {
95 byte = *buffer++;
96 num_read++;
97
98 result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
99
100 shift += 7;
101
102 } while (byte & 0x80);
103
104 *len = num_read;
105
106 return result;
107 }
108
109 // Read a signed LEB128 number. These are like regular LEB128
110 // numbers, except the last byte may have a sign bit set.
111
ReadSignedLEB128(const uint8_t * buffer,size_t * len)112 inline int64_t ByteReader::ReadSignedLEB128(const uint8_t *buffer,
113 size_t* len) const {
114 int64_t result = 0;
115 unsigned int shift = 0;
116 size_t num_read = 0;
117 uint8_t byte;
118
119 do {
120 byte = *buffer++;
121 num_read++;
122 result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
123 shift += 7;
124 } while (byte & 0x80);
125
126 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
127 result |= -((static_cast<int64_t>(1)) << shift);
128 *len = num_read;
129 return result;
130 }
131
ReadOffset(const uint8_t * buffer)132 inline uint64_t ByteReader::ReadOffset(const uint8_t *buffer) const {
133 assert(this->offset_reader_);
134 return (this->*offset_reader_)(buffer);
135 }
136
ReadAddress(const uint8_t * buffer)137 inline uint64_t ByteReader::ReadAddress(const uint8_t *buffer) const {
138 assert(this->address_reader_);
139 return (this->*address_reader_)(buffer);
140 }
141
SetCFIDataBase(uint64_t section_base,const uint8_t * buffer_base)142 inline void ByteReader::SetCFIDataBase(uint64_t section_base,
143 const uint8_t *buffer_base) {
144 section_base_ = section_base;
145 buffer_base_ = buffer_base;
146 have_section_base_ = true;
147 }
148
SetTextBase(uint64_t text_base)149 inline void ByteReader::SetTextBase(uint64_t text_base) {
150 text_base_ = text_base;
151 have_text_base_ = true;
152 }
153
SetDataBase(uint64_t data_base)154 inline void ByteReader::SetDataBase(uint64_t data_base) {
155 data_base_ = data_base;
156 have_data_base_ = true;
157 }
158
SetFunctionBase(uint64_t function_base)159 inline void ByteReader::SetFunctionBase(uint64_t function_base) {
160 function_base_ = function_base;
161 have_function_base_ = true;
162 }
163
ClearFunctionBase()164 inline void ByteReader::ClearFunctionBase() {
165 have_function_base_ = false;
166 }
167
168 } // namespace dwarf2reader
169
170 #endif // UTIL_DEBUGINFO_BYTEREADER_INL_H__
171