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