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> |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 20 | #include <string.h> |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 22 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 23 | #include <cstdlib> |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 24 | #ifndef __APPLE__ |
| 25 | #include <link.h> // for dl_iterate_phdr. |
| 26 | #endif |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 27 | #include <sstream> |
| 28 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 29 | // dlopen_ext support from bionic. |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 30 | #ifdef __ANDROID__ |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 31 | #include "android/dlext.h" |
| 32 | #endif |
| 33 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | #include "art_method-inl.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 35 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 36 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 37 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 38 | #include "elf_file.h" |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 39 | #include "elf_utils.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 40 | #include "oat.h" |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 41 | #include "mem_map.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | #include "mirror/class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 43 | #include "mirror/object-inl.h" |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 44 | #include "oat_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 45 | #include "oat_file_manager.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 46 | #include "os.h" |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 47 | #include "runtime.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | #include "utils.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame^] | 49 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 50 | #include "vmap_table.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | |
| 52 | namespace art { |
| 53 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 54 | // Whether OatFile::Open will try DlOpen() first. Fallback is our own ELF loader. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 55 | static constexpr bool kUseDlopen = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 56 | |
| 57 | // Whether OatFile::Open will try DlOpen() on the host. On the host we're not linking against |
| 58 | // bionic, so cannot take advantage of the support for changed semantics (loading the same soname |
| 59 | // multiple times). However, if/when we switch the above, we likely want to switch this, too, |
| 60 | // to get test coverage of the code paths. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 61 | static constexpr bool kUseDlopenOnHost = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 62 | |
| 63 | // For debugging, Open will print DlOpen error message if set to true. |
| 64 | static constexpr bool kPrintDlOpenErrorMessage = false; |
| 65 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 66 | std::string OatFile::ResolveRelativeEncodedDexLocation( |
| 67 | const char* abs_dex_location, const std::string& rel_dex_location) { |
| 68 | if (abs_dex_location != nullptr && rel_dex_location[0] != '/') { |
| 69 | // Strip :classes<N>.dex used for secondary multidex files. |
| 70 | std::string base = DexFile::GetBaseLocation(rel_dex_location); |
| 71 | std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location); |
| 72 | |
| 73 | // Check if the base is a suffix of the provided abs_dex_location. |
| 74 | std::string target_suffix = "/" + base; |
| 75 | std::string abs_location(abs_dex_location); |
| 76 | if (abs_location.size() > target_suffix.size()) { |
| 77 | size_t pos = abs_location.size() - target_suffix.size(); |
| 78 | if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) { |
| 79 | return abs_location + multidex_suffix; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return rel_dex_location; |
| 84 | } |
| 85 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 86 | void OatFile::CheckLocation(const std::string& location) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 87 | CHECK(!location.empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 90 | OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file, |
| 91 | const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 92 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 93 | std::string* error_msg) { |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 94 | std::unique_ptr<OatFile> oat_file(new OatFile(location, false)); |
| 95 | oat_file->elf_file_.reset(elf_file); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 96 | uint64_t offset, size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 97 | bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 98 | CHECK(has_section); |
| 99 | oat_file->begin_ = elf_file->Begin() + offset; |
| 100 | oat_file->end_ = elf_file->Begin() + size + offset; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 101 | // Ignore the optional .bss section when opening non-executable. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 102 | return oat_file->Setup(abs_dex_location, error_msg) ? oat_file.release() : nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | OatFile* OatFile::Open(const std::string& filename, |
| 106 | const std::string& location, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 107 | uint8_t* requested_base, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 108 | uint8_t* oat_file_begin, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 109 | bool executable, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 110 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 111 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 112 | CHECK(!filename.empty()) << location; |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 113 | CheckLocation(location); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 114 | std::unique_ptr<OatFile> ret; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 115 | |
| 116 | // Use dlopen only when flagged to do so, and when it's OK to load things executable. |
| 117 | // TODO: Also try when not executable? The issue here could be re-mapping as writable (as |
| 118 | // !executable is a sign that we may want to patch), which may not be allowed for |
| 119 | // various reasons. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 120 | // dlopen always returns the same library if it is already opened on the host. For this reason |
| 121 | // we only use dlopen if we are the target or we do not already have the dex file opened. Having |
| 122 | // the same library loaded multiple times at different addresses is required for class unloading |
| 123 | // and for having dex caches arrays in the .bss section. |
| 124 | Runtime* const runtime = Runtime::Current(); |
| 125 | OatFileManager* const manager = (runtime != nullptr) ? &runtime->GetOatFileManager() : nullptr; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 126 | if (kUseDlopen && executable) { |
| 127 | bool success = kIsTargetBuild; |
| 128 | bool reserved_location = false; |
| 129 | // Manager may be null if we are running without a runtime. |
| 130 | if (!success && kUseDlopenOnHost && manager != nullptr) { |
Mathieu Chartier | 24a0fc8 | 2015-10-13 16:38:52 -0700 | [diff] [blame] | 131 | // RegisterOatFileLocation returns false if we are not the first caller to register that |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 132 | // location. |
| 133 | reserved_location = manager->RegisterOatFileLocation(location); |
| 134 | success = reserved_location; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 135 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 136 | if (success) { |
| 137 | // Try to use dlopen. This may fail for various reasons, outlined below. We try dlopen, as |
| 138 | // this will register the oat file with the linker and allows libunwind to find our info. |
| 139 | ret.reset(OpenDlopen(filename, location, requested_base, abs_dex_location, error_msg)); |
| 140 | if (reserved_location) { |
| 141 | manager->UnRegisterOatFileLocation(location); |
| 142 | } |
| 143 | if (ret != nullptr) { |
| 144 | return ret.release(); |
| 145 | } |
| 146 | if (kPrintDlOpenErrorMessage) { |
| 147 | LOG(ERROR) << "Failed to dlopen: " << *error_msg; |
| 148 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 152 | // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: |
| 153 | // |
| 154 | // On target, dlopen may fail when compiling due to selinux restrictions on installd. |
| 155 | // |
| 156 | // We use our own ELF loader for Quick to deal with legacy apps that |
| 157 | // open a generated dex file by name, remove the file, then open |
| 158 | // another generated dex file with the same name. http://b/10614658 |
| 159 | // |
| 160 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 161 | // |
| 162 | // |
| 163 | // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually |
| 164 | // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 165 | std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str())); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 166 | if (file == nullptr) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 167 | *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); |
| 168 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 169 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 170 | ret.reset(OpenElfFile(file.get(), location, requested_base, oat_file_begin, false, executable, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 171 | abs_dex_location, error_msg)); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 172 | |
| 173 | // It would be nice to unlink here. But we might have opened the file created by the |
| 174 | // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API |
| 175 | // to allow removal when we know the ELF must be borked. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 176 | return ret.release(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 179 | OatFile* OatFile::OpenWritable(File* file, const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 180 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 181 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 182 | CheckLocation(location); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 183 | return OpenElfFile(file, location, nullptr, nullptr, true, false, abs_dex_location, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 186 | OatFile* OatFile::OpenReadable(File* file, const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 187 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 188 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 189 | CheckLocation(location); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 190 | return OpenElfFile(file, location, nullptr, nullptr, false, false, abs_dex_location, error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 193 | OatFile* OatFile::OpenDlopen(const std::string& elf_filename, |
| 194 | const std::string& location, |
| 195 | uint8_t* requested_base, |
| 196 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 197 | std::string* error_msg) { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 198 | std::unique_ptr<OatFile> oat_file(new OatFile(location, true)); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 199 | bool success = oat_file->Dlopen(elf_filename, requested_base, abs_dex_location, error_msg); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 200 | if (!success) { |
| 201 | return nullptr; |
| 202 | } |
| 203 | return oat_file.release(); |
| 204 | } |
| 205 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 206 | OatFile* OatFile::OpenElfFile(File* file, |
| 207 | const std::string& location, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 208 | uint8_t* requested_base, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 209 | uint8_t* oat_file_begin, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 210 | bool writable, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 211 | bool executable, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 212 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 213 | std::string* error_msg) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 214 | std::unique_ptr<OatFile> oat_file(new OatFile(location, executable)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 215 | bool success = oat_file->ElfFileOpen(file, requested_base, oat_file_begin, writable, executable, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 216 | abs_dex_location, error_msg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 217 | if (!success) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 218 | CHECK(!error_msg->empty()); |
| 219 | return nullptr; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 220 | } |
| 221 | return oat_file.release(); |
| 222 | } |
| 223 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 224 | OatFile::OatFile(const std::string& location, bool is_executable) |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 225 | : location_(location), begin_(nullptr), end_(nullptr), bss_begin_(nullptr), bss_end_(nullptr), |
| 226 | is_executable_(is_executable), dlopen_handle_(nullptr), |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 227 | secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 228 | CHECK(!location_.empty()); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 229 | Runtime* const runtime = Runtime::Current(); |
| 230 | if (runtime != nullptr && !runtime->IsAotCompiler()) { |
| 231 | runtime->GetOatFileManager().RegisterOatFileLocation(location); |
| 232 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 233 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 234 | |
| 235 | OatFile::~OatFile() { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 236 | STLDeleteElements(&oat_dex_files_storage_); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 237 | if (dlopen_handle_ != nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 238 | dlclose(dlopen_handle_); |
| 239 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 240 | Runtime* const runtime = Runtime::Current(); |
| 241 | if (runtime != nullptr && !runtime->IsAotCompiler()) { |
| 242 | runtime->GetOatFileManager().UnRegisterOatFileLocation(location_); |
| 243 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 246 | bool OatFile::Dlopen(const std::string& elf_filename, uint8_t* requested_base, |
| 247 | const char* abs_dex_location, std::string* error_msg) { |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 248 | #ifdef __APPLE__ |
| 249 | // The dl_iterate_phdr syscall is missing. There is similar API on OSX, |
| 250 | // but let's fallback to the custom loading code for the time being. |
| 251 | UNUSED(elf_filename); |
| 252 | UNUSED(requested_base); |
| 253 | UNUSED(abs_dex_location); |
| 254 | UNUSED(error_msg); |
| 255 | return false; |
| 256 | #else |
Vladimir Marko | 6958e4f | 2015-09-17 20:22:02 +0100 | [diff] [blame] | 257 | { |
| 258 | UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr)); |
| 259 | if (absolute_path == nullptr) { |
| 260 | *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); |
| 261 | return false; |
| 262 | } |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 263 | #ifdef __ANDROID__ |
Vladimir Marko | 6958e4f | 2015-09-17 20:22:02 +0100 | [diff] [blame] | 264 | android_dlextinfo extinfo; |
| 265 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | ANDROID_DLEXT_FORCE_FIXED_VADDR; |
| 266 | dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 267 | #else |
Vladimir Marko | 6958e4f | 2015-09-17 20:22:02 +0100 | [diff] [blame] | 268 | dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 269 | #endif |
Vladimir Marko | 6958e4f | 2015-09-17 20:22:02 +0100 | [diff] [blame] | 270 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 271 | if (dlopen_handle_ == nullptr) { |
| 272 | *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); |
| 273 | return false; |
| 274 | } |
| 275 | begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatdata")); |
| 276 | if (begin_ == nullptr) { |
| 277 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(), |
| 278 | dlerror()); |
| 279 | return false; |
| 280 | } |
| 281 | if (requested_base != nullptr && begin_ != requested_base) { |
| 282 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 283 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
| 284 | "oatdata=%p != expected=%p, %s. See process maps in the log.", |
| 285 | begin_, requested_base, elf_filename.c_str()); |
| 286 | return false; |
| 287 | } |
| 288 | end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatlastword")); |
| 289 | if (end_ == nullptr) { |
| 290 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(), |
| 291 | dlerror()); |
| 292 | return false; |
| 293 | } |
| 294 | // Readjust to be non-inclusive upper bound. |
| 295 | end_ += sizeof(uint32_t); |
| 296 | |
| 297 | bss_begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbss")); |
| 298 | if (bss_begin_ == nullptr) { |
| 299 | // No .bss section. Clear dlerror(). |
| 300 | bss_end_ = nullptr; |
| 301 | dlerror(); |
| 302 | } else { |
| 303 | bss_end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbsslastword")); |
| 304 | if (bss_end_ == nullptr) { |
| 305 | *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", |
| 306 | elf_filename.c_str()); |
| 307 | return false; |
| 308 | } |
| 309 | // Readjust to be non-inclusive upper bound. |
| 310 | bss_end_ += sizeof(uint32_t); |
| 311 | } |
| 312 | |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 313 | // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions. |
| 314 | struct dl_iterate_context { |
| 315 | static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) { |
| 316 | auto* context = reinterpret_cast<dl_iterate_context*>(data); |
David Srbecky | aa03870 | 2015-06-17 02:11:07 +0100 | [diff] [blame] | 317 | // See whether this callback corresponds to the file which we have just loaded. |
| 318 | bool contains_begin = false; |
| 319 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 320 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 321 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 322 | info->dlpi_phdr[i].p_vaddr); |
| 323 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
| 324 | if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) { |
| 325 | contains_begin = true; |
| 326 | break; |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
David Srbecky | aa03870 | 2015-06-17 02:11:07 +0100 | [diff] [blame] | 330 | // Add dummy mmaps for this file. |
| 331 | if (contains_begin) { |
| 332 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 333 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 334 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 335 | info->dlpi_phdr[i].p_vaddr); |
| 336 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
David Srbecky | 82e73dc | 2015-06-17 18:36:23 +0100 | [diff] [blame] | 337 | MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz); |
| 338 | context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap)); |
David Srbecky | aa03870 | 2015-06-17 02:11:07 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | return 1; // Stop iteration and return 1 from dl_iterate_phdr. |
| 342 | } |
| 343 | return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 344 | } |
David Srbecky | 82e73dc | 2015-06-17 18:36:23 +0100 | [diff] [blame] | 345 | const uint8_t* const begin_; |
| 346 | std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_; |
| 347 | } context = { begin_, &dlopen_mmaps_ }; |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 348 | |
David Srbecky | aa03870 | 2015-06-17 02:11:07 +0100 | [diff] [blame] | 349 | if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) { |
David Srbecky | 5dedb80 | 2015-06-17 00:08:02 +0100 | [diff] [blame] | 350 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 351 | LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but can not find its mmaps."; |
| 352 | } |
| 353 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 354 | return Setup(abs_dex_location, error_msg); |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 355 | #endif // __APPLE__ |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 358 | bool OatFile::ElfFileOpen(File* file, uint8_t* requested_base, uint8_t* oat_file_begin, |
| 359 | bool writable, bool executable, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 360 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 361 | std::string* error_msg) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 362 | // TODO: rename requested_base to oat_data_begin |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 363 | elf_file_.reset(ElfFile::Open(file, writable, /*program_header_only*/true, error_msg, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 364 | oat_file_begin)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 365 | if (elf_file_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 366 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 367 | return false; |
| 368 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 369 | bool loaded = elf_file_->Load(executable, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 370 | if (!loaded) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 371 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | begin_ = elf_file_->FindDynamicSymbolAddress("oatdata"); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 375 | if (begin_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 376 | *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] | 377 | return false; |
| 378 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 379 | if (requested_base != nullptr && begin_ != requested_base) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 380 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 381 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 382 | "oatdata=%p != expected=%p. See process maps in the log.", |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 383 | begin_, requested_base); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | end_ = elf_file_->FindDynamicSymbolAddress("oatlastword"); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 387 | if (end_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 388 | *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] | 389 | return false; |
| 390 | } |
| 391 | // Readjust to be non-inclusive upper bound. |
| 392 | end_ += sizeof(uint32_t); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 393 | |
| 394 | bss_begin_ = elf_file_->FindDynamicSymbolAddress("oatbss"); |
| 395 | if (bss_begin_ == nullptr) { |
| 396 | // No .bss section. Clear dlerror(). |
| 397 | bss_end_ = nullptr; |
| 398 | dlerror(); |
| 399 | } else { |
| 400 | bss_end_ = elf_file_->FindDynamicSymbolAddress("oatbsslastword"); |
| 401 | if (bss_end_ == nullptr) { |
| 402 | *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", |
| 403 | file->GetPath().c_str()); |
| 404 | return false; |
| 405 | } |
| 406 | // Readjust to be non-inclusive upper bound. |
| 407 | bss_end_ += sizeof(uint32_t); |
| 408 | } |
| 409 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 410 | return Setup(abs_dex_location, error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 411 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 412 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 413 | bool OatFile::Setup(const char* abs_dex_location, std::string* error_msg) { |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 414 | if (!GetOatHeader().IsValid()) { |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 415 | std::string cause = GetOatHeader().GetValidationErrorMessage(); |
| 416 | *error_msg = StringPrintf("Invalid oat header for '%s': %s", GetLocation().c_str(), |
| 417 | cause.c_str()); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 418 | return false; |
| 419 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 420 | const uint8_t* oat = Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 421 | oat += sizeof(OatHeader); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 422 | if (oat > End()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 423 | *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] | 424 | return false; |
| 425 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 426 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 427 | oat += GetOatHeader().GetKeyValueStoreSize(); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 428 | if (oat > End()) { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 429 | *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: " |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 430 | "%p + %zd + %ud <= %p", GetLocation().c_str(), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 431 | Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 432 | End()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame^] | 436 | size_t pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet()); |
| 437 | const uint8_t* dex_cache_arrays = bss_begin_; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 438 | uint32_t dex_file_count = GetOatHeader().GetDexFileCount(); |
| 439 | oat_dex_files_storage_.reserve(dex_file_count); |
| 440 | for (size_t i = 0; i < dex_file_count; i++) { |
Dmitry Petrochenko | 83bef92 | 2014-02-03 12:34:59 +0700 | [diff] [blame] | 441 | uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 442 | if (UNLIKELY(dex_file_location_size == 0U)) { |
| 443 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name", |
| 444 | GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 445 | return false; |
| 446 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 447 | oat += sizeof(dex_file_location_size); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 448 | if (UNLIKELY(oat > End())) { |
| 449 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file " |
| 450 | "location size", GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 451 | return false; |
| 452 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 453 | |
| 454 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 455 | oat += dex_file_location_size; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 456 | if (UNLIKELY(oat > End())) { |
| 457 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file " |
| 458 | "location", GetLocation().c_str(), i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 459 | return false; |
| 460 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 461 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 462 | std::string dex_file_location = ResolveRelativeEncodedDexLocation( |
| 463 | abs_dex_location, |
| 464 | std::string(dex_file_location_data, dex_file_location_size)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 465 | |
| 466 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 467 | oat += sizeof(dex_file_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 468 | if (UNLIKELY(oat > End())) { |
| 469 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after " |
| 470 | "dex file checksum", GetLocation().c_str(), i, |
| 471 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 472 | return false; |
| 473 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 474 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 475 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 476 | if (UNLIKELY(dex_file_offset == 0U)) { |
| 477 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex " |
| 478 | "file offset", GetLocation().c_str(), i, dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 479 | return false; |
| 480 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 481 | if (UNLIKELY(dex_file_offset > Size())) { |
| 482 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file " |
| 483 | "offset %ud > %zd", GetLocation().c_str(), i, |
| 484 | dex_file_location.c_str(), dex_file_offset, Size()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 485 | return false; |
| 486 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 487 | oat += sizeof(dex_file_offset); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 488 | if (UNLIKELY(oat > End())) { |
| 489 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " |
Brian Carlstrom | 37628b7 | 2014-10-28 13:54:26 -0700 | [diff] [blame] | 490 | "after dex file offsets", GetLocation().c_str(), i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 491 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 492 | return false; |
| 493 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 494 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 495 | const uint8_t* dex_file_pointer = Begin() + dex_file_offset; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 496 | if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) { |
| 497 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid " |
Brian Carlstrom | 37628b7 | 2014-10-28 13:54:26 -0700 | [diff] [blame] | 498 | "dex file magic '%s'", GetLocation().c_str(), i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 499 | dex_file_location.c_str(), dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 500 | return false; |
| 501 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 502 | if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) { |
| 503 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid " |
Brian Carlstrom | 37628b7 | 2014-10-28 13:54:26 -0700 | [diff] [blame] | 504 | "dex file version '%s'", GetLocation().c_str(), i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 505 | dex_file_location.c_str(), dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 506 | return false; |
| 507 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 508 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
| 509 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 510 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 511 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 512 | if (UNLIKELY(oat > End())) { |
| 513 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated " |
Brian Carlstrom | 37628b7 | 2014-10-28 13:54:26 -0700 | [diff] [blame] | 514 | "method offsets", GetLocation().c_str(), i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 515 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 516 | return false; |
| 517 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 518 | |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame^] | 519 | const uint8_t* current_dex_cache_arrays = nullptr; |
| 520 | if (dex_cache_arrays != nullptr) { |
| 521 | DexCacheArraysLayout layout(pointer_size, *header); |
| 522 | if (layout.Size() != 0u) { |
| 523 | if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) { |
| 524 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with " |
| 525 | "truncated dex cache arrays, %zd < %zd.", |
| 526 | GetLocation().c_str(), i, dex_file_location.c_str(), |
| 527 | static_cast<size_t>(bss_end_ - dex_cache_arrays), layout.Size()); |
| 528 | return false; |
| 529 | } |
| 530 | current_dex_cache_arrays = dex_cache_arrays; |
| 531 | dex_cache_arrays += layout.Size(); |
| 532 | } |
| 533 | } |
| 534 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 535 | std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str()); |
| 536 | |
| 537 | // Create the OatDexFile and add it to the owning container. |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 538 | OatDexFile* oat_dex_file = new OatDexFile(this, |
| 539 | dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 540 | canonical_location, |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 541 | dex_file_checksum, |
| 542 | dex_file_pointer, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame^] | 543 | methods_offsets_pointer, |
| 544 | current_dex_cache_arrays); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 545 | oat_dex_files_storage_.push_back(oat_dex_file); |
| 546 | |
| 547 | // Add the location and canonical location (if different) to the oat_dex_files_ table. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 548 | StringPiece key(oat_dex_file->GetDexFileLocation()); |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 549 | oat_dex_files_.Put(key, oat_dex_file); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 550 | if (canonical_location != dex_file_location) { |
| 551 | StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation()); |
| 552 | oat_dex_files_.Put(canonical_key, oat_dex_file); |
| 553 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 554 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame^] | 555 | |
| 556 | if (dex_cache_arrays != bss_end_) { |
| 557 | // We expect the bss section to be either empty (dex_cache_arrays and bss_end_ |
| 558 | // both null) or contain just the dex cache arrays and nothing else. |
| 559 | *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zd bytes.", |
| 560 | GetLocation().c_str(), |
| 561 | static_cast<size_t>(bss_end_ - dex_cache_arrays)); |
| 562 | return false; |
| 563 | } |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 564 | return true; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 568 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 571 | const uint8_t* OatFile::Begin() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 572 | CHECK(begin_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 573 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 576 | const uint8_t* OatFile::End() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 577 | CHECK(end_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 578 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 581 | const uint8_t* OatFile::BssBegin() const { |
| 582 | return bss_begin_; |
| 583 | } |
| 584 | |
| 585 | const uint8_t* OatFile::BssEnd() const { |
| 586 | return bss_end_; |
| 587 | } |
| 588 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 589 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location, |
| 590 | const uint32_t* dex_location_checksum, |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 591 | bool warn_if_not_found) const { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 592 | // NOTE: We assume here that the canonical location for a given dex_location never |
| 593 | // changes. If it does (i.e. some symlink used by the filename changes) we may return |
| 594 | // an incorrect OatDexFile. As long as we have a checksum to check, we shall return |
| 595 | // an identical file or fail; otherwise we may see some unpredictable failures. |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 596 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 597 | // TODO: Additional analysis of usage patterns to see if this can be simplified |
| 598 | // without any performance loss, for example by not doing the first lock-free lookup. |
| 599 | |
| 600 | const OatFile::OatDexFile* oat_dex_file = nullptr; |
| 601 | StringPiece key(dex_location); |
| 602 | // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations |
| 603 | // directly mentioned in the oat file and doesn't require locking. |
| 604 | auto primary_it = oat_dex_files_.find(key); |
| 605 | if (primary_it != oat_dex_files_.end()) { |
| 606 | oat_dex_file = primary_it->second; |
| 607 | DCHECK(oat_dex_file != nullptr); |
| 608 | } else { |
| 609 | // This dex_location is not one of the dex locations directly mentioned in the |
| 610 | // oat file. The correct lookup is via the canonical location but first see in |
| 611 | // the secondary_oat_dex_files_ whether we've looked up this location before. |
| 612 | MutexLock mu(Thread::Current(), secondary_lookup_lock_); |
| 613 | auto secondary_lb = secondary_oat_dex_files_.lower_bound(key); |
| 614 | if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 615 | oat_dex_file = secondary_lb->second; // May be null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 616 | } else { |
| 617 | // We haven't seen this dex_location before, we must check the canonical location. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 618 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 619 | if (dex_canonical_location != dex_location) { |
| 620 | StringPiece canonical_key(dex_canonical_location); |
| 621 | auto canonical_it = oat_dex_files_.find(canonical_key); |
| 622 | if (canonical_it != oat_dex_files_.end()) { |
| 623 | oat_dex_file = canonical_it->second; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 624 | } // else keep null. |
| 625 | } // else keep null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 626 | |
| 627 | // Copy the key to the string_cache_ and store the result in secondary map. |
| 628 | string_cache_.emplace_back(key.data(), key.length()); |
| 629 | StringPiece key_copy(string_cache_.back()); |
| 630 | secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 633 | if (oat_dex_file != nullptr && |
| 634 | (dex_location_checksum == nullptr || |
| 635 | oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) { |
| 636 | return oat_dex_file; |
| 637 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 638 | |
| 639 | if (warn_if_not_found) { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 640 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 641 | std::string checksum("<unspecified>"); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 642 | if (dex_location_checksum != nullptr) { |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 643 | checksum = StringPrintf("0x%08x", *dex_location_checksum); |
| 644 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 645 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 646 | << " ( canonical path " << dex_canonical_location << ")" |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 647 | << " with checksum " << checksum << " in OatFile " << GetLocation(); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 648 | if (kIsDebugBuild) { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 649 | for (const OatDexFile* odf : oat_dex_files_storage_) { |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 650 | LOG(WARNING) << "OatFile " << GetLocation() |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 651 | << " contains OatDexFile " << odf->GetDexFileLocation() |
| 652 | << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")" |
| 653 | << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum(); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 654 | } |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 655 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 656 | } |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 657 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 658 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 661 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 662 | const std::string& dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 663 | const std::string& canonical_dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 664 | uint32_t dex_file_location_checksum, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 665 | const uint8_t* dex_file_pointer, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame^] | 666 | const uint32_t* oat_class_offsets_pointer, |
| 667 | const uint8_t* dex_cache_arrays) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 668 | : oat_file_(oat_file), |
| 669 | dex_file_location_(dex_file_location), |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 670 | canonical_dex_file_location_(canonical_dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 671 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 672 | dex_file_pointer_(dex_file_pointer), |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame^] | 673 | oat_class_offsets_pointer_(oat_class_offsets_pointer), |
| 674 | dex_cache_arrays_(dex_cache_arrays) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 675 | |
| 676 | OatFile::OatDexFile::~OatDexFile() {} |
| 677 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 678 | size_t OatFile::OatDexFile::FileSize() const { |
| 679 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 680 | } |
| 681 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 682 | std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const { |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 683 | return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 684 | dex_file_location_checksum_, this, error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 687 | uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const { |
| 688 | return oat_class_offsets_pointer_[class_def_index]; |
| 689 | } |
| 690 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 691 | OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 692 | uint32_t oat_class_offset = GetOatClassOffset(class_def_index); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 693 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 694 | const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 695 | CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 696 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 697 | const uint8_t* status_pointer = oat_class_pointer; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 698 | CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 699 | mirror::Class::Status status = |
| 700 | static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer)); |
| 701 | CHECK_LT(status, mirror::Class::kStatusMax); |
| 702 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 703 | const uint8_t* type_pointer = status_pointer + sizeof(uint16_t); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 704 | CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 705 | OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); |
| 706 | CHECK_LT(type, kOatClassMax); |
| 707 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 708 | const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 709 | CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 710 | |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 711 | uint32_t bitmap_size = 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 712 | const uint8_t* bitmap_pointer = nullptr; |
| 713 | const uint8_t* methods_pointer = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 714 | if (type != kOatClassNoneCompiled) { |
| 715 | if (type == kOatClassSomeCompiled) { |
| 716 | bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); |
| 717 | bitmap_pointer = after_type_pointer + sizeof(bitmap_size); |
| 718 | CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 719 | methods_pointer = bitmap_pointer + bitmap_size; |
| 720 | } else { |
| 721 | methods_pointer = after_type_pointer; |
| 722 | } |
| 723 | CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 724 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 725 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 726 | return OatFile::OatClass(oat_file_, |
| 727 | status, |
| 728 | type, |
| 729 | bitmap_size, |
| 730 | reinterpret_cast<const uint32_t*>(bitmap_pointer), |
| 731 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 734 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 735 | mirror::Class::Status status, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 736 | OatClassType type, |
| 737 | uint32_t bitmap_size, |
| 738 | const uint32_t* bitmap_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 739 | const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 740 | : oat_file_(oat_file), status_(status), type_(type), |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 741 | bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 742 | switch (type_) { |
| 743 | case kOatClassAllCompiled: { |
| 744 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 745 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 746 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 747 | break; |
| 748 | } |
| 749 | case kOatClassSomeCompiled: { |
| 750 | CHECK_NE(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 751 | CHECK(bitmap_pointer != nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 752 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | case kOatClassNoneCompiled: { |
| 756 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 757 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 758 | CHECK(methods_pointer_ == nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 759 | break; |
| 760 | } |
| 761 | case kOatClassMax: { |
| 762 | LOG(FATAL) << "Invalid OatClassType " << type_; |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 767 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 768 | uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const { |
| 769 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 770 | if (oat_method_offsets == nullptr) { |
| 771 | return 0u; |
| 772 | } |
| 773 | return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin(); |
| 774 | } |
| 775 | |
| 776 | const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 777 | // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index. |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 778 | if (methods_pointer_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 779 | CHECK_EQ(kOatClassNoneCompiled, type_); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 780 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 781 | } |
| 782 | size_t methods_pointer_index; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 783 | if (bitmap_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 784 | CHECK_EQ(kOatClassAllCompiled, type_); |
| 785 | methods_pointer_index = method_index; |
| 786 | } else { |
| 787 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 788 | if (!BitVector::IsBitSet(bitmap_, method_index)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 789 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 790 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 791 | size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); |
| 792 | methods_pointer_index = num_set_bits; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 793 | } |
| 794 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 795 | return &oat_method_offsets; |
| 796 | } |
| 797 | |
| 798 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 799 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 800 | if (oat_method_offsets == nullptr) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 801 | return OatMethod(nullptr, 0); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 802 | } |
| 803 | if (oat_file_->IsExecutable() || |
| 804 | Runtime::Current() == nullptr || // This case applies for oatdump. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 805 | Runtime::Current()->IsAotCompiler()) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 806 | return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 807 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 808 | // We aren't allowed to use the compiled code. We just force it down the interpreted / jit |
| 809 | // version. |
| 810 | return OatMethod(oat_file_->Begin(), 0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 813 | void OatFile::OatMethod::LinkMethod(ArtMethod* method) const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 814 | CHECK(method != nullptr); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 815 | method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 818 | bool OatFile::IsPic() const { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 819 | return GetOatHeader().IsPic(); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 820 | // TODO: Check against oat_patches. b/18144996 |
| 821 | } |
| 822 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 823 | bool OatFile::IsDebuggable() const { |
| 824 | return GetOatHeader().IsDebuggable(); |
| 825 | } |
| 826 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 827 | static constexpr char kDexClassPathEncodingSeparator = '*'; |
| 828 | |
| 829 | std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) { |
| 830 | std::ostringstream out; |
| 831 | |
| 832 | for (const DexFile* dex_file : dex_files) { |
| 833 | out << dex_file->GetLocation().c_str(); |
| 834 | out << kDexClassPathEncodingSeparator; |
| 835 | out << dex_file->GetLocationChecksum(); |
| 836 | out << kDexClassPathEncodingSeparator; |
| 837 | } |
| 838 | |
| 839 | return out.str(); |
| 840 | } |
| 841 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 842 | bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 843 | if (dex_dependencies == nullptr || dex_dependencies[0] == 0) { |
| 844 | // No dependencies. |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | // Assumption: this is not performance-critical. So it's OK to do this with a std::string and |
| 849 | // Split() instead of manual parsing of the combined char*. |
| 850 | std::vector<std::string> split; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 851 | Split(dex_dependencies, kDexClassPathEncodingSeparator, &split); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 852 | if (split.size() % 2 != 0) { |
| 853 | // Expected pairs of location and checksum. |
| 854 | *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies); |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | for (auto it = split.begin(), end = split.end(); it != end; it += 2) { |
| 859 | std::string& location = *it; |
| 860 | std::string& checksum = *(it + 1); |
| 861 | int64_t converted = strtoll(checksum.c_str(), nullptr, 10); |
| 862 | if (converted == 0) { |
| 863 | // Conversion error. |
| 864 | *msg = StringPrintf("Conversion error for %s", checksum.c_str()); |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | uint32_t dex_checksum; |
| 869 | std::string error_msg; |
| 870 | if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 871 | &dex_checksum, |
| 872 | &error_msg)) { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 873 | if (converted != dex_checksum) { |
| 874 | *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u", |
| 875 | location.c_str(), converted, dex_checksum); |
| 876 | return false; |
| 877 | } |
| 878 | } else { |
| 879 | // Problem retrieving checksum. |
| 880 | // TODO: odex files? |
| 881 | *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(), |
| 882 | error_msg.c_str()); |
| 883 | return false; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | return true; |
| 888 | } |
| 889 | |
| 890 | bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 891 | std::vector<std::string>* locations) { |
| 892 | DCHECK(locations != nullptr); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 893 | if (dex_dependencies == nullptr || dex_dependencies[0] == 0) { |
| 894 | return true; |
| 895 | } |
| 896 | |
| 897 | // Assumption: this is not performance-critical. So it's OK to do this with a std::string and |
| 898 | // Split() instead of manual parsing of the combined char*. |
| 899 | std::vector<std::string> split; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 900 | Split(dex_dependencies, kDexClassPathEncodingSeparator, &split); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 901 | if (split.size() % 2 != 0) { |
| 902 | // Expected pairs of location and checksum. |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | for (auto it = split.begin(), end = split.end(); it != end; it += 2) { |
| 907 | locations->push_back(*it); |
| 908 | } |
| 909 | |
| 910 | return true; |
| 911 | } |
| 912 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 913 | } // namespace art |