summaryrefslogtreecommitdiff
path: root/lib/StubLayout.cpp
blob: 733e66568fecc00eafc0846e86f76015ccb51984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright 2011, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "StubLayout.h"

#include "utils/flush_cpu_cache.h"
#include "utils/raw_ostream.h"

#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>

#if defined(__arm__)
#define STUB_SIZE 8
#elif defined(__mips__)
#define STUB_SIZE 16
#endif

StubLayout::StubLayout() : table(NULL), count(0) {
}

void StubLayout::initStubTable(unsigned char *table_, size_t count_) {
  table = table_;
  count = count_;
}

void *StubLayout::allocateStub(void *addr) {
#if defined(__arm__) || defined(__mips__)
  // Check if we have created this stub or not.
  std::map<void *, void *>::iterator index_iter = stub_index.find(addr);

  if (index_iter != stub_index.end()) {
    return index_iter->second;
  }

  // We have to create a new stub
  if (count == 0) {
    // No free stub slot is available
    return NULL;
  }

  // Initialize the stub
  unsigned char *stub = table;
  setStubAddress(stub, addr);
  stub_index.insert(std::make_pair(addr, stub));

  // Increase the free stub slot pointer
  table += STUB_SIZE;
  count--;

  return stub;

#else
  return NULL;
#endif
}

void StubLayout::setStubAddress(void *stub_, void *addr) {
#if defined(__arm__)
  uint8_t *stub = (uint8_t *)stub_;
  stub[0] = 0x04; // ldr pc, [pc, #-4]
  stub[1] = 0xf0; // ldr pc, [pc, #-4]
  stub[2] = 0x1f; // ldr pc, [pc, #-4]
  stub[3] = 0xe5; // ldr pc, [pc, #-4]

  void **target = (void **)(stub + 4);
  *target = addr;

#elif defined(__mips__)
  uint32_t addr32 = (uint32_t)(uintptr_t)addr;
  uint16_t addr_hi16 = (addr32 >> 16) &  0xffff;
  uint16_t addr_lo16 = addr32 & 0xffff;

  uint32_t *stub = (uint32_t *)stub_;
  stub[0] = 0x3c190000ul | addr_hi16; // lui
  stub[1] = 0x37390000ul | addr_lo16; // ori
  stub[2] = 0x03200008ul; // jr (jump register)
  stub[3] = 0x00000000ul; // nop

#else
  rsl_assert(0 && "Stub is not supported on this platform");
#endif
}

size_t StubLayout::calcStubTableSize(size_t count) const {
#if defined(__arm__) || defined(__mips__)
  return count * STUB_SIZE;
#else
  return 0;
#endif
}