Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 1 | /* |
| 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 Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 20 | #include "elf_image.h" |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 21 | #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 | |
| 28 | namespace art { |
| 29 | namespace compiler_llvm { |
| 30 | |
| 31 | |
| 32 | ElfLoader::~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 Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 40 | bool ElfLoader::LoadElfAt(size_t elf_idx, const ElfImage& elf_image) { |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 41 | 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 Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 50 | rsloaderCreateExec(elf_image.begin(), elf_image.size(), |
| 51 | art_find_runtime_support_func, NULL); |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 52 | |
| 53 | if (executable == NULL) { |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 54 | LOG(WARNING) << "Failed to load ELF" |
| 55 | << " image: " << elf_image.begin() |
| 56 | << " size: " << elf_image.size(); |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | executables_[elf_idx] = executable; |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | const 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 | |
| 73 | const Method::InvokeStub* ElfLoader:: |
| 74 | GetMethodInvokeStubAddr(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 | |
| 82 | const 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 |