blob: 6acf076ef9c1d070c71a3758d95ee28c3fbf63e9 [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
Elliott Hughes13b835a2012-03-13 19:45:22 -070017#include "utils_llvm.h"
18
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080019#include <fcntl.h>
20#include <string.h>
21#include <sys/mman.h>
Elliott Hughes13b835a2012-03-13 19:45:22 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080025
Elliott Hughes13b835a2012-03-13 19:45:22 -070026#include "android/librsloader.h"
Logan Chien7e6e33d2012-01-31 09:22:09 +080027#include "class_loader.h"
28#include "object.h"
29#include "object_utils.h"
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -080030#include "runtime_support_llvm.h"
31
Logan Chien7e6e33d2012-01-31 09:22:09 +080032namespace art {
33
34std::string MangleForLLVM(const std::string& s) {
35 std::string result;
36 size_t char_count = CountModifiedUtf8Chars(s.c_str());
37 const char* cp = &s[0];
38 for (size_t i = 0; i < char_count; ++i) {
39 uint16_t ch = GetUtf16FromUtf8(&cp);
40 if (ch == '$' || ch == '<' || ch == '>' || ch > 127) {
41 StringAppendF(&result, "_0%04x", ch);
42 } else {
43 switch (ch) {
44 case '_':
45 result += "_1";
46 break;
47 case ';':
48 result += "_2";
49 break;
50 case '[':
51 result += "_3";
52 break;
53 case '/':
54 result += "_";
55 break;
56 default:
57 result.push_back(ch);
58 break;
59 }
60 }
61 }
62 return result;
63}
64
65std::string LLVMShortName(const Method* m) {
66 MethodHelper mh(m);
67 std::string class_name(mh.GetDeclaringClassDescriptor());
68 // Remove the leading 'L' and trailing ';'...
69 CHECK_EQ(class_name[0], 'L') << class_name;
70 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
71 class_name.erase(0, 1);
72 class_name.erase(class_name.size() - 1, 1);
73
74 std::string method_name(mh.GetName());
75
76 std::string short_name;
77 short_name += "Art_";
78 short_name += MangleForLLVM(class_name);
79 short_name += "_";
80 short_name += MangleForLLVM(method_name);
81 return short_name;
82}
83
84std::string LLVMLongName(const Method* m) {
85 std::string long_name;
86 long_name += LLVMShortName(m);
87 long_name += "__";
88
89 std::string signature(MethodHelper(m).GetSignature());
90 signature.erase(0, 1);
91 signature.erase(signature.begin() + signature.find(')'), signature.end());
92
93 long_name += MangleForLLVM(signature);
94
95 return long_name;
96}
97
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -080098std::string LLVMStubName(const Method* m) {
99 MethodHelper mh(m);
100 std::string stub_name;
101 if (m->IsStatic()) {
102 stub_name += "ArtSUpcall_";
103 } else {
104 stub_name += "ArtUpcall_";
105 }
106 stub_name += mh.GetShorty();
107
108 return stub_name;
109}
110
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800111void LLVMLinkLoadMethod(const std::string& file_name, Method* method) {
112 CHECK(method != NULL);
113
114 int fd = open(file_name.c_str(), O_RDONLY);
115 CHECK(fd >= 0) << "Error: ELF not found: " << file_name;
116
117 struct stat sb;
118 CHECK(fstat(fd, &sb) == 0) << "Error: Unable to stat ELF: " << file_name;
119
120 unsigned char const *image = (unsigned char const *)
121 mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
122 CHECK(image != MAP_FAILED) << "Error: Unable to mmap ELF: " << file_name;
123
Logan Chien2771fb12012-03-06 16:28:35 +0800124 RSExecRef relocatable =
125 rsloaderCreateExec(image, sb.st_size, art_find_runtime_support_func, 0);
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800126 CHECK(relocatable) << "Error: Unable to load ELF: " << file_name;
127
128 const void *addr = rsloaderGetSymbolAddress(relocatable, LLVMLongName(method).c_str());
129 CHECK(addr) << "Error: ELF (" << file_name << ") has no symbol " << LLVMLongName(method);
130 method->SetCode(reinterpret_cast<const uint32_t*>(addr));
131
132 method->SetFrameSizeInBytes(0);
133 method->SetCoreSpillMask(0);
134 method->SetFpSpillMask(0);
135 method->SetMappingTable(reinterpret_cast<const uint32_t*>(NULL));
136 method->SetVmapTable(reinterpret_cast<const uint16_t*>(NULL));
137 method->SetGcMap(reinterpret_cast<const uint8_t*>(NULL));
138
139 addr = rsloaderGetSymbolAddress(relocatable, LLVMStubName(method).c_str());
140 CHECK(addr) << "Error: ELF (" << file_name << ") has no symbol " << LLVMStubName(method);
141 method->SetInvokeStub(reinterpret_cast<void (*)(const art::Method*, art::Object*, art::Thread*,
142 art::byte*, art::JValue*)>(addr));
143
144 close(fd);
145}
146
Logan Chien7e6e33d2012-01-31 09:22:09 +0800147} // namespace art