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 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 21 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 22 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 23 | #include "elf_file.h" |
| 24 | #include "oat.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 25 | #include "mirror/art_method.h" |
| 26 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 28 | #include "mirror/object-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 29 | #include "os.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "utils.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 31 | #include "vmap_table.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 35 | std::string OatFile::DexFilenameToOdexFilename(const std::string& location) { |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 36 | CHECK_GE(location.size(), 4U) << location; // must be at least .123 |
| 37 | size_t dot_index = location.size() - 3 - 1; // 3=dex or zip or apk |
| 38 | CHECK_EQ('.', location[dot_index]) << location; |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 39 | std::string odex_location(location); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 40 | odex_location.resize(dot_index + 1); |
| 41 | CHECK_EQ('.', odex_location[odex_location.size()-1]) << location << " " << odex_location; |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 42 | odex_location += "odex"; |
| 43 | return odex_location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 46 | void OatFile::CheckLocation(const std::string& location) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 47 | CHECK(!location.empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 50 | OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents, |
| 51 | const std::string& location) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 52 | CHECK(!oat_contents.empty()) << location; |
| 53 | CheckLocation(location); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 54 | UniquePtr<OatFile> oat_file(new OatFile(location)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 55 | oat_file->begin_ = &oat_contents[0]; |
| 56 | oat_file->end_ = &oat_contents[oat_contents.size()]; |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 57 | return oat_file->Setup() ? oat_file.release() : NULL; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | OatFile* OatFile::Open(const std::string& filename, |
| 61 | const std::string& location, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 62 | byte* requested_base, |
| 63 | bool executable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 64 | CHECK(!filename.empty()) << location; |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 65 | CheckLocation(filename); |
Brian Carlstrom | eeb9888 | 2013-10-03 15:53:49 -0700 | [diff] [blame] | 66 | #ifdef ART_USE_PORTABLE_COMPILER |
| 67 | // If we are using PORTABLE, use dlopen to deal with relocations. |
| 68 | // |
| 69 | // We use our own ELF loader for Quick to deal with legacy apps that |
| 70 | // open a generated dex file by name, remove the file, then open |
| 71 | // another generated dex file with the same name. http://b/10614658 |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 72 | if (executable) { |
| 73 | return OpenDlopen(filename, location, requested_base); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 74 | } |
Brian Carlstrom | eeb9888 | 2013-10-03 15:53:49 -0700 | [diff] [blame] | 75 | #endif |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 76 | // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: |
| 77 | // |
| 78 | // On target, dlopen may fail when compiling due to selinux restrictions on installd. |
| 79 | // |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 80 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
| 81 | // 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] | 82 | UniquePtr<File> file(OS::OpenFileForReading(filename.c_str())); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 83 | if (file.get() == NULL) { |
| 84 | return NULL; |
| 85 | } |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 86 | return OpenElfFile(file.get(), location, requested_base, false, executable); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 89 | OatFile* OatFile::OpenWritable(File* file, const std::string& location) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 90 | CheckLocation(location); |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 91 | return OpenElfFile(file, location, NULL, true, false); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | OatFile* OatFile::OpenDlopen(const std::string& elf_filename, |
| 95 | const std::string& location, |
| 96 | byte* requested_base) { |
| 97 | UniquePtr<OatFile> oat_file(new OatFile(location)); |
| 98 | bool success = oat_file->Dlopen(elf_filename, requested_base); |
| 99 | if (!success) { |
| 100 | return NULL; |
| 101 | } |
| 102 | return oat_file.release(); |
| 103 | } |
| 104 | |
| 105 | OatFile* OatFile::OpenElfFile(File* file, |
| 106 | const std::string& location, |
| 107 | byte* requested_base, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 108 | bool writable, |
| 109 | bool executable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 110 | UniquePtr<OatFile> oat_file(new OatFile(location)); |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 111 | bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 112 | if (!success) { |
| 113 | return NULL; |
| 114 | } |
| 115 | return oat_file.release(); |
| 116 | } |
| 117 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 118 | OatFile::OatFile(const std::string& location) |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 119 | : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 120 | CHECK(!location_.empty()); |
| 121 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 122 | |
| 123 | OatFile::~OatFile() { |
| 124 | STLDeleteValues(&oat_dex_files_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 125 | if (dlopen_handle_ != NULL) { |
| 126 | dlclose(dlopen_handle_); |
| 127 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 130 | bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 131 | char* absolute_path = realpath(elf_filename.c_str(), NULL); |
| 132 | if (absolute_path == NULL) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 133 | VLOG(class_linker) << "Failed to find absolute path for " << elf_filename; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 134 | return false; |
| 135 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 136 | dlopen_handle_ = dlopen(absolute_path, RTLD_NOW); |
| 137 | free(absolute_path); |
| 138 | if (dlopen_handle_ == NULL) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 139 | VLOG(class_linker) << "Failed to dlopen " << elf_filename << ": " << dlerror(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 142 | begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata")); |
| 143 | if (begin_ == NULL) { |
| 144 | LOG(WARNING) << "Failed to find oatdata symbol in " << elf_filename << ": " << dlerror(); |
| 145 | return false; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 146 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 147 | if (requested_base != NULL && begin_ != requested_base) { |
| 148 | std::string maps; |
| 149 | ReadFileToString("/proc/self/maps", &maps); |
| 150 | LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata=" |
| 151 | << reinterpret_cast<const void*>(begin_) << " != expected=" |
| 152 | << reinterpret_cast<const void*>(requested_base) |
| 153 | << " /proc/self/maps:\n" << maps; |
| 154 | return false; |
| 155 | } |
| 156 | end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword")); |
| 157 | if (end_ == NULL) { |
| 158 | LOG(WARNING) << "Failed to find oatlastword symbol in " << elf_filename << ": " << dlerror(); |
| 159 | return false; |
| 160 | } |
| 161 | // Readjust to be non-inclusive upper bound. |
| 162 | end_ += sizeof(uint32_t); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 163 | return Setup(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 164 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 165 | |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 166 | bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 167 | elf_file_.reset(ElfFile::Open(file, writable, true)); |
| 168 | if (elf_file_.get() == NULL) { |
Brian Carlstrom | eeb9888 | 2013-10-03 15:53:49 -0700 | [diff] [blame] | 169 | if (writable) { |
| 170 | PLOG(WARNING) << "Failed to open ELF file for " << file->GetPath(); |
| 171 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 172 | return false; |
| 173 | } |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 174 | bool loaded = elf_file_->Load(executable); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 175 | if (!loaded) { |
| 176 | LOG(WARNING) << "Failed to load ELF file " << file->GetPath(); |
| 177 | return false; |
| 178 | } |
| 179 | begin_ = elf_file_->FindDynamicSymbolAddress("oatdata"); |
| 180 | if (begin_ == NULL) { |
| 181 | LOG(WARNING) << "Failed to find oatdata symbol in " << file->GetPath(); |
| 182 | return false; |
| 183 | } |
| 184 | if (requested_base != NULL && begin_ != requested_base) { |
| 185 | std::string maps; |
| 186 | ReadFileToString("/proc/self/maps", &maps); |
| 187 | LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata=" |
| 188 | << reinterpret_cast<const void*>(begin_) << " != expected=" |
| 189 | << reinterpret_cast<const void*>(requested_base) |
| 190 | << " /proc/self/maps:\n" << maps; |
| 191 | return false; |
| 192 | } |
| 193 | end_ = elf_file_->FindDynamicSymbolAddress("oatlastword"); |
| 194 | if (end_ == NULL) { |
| 195 | LOG(WARNING) << "Failed to find oatlastword symbol in " << file->GetPath(); |
| 196 | return false; |
| 197 | } |
| 198 | // Readjust to be non-inclusive upper bound. |
| 199 | end_ += sizeof(uint32_t); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 200 | return Setup(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 201 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 202 | |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 203 | bool OatFile::Setup() { |
| 204 | if (!GetOatHeader().IsValid()) { |
| 205 | LOG(WARNING) << "Invalid oat magic for " << GetLocation(); |
| 206 | return false; |
| 207 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 208 | const byte* oat = Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 209 | oat += sizeof(OatHeader); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 210 | if (oat > End()) { |
| 211 | LOG(ERROR) << "In oat file " << GetLocation() << " found truncated OatHeader"; |
| 212 | return false; |
| 213 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 214 | |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 215 | oat += GetOatHeader().GetImageFileLocationSize(); |
| 216 | if (oat > End()) { |
| 217 | LOG(ERROR) << "In oat file " << GetLocation() << " found truncated image file location: " |
| 218 | << reinterpret_cast<const void*>(Begin()) |
| 219 | << "+" << sizeof(OatHeader) |
| 220 | << "+" << GetOatHeader().GetImageFileLocationSize() |
| 221 | << "<=" << reinterpret_cast<const void*>(End()); |
| 222 | return false; |
| 223 | } |
| 224 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 225 | for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 226 | size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 227 | if (dex_file_location_size == 0U) { |
| 228 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 229 | << " with empty location name"; |
| 230 | return false; |
| 231 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 232 | oat += sizeof(dex_file_location_size); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 233 | if (oat > End()) { |
| 234 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 235 | << " truncated after dex file location size"; |
| 236 | return false; |
| 237 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 238 | |
| 239 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 240 | oat += dex_file_location_size; |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 241 | if (oat > End()) { |
| 242 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 243 | << " with truncated dex file location"; |
| 244 | return false; |
| 245 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 246 | |
| 247 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 248 | |
| 249 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 250 | oat += sizeof(dex_file_checksum); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 251 | if (oat > End()) { |
| 252 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 253 | << " for "<< dex_file_location |
| 254 | << " truncated after dex file checksum"; |
| 255 | return false; |
| 256 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 257 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 258 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 259 | if (dex_file_offset == 0U) { |
| 260 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 261 | << " for "<< dex_file_location |
| 262 | << " with zero dex file offset"; |
| 263 | return false; |
| 264 | } |
| 265 | if (dex_file_offset > Size()) { |
| 266 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 267 | << " for "<< dex_file_location |
| 268 | << " with dex file offset" << dex_file_offset << " > " << Size(); |
| 269 | return false; |
| 270 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 271 | oat += sizeof(dex_file_offset); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 272 | if (oat > End()) { |
| 273 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 274 | << " for "<< dex_file_location |
| 275 | << " truncated after dex file offset"; |
| 276 | return false; |
| 277 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 278 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 279 | const uint8_t* dex_file_pointer = Begin() + dex_file_offset; |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 280 | if (!DexFile::IsMagicValid(dex_file_pointer)) { |
| 281 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 282 | << " for "<< dex_file_location |
| 283 | << " with invalid dex file magic: " << dex_file_pointer; |
| 284 | return false; |
| 285 | } |
| 286 | if (!DexFile::IsVersionValid(dex_file_pointer)) { |
| 287 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 288 | << " for "<< dex_file_location |
| 289 | << " with invalid dex file version: " << dex_file_pointer; |
| 290 | return false; |
| 291 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 292 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
| 293 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 294 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 295 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 296 | if (oat > End()) { |
| 297 | LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i |
| 298 | << " for "<< dex_file_location |
| 299 | << " with truncated method offsets"; |
| 300 | return false; |
| 301 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 302 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 303 | oat_dex_files_.Put(dex_file_location, new OatDexFile(this, |
| 304 | dex_file_location, |
| 305 | dex_file_checksum, |
| 306 | dex_file_pointer, |
| 307 | methods_offsets_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 308 | } |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 309 | return true; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 313 | return *reinterpret_cast<const OatHeader*>(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::Begin() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 317 | CHECK(begin_ != NULL); |
| 318 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 321 | const byte* OatFile::End() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 322 | CHECK(end_ != NULL); |
| 323 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame^] | 326 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_location, |
| 327 | const uint32_t* const dex_location_checksum, |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 328 | bool warn_if_not_found) const { |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame^] | 329 | Table::const_iterator it = oat_dex_files_.find(dex_location); |
| 330 | if (it != oat_dex_files_.end()) { |
| 331 | const OatFile::OatDexFile* oat_dex_file = it->second; |
| 332 | if (dex_location_checksum == NULL || |
| 333 | oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) { |
| 334 | return oat_dex_file; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (warn_if_not_found) { |
| 339 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location |
| 340 | << " in OatFile " << GetLocation(); |
| 341 | if (kIsDebugBuild) { |
| 342 | for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) { |
| 343 | LOG(WARNING) << "OatFile " << GetLocation() |
| 344 | << " contains OatDexFile " << it->second->GetDexFileLocation(); |
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 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 376 | const DexFile* OatFile::OatDexFile::OpenDexFile() const { |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 377 | return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 378 | dex_file_location_checksum_); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Ian Rogers | ee39a10 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 381 | const 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(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 386 | mirror::Class::Status status = *reinterpret_cast<const mirror::Class::Status*>(oat_class_pointer); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 387 | |
| 388 | const byte* methods_pointer = oat_class_pointer + sizeof(status); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 389 | CHECK_LT(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 390 | |
| 391 | return new OatClass(oat_file_, |
| 392 | status, |
| 393 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 396 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 397 | mirror::Class::Status status, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 398 | const OatMethodOffsets* methods_pointer) |
| 399 | : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 400 | |
| 401 | OatFile::OatClass::~OatClass() {} |
| 402 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 403 | mirror::Class::Status OatFile::OatClass::GetStatus() const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 404 | return status_; |
| 405 | } |
| 406 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 407 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 408 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index]; |
| 409 | return OatMethod( |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 410 | oat_file_->Begin(), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 411 | oat_method_offsets.code_offset_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 412 | oat_method_offsets.frame_size_in_bytes_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 413 | oat_method_offsets.core_spill_mask_, |
| 414 | oat_method_offsets.fp_spill_mask_, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 415 | oat_method_offsets.mapping_table_offset_, |
| 416 | oat_method_offsets.vmap_table_offset_, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 417 | oat_method_offsets.gc_map_offset_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 420 | OatFile::OatMethod::OatMethod(const byte* base, |
| 421 | const uint32_t code_offset, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 422 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 423 | const uint32_t core_spill_mask, |
| 424 | const uint32_t fp_spill_mask, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 425 | const uint32_t mapping_table_offset, |
| 426 | const uint32_t vmap_table_offset, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 427 | const uint32_t gc_map_offset) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 428 | : begin_(base), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 429 | code_offset_(code_offset), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 430 | frame_size_in_bytes_(frame_size_in_bytes), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 431 | core_spill_mask_(core_spill_mask), |
| 432 | fp_spill_mask_(fp_spill_mask), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 433 | mapping_table_offset_(mapping_table_offset), |
| 434 | vmap_table_offset_(vmap_table_offset), |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 435 | native_gc_map_offset_(gc_map_offset) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 436 | #ifndef NDEBUG |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 437 | if (mapping_table_offset_ != 0) { // implies non-native, non-stub code |
| 438 | if (vmap_table_offset_ == 0) { |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 439 | DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + |
| 440 | __builtin_popcount(fp_spill_mask_))); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 441 | } else { |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 442 | VmapTable vmap_table(reinterpret_cast<const uint8_t*>(begin_ + vmap_table_offset_)); |
| 443 | |
| 444 | DCHECK_EQ(vmap_table.Size(), static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + |
| 445 | __builtin_popcount(fp_spill_mask_))); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 446 | } |
| 447 | } else { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 448 | DCHECK_EQ(vmap_table_offset_, 0U); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 449 | } |
| 450 | #endif |
| 451 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 452 | |
| 453 | OatFile::OatMethod::~OatMethod() {} |
| 454 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 455 | const void* OatFile::OatMethod::GetCode() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 456 | return GetOatPointer<const void*>(code_offset_); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | uint32_t OatFile::OatMethod::GetCodeSize() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 460 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 461 | // TODO: With Quick, we store the size before the code. With |
| 462 | // Portable, the code is in a .o file we don't manage ourselves. ELF |
| 463 | // symbols do have a concept of size, so we could capture that and |
| 464 | // store it somewhere, such as the OatMethod. |
| 465 | return 0; |
| 466 | #else |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 467 | uintptr_t code = reinterpret_cast<uint32_t>(GetCode()); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 468 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 469 | if (code == 0) { |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 470 | return 0; |
| 471 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 472 | // TODO: make this Thumb2 specific |
| 473 | code &= ~0x1; |
| 474 | return reinterpret_cast<uint32_t*>(code)[-1]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 475 | #endif |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 476 | } |
| 477 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 478 | void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 479 | CHECK(method != NULL); |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 480 | method->SetEntryPointFromCompiledCode(GetCode()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 481 | method->SetFrameSizeInBytes(frame_size_in_bytes_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 482 | method->SetCoreSpillMask(core_spill_mask_); |
| 483 | method->SetFpSpillMask(fp_spill_mask_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 484 | method->SetMappingTable(GetMappingTable()); |
| 485 | method->SetVmapTable(GetVmapTable()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 486 | method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 489 | } // namespace art |