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.h"
18 
19 #include "compiled_method.h"
20 #include "linker/linker_patch.h"
21 
22 namespace art {
23 namespace linker {
24 
PatchPcRelativeReference(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset,uint32_t target_offset)25 void X86RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code,
26                                                   const LinkerPatch& patch,
27                                                   uint32_t patch_offset,
28                                                   uint32_t target_offset) {
29   uint32_t anchor_literal_offset = patch.PcInsnOffset();
30   uint32_t literal_offset = patch.LiteralOffset();
31 
32   // Check that the anchor points to pop in a "call +0; pop <reg>" sequence.
33   DCHECK_GE(anchor_literal_offset, 5u);
34   DCHECK_LT(anchor_literal_offset, code->size());
35   DCHECK_EQ((*code)[anchor_literal_offset - 5u], 0xe8u);
36   DCHECK_EQ((*code)[anchor_literal_offset - 4u], 0x00u);
37   DCHECK_EQ((*code)[anchor_literal_offset - 3u], 0x00u);
38   DCHECK_EQ((*code)[anchor_literal_offset - 2u], 0x00u);
39   DCHECK_EQ((*code)[anchor_literal_offset - 1u], 0x00u);
40   DCHECK_EQ((*code)[anchor_literal_offset] & 0xf8u, 0x58u);
41 
42   // Check that the patched data contains kPlaceholder32BitOffset.
43   // Must match CodeGeneratorX86_64::kPlaceholder32BitOffset.
44   constexpr int kPlaceholder32BitOffset = 256;
45   DCHECK_LE(literal_offset, code->size());
46   DCHECK_EQ((*code)[literal_offset + 0u], static_cast<uint8_t>(kPlaceholder32BitOffset >> 0));
47   DCHECK_EQ((*code)[literal_offset + 1u], static_cast<uint8_t>(kPlaceholder32BitOffset >> 8));
48   DCHECK_EQ((*code)[literal_offset + 2u], static_cast<uint8_t>(kPlaceholder32BitOffset >> 16));
49   DCHECK_EQ((*code)[literal_offset + 3u], static_cast<uint8_t>(kPlaceholder32BitOffset >> 24));
50 
51   // Apply patch.
52   uint32_t anchor_offset = patch_offset - literal_offset + anchor_literal_offset;
53   uint32_t diff = target_offset - anchor_offset;
54   (*code)[literal_offset + 0u] = static_cast<uint8_t>(diff >> 0);
55   (*code)[literal_offset + 1u] = static_cast<uint8_t>(diff >> 8);
56   (*code)[literal_offset + 2u] = static_cast<uint8_t>(diff >> 16);
57   (*code)[literal_offset + 3u] = static_cast<uint8_t>(diff >> 24);
58 }
59 
PatchEntrypointCall(std::vector<uint8_t> * code ATTRIBUTE_UNUSED,const LinkerPatch & patch ATTRIBUTE_UNUSED,uint32_t patch_offset ATTRIBUTE_UNUSED)60 void X86RelativePatcher::PatchEntrypointCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
61                                              const LinkerPatch& patch ATTRIBUTE_UNUSED,
62                                              uint32_t patch_offset ATTRIBUTE_UNUSED) {
63   LOG(FATAL) << "UNIMPLEMENTED";
64 }
65 
PatchBakerReadBarrierBranch(std::vector<uint8_t> * code ATTRIBUTE_UNUSED,const LinkerPatch & patch ATTRIBUTE_UNUSED,uint32_t patch_offset ATTRIBUTE_UNUSED)66 void X86RelativePatcher::PatchBakerReadBarrierBranch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
67                                                      const LinkerPatch& patch ATTRIBUTE_UNUSED,
68                                                      uint32_t patch_offset ATTRIBUTE_UNUSED) {
69   LOG(FATAL) << "UNIMPLEMENTED";
70 }
71 
72 }  // namespace linker
73 }  // namespace art
74