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" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/class.h" |
| 26 | #include "mirror/abstract_method.h" |
| 27 | #include "mirror/abstract_method-inl.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" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 34 | std::string OatFile::DexFilenameToOatFilename(const std::string& location) { |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 35 | CHECK(IsValidDexFilename(location) || IsValidZipFilename(location)); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 36 | std::string oat_location(location); |
| 37 | oat_location += ".oat"; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 38 | return oat_location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 41 | void OatFile::CheckLocation(const std::string& location) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 42 | CHECK(!location.empty()); |
| 43 | if (!IsValidOatFilename(location)) { |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 44 | LOG(WARNING) << "Attempting to open oat file with unknown extension '" << location << "'"; |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 45 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 48 | OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents, |
| 49 | const std::string& location) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 50 | CHECK(!oat_contents.empty()) << location; |
| 51 | CheckLocation(location); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 52 | UniquePtr<OatFile> oat_file(new OatFile(location)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 53 | oat_file->begin_ = &oat_contents[0]; |
| 54 | oat_file->end_ = &oat_contents[oat_contents.size()]; |
| 55 | oat_file->Setup(); |
| 56 | return oat_file.release(); |
| 57 | } |
| 58 | |
| 59 | OatFile* OatFile::Open(const std::string& filename, |
| 60 | const std::string& location, |
| 61 | byte* requested_base) { |
| 62 | CHECK(!filename.empty()) << location; |
| 63 | CheckLocation(location); |
Jeff Hao | f9e609f | 2013-02-15 16:36:29 -0800 | [diff] [blame] | 64 | /* |
| 65 | * TODO: Reenable dlopen when it works again on MIPS. It may have broken from this change: |
| 66 | * commit 818d98eb563ad5d7293b8b5c40f3dabf745e611f |
| 67 | * Author: Brian Carlstrom <bdc@google.com> |
| 68 | * Date: Sun Feb 10 21:38:12 2013 -0800 |
| 69 | * |
| 70 | * Fix MIPS to use standard kPageSize=0x1000 section alignment for ELF sections |
| 71 | * |
| 72 | * Change-Id: I905f0c5f75921a65bd7426a54d6258c780d85d0e |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 73 | */ |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 74 | OatFile* result = OpenDlopen(filename, location, requested_base); |
| 75 | if (result != NULL) { |
| 76 | return result; |
| 77 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 78 | // On target, only used dlopen to load. |
| 79 | if (kIsTargetBuild) { |
| 80 | return NULL; |
| 81 | } |
| 82 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
| 83 | // This won't work for portable runtime execution because it doesn't process relocations. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 84 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false, false)); |
| 85 | if (file.get() == NULL) { |
| 86 | return NULL; |
| 87 | } |
| 88 | return OpenElfFile(file.get(), location, requested_base, false); |
| 89 | } |
| 90 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 91 | OatFile* OatFile::OpenWritable(File* file, const std::string& location) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 92 | CheckLocation(location); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 93 | return OpenElfFile(file, location, NULL, true); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | OatFile* OatFile::OpenDlopen(const std::string& elf_filename, |
| 97 | const std::string& location, |
| 98 | byte* requested_base) { |
| 99 | UniquePtr<OatFile> oat_file(new OatFile(location)); |
| 100 | bool success = oat_file->Dlopen(elf_filename, requested_base); |
| 101 | if (!success) { |
| 102 | return NULL; |
| 103 | } |
| 104 | return oat_file.release(); |
| 105 | } |
| 106 | |
| 107 | OatFile* OatFile::OpenElfFile(File* file, |
| 108 | const std::string& location, |
| 109 | byte* requested_base, |
| 110 | bool writable) { |
| 111 | UniquePtr<OatFile> oat_file(new OatFile(location)); |
| 112 | bool success = oat_file->ElfFileOpen(file, requested_base, writable); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 113 | if (!success) { |
| 114 | return NULL; |
| 115 | } |
| 116 | return oat_file.release(); |
| 117 | } |
| 118 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 119 | OatFile::OatFile(const std::string& location) |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 120 | : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 121 | CHECK(!location_.empty()); |
| 122 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 123 | |
| 124 | OatFile::~OatFile() { |
| 125 | STLDeleteValues(&oat_dex_files_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 126 | if (dlopen_handle_ != NULL) { |
| 127 | dlclose(dlopen_handle_); |
| 128 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 131 | bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base) { |
| 132 | |
| 133 | char* absolute_path = realpath(elf_filename.c_str(), NULL); |
| 134 | if (absolute_path == NULL) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 137 | dlopen_handle_ = dlopen(absolute_path, RTLD_NOW); |
| 138 | free(absolute_path); |
| 139 | if (dlopen_handle_ == NULL) { |
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); |
| 163 | Setup(); |
| 164 | return true; |
| 165 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 166 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 167 | bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable) { |
| 168 | elf_file_.reset(ElfFile::Open(file, writable, true)); |
| 169 | if (elf_file_.get() == NULL) { |
| 170 | PLOG(WARNING) << "Failed to create ELF file for " << file->GetPath(); |
| 171 | return false; |
| 172 | } |
| 173 | bool loaded = elf_file_->Load(); |
| 174 | if (!loaded) { |
| 175 | LOG(WARNING) << "Failed to load ELF file " << file->GetPath(); |
| 176 | return false; |
| 177 | } |
| 178 | begin_ = elf_file_->FindDynamicSymbolAddress("oatdata"); |
| 179 | if (begin_ == NULL) { |
| 180 | LOG(WARNING) << "Failed to find oatdata symbol in " << file->GetPath(); |
| 181 | return false; |
| 182 | } |
| 183 | if (requested_base != NULL && begin_ != requested_base) { |
| 184 | std::string maps; |
| 185 | ReadFileToString("/proc/self/maps", &maps); |
| 186 | LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata=" |
| 187 | << reinterpret_cast<const void*>(begin_) << " != expected=" |
| 188 | << reinterpret_cast<const void*>(requested_base) |
| 189 | << " /proc/self/maps:\n" << maps; |
| 190 | return false; |
| 191 | } |
| 192 | end_ = elf_file_->FindDynamicSymbolAddress("oatlastword"); |
| 193 | if (end_ == NULL) { |
| 194 | LOG(WARNING) << "Failed to find oatlastword symbol in " << file->GetPath(); |
| 195 | return false; |
| 196 | } |
| 197 | // Readjust to be non-inclusive upper bound. |
| 198 | end_ += sizeof(uint32_t); |
| 199 | Setup(); |
| 200 | return true; |
| 201 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 202 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 203 | void OatFile::Setup() { |
| 204 | const byte* oat = Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 205 | oat += sizeof(OatHeader); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 206 | oat += GetOatHeader().GetImageFileLocationSize(); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 207 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 208 | CHECK_LE(oat, End()) |
| 209 | << reinterpret_cast<const void*>(Begin()) |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 210 | << "+" << sizeof(OatHeader) |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 211 | << "+" << GetOatHeader().GetImageFileLocationSize() |
| 212 | << "<=" << reinterpret_cast<const void*>(End()) |
| 213 | << " " << GetLocation(); |
| 214 | for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 215 | size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 216 | CHECK_GT(dex_file_location_size, 0U) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 217 | oat += sizeof(dex_file_location_size); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 218 | CHECK_LT(oat, End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 219 | |
| 220 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 221 | oat += dex_file_location_size; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 222 | CHECK_LT(oat, End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 223 | |
| 224 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 225 | |
| 226 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 227 | oat += sizeof(dex_file_checksum); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 228 | CHECK_LT(oat, End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 229 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 230 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 231 | CHECK_GT(dex_file_offset, 0U) << GetLocation(); |
| 232 | CHECK_LT(dex_file_offset, Size()) << GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 233 | oat += sizeof(dex_file_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 234 | CHECK_LT(oat, End()) << GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 235 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 236 | const uint8_t* dex_file_pointer = Begin() + dex_file_offset; |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 237 | CHECK(DexFile::IsMagicValid(dex_file_pointer)) |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 238 | << GetLocation() << " " << dex_file_pointer; |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 239 | CHECK(DexFile::IsVersionValid(dex_file_pointer)) |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 240 | << GetLocation() << " " << dex_file_pointer; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 241 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
| 242 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 243 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 244 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 245 | CHECK_LE(oat, End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 246 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 247 | oat_dex_files_.Put(dex_file_location, new OatDexFile(this, |
| 248 | dex_file_location, |
| 249 | dex_file_checksum, |
| 250 | dex_file_pointer, |
| 251 | methods_offsets_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 252 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 256 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 259 | const byte* OatFile::Begin() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 260 | CHECK(begin_ != NULL); |
| 261 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 264 | const byte* OatFile::End() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 265 | CHECK(end_ != NULL); |
| 266 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 269 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location, |
| 270 | bool warn_if_not_found) const { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 271 | Table::const_iterator it = oat_dex_files_.find(dex_file_location); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 272 | if (it == oat_dex_files_.end()) { |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 273 | if (warn_if_not_found) { |
| 274 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location; |
| 275 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 276 | return NULL; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 277 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 278 | return it->second; |
| 279 | } |
| 280 | |
| 281 | std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const { |
| 282 | std::vector<const OatFile::OatDexFile*> result; |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 283 | 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] | 284 | result.push_back(it->second); |
| 285 | } |
| 286 | return result; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 290 | const std::string& dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 291 | uint32_t dex_file_location_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 292 | const byte* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 293 | const uint32_t* oat_class_offsets_pointer) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 294 | : oat_file_(oat_file), |
| 295 | dex_file_location_(dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 296 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 297 | dex_file_pointer_(dex_file_pointer), |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 298 | oat_class_offsets_pointer_(oat_class_offsets_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 299 | |
| 300 | OatFile::OatDexFile::~OatDexFile() {} |
| 301 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 302 | size_t OatFile::OatDexFile::FileSize() const { |
| 303 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 304 | } |
| 305 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 306 | const DexFile* OatFile::OatDexFile::OpenDexFile() const { |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 307 | return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 308 | dex_file_location_checksum_); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 311 | const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 312 | uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index]; |
| 313 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 314 | const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
| 315 | CHECK_LT(oat_class_pointer, oat_file_->End()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 316 | 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] | 317 | |
| 318 | const byte* methods_pointer = oat_class_pointer + sizeof(status); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 319 | CHECK_LT(methods_pointer, oat_file_->End()); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 320 | |
| 321 | return new OatClass(oat_file_, |
| 322 | status, |
| 323 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 326 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 327 | mirror::Class::Status status, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 328 | const OatMethodOffsets* methods_pointer) |
| 329 | : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 330 | |
| 331 | OatFile::OatClass::~OatClass() {} |
| 332 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 333 | mirror::Class::Status OatFile::OatClass::GetStatus() const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 334 | return status_; |
| 335 | } |
| 336 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 337 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 338 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index]; |
| 339 | return OatMethod( |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 340 | oat_file_->Begin(), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 341 | oat_method_offsets.code_offset_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 342 | oat_method_offsets.frame_size_in_bytes_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 343 | oat_method_offsets.core_spill_mask_, |
| 344 | oat_method_offsets.fp_spill_mask_, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 345 | oat_method_offsets.mapping_table_offset_, |
| 346 | oat_method_offsets.vmap_table_offset_, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 347 | oat_method_offsets.gc_map_offset_, |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 348 | oat_method_offsets.invoke_stub_offset_ |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 349 | #if defined(ART_USE_PORTABLE_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 350 | , oat_method_offsets.proxy_stub_offset_ |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 351 | #endif |
| 352 | ); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 355 | OatFile::OatMethod::OatMethod(const byte* base, |
| 356 | const uint32_t code_offset, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 357 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 358 | const uint32_t core_spill_mask, |
| 359 | const uint32_t fp_spill_mask, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 360 | const uint32_t mapping_table_offset, |
| 361 | const uint32_t vmap_table_offset, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 362 | const uint32_t gc_map_offset, |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 363 | const uint32_t invoke_stub_offset |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 364 | #if defined(ART_USE_PORTABLE_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 365 | , const uint32_t proxy_stub_offset |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 366 | #endif |
| 367 | ) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 368 | : begin_(base), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 369 | code_offset_(code_offset), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 370 | frame_size_in_bytes_(frame_size_in_bytes), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 371 | core_spill_mask_(core_spill_mask), |
| 372 | fp_spill_mask_(fp_spill_mask), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 373 | mapping_table_offset_(mapping_table_offset), |
| 374 | vmap_table_offset_(vmap_table_offset), |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 375 | native_gc_map_offset_(gc_map_offset), |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 376 | invoke_stub_offset_(invoke_stub_offset) |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 377 | #if defined(ART_USE_PORTABLE_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 378 | , proxy_stub_offset_(proxy_stub_offset) |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 379 | #endif |
| 380 | { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 381 | #ifndef NDEBUG |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 382 | if (mapping_table_offset_ != 0) { // implies non-native, non-stub code |
| 383 | if (vmap_table_offset_ == 0) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 384 | DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 385 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 386 | const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 387 | DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 388 | } |
| 389 | } else { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 390 | DCHECK_EQ(vmap_table_offset_, 0U); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 391 | } |
| 392 | #endif |
| 393 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 394 | |
| 395 | OatFile::OatMethod::~OatMethod() {} |
| 396 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 397 | const void* OatFile::OatMethod::GetCode() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 398 | return GetOatPointer<const void*>(code_offset_); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | uint32_t OatFile::OatMethod::GetCodeSize() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 402 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 403 | // TODO: With Quick, we store the size before the code. With |
| 404 | // Portable, the code is in a .o file we don't manage ourselves. ELF |
| 405 | // symbols do have a concept of size, so we could capture that and |
| 406 | // store it somewhere, such as the OatMethod. |
| 407 | return 0; |
| 408 | #else |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 409 | uintptr_t code = reinterpret_cast<uint32_t>(GetCode()); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 410 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 411 | if (code == 0) { |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 412 | return 0; |
| 413 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 414 | // TODO: make this Thumb2 specific |
| 415 | code &= ~0x1; |
| 416 | return reinterpret_cast<uint32_t*>(code)[-1]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 417 | #endif |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 418 | } |
| 419 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 420 | mirror::AbstractMethod::InvokeStub* OatFile::OatMethod::GetInvokeStub() const { |
Ian Rogers | 1b09b09 | 2012-08-20 15:35:52 -0700 | [diff] [blame] | 421 | const byte* stub = GetOatPointer<const byte*>(invoke_stub_offset_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 422 | return reinterpret_cast<mirror::AbstractMethod::InvokeStub*>(const_cast<byte*>(stub)); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | uint32_t OatFile::OatMethod::GetInvokeStubSize() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 426 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 427 | return 0; |
| 428 | #else |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 429 | uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub()); |
| 430 | if (code == 0) { |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 431 | return 0; |
| 432 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 433 | // TODO: make this Thumb2 specific |
| 434 | code &= ~0x1; |
| 435 | return reinterpret_cast<uint32_t*>(code)[-1]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 436 | #endif |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 437 | } |
| 438 | |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 439 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 440 | const void* OatFile::OatMethod::GetProxyStub() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 441 | return GetOatPointer<const void*>(proxy_stub_offset_); |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 442 | } |
| 443 | #endif |
| 444 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 445 | void OatFile::OatMethod::LinkMethod(mirror::AbstractMethod* method) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 446 | CHECK(method != NULL); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 447 | method->SetCode(GetCode()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 448 | method->SetFrameSizeInBytes(frame_size_in_bytes_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 449 | method->SetCoreSpillMask(core_spill_mask_); |
| 450 | method->SetFpSpillMask(fp_spill_mask_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 451 | method->SetMappingTable(GetMappingTable()); |
| 452 | method->SetVmapTable(GetVmapTable()); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 453 | method->SetNativeGcMap(GetNativeGcMap()); // Note, used by native methods in work around JNI mode. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 454 | method->SetInvokeStub(GetInvokeStub()); |
| 455 | } |
| 456 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 457 | } // namespace art |