Home | History | Annotate | Download | only in arm
      1 /*
      2  * Copyright (C) 2015 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/arm/relative_patcher_thumb2.h"
     18 
     19 #include "art_method.h"
     20 #include "compiled_method.h"
     21 #include "utils/arm/assembler_thumb2.h"
     22 
     23 namespace art {
     24 namespace linker {
     25 
     26 // PC displacement from patch location; Thumb2 PC is always at instruction address + 4.
     27 static constexpr int32_t kPcDisplacement = 4;
     28 
     29 // Maximum positive and negative displacement for method call measured from the patch location.
     30 // (Signed 25 bit displacement with the last bit 0 has range [-2^24, 2^24-2] measured from
     31 // the Thumb2 PC pointing right after the BL, i.e. 4 bytes later than the patch location.)
     32 constexpr uint32_t kMaxMethodCallPositiveDisplacement = (1u << 24) - 2 + kPcDisplacement;
     33 constexpr uint32_t kMaxMethodCallNegativeDisplacement = (1u << 24) - kPcDisplacement;
     34 
     35 Thumb2RelativePatcher::Thumb2RelativePatcher(RelativePatcherTargetProvider* provider)
     36     : ArmBaseRelativePatcher(provider, kThumb2) {
     37 }
     38 
     39 void Thumb2RelativePatcher::PatchCall(std::vector<uint8_t>* code,
     40                                       uint32_t literal_offset,
     41                                       uint32_t patch_offset,
     42                                       uint32_t target_offset) {
     43   DCHECK_LE(literal_offset + 4u, code->size());
     44   DCHECK_EQ(literal_offset & 1u, 0u);
     45   DCHECK_EQ(patch_offset & 1u, 0u);
     46   DCHECK_EQ(target_offset & 1u, 1u);  // Thumb2 mode bit.
     47   uint32_t displacement = CalculateMethodCallDisplacement(patch_offset, target_offset & ~1u);
     48   displacement -= kPcDisplacement;  // The base PC is at the end of the 4-byte patch.
     49   DCHECK_EQ(displacement & 1u, 0u);
     50   DCHECK((displacement >> 24) == 0u || (displacement >> 24) == 255u);  // 25-bit signed.
     51   uint32_t signbit = (displacement >> 31) & 0x1;
     52   uint32_t i1 = (displacement >> 23) & 0x1;
     53   uint32_t i2 = (displacement >> 22) & 0x1;
     54   uint32_t imm10 = (displacement >> 12) & 0x03ff;
     55   uint32_t imm11 = (displacement >> 1) & 0x07ff;
     56   uint32_t j1 = i1 ^ (signbit ^ 1);
     57   uint32_t j2 = i2 ^ (signbit ^ 1);
     58   uint32_t value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) | imm11;
     59   value |= 0xf000d000;  // BL
     60 
     61   // Check that we're just overwriting an existing BL.
     62   DCHECK_EQ(GetInsn32(code, literal_offset) & 0xf800d000, 0xf000d000);
     63   // Write the new BL.
     64   SetInsn32(code, literal_offset, value);
     65 }
     66 
     67 void Thumb2RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code,
     68                                                      const LinkerPatch& patch,
     69                                                      uint32_t patch_offset,
     70                                                      uint32_t target_offset) {
     71   uint32_t literal_offset = patch.LiteralOffset();
     72   uint32_t pc_literal_offset = patch.PcInsnOffset();
     73   uint32_t pc_base = patch_offset + (pc_literal_offset - literal_offset) + 4u /* PC adjustment */;
     74   uint32_t diff = target_offset - pc_base;
     75 
     76   uint32_t insn = GetInsn32(code, literal_offset);
     77   DCHECK_EQ(insn & 0xff7ff0ffu, 0xf2400000u);  // MOVW/MOVT, unpatched (imm16 == 0).
     78   uint32_t diff16 = ((insn & 0x00800000u) != 0u) ? (diff >> 16) : (diff & 0xffffu);
     79   uint32_t imm4 = (diff16 >> 12) & 0xfu;
     80   uint32_t imm = (diff16 >> 11) & 0x1u;
     81   uint32_t imm3 = (diff16 >> 8) & 0x7u;
     82   uint32_t imm8 = diff16 & 0xffu;
     83   insn = (insn & 0xfbf08f00u) | (imm << 26) | (imm4 << 16) | (imm3 << 12) | imm8;
     84   SetInsn32(code, literal_offset, insn);
     85 }
     86 
     87 void Thumb2RelativePatcher::PatchBakerReadBarrierBranch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
     88                                                         const LinkerPatch& patch ATTRIBUTE_UNUSED,
     89                                                         uint32_t patch_offset ATTRIBUTE_UNUSED) {
     90   LOG(FATAL) << "UNIMPLEMENTED";
     91 }
     92 
     93 ArmBaseRelativePatcher::ThunkKey Thumb2RelativePatcher::GetBakerReadBarrierKey(
     94     const LinkerPatch& patch ATTRIBUTE_UNUSED) {
     95   LOG(FATAL) << "UNIMPLEMENTED";
     96   UNREACHABLE();
     97 }
     98 
     99 std::vector<uint8_t> Thumb2RelativePatcher::CompileThunk(const ThunkKey& key) {
    100   DCHECK(key.GetType() == ThunkType::kMethodCall);
    101   // The thunk just uses the entry point in the ArtMethod. This works even for calls
    102   // to the generic JNI and interpreter trampolines.
    103   ArenaPool pool;
    104   ArenaAllocator arena(&pool);
    105   arm::Thumb2Assembler assembler(&arena);
    106   assembler.LoadFromOffset(
    107       arm::kLoadWord, arm::PC, arm::R0,
    108       ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
    109   assembler.bkpt(0);
    110   assembler.FinalizeCode();
    111   std::vector<uint8_t> thunk_code(assembler.CodeSize());
    112   MemoryRegion code(thunk_code.data(), thunk_code.size());
    113   assembler.FinalizeInstructions(code);
    114   return thunk_code;
    115 }
    116 
    117 uint32_t Thumb2RelativePatcher::MaxPositiveDisplacement(ThunkType type) {
    118   DCHECK(type == ThunkType::kMethodCall);
    119   return kMaxMethodCallPositiveDisplacement;
    120 }
    121 
    122 uint32_t Thumb2RelativePatcher::MaxNegativeDisplacement(ThunkType type) {
    123   DCHECK(type == ThunkType::kMethodCall);
    124   return kMaxMethodCallNegativeDisplacement;
    125 }
    126 
    127 void Thumb2RelativePatcher::SetInsn32(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
    128   DCHECK_LE(offset + 4u, code->size());
    129   DCHECK_EQ(offset & 1u, 0u);
    130   uint8_t* addr = &(*code)[offset];
    131   addr[0] = (value >> 16) & 0xff;
    132   addr[1] = (value >> 24) & 0xff;
    133   addr[2] = (value >> 0) & 0xff;
    134   addr[3] = (value >> 8) & 0xff;
    135 }
    136 
    137 uint32_t Thumb2RelativePatcher::GetInsn32(ArrayRef<const uint8_t> code, uint32_t offset) {
    138   DCHECK_LE(offset + 4u, code.size());
    139   DCHECK_EQ(offset & 1u, 0u);
    140   const uint8_t* addr = &code[offset];
    141   return
    142       (static_cast<uint32_t>(addr[0]) << 16) +
    143       (static_cast<uint32_t>(addr[1]) << 24) +
    144       (static_cast<uint32_t>(addr[2]) << 0)+
    145       (static_cast<uint32_t>(addr[3]) << 8);
    146 }
    147 
    148 template <typename Vector>
    149 uint32_t Thumb2RelativePatcher::GetInsn32(Vector* code, uint32_t offset) {
    150   static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
    151   return GetInsn32(ArrayRef<const uint8_t>(*code), offset);
    152 }
    153 
    154 }  // namespace linker
    155 }  // namespace art
    156