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
21 namespace art {
22 namespace linker {
23
PatchDexCacheReference(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset,uint32_t target_offset)24 void X86RelativePatcher::PatchDexCacheReference(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 kDummy32BitOffset.
42 constexpr int kDummy32BitOffset = 256; // Must match X86Mir2Lir::kDummy32BitOffset.
43 DCHECK_LE(literal_offset, code->size());
44 DCHECK_EQ((*code)[literal_offset + 0u], static_cast<uint8_t>(kDummy32BitOffset >> 0));
45 DCHECK_EQ((*code)[literal_offset + 1u], static_cast<uint8_t>(kDummy32BitOffset >> 8));
46 DCHECK_EQ((*code)[literal_offset + 2u], static_cast<uint8_t>(kDummy32BitOffset >> 16));
47 DCHECK_EQ((*code)[literal_offset + 3u], static_cast<uint8_t>(kDummy32BitOffset >> 24));
48
49 // Apply patch.
50 uint32_t anchor_offset = patch_offset - literal_offset + anchor_literal_offset;
51 uint32_t diff = target_offset - anchor_offset;
52 (*code)[literal_offset + 0u] = static_cast<uint8_t>(diff >> 0);
53 (*code)[literal_offset + 1u] = static_cast<uint8_t>(diff >> 8);
54 (*code)[literal_offset + 2u] = static_cast<uint8_t>(diff >> 16);
55 (*code)[literal_offset + 3u] = static_cast<uint8_t>(diff >> 24);
56 }
57
58 } // namespace linker
59 } // namespace art
60