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