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" |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 22 | #include "oat_file.h" |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 23 | #include "object.h" |
| 24 | #include "runtime_support_llvm.h" |
| 25 | #include "utils_llvm.h" |
| 26 | |
| 27 | #include <android/librsloader.h> |
| 28 | |
| 29 | namespace art { |
| 30 | namespace compiler_llvm { |
| 31 | |
| 32 | |
| 33 | ElfLoader::~ElfLoader() { |
| 34 | // Release every ELF object |
| 35 | for (size_t i = 0; i < executables_.size(); ++i) { |
| 36 | rsloaderDisposeExec(executables_[i]); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 41 | bool ElfLoader::LoadElfAt(size_t elf_idx, |
| 42 | const ElfImage& elf_image, |
| 43 | OatFile::RelocationBehavior reloc) { |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 44 | if (elf_idx < executables_.size() && executables_[elf_idx] != NULL) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | if (elf_idx >= executables_.size()) { |
| 49 | executables_.resize(elf_idx + 1); |
| 50 | } |
| 51 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 52 | RSExecRef executable = rsloaderLoadExecutable(elf_image.begin(), |
| 53 | elf_image.size()); |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 54 | |
| 55 | if (executable == NULL) { |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 56 | LOG(WARNING) << "Failed to load ELF" |
Logan Chien | 3fe0c60 | 2012-03-27 21:14:37 +0800 | [diff] [blame] | 57 | << " image: " << static_cast<const void*>(elf_image.begin()) |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 58 | << " size: " << elf_image.size(); |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 62 | if (reloc == OatFile::kRelocAll) { |
| 63 | if (!rsloaderRelocateExecutable(executable, |
| 64 | art_find_runtime_support_func, NULL)) { |
| 65 | LOG(ERROR) << "Failed to relocate the ELF image"; |
| 66 | rsloaderDisposeExec(executable); |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 71 | executables_[elf_idx] = executable; |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 76 | void ElfLoader::RelocateExecutable() { |
| 77 | for (size_t i = 0; i < executables_.size(); ++i) { |
| 78 | if (executables_[i] != NULL && |
| 79 | !rsloaderRelocateExecutable(executables_[i], |
| 80 | art_find_runtime_support_func, NULL)) { |
| 81 | LOG(FATAL) << "Failed to relocate ELF image " << i; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 87 | const void* ElfLoader::GetMethodCodeAddr(uint16_t elf_idx, |
| 88 | uint16_t elf_func_idx) const { |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 89 | CHECK_LT(elf_idx, executables_.size()); |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 90 | return GetAddr(elf_idx, ElfFuncName(elf_func_idx).c_str()); |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | const Method::InvokeStub* ElfLoader:: |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 95 | GetMethodInvokeStubAddr(uint16_t elf_idx, uint16_t elf_func_idx) const { |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 96 | CHECK_LT(elf_idx, executables_.size()); |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 97 | return reinterpret_cast<const Method::InvokeStub*>( |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 98 | GetAddr(elf_idx, ElfFuncName(elf_func_idx).c_str())); |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
Logan Chien | 14924fe | 2012-04-03 18:33:37 +0800 | [diff] [blame] | 102 | size_t ElfLoader::GetCodeSize(uint16_t elf_idx, uint16_t elf_func_idx) const { |
| 103 | CHECK_LT(elf_idx, executables_.size()); |
| 104 | CHECK(executables_[elf_idx] != NULL); |
| 105 | return rsloaderGetSymbolSize(executables_[elf_idx], |
| 106 | ElfFuncName(elf_func_idx).c_str()); |
| 107 | } |
| 108 | |
| 109 | |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 110 | const void* ElfLoader::GetAddr(size_t elf_idx, const char* sym_name) const { |
| 111 | CHECK_LT(elf_idx, executables_.size()); |
| 112 | CHECK(executables_[elf_idx] != NULL); |
| 113 | return rsloaderGetSymbolAddress(executables_[elf_idx], sym_name); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | } // namespace compiler_llvm |
| 118 | } // namespace art |