StubLayout.cpp revision 30e2a4c8d848bc788cc5600e6b929b4f761f3f03
1/* 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> 25 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 64size_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; 80 stub[2] = 0x0f; 81 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 101size_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} 131