blob: a02f5005e8c643abf89e54213404dca79d1409cc [file] [log] [blame]
Alexey Frunze19f6c692016-11-30 19:19:55 -08001/*
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
Alexey Frunze19f6c692016-11-30 19:19:55 -080017#include "linker/mips64/relative_patcher_mips64.h"
18
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070019#include "linker/relative_patcher_test.h"
20
Alexey Frunze19f6c692016-11-30 19:19:55 -080021namespace art {
22namespace linker {
23
24class Mips64RelativePatcherTest : public RelativePatcherTest {
25 public:
Vladimir Marko33bff252017-11-01 14:35:42 +000026 Mips64RelativePatcherTest() : RelativePatcherTest(InstructionSet::kMips64, "default") {}
Alexey Frunze19f6c692016-11-30 19:19:55 -080027
28 protected:
29 static const uint8_t kUnpatchedPcRelativeRawCode[];
30 static const uint8_t kUnpatchedPcRelativeCallRawCode[];
Alexey Frunze5fa5c042017-06-01 21:07:52 -070031 static const uint32_t kLiteralOffsetHigh;
32 static const uint32_t kLiteralOffsetLow1;
33 static const uint32_t kLiteralOffsetLow2;
Alexey Frunze19f6c692016-11-30 19:19:55 -080034 static const uint32_t kAnchorOffset;
35 static const ArrayRef<const uint8_t> kUnpatchedPcRelativeCode;
Alexey Frunze19f6c692016-11-30 19:19:55 -080036
37 uint32_t GetMethodOffset(uint32_t method_idx) {
38 auto result = method_offset_map_.FindMethodOffset(MethodRef(method_idx));
39 CHECK(result.first);
40 return result.second;
41 }
42
43 void CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches, uint32_t target_offset);
Vladimir Marko5f078202017-05-18 13:32:53 +010044 void TestStringBssEntry(uint32_t bss_begin, uint32_t string_entry_offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -080045 void TestStringReference(uint32_t string_offset);
46};
47
48const uint8_t Mips64RelativePatcherTest::kUnpatchedPcRelativeRawCode[] = {
Alexey Frunze5fa5c042017-06-01 21:07:52 -070049 0x34, 0x12, 0x5E, 0xEE, // auipc s2, high(diff); placeholder = 0x1234
Alexey Frunzef63f5692016-12-13 17:43:11 -080050 0x78, 0x56, 0x52, 0x66, // daddiu s2, s2, low(diff); placeholder = 0x5678
Alexey Frunze5fa5c042017-06-01 21:07:52 -070051 0x78, 0x56, 0x52, 0x9E, // lwu s2, (low(diff))(s2) ; placeholder = 0x5678
Alexey Frunze19f6c692016-11-30 19:19:55 -080052};
Alexey Frunze5fa5c042017-06-01 21:07:52 -070053const uint32_t Mips64RelativePatcherTest::kLiteralOffsetHigh = 0; // At auipc.
54const uint32_t Mips64RelativePatcherTest::kLiteralOffsetLow1 = 4; // At daddiu.
55const uint32_t Mips64RelativePatcherTest::kLiteralOffsetLow2 = 8; // At lwu.
Alexey Frunze19f6c692016-11-30 19:19:55 -080056const uint32_t Mips64RelativePatcherTest::kAnchorOffset = 0; // At auipc (where PC+0 points).
57const ArrayRef<const uint8_t> Mips64RelativePatcherTest::kUnpatchedPcRelativeCode(
58 kUnpatchedPcRelativeRawCode);
Alexey Frunze19f6c692016-11-30 19:19:55 -080059
60void Mips64RelativePatcherTest::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);
Alexey Frunze5fa5c042017-06-01 21:07:52 -070069 diff += (diff & 0x8000) << 1; // Account for sign extension in daddiu/lwu.
Alexey Frunze19f6c692016-11-30 19:19:55 -080070
71 const uint8_t expected_code[] = {
72 static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24), 0x5E, 0xEE,
Alexey Frunzef63f5692016-12-13 17:43:11 -080073 static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x66,
Alexey Frunze5fa5c042017-06-01 21:07:52 -070074 static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x9E,
Alexey Frunze19f6c692016-11-30 19:19:55 -080075 };
76 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
77}
78
Vladimir Marko5f078202017-05-18 13:32:53 +010079void 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 Frunze19f6c692016-11-30 19:19:55 -080084 LinkerPatch patches[] = {
Alexey Frunze5fa5c042017-06-01 21:07:52 -070085 LinkerPatch::StringBssEntryPatch(kLiteralOffsetHigh, nullptr, kAnchorOffset, kStringIndex),
86 LinkerPatch::StringBssEntryPatch(kLiteralOffsetLow1, nullptr, kAnchorOffset, kStringIndex),
87 LinkerPatch::StringBssEntryPatch(kLiteralOffsetLow2, nullptr, kAnchorOffset, kStringIndex)
Alexey Frunze19f6c692016-11-30 19:19:55 -080088 };
Vladimir Marko5f078202017-05-18 13:32:53 +010089 CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), bss_begin_ + string_entry_offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -080090}
91
Vladimir Marko5f078202017-05-18 13:32:53 +010092TEST_F(Mips64RelativePatcherTest, StringBssEntry) {
93 TestStringBssEntry(/* bss_begin */ 0x12345678, /* string_entry_offset */ 0x1234);
Alexey Frunze19f6c692016-11-30 19:19:55 -080094}
95
Alexey Frunze19f6c692016-11-30 19:19:55 -080096} // namespace linker
97} // namespace art