blob: f309b82af1238be38febf98a9b1c3d9e027090cc [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"
20#include "logging.h"
21#include "object.h"
22#include "runtime_support_llvm.h"
23#include "utils_llvm.h"
24
25#include <android/librsloader.h>
26
27namespace art {
28namespace compiler_llvm {
29
30
31ElfLoader::~ElfLoader() {
32 // Release every ELF object
33 for (size_t i = 0; i < executables_.size(); ++i) {
34 rsloaderDisposeExec(executables_[i]);
35 }
36}
37
38
39bool ElfLoader::LoadElfAt(size_t elf_idx, const byte* image, size_t size) {
40 if (elf_idx < executables_.size() && executables_[elf_idx] != NULL) {
41 return false;
42 }
43
44 if (elf_idx >= executables_.size()) {
45 executables_.resize(elf_idx + 1);
46 }
47
48 RSExecRef executable =
49 rsloaderCreateExec(reinterpret_cast<const unsigned char*>(image),
50 size, art_find_runtime_support_func, NULL);
51
52 if (executable == NULL) {
53 LOG(WARNING) << "Failed to load ELF image: " << image << " size: " << size;
54 return false;
55 }
56
57 executables_[elf_idx] = executable;
58 return true;
59}
60
61
62const void* ElfLoader::GetMethodCodeAddr(size_t elf_idx,
63 const Method* method) const {
64 CHECK_LT(elf_idx, executables_.size());
65 CHECK(method != NULL);
66 return GetAddr(elf_idx, LLVMLongName(method).c_str());
67}
68
69
70const Method::InvokeStub* ElfLoader::
71GetMethodInvokeStubAddr(size_t elf_idx, const Method* method) const {
72 CHECK_LT(elf_idx, executables_.size());
73 CHECK(method != NULL);
74 return reinterpret_cast<const Method::InvokeStub*>(
75 GetAddr(elf_idx, LLVMStubName(method).c_str()));
76}
77
78
79const void* ElfLoader::GetAddr(size_t elf_idx, const char* sym_name) const {
80 CHECK_LT(elf_idx, executables_.size());
81 CHECK(executables_[elf_idx] != NULL);
82 return rsloaderGetSymbolAddress(executables_[elf_idx], sym_name);
83}
84
85
86} // namespace compiler_llvm
87} // namespace art