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 #ifndef ART_COMPILER_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_
18 #define ART_COMPILER_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_
19 
20 #include "linker/arm/relative_patcher_arm_base.h"
21 #include "utils/array_ref.h"
22 
23 namespace art {
24 namespace linker {
25 
26 class Arm64RelativePatcher FINAL : public ArmBaseRelativePatcher {
27  public:
28   Arm64RelativePatcher(RelativePatcherTargetProvider* provider,
29                        const Arm64InstructionSetFeatures* features);
30 
31   uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method,
32                         MethodReference method_ref) OVERRIDE;
33   uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE;
34   uint32_t WriteThunks(OutputStream* out, uint32_t offset) OVERRIDE;
35   void PatchCall(std::vector<uint8_t>* code, uint32_t literal_offset,
36                  uint32_t patch_offset, uint32_t target_offset) OVERRIDE;
37   void PatchDexCacheReference(std::vector<uint8_t>* code, const LinkerPatch& patch,
38                               uint32_t patch_offset, uint32_t target_offset) OVERRIDE;
39 
40  private:
41   static std::vector<uint8_t> CompileThunkCode();
42   static uint32_t PatchAdrp(uint32_t adrp, uint32_t disp);
43 
44   static bool NeedsErratum843419Thunk(ArrayRef<const uint8_t> code, uint32_t literal_offset,
45                                       uint32_t patch_offset);
46   void SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value);
47   static uint32_t GetInsn(ArrayRef<const uint8_t> code, uint32_t offset);
48 
49   template <typename Alloc>
50   static uint32_t GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset);
51 
52   // Maximum positive and negative displacement measured from the patch location.
53   // (Signed 28 bit displacement with the last bit 0 has range [-2^27, 2^27-4] measured from
54   // the ARM64 PC pointing to the BL.)
55   static constexpr uint32_t kMaxPositiveDisplacement = (1u << 27) - 4u;
56   static constexpr uint32_t kMaxNegativeDisplacement = (1u << 27);
57 
58   // The ADRP thunk for erratum 843419 is 2 instructions, i.e. 8 bytes.
59   static constexpr uint32_t kAdrpThunkSize = 8u;
60 
61   const bool fix_cortex_a53_843419_;
62   // Map original patch_offset to thunk offset.
63   std::vector<std::pair<uint32_t, uint32_t>> adrp_thunk_locations_;
64   size_t reserved_adrp_thunks_;
65   size_t processed_adrp_thunks_;
66   std::vector<uint8_t> current_method_thunks_;
67 
68   DISALLOW_COPY_AND_ASSIGN(Arm64RelativePatcher);
69 };
70 
71 }  // namespace linker
72 }  // namespace art
73 
74 #endif  // ART_COMPILER_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_
75