blob: bc2a7f84da1e4ac0271e448c6b15b22b6f9ccad3 [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 -0800110void LLVMLinkLoadMethod(const std::string& file_name, Method* method) {
111 CHECK(method != NULL);
112
113 int fd = open(file_name.c_str(), O_RDONLY);
114 CHECK(fd >= 0) << "Error: ELF not found: " << file_name;
115
116 struct stat sb;
117 CHECK(fstat(fd, &sb) == 0) << "Error: Unable to stat ELF: " << file_name;
118
119 unsigned char const *image = (unsigned char const *)
120 mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
121 CHECK(image != MAP_FAILED) << "Error: Unable to mmap ELF: " << file_name;
122
Logan Chien2771fb12012-03-06 16:28:35 +0800123 RSExecRef relocatable =
124 rsloaderCreateExec(image, sb.st_size, art_find_runtime_support_func, 0);
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800125 CHECK(relocatable) << "Error: Unable to load ELF: " << file_name;
126
127 const void *addr = rsloaderGetSymbolAddress(relocatable, LLVMLongName(method).c_str());
128 CHECK(addr) << "Error: ELF (" << file_name << ") has no symbol " << LLVMLongName(method);
129 method->SetCode(reinterpret_cast<const uint32_t*>(addr));
130
131 method->SetFrameSizeInBytes(0);
132 method->SetCoreSpillMask(0);
133 method->SetFpSpillMask(0);
134 method->SetMappingTable(reinterpret_cast<const uint32_t*>(NULL));
135 method->SetVmapTable(reinterpret_cast<const uint16_t*>(NULL));
136 method->SetGcMap(reinterpret_cast<const uint8_t*>(NULL));
137
138 addr = rsloaderGetSymbolAddress(relocatable, LLVMStubName(method).c_str());
139 CHECK(addr) << "Error: ELF (" << file_name << ") has no symbol " << LLVMStubName(method);
140 method->SetInvokeStub(reinterpret_cast<void (*)(const art::Method*, art::Object*, art::Thread*,
141 art::byte*, art::JValue*)>(addr));
142
143 close(fd);
144}
145
Logan Chien7e6e33d2012-01-31 09:22:09 +0800146} // namespace art