1 /*
2 * Copyright (C) 2016 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 "linker/relative_patcher_test.h"
18 #include "linker/mips/relative_patcher_mips.h"
19
20 namespace art {
21 namespace linker {
22
23 class Mips32r6RelativePatcherTest : public RelativePatcherTest {
24 public:
Mips32r6RelativePatcherTest()25 Mips32r6RelativePatcherTest() : RelativePatcherTest(kMips, "mips32r6") {}
26
27 protected:
28 static const uint8_t kUnpatchedPcRelativeRawCode[];
29 static const uint32_t kLiteralOffsetHigh;
30 static const uint32_t kLiteralOffsetLow1;
31 static const uint32_t kLiteralOffsetLow2;
32 static const uint32_t kAnchorOffset;
33 static const ArrayRef<const uint8_t> kUnpatchedPcRelativeCode;
34
GetMethodOffset(uint32_t method_idx)35 uint32_t GetMethodOffset(uint32_t method_idx) {
36 auto result = method_offset_map_.FindMethodOffset(MethodRef(method_idx));
37 CHECK(result.first);
38 return result.second;
39 }
40
41 void CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches, uint32_t target_offset);
42 void TestStringBssEntry(uint32_t bss_begin, uint32_t string_entry_offset);
43 void TestStringReference(uint32_t string_offset);
44 };
45
46 const uint8_t Mips32r6RelativePatcherTest::kUnpatchedPcRelativeRawCode[] = {
47 0x34, 0x12, 0x5E, 0xEE, // auipc s2, high(diff); placeholder = 0x1234
48 0x78, 0x56, 0x52, 0x26, // addiu s2, s2, low(diff); placeholder = 0x5678
49 0x78, 0x56, 0x52, 0x8E, // lw s2, (low(diff))(s2) ; placeholder = 0x5678
50 };
51 const uint32_t Mips32r6RelativePatcherTest::kLiteralOffsetHigh = 0; // At auipc.
52 const uint32_t Mips32r6RelativePatcherTest::kLiteralOffsetLow1 = 4; // At addiu.
53 const uint32_t Mips32r6RelativePatcherTest::kLiteralOffsetLow2 = 8; // At lw.
54 const uint32_t Mips32r6RelativePatcherTest::kAnchorOffset = 0; // At auipc (where PC+0 points).
55 const ArrayRef<const uint8_t> Mips32r6RelativePatcherTest::kUnpatchedPcRelativeCode(
56 kUnpatchedPcRelativeRawCode);
57
CheckPcRelativePatch(const ArrayRef<const LinkerPatch> & patches,uint32_t target_offset)58 void Mips32r6RelativePatcherTest::CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches,
59 uint32_t target_offset) {
60 AddCompiledMethod(MethodRef(1u), kUnpatchedPcRelativeCode, ArrayRef<const LinkerPatch>(patches));
61 Link();
62
63 auto result = method_offset_map_.FindMethodOffset(MethodRef(1u));
64 ASSERT_TRUE(result.first);
65
66 uint32_t diff = target_offset - (result.second + kAnchorOffset);
67 diff += (diff & 0x8000) << 1; // Account for sign extension in addiu/lw.
68
69 const uint8_t expected_code[] = {
70 static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24), 0x5E, 0xEE,
71 static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x26,
72 static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x8E,
73 };
74 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
75 }
76
TestStringBssEntry(uint32_t bss_begin,uint32_t string_entry_offset)77 void Mips32r6RelativePatcherTest::TestStringBssEntry(uint32_t bss_begin,
78 uint32_t string_entry_offset) {
79 constexpr uint32_t kStringIndex = 1u;
80 string_index_to_offset_map_.Put(kStringIndex, string_entry_offset);
81 bss_begin_ = bss_begin;
82 LinkerPatch patches[] = {
83 LinkerPatch::StringBssEntryPatch(kLiteralOffsetHigh, nullptr, kAnchorOffset, kStringIndex),
84 LinkerPatch::StringBssEntryPatch(kLiteralOffsetLow1, nullptr, kAnchorOffset, kStringIndex),
85 LinkerPatch::StringBssEntryPatch(kLiteralOffsetLow2, nullptr, kAnchorOffset, kStringIndex)
86 };
87 CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), bss_begin_ + string_entry_offset);
88 }
89
TestStringReference(uint32_t string_offset)90 void Mips32r6RelativePatcherTest::TestStringReference(uint32_t string_offset) {
91 constexpr uint32_t kStringIndex = 1u;
92 string_index_to_offset_map_.Put(kStringIndex, string_offset);
93 LinkerPatch patches[] = {
94 LinkerPatch::RelativeStringPatch(kLiteralOffsetHigh, nullptr, kAnchorOffset, kStringIndex),
95 LinkerPatch::RelativeStringPatch(kLiteralOffsetLow1, nullptr, kAnchorOffset, kStringIndex),
96 LinkerPatch::RelativeStringPatch(kLiteralOffsetLow2, nullptr, kAnchorOffset, kStringIndex)
97 };
98 CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), string_offset);
99 }
100
TEST_F(Mips32r6RelativePatcherTest,StringBssEntry)101 TEST_F(Mips32r6RelativePatcherTest, StringBssEntry) {
102 TestStringBssEntry(/* bss_begin */ 0x12345678, /* string_entry_offset */ 0x1234);
103 }
104
TEST_F(Mips32r6RelativePatcherTest,StringReference)105 TEST_F(Mips32r6RelativePatcherTest, StringReference) {
106 TestStringReference(/* string_offset*/ 0x87651234);
107 }
108
109 } // namespace linker
110 } // namespace art
111