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