xref: /aosp_15_r20/art/dex2oat/linker/x86/relative_patcher_x86_base.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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,MethodReference method_ref)24 uint32_t X86BaseRelativePatcher::ReserveSpace(
25     uint32_t offset,
26     [[maybe_unused]] const CompiledMethod* compiled_method,
27     [[maybe_unused]] MethodReference method_ref) {
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,uint32_t offset)35 uint32_t X86BaseRelativePatcher::WriteThunks([[maybe_unused]] OutputStream* out, uint32_t offset) {
36   return offset;  // No thunks added; no limit on relative call distance.
37 }
38 
GenerateThunkDebugInfo(uint32_t executable_offset)39 std::vector<debug::MethodDebugInfo> X86BaseRelativePatcher::GenerateThunkDebugInfo(
40     [[maybe_unused]] uint32_t executable_offset) {
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