Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 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 kLiteralOffset; |
| 31 | static const uint32_t kAnchorOffset; |
| 32 | static const ArrayRef<const uint8_t> kUnpatchedPcRelativeCode; |
| 33 | static const ArrayRef<const uint8_t> kUnpatchedPcRelativeCallCode; |
| 34 | |
| 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); |
Vladimir Marko | 5f07820 | 2017-05-18 13:32:53 +0100 | [diff] [blame] | 42 | void TestStringBssEntry(uint32_t bss_begin, uint32_t string_entry_offset); |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 43 | void TestStringReference(uint32_t string_offset); |
| 44 | }; |
| 45 | |
| 46 | const uint8_t Mips64RelativePatcherTest::kUnpatchedPcRelativeRawCode[] = { |
| 47 | 0x34, 0x12, 0x5E, 0xEE, // auipc s2, high(diff); placeholder = 0x1234 |
Alexey Frunze | f63f569 | 2016-12-13 17:43:11 -0800 | [diff] [blame] | 48 | 0x78, 0x56, 0x52, 0x66, // daddiu s2, s2, low(diff); placeholder = 0x5678 |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 49 | }; |
| 50 | const uint8_t Mips64RelativePatcherTest::kUnpatchedPcRelativeCallRawCode[] = { |
| 51 | 0x34, 0x12, 0x3E, 0xEC, // auipc at, high(diff); placeholder = 0x1234 |
| 52 | 0x78, 0x56, 0x01, 0xF8, // jialc at, low(diff); placeholder = 0x5678 |
| 53 | }; |
| 54 | const uint32_t Mips64RelativePatcherTest::kLiteralOffset = 0; // At auipc (where patching starts). |
| 55 | const uint32_t Mips64RelativePatcherTest::kAnchorOffset = 0; // At auipc (where PC+0 points). |
| 56 | const ArrayRef<const uint8_t> Mips64RelativePatcherTest::kUnpatchedPcRelativeCode( |
| 57 | kUnpatchedPcRelativeRawCode); |
| 58 | const ArrayRef<const uint8_t> Mips64RelativePatcherTest::kUnpatchedPcRelativeCallCode( |
| 59 | kUnpatchedPcRelativeCallRawCode); |
| 60 | |
| 61 | void Mips64RelativePatcherTest::CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches, |
| 62 | uint32_t target_offset) { |
| 63 | AddCompiledMethod(MethodRef(1u), kUnpatchedPcRelativeCode, ArrayRef<const LinkerPatch>(patches)); |
| 64 | Link(); |
| 65 | |
| 66 | auto result = method_offset_map_.FindMethodOffset(MethodRef(1u)); |
| 67 | ASSERT_TRUE(result.first); |
| 68 | |
| 69 | uint32_t diff = target_offset - (result.second + kAnchorOffset); |
| 70 | diff += (diff & 0x8000) << 1; // Account for sign extension in instruction following auipc. |
| 71 | |
| 72 | const uint8_t expected_code[] = { |
| 73 | static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24), 0x5E, 0xEE, |
Alexey Frunze | f63f569 | 2016-12-13 17:43:11 -0800 | [diff] [blame] | 74 | static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x66, |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 75 | }; |
| 76 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code))); |
| 77 | } |
| 78 | |
Vladimir Marko | 5f07820 | 2017-05-18 13:32:53 +0100 | [diff] [blame] | 79 | void Mips64RelativePatcherTest::TestStringBssEntry(uint32_t bss_begin, |
| 80 | uint32_t string_entry_offset) { |
| 81 | constexpr uint32_t kStringIndex = 1u; |
| 82 | string_index_to_offset_map_.Put(kStringIndex, string_entry_offset); |
| 83 | bss_begin_ = bss_begin; |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 84 | LinkerPatch patches[] = { |
Vladimir Marko | 5f07820 | 2017-05-18 13:32:53 +0100 | [diff] [blame] | 85 | LinkerPatch::StringBssEntryPatch(kLiteralOffset, nullptr, kAnchorOffset, kStringIndex) |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 86 | }; |
Vladimir Marko | 5f07820 | 2017-05-18 13:32:53 +0100 | [diff] [blame] | 87 | CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), bss_begin_ + string_entry_offset); |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Vladimir Marko | 5f07820 | 2017-05-18 13:32:53 +0100 | [diff] [blame] | 90 | TEST_F(Mips64RelativePatcherTest, StringBssEntry) { |
| 91 | TestStringBssEntry(/* bss_begin */ 0x12345678, /* string_entry_offset */ 0x1234); |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST_F(Mips64RelativePatcherTest, CallOther) { |
| 95 | LinkerPatch method1_patches[] = { |
| 96 | LinkerPatch::RelativeCodePatch(kLiteralOffset, nullptr, 2u), |
| 97 | }; |
| 98 | AddCompiledMethod(MethodRef(1u), |
| 99 | kUnpatchedPcRelativeCallCode, |
| 100 | ArrayRef<const LinkerPatch>(method1_patches)); |
| 101 | LinkerPatch method2_patches[] = { |
| 102 | LinkerPatch::RelativeCodePatch(kLiteralOffset, nullptr, 1u), |
| 103 | }; |
| 104 | AddCompiledMethod(MethodRef(2u), |
| 105 | kUnpatchedPcRelativeCallCode, |
| 106 | ArrayRef<const LinkerPatch>(method2_patches)); |
| 107 | Link(); |
| 108 | |
| 109 | uint32_t method1_offset = GetMethodOffset(1u); |
| 110 | uint32_t method2_offset = GetMethodOffset(2u); |
| 111 | uint32_t diff_after = method2_offset - (method1_offset + kAnchorOffset /* PC adjustment */); |
| 112 | diff_after += (diff_after & 0x8000) << 1; // Account for sign extension in jialc. |
| 113 | static const uint8_t method1_expected_code[] = { |
| 114 | static_cast<uint8_t>(diff_after >> 16), static_cast<uint8_t>(diff_after >> 24), 0x3E, 0xEC, |
| 115 | static_cast<uint8_t>(diff_after), static_cast<uint8_t>(diff_after >> 8), 0x01, 0xF8, |
| 116 | }; |
| 117 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(method1_expected_code))); |
| 118 | uint32_t diff_before = method1_offset - (method2_offset + kAnchorOffset /* PC adjustment */); |
| 119 | diff_before += (diff_before & 0x8000) << 1; // Account for sign extension in jialc. |
| 120 | static const uint8_t method2_expected_code[] = { |
| 121 | static_cast<uint8_t>(diff_before >> 16), static_cast<uint8_t>(diff_before >> 24), 0x3E, 0xEC, |
| 122 | static_cast<uint8_t>(diff_before), static_cast<uint8_t>(diff_before >> 8), 0x01, 0xF8, |
| 123 | }; |
| 124 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(2u), ArrayRef<const uint8_t>(method2_expected_code))); |
| 125 | } |
| 126 | |
| 127 | } // namespace linker |
| 128 | } // namespace art |