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