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