blob: f12d2fe3ea6a363159d34ac7d80bcaa3889e9d55 [file] [log] [blame]
Christopher Ferris72a6fa62017-03-21 12:41:17 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18
19#include <ios>
20#include <vector>
21
22#include <gtest/gtest.h>
23
Christopher Ferrisd226a512017-07-14 10:37:19 -070024#include <unwindstack/DwarfMemory.h>
Christopher Ferris72a6fa62017-03-21 12:41:17 -070025
26#include "MemoryFake.h"
27
Christopher Ferrisd226a512017-07-14 10:37:19 -070028namespace unwindstack {
29
Christopher Ferris72a6fa62017-03-21 12:41:17 -070030class DwarfMemoryTest : public ::testing::Test {
31 protected:
32 void SetUp() override {
33 memory_.Clear();
34 dwarf_mem_.reset(new DwarfMemory(&memory_));
35 }
36
37 template <typename AddressType>
38 void GetEncodedSizeTest(uint8_t value, size_t expected);
39 template <typename AddressType>
40 void ReadEncodedValue_omit();
41 template <typename AddressType>
42 void ReadEncodedValue_leb128();
43 template <typename AddressType>
44 void ReadEncodedValue_data1();
45 template <typename AddressType>
46 void ReadEncodedValue_data2();
47 template <typename AddressType>
48 void ReadEncodedValue_data4();
49 template <typename AddressType>
50 void ReadEncodedValue_data8();
51 template <typename AddressType>
52 void ReadEncodedValue_non_zero_adjust();
53 template <typename AddressType>
54 void ReadEncodedValue_overflow();
Christopher Ferris9e484bd2017-08-10 17:37:32 -070055 template <typename AddressType>
56 void ReadEncodedValue_high_bit_set();
Christopher Ferris72a6fa62017-03-21 12:41:17 -070057
58 MemoryFake memory_;
59 std::unique_ptr<DwarfMemory> dwarf_mem_;
60};
61
62TEST_F(DwarfMemoryTest, ReadBytes) {
63 memory_.SetMemory(0, std::vector<uint8_t>{0x10, 0x18, 0xff, 0xfe});
64
65 uint8_t byte;
66 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
67 ASSERT_EQ(0x10U, byte);
68 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
69 ASSERT_EQ(0x18U, byte);
70 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
71 ASSERT_EQ(0xffU, byte);
72 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
73 ASSERT_EQ(0xfeU, byte);
74 ASSERT_EQ(4U, dwarf_mem_->cur_offset());
75
76 dwarf_mem_->set_cur_offset(2);
77 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
78 ASSERT_EQ(0xffU, byte);
79 ASSERT_EQ(3U, dwarf_mem_->cur_offset());
80}
81
82TEST_F(DwarfMemoryTest, ReadSigned_check) {
83 uint64_t value;
84
85 // Signed 8 byte reads.
86 memory_.SetData8(0, static_cast<uint8_t>(-10));
87 memory_.SetData8(1, 200);
88 ASSERT_TRUE(dwarf_mem_->ReadSigned<int8_t>(&value));
89 ASSERT_EQ(static_cast<int8_t>(-10), static_cast<int8_t>(value));
90 ASSERT_TRUE(dwarf_mem_->ReadSigned<int8_t>(&value));
91 ASSERT_EQ(static_cast<int8_t>(200), static_cast<int8_t>(value));
92
93 // Signed 16 byte reads.
94 memory_.SetData16(0x10, static_cast<uint16_t>(-1000));
95 memory_.SetData16(0x12, 50100);
96 dwarf_mem_->set_cur_offset(0x10);
97 ASSERT_TRUE(dwarf_mem_->ReadSigned<int16_t>(&value));
98 ASSERT_EQ(static_cast<int16_t>(-1000), static_cast<int16_t>(value));
99 ASSERT_TRUE(dwarf_mem_->ReadSigned<int16_t>(&value));
100 ASSERT_EQ(static_cast<int16_t>(50100), static_cast<int16_t>(value));
101
102 // Signed 32 byte reads.
103 memory_.SetData32(0x100, static_cast<uint32_t>(-1000000000));
104 memory_.SetData32(0x104, 3000000000);
105 dwarf_mem_->set_cur_offset(0x100);
106 ASSERT_TRUE(dwarf_mem_->ReadSigned<int32_t>(&value));
107 ASSERT_EQ(static_cast<int32_t>(-1000000000), static_cast<int32_t>(value));
108 ASSERT_TRUE(dwarf_mem_->ReadSigned<int32_t>(&value));
109 ASSERT_EQ(static_cast<int32_t>(3000000000), static_cast<int32_t>(value));
110
111 // Signed 64 byte reads.
112 memory_.SetData64(0x200, static_cast<uint64_t>(-2000000000000LL));
113 memory_.SetData64(0x208, 5000000000000LL);
114 dwarf_mem_->set_cur_offset(0x200);
115 ASSERT_TRUE(dwarf_mem_->ReadSigned<int64_t>(&value));
116 ASSERT_EQ(static_cast<int64_t>(-2000000000000), static_cast<int64_t>(value));
117 ASSERT_TRUE(dwarf_mem_->ReadSigned<int64_t>(&value));
118 ASSERT_EQ(static_cast<int64_t>(5000000000000), static_cast<int64_t>(value));
119}
120
121TEST_F(DwarfMemoryTest, ReadULEB128) {
122 memory_.SetMemory(0, std::vector<uint8_t>{0x01, 0x80, 0x24, 0xff, 0xc3, 0xff, 0x7f});
123
124 uint64_t value;
125 ASSERT_TRUE(dwarf_mem_->ReadULEB128(&value));
126 ASSERT_EQ(1U, dwarf_mem_->cur_offset());
127 ASSERT_EQ(1U, value);
128
129 ASSERT_TRUE(dwarf_mem_->ReadULEB128(&value));
130 ASSERT_EQ(3U, dwarf_mem_->cur_offset());
131 ASSERT_EQ(0x1200U, value);
132
133 ASSERT_TRUE(dwarf_mem_->ReadULEB128(&value));
134 ASSERT_EQ(7U, dwarf_mem_->cur_offset());
135 ASSERT_EQ(0xfffe1ffU, value);
136}
137
138TEST_F(DwarfMemoryTest, ReadSLEB128) {
139 memory_.SetMemory(0, std::vector<uint8_t>{0x06, 0x40, 0x82, 0x34, 0x89, 0x64, 0xf9, 0xc3, 0x8f,
140 0x2f, 0xbf, 0xc3, 0xf7, 0x5f});
141
142 int64_t value;
143 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
144 ASSERT_EQ(1U, dwarf_mem_->cur_offset());
145 ASSERT_EQ(6U, value);
146
147 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
148 ASSERT_EQ(2U, dwarf_mem_->cur_offset());
149 ASSERT_EQ(0xffffffffffffffc0ULL, static_cast<uint64_t>(value));
150
151 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
152 ASSERT_EQ(4U, dwarf_mem_->cur_offset());
153 ASSERT_EQ(0x1a02U, value);
154
155 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
156 ASSERT_EQ(6U, dwarf_mem_->cur_offset());
157 ASSERT_EQ(0xfffffffffffff209ULL, static_cast<uint64_t>(value));
158
159 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
160 ASSERT_EQ(10U, dwarf_mem_->cur_offset());
161 ASSERT_EQ(0x5e3e1f9U, value);
162
163 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
164 ASSERT_EQ(14U, dwarf_mem_->cur_offset());
165 ASSERT_EQ(0xfffffffffbfde1bfULL, static_cast<uint64_t>(value));
166}
167
168template <typename AddressType>
169void DwarfMemoryTest::GetEncodedSizeTest(uint8_t value, size_t expected) {
170 for (size_t i = 0; i < 16; i++) {
171 uint8_t encoding = (i << 4) | value;
172 ASSERT_EQ(expected, dwarf_mem_->GetEncodedSize<AddressType>(encoding))
173 << "encoding 0x" << std::hex << static_cast<uint32_t>(encoding) << " test value 0x"
174 << static_cast<size_t>(value);
175 }
176}
177
178TEST_F(DwarfMemoryTest, GetEncodedSize_absptr_uint32_t) {
179 GetEncodedSizeTest<uint32_t>(0, sizeof(uint32_t));
180}
181
182TEST_F(DwarfMemoryTest, GetEncodedSize_absptr_uint64_t) {
183 GetEncodedSizeTest<uint64_t>(0, sizeof(uint64_t));
184}
185
186TEST_F(DwarfMemoryTest, GetEncodedSize_data1) {
187 // udata1
188 GetEncodedSizeTest<uint32_t>(0x0d, 1);
189 GetEncodedSizeTest<uint64_t>(0x0d, 1);
190
191 // sdata1
192 GetEncodedSizeTest<uint32_t>(0x0e, 1);
193 GetEncodedSizeTest<uint64_t>(0x0e, 1);
194}
195
196TEST_F(DwarfMemoryTest, GetEncodedSize_data2) {
197 // udata2
198 GetEncodedSizeTest<uint32_t>(0x02, 2);
199 GetEncodedSizeTest<uint64_t>(0x02, 2);
200
201 // sdata2
202 GetEncodedSizeTest<uint32_t>(0x0a, 2);
203 GetEncodedSizeTest<uint64_t>(0x0a, 2);
204}
205
206TEST_F(DwarfMemoryTest, GetEncodedSize_data4) {
207 // udata4
208 GetEncodedSizeTest<uint32_t>(0x03, 4);
209 GetEncodedSizeTest<uint64_t>(0x03, 4);
210
211 // sdata4
212 GetEncodedSizeTest<uint32_t>(0x0b, 4);
213 GetEncodedSizeTest<uint64_t>(0x0b, 4);
214}
215
216TEST_F(DwarfMemoryTest, GetEncodedSize_data8) {
217 // udata8
218 GetEncodedSizeTest<uint32_t>(0x04, 8);
219 GetEncodedSizeTest<uint64_t>(0x04, 8);
220
221 // sdata8
222 GetEncodedSizeTest<uint32_t>(0x0c, 8);
223 GetEncodedSizeTest<uint64_t>(0x0c, 8);
224}
225
226TEST_F(DwarfMemoryTest, GetEncodedSize_unknown) {
227 GetEncodedSizeTest<uint32_t>(0x01, 0);
228 GetEncodedSizeTest<uint64_t>(0x01, 0);
229
230 GetEncodedSizeTest<uint32_t>(0x09, 0);
231 GetEncodedSizeTest<uint64_t>(0x09, 0);
232
233 GetEncodedSizeTest<uint32_t>(0x0f, 0);
234 GetEncodedSizeTest<uint64_t>(0x0f, 0);
235}
236
237template <typename AddressType>
238void DwarfMemoryTest::ReadEncodedValue_omit() {
239 uint64_t value = 123;
240 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0xff, &value));
241 ASSERT_EQ(0U, value);
242}
243
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700244TEST_F(DwarfMemoryTest, ReadEncodedValue_omit_uint32_t) {
245 ReadEncodedValue_omit<uint32_t>();
246}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700247
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700248TEST_F(DwarfMemoryTest, ReadEncodedValue_omit_uint64_t) {
249 ReadEncodedValue_omit<uint64_t>();
250}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700251
252TEST_F(DwarfMemoryTest, ReadEncodedValue_absptr_uint32_t) {
253 uint64_t value = 100;
254 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<uint32_t>(0x00, &value));
255
256 memory_.SetData32(0, 0x12345678);
257
258 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<uint32_t>(0x00, &value));
259 ASSERT_EQ(4U, dwarf_mem_->cur_offset());
260 ASSERT_EQ(0x12345678U, value);
261}
262
263TEST_F(DwarfMemoryTest, ReadEncodedValue_absptr_uint64_t) {
264 uint64_t value = 100;
265 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<uint64_t>(0x00, &value));
266
267 memory_.SetData64(0, 0x12345678f1f2f3f4ULL);
268
269 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<uint64_t>(0x00, &value));
270 ASSERT_EQ(8U, dwarf_mem_->cur_offset());
271 ASSERT_EQ(0x12345678f1f2f3f4ULL, value);
272}
273
274TEST_F(DwarfMemoryTest, ReadEncodedValue_aligned_uint32_t) {
275 uint64_t value = 100;
276 dwarf_mem_->set_cur_offset(1);
277 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<uint32_t>(0x50, &value));
278
279 memory_.SetData32(4, 0x12345678);
280
281 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<uint32_t>(0x50, &value));
282 ASSERT_EQ(8U, dwarf_mem_->cur_offset());
283 ASSERT_EQ(0x12345678U, value);
284}
285
286TEST_F(DwarfMemoryTest, ReadEncodedValue_aligned_uint64_t) {
287 uint64_t value = 100;
288 dwarf_mem_->set_cur_offset(1);
289 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<uint64_t>(0x50, &value));
290
291 memory_.SetData64(8, 0x12345678f1f2f3f4ULL);
292
293 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<uint64_t>(0x50, &value));
294 ASSERT_EQ(16U, dwarf_mem_->cur_offset());
295 ASSERT_EQ(0x12345678f1f2f3f4ULL, value);
296}
297
298template <typename AddressType>
299void DwarfMemoryTest::ReadEncodedValue_leb128() {
300 memory_.SetMemory(0, std::vector<uint8_t>{0x80, 0x42});
301
302 uint64_t value = 100;
303 // uleb128
304 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x01, &value));
305 ASSERT_EQ(0x2100U, value);
306
307 dwarf_mem_->set_cur_offset(0);
308 // sleb128
309 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x09, &value));
310 ASSERT_EQ(0xffffffffffffe100ULL, value);
311}
312
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700313TEST_F(DwarfMemoryTest, ReadEncodedValue_leb128_uint32_t) {
314 ReadEncodedValue_leb128<uint32_t>();
315}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700316
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700317TEST_F(DwarfMemoryTest, ReadEncodedValue_leb128_uint64_t) {
318 ReadEncodedValue_leb128<uint64_t>();
319}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700320
321template <typename AddressType>
322void DwarfMemoryTest::ReadEncodedValue_data1() {
323 memory_.SetData8(0, 0xe0);
324
325 uint64_t value = 0;
326 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0d, &value));
327 ASSERT_EQ(0xe0U, value);
328
329 dwarf_mem_->set_cur_offset(0);
330 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0e, &value));
331 ASSERT_EQ(0xffffffffffffffe0ULL, value);
332}
333
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700334TEST_F(DwarfMemoryTest, ReadEncodedValue_data1_uint32_t) {
335 ReadEncodedValue_data1<uint32_t>();
336}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700337
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700338TEST_F(DwarfMemoryTest, ReadEncodedValue_data1_uint64_t) {
339 ReadEncodedValue_data1<uint64_t>();
340}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700341
342template <typename AddressType>
343void DwarfMemoryTest::ReadEncodedValue_data2() {
344 memory_.SetData16(0, 0xe000);
345
346 uint64_t value = 0;
347 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x02, &value));
348 ASSERT_EQ(0xe000U, value);
349
350 dwarf_mem_->set_cur_offset(0);
351 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0a, &value));
352 ASSERT_EQ(0xffffffffffffe000ULL, value);
353}
354
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700355TEST_F(DwarfMemoryTest, ReadEncodedValue_data2_uint32_t) {
356 ReadEncodedValue_data2<uint32_t>();
357}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700358
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700359TEST_F(DwarfMemoryTest, ReadEncodedValue_data2_uint64_t) {
360 ReadEncodedValue_data2<uint64_t>();
361}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700362
363template <typename AddressType>
364void DwarfMemoryTest::ReadEncodedValue_data4() {
365 memory_.SetData32(0, 0xe0000000);
366
367 uint64_t value = 0;
368 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x03, &value));
369 ASSERT_EQ(0xe0000000U, value);
370
371 dwarf_mem_->set_cur_offset(0);
372 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0b, &value));
373 ASSERT_EQ(0xffffffffe0000000ULL, value);
374}
375
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700376TEST_F(DwarfMemoryTest, ReadEncodedValue_data4_uint32_t) {
377 ReadEncodedValue_data4<uint32_t>();
378}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700379
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700380TEST_F(DwarfMemoryTest, ReadEncodedValue_data4_uint64_t) {
381 ReadEncodedValue_data4<uint64_t>();
382}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700383
384template <typename AddressType>
385void DwarfMemoryTest::ReadEncodedValue_data8() {
386 memory_.SetData64(0, 0xe000000000000000ULL);
387
388 uint64_t value = 0;
389 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x04, &value));
390 ASSERT_EQ(0xe000000000000000ULL, value);
391
392 dwarf_mem_->set_cur_offset(0);
393 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0c, &value));
394 ASSERT_EQ(0xe000000000000000ULL, value);
395}
396
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700397TEST_F(DwarfMemoryTest, ReadEncodedValue_data8_uint32_t) {
398 ReadEncodedValue_data8<uint32_t>();
399}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700400
Christopher Ferrisf6d54312017-07-11 15:29:32 -0700401TEST_F(DwarfMemoryTest, ReadEncodedValue_data8_uint64_t) {
402 ReadEncodedValue_data8<uint64_t>();
403}
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700404
405template <typename AddressType>
406void DwarfMemoryTest::ReadEncodedValue_non_zero_adjust() {
407 memory_.SetData64(0, 0xe000000000000000ULL);
408
409 uint64_t value = 0;
410 dwarf_mem_->set_pc_offset(0x2000);
411 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x14, &value));
412 ASSERT_EQ(0xe000000000002000ULL, value);
413}
414
415TEST_F(DwarfMemoryTest, ReadEncodedValue_non_zero_adjust_uint32_t) {
416 ReadEncodedValue_non_zero_adjust<uint32_t>();
417}
418
419TEST_F(DwarfMemoryTest, ReadEncodedValue_non_zero_adjust_uint64_t) {
420 ReadEncodedValue_non_zero_adjust<uint64_t>();
421}
422
423template <typename AddressType>
424void DwarfMemoryTest::ReadEncodedValue_overflow() {
425 memory_.SetData64(0, 0);
426
427 uint64_t value = 0;
428 dwarf_mem_->set_cur_offset(UINT64_MAX);
429 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<AddressType>(0x50, &value));
430}
431
432TEST_F(DwarfMemoryTest, ReadEncodedValue_overflow_uint32_t) {
433 ReadEncodedValue_overflow<uint32_t>();
434}
435
436TEST_F(DwarfMemoryTest, ReadEncodedValue_overflow_uint64_t) {
437 ReadEncodedValue_overflow<uint64_t>();
438}
439
Christopher Ferris9e484bd2017-08-10 17:37:32 -0700440template <typename AddressType>
441void DwarfMemoryTest::ReadEncodedValue_high_bit_set() {
442 uint64_t value;
443 memory_.SetData32(0, 0x15234);
444 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<AddressType>(0xc3, &value));
445
446 dwarf_mem_->set_func_offset(0x60000);
447 dwarf_mem_->set_cur_offset(0);
448 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0xc3, &value));
449 ASSERT_EQ(0x75234U, value);
450}
451
452TEST_F(DwarfMemoryTest, ReadEncodedValue_high_bit_set_uint32_t) {
453 ReadEncodedValue_high_bit_set<uint32_t>();
454}
455
456TEST_F(DwarfMemoryTest, ReadEncodedValue_high_bit_set_uint64_t) {
457 ReadEncodedValue_high_bit_set<uint64_t>();
458}
459
Christopher Ferris72a6fa62017-03-21 12:41:17 -0700460TEST_F(DwarfMemoryTest, AdjustEncodedValue_absptr) {
461 uint64_t value = 0x1234;
462 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x00, &value));
463 ASSERT_EQ(0x1234U, value);
464}
465
466TEST_F(DwarfMemoryTest, AdjustEncodedValue_pcrel) {
467 uint64_t value = 0x1234;
468 ASSERT_FALSE(dwarf_mem_->AdjustEncodedValue(0x10, &value));
469
470 dwarf_mem_->set_pc_offset(0x2000);
471 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x10, &value));
472 ASSERT_EQ(0x3234U, value);
473
474 dwarf_mem_->set_pc_offset(static_cast<uint64_t>(-4));
475 value = 0x1234;
476 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x10, &value));
477 ASSERT_EQ(0x1230U, value);
478}
479
480TEST_F(DwarfMemoryTest, AdjustEncodedValue_textrel) {
481 uint64_t value = 0x8234;
482 ASSERT_FALSE(dwarf_mem_->AdjustEncodedValue(0x20, &value));
483
484 dwarf_mem_->set_text_offset(0x1000);
485 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x20, &value));
486 ASSERT_EQ(0x9234U, value);
487
488 dwarf_mem_->set_text_offset(static_cast<uint64_t>(-16));
489 value = 0x8234;
490 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x20, &value));
491 ASSERT_EQ(0x8224U, value);
492}
493
494TEST_F(DwarfMemoryTest, AdjustEncodedValue_datarel) {
495 uint64_t value = 0xb234;
496 ASSERT_FALSE(dwarf_mem_->AdjustEncodedValue(0x30, &value));
497
498 dwarf_mem_->set_data_offset(0x1200);
499 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x30, &value));
500 ASSERT_EQ(0xc434U, value);
501
502 dwarf_mem_->set_data_offset(static_cast<uint64_t>(-256));
503 value = 0xb234;
504 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x30, &value));
505 ASSERT_EQ(0xb134U, value);
506}
507
508TEST_F(DwarfMemoryTest, AdjustEncodedValue_funcrel) {
509 uint64_t value = 0x15234;
510 ASSERT_FALSE(dwarf_mem_->AdjustEncodedValue(0x40, &value));
511
512 dwarf_mem_->set_func_offset(0x60000);
513 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x40, &value));
514 ASSERT_EQ(0x75234U, value);
515
516 dwarf_mem_->set_func_offset(static_cast<uint64_t>(-4096));
517 value = 0x15234;
518 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x40, &value));
519 ASSERT_EQ(0x14234U, value);
520}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700521
522} // namespace unwindstack