blob: ca50f6facbaa1b8c4a3c15783859f6f30a131ac8 [file] [log] [blame]
Logan Chien7e6e33d2012-01-31 09:22:09 +08001/*
2 * Copyright (C) 2012 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
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080017#include <android/librsloader.h>
18#include <cutils/log.h>
19#include <fcntl.h>
20#include <string.h>
21#include <sys/mman.h>
22
Logan Chien7e6e33d2012-01-31 09:22:09 +080023#include "utils_llvm.h"
24
25#include "class_loader.h"
26#include "object.h"
27#include "object_utils.h"
28
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -080029#include "runtime_support_llvm.h"
30
Logan Chien7e6e33d2012-01-31 09:22:09 +080031namespace art {
32
33std::string MangleForLLVM(const std::string& s) {
34 std::string result;
35 size_t char_count = CountModifiedUtf8Chars(s.c_str());
36 const char* cp = &s[0];
37 for (size_t i = 0; i < char_count; ++i) {
38 uint16_t ch = GetUtf16FromUtf8(&cp);
39 if (ch == '$' || ch == '<' || ch == '>' || ch > 127) {
40 StringAppendF(&result, "_0%04x", ch);
41 } else {
42 switch (ch) {
43 case '_':
44 result += "_1";
45 break;
46 case ';':
47 result += "_2";
48 break;
49 case '[':
50 result += "_3";
51 break;
52 case '/':
53 result += "_";
54 break;
55 default:
56 result.push_back(ch);
57 break;
58 }
59 }
60 }
61 return result;
62}
63
64std::string LLVMShortName(const Method* m) {
65 MethodHelper mh(m);
66 std::string class_name(mh.GetDeclaringClassDescriptor());
67 // Remove the leading 'L' and trailing ';'...
68 CHECK_EQ(class_name[0], 'L') << class_name;
69 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
70 class_name.erase(0, 1);
71 class_name.erase(class_name.size() - 1, 1);
72
73 std::string method_name(mh.GetName());
74
75 std::string short_name;
76 short_name += "Art_";
77 short_name += MangleForLLVM(class_name);
78 short_name += "_";
79 short_name += MangleForLLVM(method_name);
80 return short_name;
81}
82
83std::string LLVMLongName(const Method* m) {
84 std::string long_name;
85 long_name += LLVMShortName(m);
86 long_name += "__";
87
88 std::string signature(MethodHelper(m).GetSignature());
89 signature.erase(0, 1);
90 signature.erase(signature.begin() + signature.find(')'), signature.end());
91
92 long_name += MangleForLLVM(signature);
93
94 return long_name;
95}
96
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080097std::string LLVMStubName(const Method* m) {
98 MethodHelper mh(m);
99 std::string stub_name;
100 if (m->IsStatic()) {
101 stub_name += "ArtSUpcall_";
102 } else {
103 stub_name += "ArtUpcall_";
104 }
105 stub_name += mh.GetShorty();
106
107 return stub_name;
108}
109
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800110// Linker's call back function. Added some for debugging.
111void* find_sym(void* context, char const* name) {
112 struct func_entry_t {
113 char const* name;
114 size_t name_len;
115 void* addr;
116 };
117
118 static struct func_entry_t const tab[] = {
119#define DEF(NAME, ADDR) \
120 { NAME, sizeof(NAME) - 1, (void *)(&(ADDR)) },
121
122 DEF("art_push_shadow_frame_from_code", art_push_shadow_frame_from_code)
123 DEF("art_pop_shadow_frame_from_code", art_pop_shadow_frame_from_code)
124 DEF("art_is_exception_pending_from_code", art_is_exception_pending_from_code)
Shih-wei Liao113f4fc2012-03-04 14:28:35 -0800125 DEF("art_throw_div_zero_from_code", art_throw_div_zero_from_code)
126 DEF("art_throw_array_bounds_from_code", art_throw_array_bounds_from_code)
127 DEF("art_throw_no_such_method_from_code", art_throw_no_such_method_from_code)
128 DEF("art_throw_null_pointer_exception_from_code", art_throw_null_pointer_exception_from_code)
129 DEF("art_throw_stack_overflow_from_code", art_throw_stack_overflow_from_code)
130 DEF("art_throw_exception_from_code", art_throw_exception_from_code)
131 DEF("art_find_catch_block_from_code", art_find_catch_block_from_code)
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800132 DEF("art_test_suspend_from_code", art_test_suspend_from_code)
133 DEF("art_set_current_thread_from_code", art_set_current_thread_from_code)
134 DEF("printf", printf)
135 DEF("scanf", scanf)
136 DEF("__isoc99_scanf", scanf)
137 DEF("rand", rand)
138 DEF("time", time)
139 DEF("srand", srand)
140#undef DEF
141 };
142
143 static size_t const tab_size = sizeof(tab) / sizeof(struct func_entry_t);
144
145 // Note: Since our table is small, we are using trivial O(n) searching
146 // function. For bigger table, it will be better to use binary
147 // search or hash function.
148 size_t i;
149 size_t name_len = strlen(name);
150 for (i = 0; i < tab_size; ++i) {
151 if (name_len == tab[i].name_len && strcmp(name, tab[i].name) == 0) {
152 return tab[i].addr;
153 }
154 }
155
Shih-wei Liao6edfde42012-03-01 15:49:12 -0800156 LOG(FATAL) << "Error: Can't find symbol " << name;
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800157 return 0;
158}
159
160void LLVMLinkLoadMethod(const std::string& file_name, Method* method) {
161 CHECK(method != NULL);
162
163 int fd = open(file_name.c_str(), O_RDONLY);
164 CHECK(fd >= 0) << "Error: ELF not found: " << file_name;
165
166 struct stat sb;
167 CHECK(fstat(fd, &sb) == 0) << "Error: Unable to stat ELF: " << file_name;
168
169 unsigned char const *image = (unsigned char const *)
170 mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
171 CHECK(image != MAP_FAILED) << "Error: Unable to mmap ELF: " << file_name;
172
173 RSExecRef relocatable = rsloaderCreateExec(image, sb.st_size, find_sym, 0);
174 CHECK(relocatable) << "Error: Unable to load ELF: " << file_name;
175
176 const void *addr = rsloaderGetSymbolAddress(relocatable, LLVMLongName(method).c_str());
177 CHECK(addr) << "Error: ELF (" << file_name << ") has no symbol " << LLVMLongName(method);
178 method->SetCode(reinterpret_cast<const uint32_t*>(addr));
179
180 method->SetFrameSizeInBytes(0);
181 method->SetCoreSpillMask(0);
182 method->SetFpSpillMask(0);
183 method->SetMappingTable(reinterpret_cast<const uint32_t*>(NULL));
184 method->SetVmapTable(reinterpret_cast<const uint16_t*>(NULL));
185 method->SetGcMap(reinterpret_cast<const uint8_t*>(NULL));
186
187 addr = rsloaderGetSymbolAddress(relocatable, LLVMStubName(method).c_str());
188 CHECK(addr) << "Error: ELF (" << file_name << ") has no symbol " << LLVMStubName(method);
189 method->SetInvokeStub(reinterpret_cast<void (*)(const art::Method*, art::Object*, art::Thread*,
190 art::byte*, art::JValue*)>(addr));
191
192 close(fd);
193}
194
Logan Chien7e6e33d2012-01-31 09:22:09 +0800195} // namespace art