1 /*
2  * Copyright (C) 2016 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 "multi_oat_relative_patcher.h"
18 
19 #include <android-base/logging.h>
20 
21 #include "base/bit_utils.h"
22 #include "base/globals.h"
23 #include "driver/compiled_method_storage.h"
24 
25 namespace art {
26 namespace linker {
27 
GetThunkCode(const LinkerPatch & patch,ArrayRef<const uint8_t> * code,std::string * debug_name)28 void MultiOatRelativePatcher::ThunkProvider::GetThunkCode(const LinkerPatch& patch,
29                                                           /*out*/ ArrayRef<const uint8_t>* code,
30                                                           /*out*/ std::string* debug_name) {
31   *code = storage_->GetThunkCode(patch, debug_name);
32   DCHECK(!code->empty());
33 }
34 
35 
MultiOatRelativePatcher(InstructionSet instruction_set,const InstructionSetFeatures * features,CompiledMethodStorage * storage)36 MultiOatRelativePatcher::MultiOatRelativePatcher(InstructionSet instruction_set,
37                                                  const InstructionSetFeatures* features,
38                                                  CompiledMethodStorage* storage)
39     : thunk_provider_(storage),
40       method_offset_map_(),
41       relative_patcher_(RelativePatcher::Create(instruction_set,
42                                                 features,
43                                                 &thunk_provider_,
44                                                 &method_offset_map_)),
45       adjustment_(0u),
46       instruction_set_(instruction_set),
47       start_size_code_alignment_(0u),
48       start_size_relative_call_thunks_(0u),
49       start_size_misc_thunks_(0u) {
50 }
51 
StartOatFile(uint32_t adjustment)52 void MultiOatRelativePatcher::StartOatFile(uint32_t adjustment) {
53   DCHECK_ALIGNED(adjustment, kPageSize);
54   adjustment_ = adjustment;
55 
56   start_size_code_alignment_ = relative_patcher_->CodeAlignmentSize();
57   start_size_relative_call_thunks_ = relative_patcher_->RelativeCallThunksSize();
58   start_size_misc_thunks_ = relative_patcher_->MiscThunksSize();
59 }
60 
CodeAlignmentSize() const61 uint32_t MultiOatRelativePatcher::CodeAlignmentSize() const {
62   DCHECK_GE(relative_patcher_->CodeAlignmentSize(), start_size_code_alignment_);
63   return relative_patcher_->CodeAlignmentSize() - start_size_code_alignment_;
64 }
65 
RelativeCallThunksSize() const66 uint32_t MultiOatRelativePatcher::RelativeCallThunksSize() const {
67   DCHECK_GE(relative_patcher_->RelativeCallThunksSize(), start_size_relative_call_thunks_);
68   return relative_patcher_->RelativeCallThunksSize() - start_size_relative_call_thunks_;
69 }
70 
MiscThunksSize() const71 uint32_t MultiOatRelativePatcher::MiscThunksSize() const {
72   DCHECK_GE(relative_patcher_->MiscThunksSize(), start_size_misc_thunks_);
73   return relative_patcher_->MiscThunksSize() - start_size_misc_thunks_;
74 }
75 
FindMethodOffset(MethodReference ref)76 std::pair<bool, uint32_t> MultiOatRelativePatcher::MethodOffsetMap::FindMethodOffset(
77     MethodReference ref) {
78   auto it = map.find(ref);
79   if (it == map.end()) {
80     return std::pair<bool, uint32_t>(false, 0u);
81   } else {
82     return std::pair<bool, uint32_t>(true, it->second);
83   }
84 }
85 }  // namespace linker
86 }  // namespace art
87