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