blob: ccb02ad2a4df9f1be1b50bf867ec61b1d0acca53 [file] [log] [blame]
Logan Chienf7015fd2012-03-18 01:19:37 +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
17#include "elf_loader.h"
18
19#include "compiled_method.h"
Logan Chien0f0899a2012-03-23 10:48:18 +080020#include "elf_image.h"
Logan Chienf7015fd2012-03-18 01:19:37 +080021#include "logging.h"
22#include "object.h"
23#include "runtime_support_llvm.h"
24#include "utils_llvm.h"
25
26#include <android/librsloader.h>
27
28namespace art {
29namespace compiler_llvm {
30
31
32ElfLoader::~ElfLoader() {
33 // Release every ELF object
34 for (size_t i = 0; i < executables_.size(); ++i) {
35 rsloaderDisposeExec(executables_[i]);
36 }
37}
38
39
Logan Chien0f0899a2012-03-23 10:48:18 +080040bool ElfLoader::LoadElfAt(size_t elf_idx, const ElfImage& elf_image) {
Logan Chienf7015fd2012-03-18 01:19:37 +080041 if (elf_idx < executables_.size() && executables_[elf_idx] != NULL) {
42 return false;
43 }
44
45 if (elf_idx >= executables_.size()) {
46 executables_.resize(elf_idx + 1);
47 }
48
49 RSExecRef executable =
Logan Chien0f0899a2012-03-23 10:48:18 +080050 rsloaderCreateExec(elf_image.begin(), elf_image.size(),
51 art_find_runtime_support_func, NULL);
Logan Chienf7015fd2012-03-18 01:19:37 +080052
53 if (executable == NULL) {
Logan Chien0f0899a2012-03-23 10:48:18 +080054 LOG(WARNING) << "Failed to load ELF"
55 << " image: " << elf_image.begin()
56 << " size: " << elf_image.size();
Logan Chienf7015fd2012-03-18 01:19:37 +080057 return false;
58 }
59
60 executables_[elf_idx] = executable;
61 return true;
62}
63
64
65const void* ElfLoader::GetMethodCodeAddr(size_t elf_idx,
66 const Method* method) const {
67 CHECK_LT(elf_idx, executables_.size());
68 CHECK(method != NULL);
69 return GetAddr(elf_idx, LLVMLongName(method).c_str());
70}
71
72
73const Method::InvokeStub* ElfLoader::
74GetMethodInvokeStubAddr(size_t elf_idx, const Method* method) const {
75 CHECK_LT(elf_idx, executables_.size());
76 CHECK(method != NULL);
77 return reinterpret_cast<const Method::InvokeStub*>(
78 GetAddr(elf_idx, LLVMStubName(method).c_str()));
79}
80
81
82const void* ElfLoader::GetAddr(size_t elf_idx, const char* sym_name) const {
83 CHECK_LT(elf_idx, executables_.size());
84 CHECK(executables_[elf_idx] != NULL);
85 return rsloaderGetSymbolAddress(executables_[elf_idx], sym_name);
86}
87
88
89} // namespace compiler_llvm
90} // namespace art