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 #include "linker/x86/relative_patcher_x86_base.h"
18 
19 #include "debug/method_debug_info.h"
20 
21 namespace art {
22 namespace linker {
23 
ReserveSpace(uint32_t offset,const CompiledMethod * compiled_method ATTRIBUTE_UNUSED,MethodReference method_ref ATTRIBUTE_UNUSED)24 uint32_t X86BaseRelativePatcher::ReserveSpace(
25     uint32_t offset,
26     const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
27     MethodReference method_ref ATTRIBUTE_UNUSED) {
28   return offset;  // No space reserved; no limit on relative call distance.
29 }
30 
ReserveSpaceEnd(uint32_t offset)31 uint32_t X86BaseRelativePatcher::ReserveSpaceEnd(uint32_t offset) {
32   return offset;  // No space reserved; no limit on relative call distance.
33 }
34 
WriteThunks(OutputStream * out ATTRIBUTE_UNUSED,uint32_t offset)35 uint32_t X86BaseRelativePatcher::WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) {
36   return offset;  // No thunks added; no limit on relative call distance.
37 }
38 
GenerateThunkDebugInfo(uint32_t executable_offset ATTRIBUTE_UNUSED)39 std::vector<debug::MethodDebugInfo> X86BaseRelativePatcher::GenerateThunkDebugInfo(
40     uint32_t executable_offset ATTRIBUTE_UNUSED) {
41   return std::vector<debug::MethodDebugInfo>();  // No thunks added.
42 }
43 
PatchCall(std::vector<uint8_t> * code,uint32_t literal_offset,uint32_t patch_offset,uint32_t target_offset)44 void X86BaseRelativePatcher::PatchCall(std::vector<uint8_t>* code,
45                                        uint32_t literal_offset,
46                                        uint32_t patch_offset,
47                                        uint32_t target_offset) {
48   DCHECK_LE(literal_offset + 4u, code->size());
49   // Unsigned arithmetic with its well-defined overflow behavior is just fine here.
50   uint32_t displacement = target_offset - patch_offset;
51   displacement -= kPcDisplacement;  // The base PC is at the end of the 4-byte patch.
52 
53   using unaligned_int32_t __attribute__((__aligned__(1))) = int32_t;
54   reinterpret_cast<unaligned_int32_t*>(&(*code)[literal_offset])[0] = displacement;
55 }
56 
57 }  // namespace linker
58 }  // namespace art
59