Home | History | Annotate | Download | only in mips
      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/mips/relative_patcher_mips.h"
     18 
     19 #include "linker/relative_patcher_test.h"
     20 
     21 namespace art {
     22 namespace linker {
     23 
     24 class MipsRelativePatcherTest : public RelativePatcherTest {
     25  public:
     26   MipsRelativePatcherTest() : RelativePatcherTest(InstructionSet::kMips, "mips32r2") {}
     27 
     28  protected:
     29   static const uint8_t kUnpatchedPcRelativeRawCode[];
     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 MipsRelativePatcherTest::kUnpatchedPcRelativeRawCode[] = {
     48     0x00, 0x00, 0x10, 0x04,  // nal
     49     0x34, 0x12, 0x12, 0x3C,  // lui   s2, high(diff); placeholder = 0x1234
     50     0x21, 0x90, 0x5F, 0x02,  // addu  s2, s2, ra
     51     0x78, 0x56, 0x52, 0x26,  // addiu s2, s2, low(diff); placeholder = 0x5678
     52     0x78, 0x56, 0x52, 0x8E,  // lw    s2, (low(diff))(s2) ; placeholder = 0x5678
     53 };
     54 const uint32_t MipsRelativePatcherTest::kLiteralOffsetHigh = 4;  // At lui.
     55 const uint32_t MipsRelativePatcherTest::kLiteralOffsetLow1 = 12;  // At addiu.
     56 const uint32_t MipsRelativePatcherTest::kLiteralOffsetLow2 = 16;  // At lw.
     57 const uint32_t MipsRelativePatcherTest::kAnchorOffset = 8;  // At addu (where PC+0 points).
     58 const ArrayRef<const uint8_t> MipsRelativePatcherTest::kUnpatchedPcRelativeCode(
     59     kUnpatchedPcRelativeRawCode);
     60 
     61 void MipsRelativePatcherTest::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 addiu/lw.
     71 
     72   const uint8_t expected_code[] = {
     73       0x00, 0x00, 0x10, 0x04,
     74       static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24), 0x12, 0x3C,
     75       0x21, 0x90, 0x5F, 0x02,
     76       static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x26,
     77       static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x8E,
     78   };
     79   EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
     80 }
     81 
     82 void MipsRelativePatcherTest::TestStringBssEntry(uint32_t bss_begin,
     83                                                  uint32_t string_entry_offset) {
     84   constexpr uint32_t kStringIndex = 1u;
     85   string_index_to_offset_map_.Put(kStringIndex, string_entry_offset);
     86   bss_begin_ = bss_begin;
     87   LinkerPatch patches[] = {
     88       LinkerPatch::StringBssEntryPatch(kLiteralOffsetHigh, nullptr, kAnchorOffset, kStringIndex),
     89       LinkerPatch::StringBssEntryPatch(kLiteralOffsetLow1, nullptr, kAnchorOffset, kStringIndex),
     90       LinkerPatch::StringBssEntryPatch(kLiteralOffsetLow2, nullptr, kAnchorOffset, kStringIndex)
     91   };
     92   CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), bss_begin_ + string_entry_offset);
     93 }
     94 
     95 void MipsRelativePatcherTest::TestStringReference(uint32_t string_offset) {
     96   constexpr uint32_t kStringIndex = 1u;
     97   string_index_to_offset_map_.Put(kStringIndex, string_offset);
     98   LinkerPatch patches[] = {
     99       LinkerPatch::RelativeStringPatch(kLiteralOffsetHigh, nullptr, kAnchorOffset, kStringIndex),
    100       LinkerPatch::RelativeStringPatch(kLiteralOffsetLow1, nullptr, kAnchorOffset, kStringIndex),
    101       LinkerPatch::RelativeStringPatch(kLiteralOffsetLow2, nullptr, kAnchorOffset, kStringIndex)
    102   };
    103   CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), string_offset);
    104 }
    105 
    106 TEST_F(MipsRelativePatcherTest, StringBssEntry) {
    107   TestStringBssEntry(/* bss_begin */ 0x12345678, /* string_entry_offset */ 0x1234);
    108 }
    109 
    110 TEST_F(MipsRelativePatcherTest, StringReference) {
    111   TestStringReference(/* string_offset*/ 0x87651234);
    112 }
    113 
    114 }  // namespace linker
    115 }  // namespace art
    116