blob: dd4b13910a8c550290a48826fa1cab364dfc83bf [file] [log] [blame]
Stephen Hinesb53c8a52013-04-05 22:17:36 -07001/*
2 * Copyright 2011, 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 "StubLayout.h"
18
19#include "utils/flush_cpu_cache.h"
20#include "utils/raw_ostream.h"
21#include "utils/rsl_assert.h"
22
23#include <stdint.h>
24#include <stdlib.h>
Stephen Hinesb53c8a52013-04-05 22:17:36 -070025
26StubLayout::StubLayout() : table(NULL), count(0) {
27}
28
29void StubLayout::initStubTable(unsigned char *table_, size_t count_) {
30 table = table_;
31 count = count_;
32}
33
34void *StubLayout::allocateStub(void *addr) {
35 // Check if we have created this stub or not.
36 std::map<void *, void *>::iterator index_iter = stub_index.find(addr);
37
38 if (index_iter != stub_index.end()) {
39 return index_iter->second;
40 }
41
42 // We have to create a new stub
43 if (count == 0) {
44 // No free stub slot is available
45 return NULL;
46 }
47
48 // Initialize the stub
49 unsigned char *stub = table;
50 setStubAddress(stub, addr);
51 stub_index.insert(std::make_pair(addr, stub));
52
53 // Increase the free stub slot pointer
54 table += getUnitStubSize();
55 count--;
56
57 return stub;
58}
59
60size_t StubLayout::calcStubTableSize(size_t count) const {
61 return count * getUnitStubSize();
62}
63
Dave Allison30e2a4c2014-03-28 14:23:09 -070064size_t StubLayoutAARCH64::getUnitStubSize() const {
65 return 16;
66}
67
68void StubLayoutAARCH64::setStubAddress(void *stub_, void *addr) {
69 uint8_t *stub = (uint8_t *)stub_;
70
71 // First instruction:
72 // ldr x16,[pc,#8] LDR literal (pc relative)
73 // +--+---+-+--+-------------------+-----+
74 // |01|011|0|00| (#8 >> 2) = 10 |10000|
75 // +--+---+-+--+-------------------+-----+
76 // 0x58000050
77 // Little endian.
78 stub[0] = 0x50;
79 stub[1] = 0x00;
Dave Allison4e424682014-07-24 13:55:07 -070080 stub[2] = 0x00;
Dave Allison30e2a4c2014-03-28 14:23:09 -070081 stub[3] = 0x58;
82
83 // Next Instruction:
84 // br x16
85 // +-------+--+--+-----+------+-----+-----+
86 // |1101011|00|00|11111|000000|10000|00000|
87 // +-------+--+--+-----+------+-----+-----+
88 // 0xd61f0200
89
90 stub += 4;
91 stub[0] = 0x00;
92 stub[1] = 0x02;
93 stub[2] = 0x1f;
94 stub[3] = 0xd6;
95
96 // Now the absolute address (64 bits).
97 uint64_t *target = reinterpret_cast<uint64_t*>(stub + 4);
98 *target = reinterpret_cast<uint64_t>(addr);
99}
100
Stephen Hinesb53c8a52013-04-05 22:17:36 -0700101size_t StubLayoutARM::getUnitStubSize() const {
102 return 8;
103}
104
105void StubLayoutARM::setStubAddress(void *stub_, void *addr) {
106 uint8_t *stub = (uint8_t *)stub_;
107 stub[0] = 0x04; // ldr pc, [pc, #-4]
108 stub[1] = 0xf0; // ldr pc, [pc, #-4]
109 stub[2] = 0x1f; // ldr pc, [pc, #-4]
110 stub[3] = 0xe5; // ldr pc, [pc, #-4]
111
112 void **target = (void **)(stub + 4);
113 *target = addr;
114}
115
116size_t StubLayoutMIPS::getUnitStubSize() const {
117 return 16;
118}
119
120void StubLayoutMIPS::setStubAddress(void *stub_, void *addr) {
121 uint32_t addr32 = (uint32_t)(uintptr_t)addr;
122 uint16_t addr_hi16 = (addr32 >> 16) & 0xffff;
123 uint16_t addr_lo16 = addr32 & 0xffff;
124
125 uint32_t *stub = (uint32_t *)stub_;
126 stub[0] = 0x3c190000ul | addr_hi16; // lui
127 stub[1] = 0x37390000ul | addr_lo16; // ori
128 stub[2] = 0x03200008ul; // jr (jump register)
129 stub[3] = 0x00000000ul; // nop
130}
Xiaofei Wan72151aa2014-06-12 21:44:09 +0800131
132size_t StubLayoutX86::getUnitStubSize() const {
133 return 8;
134}
135
136void StubLayoutX86::setStubAddress(void *stub_, void *addr) {
137 uint8_t *stub = (uint8_t *)stub_;
138 stub[0] = 0xE9; // 32-bit pc-relative jump.
139 void **target = (void **)(stub + 1);
140 *target = addr;
141}
142
143size_t StubLayoutX86_64::getUnitStubSize() const {
144 return 16;
145}
146
147void StubLayoutX86_64::setStubAddress(void *stub_, void *addr) {
148 // x86 doesn't have proper register/mem to store the jump destination
149 // use below instructions to jump to the specified address
150
151 // jmp *0x0(%rip); jump to the location which is stored in next instruction
152 // addr; this is not a real instruction, just an address
153 uint8_t *stub = (uint8_t*)stub_;
154 stub[0] = 0xff;
155 stub[1] = 0x25;
156 stub[2] = 0x0;
157 stub[3] = 0x0;
158 stub[4] = 0x0;
159 stub[5] = 0x0;
160 uint64_t *target = reinterpret_cast<uint64_t*>(stub + 6);
161 *target = reinterpret_cast<uint64_t>(addr);
162}
163