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_ARM_RELATIVE_PATCHER_ARM_BASE_H_
18 #define ART_COMPILER_LINKER_ARM_RELATIVE_PATCHER_ARM_BASE_H_
19 
20 #include <deque>
21 
22 #include "linker/relative_patcher.h"
23 #include "method_reference.h"
24 
25 namespace art {
26 namespace linker {
27 
28 class ArmBaseRelativePatcher : public RelativePatcher {
29  public:
30   uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method,
31                         MethodReference method_ref) OVERRIDE;
32   uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE;
33   uint32_t WriteThunks(OutputStream* out, uint32_t offset) OVERRIDE;
34 
35  protected:
36   ArmBaseRelativePatcher(RelativePatcherTargetProvider* provider,
37                          InstructionSet instruction_set, std::vector<uint8_t> thunk_code,
38                          uint32_t max_positive_displacement, uint32_t max_negative_displacement);
39 
40   uint32_t ReserveSpaceInternal(uint32_t offset, const CompiledMethod* compiled_method,
41                                 MethodReference method_ref, uint32_t max_extra_space);
42   uint32_t CalculateDisplacement(uint32_t patch_offset, uint32_t target_offset);
43 
44  private:
45   bool ReserveSpaceProcessPatches(uint32_t quick_code_offset, MethodReference method_ref,
46                                   uint32_t next_aligned_offset);
47 
48   RelativePatcherTargetProvider* const provider_;
49   const InstructionSet instruction_set_;
50   const std::vector<uint8_t> thunk_code_;
51   const uint32_t max_positive_displacement_;
52   const uint32_t max_negative_displacement_;
53   std::vector<uint32_t> thunk_locations_;
54   size_t current_thunk_to_write_;
55 
56   // ReserveSpace() tracks unprocessed patches.
57   typedef std::pair<MethodReference, uint32_t> UnprocessedPatch;
58   std::deque<UnprocessedPatch> unprocessed_patches_;
59 
60   friend class Arm64RelativePatcherTest;
61   friend class Thumb2RelativePatcherTest;
62 
63   DISALLOW_COPY_AND_ASSIGN(ArmBaseRelativePatcher);
64 };
65 
66 }  // namespace linker
67 }  // namespace art
68 
69 #endif  // ART_COMPILER_LINKER_ARM_RELATIVE_PATCHER_ARM_BASE_H_
70