• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MipsRelativePatcherTest : public RelativePatcherTest {
24  public:
MipsRelativePatcherTest()25   MipsRelativePatcherTest() : RelativePatcherTest(kMips, "mips32r2") {}
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 MipsRelativePatcherTest::kUnpatchedPcRelativeRawCode[] = {
47     0x00, 0x00, 0x10, 0x04,  // nal
48     0x34, 0x12, 0x12, 0x3C,  // lui   s2, high(diff); placeholder = 0x1234
49     0x21, 0x90, 0x5F, 0x02,  // addu  s2, s2, ra
50     0x78, 0x56, 0x52, 0x26,  // addiu s2, s2, low(diff); placeholder = 0x5678
51     0x78, 0x56, 0x52, 0x8E,  // lw    s2, (low(diff))(s2) ; placeholder = 0x5678
52 };
53 const uint32_t MipsRelativePatcherTest::kLiteralOffsetHigh = 4;  // At lui.
54 const uint32_t MipsRelativePatcherTest::kLiteralOffsetLow1 = 12;  // At addiu.
55 const uint32_t MipsRelativePatcherTest::kLiteralOffsetLow2 = 16;  // At lw.
56 const uint32_t MipsRelativePatcherTest::kAnchorOffset = 8;  // At addu (where PC+0 points).
57 const ArrayRef<const uint8_t> MipsRelativePatcherTest::kUnpatchedPcRelativeCode(
58     kUnpatchedPcRelativeRawCode);
59 
CheckPcRelativePatch(const ArrayRef<const LinkerPatch> & patches,uint32_t target_offset)60 void MipsRelativePatcherTest::CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches,
61                                                    uint32_t target_offset) {
62   AddCompiledMethod(MethodRef(1u), kUnpatchedPcRelativeCode, ArrayRef<const LinkerPatch>(patches));
63   Link();
64 
65   auto result = method_offset_map_.FindMethodOffset(MethodRef(1u));
66   ASSERT_TRUE(result.first);
67 
68   uint32_t diff = target_offset - (result.second + kAnchorOffset);
69   diff += (diff & 0x8000) << 1;  // Account for sign extension in addiu/lw.
70 
71   const uint8_t expected_code[] = {
72       0x00, 0x00, 0x10, 0x04,
73       static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24), 0x12, 0x3C,
74       0x21, 0x90, 0x5F, 0x02,
75       static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x26,
76       static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x8E,
77   };
78   EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
79 }
80 
TestStringBssEntry(uint32_t bss_begin,uint32_t string_entry_offset)81 void MipsRelativePatcherTest::TestStringBssEntry(uint32_t bss_begin,
82                                                  uint32_t string_entry_offset) {
83   constexpr uint32_t kStringIndex = 1u;
84   string_index_to_offset_map_.Put(kStringIndex, string_entry_offset);
85   bss_begin_ = bss_begin;
86   LinkerPatch patches[] = {
87       LinkerPatch::StringBssEntryPatch(kLiteralOffsetHigh, nullptr, kAnchorOffset, kStringIndex),
88       LinkerPatch::StringBssEntryPatch(kLiteralOffsetLow1, nullptr, kAnchorOffset, kStringIndex),
89       LinkerPatch::StringBssEntryPatch(kLiteralOffsetLow2, nullptr, kAnchorOffset, kStringIndex)
90   };
91   CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), bss_begin_ + string_entry_offset);
92 }
93 
TestStringReference(uint32_t string_offset)94 void MipsRelativePatcherTest::TestStringReference(uint32_t string_offset) {
95   constexpr uint32_t kStringIndex = 1u;
96   string_index_to_offset_map_.Put(kStringIndex, string_offset);
97   LinkerPatch patches[] = {
98       LinkerPatch::RelativeStringPatch(kLiteralOffsetHigh, nullptr, kAnchorOffset, kStringIndex),
99       LinkerPatch::RelativeStringPatch(kLiteralOffsetLow1, nullptr, kAnchorOffset, kStringIndex),
100       LinkerPatch::RelativeStringPatch(kLiteralOffsetLow2, nullptr, kAnchorOffset, kStringIndex)
101   };
102   CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), string_offset);
103 }
104 
TEST_F(MipsRelativePatcherTest,StringBssEntry)105 TEST_F(MipsRelativePatcherTest, StringBssEntry) {
106   TestStringBssEntry(/* bss_begin */ 0x12345678, /* string_entry_offset */ 0x1234);
107 }
108 
TEST_F(MipsRelativePatcherTest,StringReference)109 TEST_F(MipsRelativePatcherTest, StringReference) {
110   TestStringReference(/* string_offset*/ 0x87651234);
111 }
112 
113 }  // namespace linker
114 }  // namespace art
115