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_DEX2OAT_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_
18 #define ART_DEX2OAT_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_
19 
20 #include "base/array_ref.h"
21 #include "linker/arm/relative_patcher_arm_base.h"
22 
23 namespace art {
24 
25 namespace arm64 {
26 class Arm64Assembler;
27 }  // namespace arm64
28 
29 namespace linker {
30 
31 class Arm64RelativePatcher final : public ArmBaseRelativePatcher {
32  public:
33   Arm64RelativePatcher(RelativePatcherThunkProvider* thunk_provider,
34                        RelativePatcherTargetProvider* target_provider,
35                        const Arm64InstructionSetFeatures* features);
36 
37   uint32_t ReserveSpace(uint32_t offset,
38                         const CompiledMethod* compiled_method,
39                         MethodReference method_ref) override;
40   uint32_t ReserveSpaceEnd(uint32_t offset) override;
41   uint32_t WriteThunks(OutputStream* out, uint32_t offset) override;
42   void PatchCall(std::vector<uint8_t>* code,
43                  uint32_t literal_offset,
44                  uint32_t patch_offset,
45                  uint32_t target_offset) override;
46   void PatchPcRelativeReference(std::vector<uint8_t>* code,
47                                 const LinkerPatch& patch,
48                                 uint32_t patch_offset,
49                                 uint32_t target_offset) override;
50   void PatchEntrypointCall(std::vector<uint8_t>* code,
51                            const LinkerPatch& patch,
52                            uint32_t patch_offset) override;
53   void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code,
54                                    const LinkerPatch& patch,
55                                    uint32_t patch_offset) override;
56 
57  protected:
58   uint32_t MaxPositiveDisplacement(const ThunkKey& key) override;
59   uint32_t MaxNegativeDisplacement(const ThunkKey& key) override;
60 
61  private:
62   static uint32_t PatchAdrp(uint32_t adrp, uint32_t disp);
63   static void PatchBl(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t displacement);
64 
65   static bool NeedsErratum843419Thunk(ArrayRef<const uint8_t> code, uint32_t literal_offset,
66                                       uint32_t patch_offset);
67   static void SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value);
68   static uint32_t GetInsn(ArrayRef<const uint8_t> code, uint32_t offset);
69 
70   template <typename Alloc>
71   static uint32_t GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset);
72 
73   const bool fix_cortex_a53_843419_;
74   // Map original patch_offset to thunk offset.
75   std::vector<std::pair<uint32_t, uint32_t>> adrp_thunk_locations_;
76   size_t reserved_adrp_thunks_;
77   size_t processed_adrp_thunks_;
78   std::vector<uint8_t> current_method_thunks_;
79 
80   friend class Arm64RelativePatcherTest;
81 
82   DISALLOW_COPY_AND_ASSIGN(Arm64RelativePatcher);
83 };
84 
85 }  // namespace linker
86 }  // namespace art
87 
88 #endif  // ART_DEX2OAT_LINKER_ARM64_RELATIVE_PATCHER_ARM64_H_
89