Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
| 17 | #include "oat_file.h" |
| 18 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 19 | #include <dlfcn.h> |
| 20 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 21 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 24 | #include "elf_file.h" |
| 25 | #include "oat.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 26 | #include "mirror/art_method.h" |
| 27 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "mirror/class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "mirror/object-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 30 | #include "os.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "utils.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 32 | #include "vmap_table.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 36 | void OatFile::CheckLocation(const std::string& location) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 37 | CHECK(!location.empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 40 | OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 41 | const std::string& location, |
| 42 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 43 | CHECK(!oat_contents.empty()) << location; |
| 44 | CheckLocation(location); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 45 | std::unique_ptr<OatFile> oat_file(new OatFile(location)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 46 | oat_file->begin_ = &oat_contents[0]; |
| 47 | oat_file->end_ = &oat_contents[oat_contents.size()]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 48 | return oat_file->Setup(error_msg) ? oat_file.release() : nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | OatFile* OatFile::Open(const std::string& filename, |
| 52 | const std::string& location, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 53 | byte* requested_base, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 54 | bool executable, |
| 55 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 56 | CHECK(!filename.empty()) << location; |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 57 | CheckLocation(filename); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 58 | if (kUsePortableCompiler) { |
| 59 | // If we are using PORTABLE, use dlopen to deal with relocations. |
| 60 | // |
| 61 | // We use our own ELF loader for Quick to deal with legacy apps that |
| 62 | // open a generated dex file by name, remove the file, then open |
| 63 | // another generated dex file with the same name. http://b/10614658 |
| 64 | if (executable) { |
| 65 | return OpenDlopen(filename, location, requested_base, error_msg); |
| 66 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 67 | } |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 68 | // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: |
| 69 | // |
| 70 | // On target, dlopen may fail when compiling due to selinux restrictions on installd. |
| 71 | // |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 72 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
| 73 | // This won't work for portable runtime execution because it doesn't process relocations. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 74 | std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str())); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 75 | if (file.get() == NULL) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 76 | *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 77 | return NULL; |
| 78 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 79 | return OpenElfFile(file.get(), location, requested_base, false, executable, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 82 | OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 83 | CheckLocation(location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 84 | return OpenElfFile(file, location, NULL, true, false, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | OatFile* OatFile::OpenDlopen(const std::string& elf_filename, |
| 88 | const std::string& location, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 89 | byte* requested_base, |
| 90 | std::string* error_msg) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 91 | std::unique_ptr<OatFile> oat_file(new OatFile(location)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 92 | bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 93 | if (!success) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 94 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 95 | } |
| 96 | return oat_file.release(); |
| 97 | } |
| 98 | |
| 99 | OatFile* OatFile::OpenElfFile(File* file, |
| 100 | const std::string& location, |
| 101 | byte* requested_base, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 102 | bool writable, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 103 | bool executable, |
| 104 | std::string* error_msg) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 105 | std::unique_ptr<OatFile> oat_file(new OatFile(location)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 106 | bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 107 | if (!success) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 108 | CHECK(!error_msg->empty()); |
| 109 | return nullptr; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 110 | } |
| 111 | return oat_file.release(); |
| 112 | } |
| 113 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 114 | OatFile::OatFile(const std::string& location) |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 115 | : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 116 | CHECK(!location_.empty()); |
| 117 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 118 | |
| 119 | OatFile::~OatFile() { |
| 120 | STLDeleteValues(&oat_dex_files_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 121 | if (dlopen_handle_ != NULL) { |
| 122 | dlclose(dlopen_handle_); |
| 123 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 126 | bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base, |
| 127 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 128 | char* absolute_path = realpath(elf_filename.c_str(), NULL); |
| 129 | if (absolute_path == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 130 | *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 133 | dlopen_handle_ = dlopen(absolute_path, RTLD_NOW); |
| 134 | free(absolute_path); |
| 135 | if (dlopen_handle_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 136 | *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 139 | begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata")); |
| 140 | if (begin_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 141 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(), |
| 142 | dlerror()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 143 | return false; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 144 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 145 | if (requested_base != NULL && begin_ != requested_base) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 146 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
| 147 | "oatdata=%p != expected=%p /proc/self/maps:\n", |
| 148 | begin_, requested_base); |
| 149 | ReadFileToString("/proc/self/maps", error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword")); |
| 153 | if (end_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 154 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(), |
| 155 | dlerror()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | // Readjust to be non-inclusive upper bound. |
| 159 | end_ += sizeof(uint32_t); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 160 | return Setup(error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 161 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 162 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 163 | bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable, |
| 164 | std::string* error_msg) { |
| 165 | elf_file_.reset(ElfFile::Open(file, writable, true, error_msg)); |
| 166 | if (elf_file_.get() == nullptr) { |
| 167 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 168 | return false; |
| 169 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 170 | bool loaded = elf_file_->Load(executable, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 171 | if (!loaded) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 172 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | begin_ = elf_file_->FindDynamicSymbolAddress("oatdata"); |
| 176 | if (begin_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 177 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | if (requested_base != NULL && begin_ != requested_base) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 181 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
| 182 | "oatdata=%p != expected=%p /proc/self/maps:\n", |
| 183 | begin_, requested_base); |
| 184 | ReadFileToString("/proc/self/maps", error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | end_ = elf_file_->FindDynamicSymbolAddress("oatlastword"); |
| 188 | if (end_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 189 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | // Readjust to be non-inclusive upper bound. |
| 193 | end_ += sizeof(uint32_t); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 194 | return Setup(error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 195 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 196 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 197 | bool OatFile::Setup(std::string* error_msg) { |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 198 | if (!GetOatHeader().IsValid()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 199 | *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str()); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 202 | const byte* oat = Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 203 | oat += sizeof(OatHeader); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 204 | if (oat > End()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 205 | *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 208 | |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 209 | oat += GetOatHeader().GetImageFileLocationSize(); |
| 210 | if (oat > End()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 211 | *error_msg = StringPrintf("In oat file '%s' found truncated image file location: " |
| 212 | "%p + %zd + %ud <= %p", GetLocation().c_str(), |
| 213 | Begin(), sizeof(OatHeader), GetOatHeader().GetImageFileLocationSize(), |
| 214 | End()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 218 | for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) { |
Dmitry Petrochenko | 83bef92 | 2014-02-03 12:34:59 +0700 | [diff] [blame] | 219 | uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 220 | if (UNLIKELY(dex_file_location_size == 0U)) { |
| 221 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name", |
| 222 | GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 223 | return false; |
| 224 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 225 | oat += sizeof(dex_file_location_size); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 226 | if (UNLIKELY(oat > End())) { |
| 227 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file " |
| 228 | "location size", GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 229 | return false; |
| 230 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 231 | |
| 232 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 233 | oat += dex_file_location_size; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 234 | if (UNLIKELY(oat > End())) { |
| 235 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file " |
| 236 | "location", GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 237 | return false; |
| 238 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 239 | |
| 240 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 241 | |
| 242 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 243 | oat += sizeof(dex_file_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 244 | if (UNLIKELY(oat > End())) { |
| 245 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after " |
| 246 | "dex file checksum", GetLocation().c_str(), i, |
| 247 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 248 | return false; |
| 249 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 250 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 251 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 252 | if (UNLIKELY(dex_file_offset == 0U)) { |
| 253 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex " |
| 254 | "file offset", GetLocation().c_str(), i, dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 257 | if (UNLIKELY(dex_file_offset > Size())) { |
| 258 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file " |
| 259 | "offset %ud > %zd", GetLocation().c_str(), i, |
| 260 | dex_file_location.c_str(), dex_file_offset, Size()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 263 | oat += sizeof(dex_file_offset); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 264 | if (UNLIKELY(oat > End())) { |
| 265 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " |
| 266 | " after dex file offsets", GetLocation().c_str(), i, |
| 267 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 268 | return false; |
| 269 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 270 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 271 | const uint8_t* dex_file_pointer = Begin() + dex_file_offset; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 272 | if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) { |
| 273 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid " |
| 274 | " dex file magic '%s'", GetLocation().c_str(), i, |
| 275 | dex_file_location.c_str(), dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 278 | if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) { |
| 279 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid " |
| 280 | " dex file version '%s'", GetLocation().c_str(), i, |
| 281 | dex_file_location.c_str(), dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 282 | return false; |
| 283 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 284 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
| 285 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 286 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 287 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 288 | if (UNLIKELY(oat > End())) { |
| 289 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated " |
| 290 | " method offsets", GetLocation().c_str(), i, |
| 291 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 292 | return false; |
| 293 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 294 | |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 295 | OatDexFile* oat_dex_file = new OatDexFile(this, |
| 296 | dex_file_location, |
| 297 | dex_file_checksum, |
| 298 | dex_file_pointer, |
| 299 | methods_offsets_pointer); |
| 300 | // Use a StringPiece backed by the oat_dex_file's internal std::string as the key. |
| 301 | StringPiece key(oat_dex_file->GetDexFileLocation()); |
| 302 | oat_dex_files_.Put(key, oat_dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 303 | } |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 304 | return true; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 308 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 311 | const byte* OatFile::Begin() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 312 | CHECK(begin_ != NULL); |
| 313 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 316 | const byte* OatFile::End() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 317 | CHECK(end_ != NULL); |
| 318 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 321 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location, |
| 322 | const uint32_t* dex_location_checksum, |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 323 | bool warn_if_not_found) const { |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 324 | Table::const_iterator it = oat_dex_files_.find(dex_location); |
| 325 | if (it != oat_dex_files_.end()) { |
| 326 | const OatFile::OatDexFile* oat_dex_file = it->second; |
| 327 | if (dex_location_checksum == NULL || |
| 328 | oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) { |
| 329 | return oat_dex_file; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (warn_if_not_found) { |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 334 | std::string checksum("<unspecified>"); |
| 335 | if (dex_location_checksum != NULL) { |
| 336 | checksum = StringPrintf("0x%08x", *dex_location_checksum); |
| 337 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 338 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 339 | << " with checksum " << checksum << " in OatFile " << GetLocation(); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 340 | if (kIsDebugBuild) { |
| 341 | for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) { |
| 342 | LOG(WARNING) << "OatFile " << GetLocation() |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 343 | << " contains OatDexFile " << it->second->GetDexFileLocation() |
| 344 | << " with checksum 0x" << std::hex << it->second->GetDexFileLocationChecksum(); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 345 | } |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 346 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 347 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 348 | return NULL; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const { |
| 352 | std::vector<const OatFile::OatDexFile*> result; |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 353 | for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 354 | result.push_back(it->second); |
| 355 | } |
| 356 | return result; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 360 | const std::string& dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 361 | uint32_t dex_file_location_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 362 | const byte* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 363 | const uint32_t* oat_class_offsets_pointer) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 364 | : oat_file_(oat_file), |
| 365 | dex_file_location_(dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 366 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 367 | dex_file_pointer_(dex_file_pointer), |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 368 | oat_class_offsets_pointer_(oat_class_offsets_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 369 | |
| 370 | OatFile::OatDexFile::~OatDexFile() {} |
| 371 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 372 | size_t OatFile::OatDexFile::FileSize() const { |
| 373 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 374 | } |
| 375 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 376 | const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const { |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 377 | return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 378 | dex_file_location_checksum_, error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 381 | OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 382 | uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index]; |
| 383 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 384 | const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 385 | CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 386 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 387 | const byte* status_pointer = oat_class_pointer; |
| 388 | CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 389 | mirror::Class::Status status = |
| 390 | static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer)); |
| 391 | CHECK_LT(status, mirror::Class::kStatusMax); |
| 392 | |
| 393 | const byte* type_pointer = status_pointer + sizeof(uint16_t); |
| 394 | CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 395 | OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); |
| 396 | CHECK_LT(type, kOatClassMax); |
| 397 | |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 398 | const byte* after_type_pointer = type_pointer + sizeof(int16_t); |
| 399 | CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 400 | |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 401 | uint32_t bitmap_size = 0; |
| 402 | const byte* bitmap_pointer = nullptr; |
| 403 | const byte* methods_pointer = nullptr; |
| 404 | if (type == kOatClassSomeCompiled) { |
| 405 | bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); |
| 406 | bitmap_pointer = after_type_pointer + sizeof(bitmap_size); |
| 407 | CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 408 | methods_pointer = bitmap_pointer + bitmap_size; |
| 409 | } else { |
| 410 | methods_pointer = after_type_pointer; |
| 411 | } |
| 412 | CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 413 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 414 | return OatClass(oat_file_, |
| 415 | status, |
| 416 | type, |
| 417 | bitmap_size, |
| 418 | reinterpret_cast<const uint32_t*>(bitmap_pointer), |
| 419 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 422 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 423 | mirror::Class::Status status, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 424 | OatClassType type, |
| 425 | uint32_t bitmap_size, |
| 426 | const uint32_t* bitmap_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 427 | const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 428 | : oat_file_(oat_file), status_(status), type_(type), |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 429 | bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 430 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 431 | switch (type_) { |
| 432 | case kOatClassAllCompiled: { |
| 433 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 434 | CHECK(bitmap_pointer == nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 435 | break; |
| 436 | } |
| 437 | case kOatClassSomeCompiled: { |
| 438 | CHECK_NE(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 439 | CHECK(bitmap_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 440 | break; |
| 441 | } |
| 442 | case kOatClassNoneCompiled: { |
| 443 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 444 | CHECK(bitmap_pointer == nullptr); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 445 | methods_pointer_ = nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | case kOatClassMax: { |
| 449 | LOG(FATAL) << "Invalid OatClassType " << type_; |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 454 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 455 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 456 | // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 457 | if (methods_pointer_ == NULL) { |
| 458 | CHECK_EQ(kOatClassNoneCompiled, type_); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 459 | return OatMethod(NULL, 0, 0); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 460 | } |
| 461 | size_t methods_pointer_index; |
| 462 | if (bitmap_ == NULL) { |
| 463 | CHECK_EQ(kOatClassAllCompiled, type_); |
| 464 | methods_pointer_index = method_index; |
| 465 | } else { |
| 466 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 467 | if (!BitVector::IsBitSet(bitmap_, method_index)) { |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 468 | return OatMethod(NULL, 0, 0); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 469 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 470 | size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); |
| 471 | methods_pointer_index = num_set_bits; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 472 | } |
| 473 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 474 | return OatMethod( |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 475 | oat_file_->Begin(), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 476 | oat_method_offsets.code_offset_, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 477 | oat_method_offsets.gc_map_offset_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 480 | OatFile::OatMethod::OatMethod(const byte* base, |
| 481 | const uint32_t code_offset, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 482 | const uint32_t gc_map_offset) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 483 | : begin_(base), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 484 | code_offset_(code_offset), |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 485 | native_gc_map_offset_(gc_map_offset) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 486 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 487 | |
| 488 | OatFile::OatMethod::~OatMethod() {} |
| 489 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 490 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 491 | uint32_t OatFile::OatMethod::GetQuickCodeSize() const { |
| 492 | uintptr_t code = reinterpret_cast<uintptr_t>(GetQuickCode()); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 493 | if (code == 0) { |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 494 | return 0; |
| 495 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 496 | // TODO: make this Thumb2 specific |
| 497 | code &= ~0x1; |
| 498 | return reinterpret_cast<uint32_t*>(code)[-1]; |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 499 | } |
| 500 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 501 | void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 502 | CHECK(method != NULL); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 503 | method->SetEntryPointFromPortableCompiledCode(GetPortableCode()); |
| 504 | method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 505 | method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 508 | } // namespace art |