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> |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 21 | #include <type_traits> |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 23 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 24 | #include <cstdlib> |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 25 | #ifndef __APPLE__ |
| 26 | #include <link.h> // for dl_iterate_phdr. |
| 27 | #endif |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 28 | #include <sstream> |
| 29 | |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 30 | // dlopen_ext support from bionic. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 31 | #ifdef ART_TARGET_ANDROID |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 32 | #include "android/dlext.h" |
| 33 | #endif |
| 34 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 35 | #include "android-base/stringprintf.h" |
| 36 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "art_method-inl.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 38 | #include "base/bit_vector.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 39 | #include "base/enums.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 40 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 41 | #include "base/systrace.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 42 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 43 | #include "dex_file_types.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 44 | #include "elf_file.h" |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 45 | #include "elf_utils.h" |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 46 | #include "gc_root.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 47 | #include "oat.h" |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 48 | #include "mem_map.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | #include "mirror/class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 50 | #include "mirror/object-inl.h" |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 51 | #include "oat_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 52 | #include "oat_file_manager.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 53 | #include "os.h" |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 54 | #include "runtime.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 55 | #include "type_lookup_table.h" |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 56 | #include "utf-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 57 | #include "utils.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 58 | #include "utils/dex_cache_arrays_layout-inl.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 59 | #include "vdex_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 60 | |
| 61 | namespace art { |
| 62 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 63 | using android::base::StringPrintf; |
| 64 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 65 | // Whether OatFile::Open will try dlopen. Fallback is our own ELF loader. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 66 | static constexpr bool kUseDlopen = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 67 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 68 | // Whether OatFile::Open will try dlopen on the host. On the host we're not linking against |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 69 | // bionic, so cannot take advantage of the support for changed semantics (loading the same soname |
| 70 | // multiple times). However, if/when we switch the above, we likely want to switch this, too, |
| 71 | // to get test coverage of the code paths. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 72 | static constexpr bool kUseDlopenOnHost = true; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 73 | |
| 74 | // For debugging, Open will print DlOpen error message if set to true. |
| 75 | static constexpr bool kPrintDlOpenErrorMessage = false; |
| 76 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 77 | // Note for OatFileBase and descendents: |
| 78 | // |
| 79 | // These are used in OatFile::Open to try all our loaders. |
| 80 | // |
| 81 | // The process is simple: |
| 82 | // |
| 83 | // 1) Allocate an instance through the standard constructor (location, executable) |
| 84 | // 2) Load() to try to open the file. |
| 85 | // 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress. |
| 86 | // 4) PreSetup() for any steps that should be done before the final setup. |
| 87 | // 5) Setup() to complete the procedure. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 88 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 89 | class OatFileBase : public OatFile { |
| 90 | public: |
| 91 | virtual ~OatFileBase() {} |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 92 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 93 | template <typename kOatFileBaseSubType> |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 94 | static OatFileBase* OpenOatFile(const std::string& vdex_filename, |
| 95 | const std::string& elf_filename, |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 96 | const std::string& location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 97 | uint8_t* requested_base, |
| 98 | uint8_t* oat_file_begin, |
| 99 | bool writable, |
| 100 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 101 | bool low_4gb, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 102 | const char* abs_dex_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 103 | std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 104 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 105 | protected: |
| 106 | OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {} |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 107 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 108 | virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 109 | std::string* error_msg) const = 0; |
| 110 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 111 | virtual void PreLoad() = 0; |
| 112 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 113 | bool LoadVdex(const std::string& vdex_filename, |
| 114 | bool writable, |
| 115 | bool low_4gb, |
| 116 | std::string* error_msg); |
| 117 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 118 | virtual bool Load(const std::string& elf_filename, |
| 119 | uint8_t* oat_file_begin, |
| 120 | bool writable, |
| 121 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 122 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 123 | std::string* error_msg) = 0; |
| 124 | |
| 125 | bool ComputeFields(uint8_t* requested_base, |
| 126 | const std::string& file_path, |
| 127 | std::string* error_msg); |
| 128 | |
| 129 | virtual void PreSetup(const std::string& elf_filename) = 0; |
| 130 | |
| 131 | bool Setup(const char* abs_dex_location, std::string* error_msg); |
| 132 | |
| 133 | // Setters exposed for ElfOatFile. |
| 134 | |
| 135 | void SetBegin(const uint8_t* begin) { |
| 136 | begin_ = begin; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 139 | void SetEnd(const uint8_t* end) { |
| 140 | end_ = end; |
| 141 | } |
| 142 | |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 143 | void SetVdex(VdexFile* vdex) { |
| 144 | vdex_.reset(vdex); |
| 145 | } |
| 146 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 147 | private: |
| 148 | DISALLOW_COPY_AND_ASSIGN(OatFileBase); |
| 149 | }; |
| 150 | |
| 151 | template <typename kOatFileBaseSubType> |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 152 | OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename, |
| 153 | const std::string& elf_filename, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 154 | const std::string& location, |
| 155 | uint8_t* requested_base, |
| 156 | uint8_t* oat_file_begin, |
| 157 | bool writable, |
| 158 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 159 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 160 | const char* abs_dex_location, |
| 161 | std::string* error_msg) { |
| 162 | std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable)); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 163 | |
| 164 | ret->PreLoad(); |
| 165 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 166 | if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) { |
| 167 | return nullptr; |
| 168 | } |
| 169 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 170 | if (!ret->Load(elf_filename, |
| 171 | oat_file_begin, |
| 172 | writable, |
| 173 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 174 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 175 | error_msg)) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 176 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 177 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 178 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 179 | if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) { |
| 180 | return nullptr; |
| 181 | } |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 182 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 183 | ret->PreSetup(elf_filename); |
| 184 | |
| 185 | if (!ret->Setup(abs_dex_location, error_msg)) { |
| 186 | return nullptr; |
| 187 | } |
| 188 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 189 | return ret.release(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 190 | } |
| 191 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 192 | bool OatFileBase::LoadVdex(const std::string& vdex_filename, |
| 193 | bool writable, |
| 194 | bool low_4gb, |
| 195 | std::string* error_msg) { |
| 196 | vdex_.reset(VdexFile::Open(vdex_filename, writable, low_4gb, error_msg)); |
| 197 | if (vdex_.get() == nullptr) { |
| 198 | *error_msg = StringPrintf("Failed to load vdex file '%s' %s", |
| 199 | vdex_filename.c_str(), |
| 200 | error_msg->c_str()); |
| 201 | return false; |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 206 | bool OatFileBase::ComputeFields(uint8_t* requested_base, |
| 207 | const std::string& file_path, |
| 208 | std::string* error_msg) { |
| 209 | std::string symbol_error_msg; |
| 210 | begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 211 | if (begin_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 212 | *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s", |
| 213 | file_path.c_str(), |
| 214 | symbol_error_msg.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | if (requested_base != nullptr && begin_ != requested_base) { |
Nicolas Geoffray | f97cf2a | 2016-03-09 15:36:23 +0000 | [diff] [blame] | 218 | // Host can fail this check. Do not dump there to avoid polluting the output. |
Andreas Gampe | 7ec0904 | 2016-04-01 17:20:49 -0700 | [diff] [blame] | 219 | if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) { |
Nicolas Geoffray | f97cf2a | 2016-03-09 15:36:23 +0000 | [diff] [blame] | 220 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 221 | } |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 222 | *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: " |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 223 | "oatdata=%p != expected=%p. See process maps in the log.", |
| 224 | begin_, requested_base); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 225 | return false; |
| 226 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 227 | end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 228 | if (end_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 229 | *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s", |
| 230 | file_path.c_str(), |
| 231 | symbol_error_msg.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | // Readjust to be non-inclusive upper bound. |
| 235 | end_ += sizeof(uint32_t); |
| 236 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 237 | bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 238 | if (bss_begin_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 239 | // No .bss section. |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 240 | bss_end_ = nullptr; |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 241 | } else { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 242 | bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 243 | if (bss_end_ == nullptr) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 244 | *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str()); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | // Readjust to be non-inclusive upper bound. |
| 248 | bss_end_ += sizeof(uint32_t); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 249 | // Find bss roots if present. |
| 250 | bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg)); |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 253 | return true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 254 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 255 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 256 | // Read an unaligned entry from the OatDexFile data in OatFile and advance the read |
| 257 | // position by the number of bytes read, i.e. sizeof(T). |
| 258 | // Return true on success, false if the read would go beyond the end of the OatFile. |
| 259 | template <typename T> |
Vladimir Marko | 722fa98 | 2015-10-19 18:18:27 +0100 | [diff] [blame] | 260 | inline static bool ReadOatDexFileData(const OatFile& oat_file, |
| 261 | /*inout*/const uint8_t** oat, |
| 262 | /*out*/T* value) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 263 | DCHECK(oat != nullptr); |
| 264 | DCHECK(value != nullptr); |
| 265 | DCHECK_LE(*oat, oat_file.End()); |
| 266 | if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) { |
| 267 | return false; |
| 268 | } |
| 269 | static_assert(std::is_trivial<T>::value, "T must be a trivial type"); |
| 270 | typedef __attribute__((__aligned__(1))) T unaligned_type; |
| 271 | *value = *reinterpret_cast<const unaligned_type*>(*oat); |
| 272 | *oat += sizeof(T); |
| 273 | return true; |
| 274 | } |
| 275 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 276 | bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) { |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 277 | if (!GetOatHeader().IsValid()) { |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 278 | std::string cause = GetOatHeader().GetValidationErrorMessage(); |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 279 | *error_msg = StringPrintf("Invalid oat header for '%s': %s", |
| 280 | GetLocation().c_str(), |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 281 | cause.c_str()); |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 282 | return false; |
| 283 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 284 | const uint8_t* oat = Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 285 | oat += sizeof(OatHeader); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 286 | if (oat > End()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 287 | *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] | 288 | return false; |
| 289 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 290 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 291 | oat += GetOatHeader().GetKeyValueStoreSize(); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 292 | if (oat > End()) { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 293 | *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: " |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 294 | "%p + %zu + %u <= %p", |
| 295 | GetLocation().c_str(), |
| 296 | Begin(), |
| 297 | sizeof(OatHeader), |
| 298 | GetOatHeader().GetKeyValueStoreSize(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 299 | End()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 303 | if (!IsAligned<alignof(GcRoot<mirror::Object>)>(bss_begin_) || |
| 304 | !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_roots_) || |
| 305 | !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) { |
| 306 | *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): " |
| 307 | "begin = %p, roots = %p, end = %p", |
| 308 | GetLocation().c_str(), |
| 309 | bss_begin_, |
| 310 | bss_roots_, |
| 311 | bss_end_); |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | if (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) { |
| 316 | *error_msg = StringPrintf("In oat file '%s' found bss roots outside .bss: " |
| 317 | "%p is outside range [%p, %p]", |
| 318 | GetLocation().c_str(), |
| 319 | bss_roots_, |
| 320 | bss_begin_, |
| 321 | bss_end_); |
| 322 | return false; |
| 323 | } |
| 324 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 325 | PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet()); |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 326 | uint8_t* dex_cache_arrays = bss_begin_; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 327 | uint8_t* dex_cache_arrays_end = (bss_roots_ != nullptr) ? bss_roots_ : bss_end_; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 328 | uint32_t dex_file_count = GetOatHeader().GetDexFileCount(); |
| 329 | oat_dex_files_storage_.reserve(dex_file_count); |
| 330 | for (size_t i = 0; i < dex_file_count; i++) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 331 | uint32_t dex_file_location_size; |
| 332 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) { |
| 333 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file " |
| 334 | "location size", |
| 335 | GetLocation().c_str(), |
| 336 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 337 | return false; |
| 338 | } |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 339 | if (UNLIKELY(dex_file_location_size == 0U)) { |
| 340 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name", |
| 341 | GetLocation().c_str(), |
| 342 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 343 | return false; |
| 344 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 345 | if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 346 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file " |
| 347 | "location", |
| 348 | GetLocation().c_str(), |
| 349 | i); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 350 | return false; |
| 351 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 352 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 353 | oat += dex_file_location_size; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 354 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 355 | std::string dex_file_location = ResolveRelativeEncodedDexLocation( |
| 356 | abs_dex_location, |
| 357 | std::string(dex_file_location_data, dex_file_location_size)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 358 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 359 | uint32_t dex_file_checksum; |
| 360 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) { |
| 361 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after " |
| 362 | "dex file checksum", |
| 363 | GetLocation().c_str(), |
| 364 | i, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 365 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 366 | return false; |
| 367 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 368 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 369 | uint32_t dex_file_offset; |
| 370 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) { |
| 371 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " |
| 372 | "after dex file offsets", |
| 373 | GetLocation().c_str(), |
| 374 | i, |
| 375 | dex_file_location.c_str()); |
| 376 | return false; |
| 377 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 378 | if (UNLIKELY(dex_file_offset == 0U)) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 379 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex " |
| 380 | "file offset", |
| 381 | GetLocation().c_str(), |
| 382 | i, |
| 383 | dex_file_location.c_str()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 384 | return false; |
| 385 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 386 | if (UNLIKELY(dex_file_offset > DexSize())) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 387 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 388 | "offset %u > %zu", |
| 389 | GetLocation().c_str(), |
| 390 | i, |
| 391 | dex_file_location.c_str(), |
| 392 | dex_file_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 393 | DexSize()); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 394 | return false; |
| 395 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 396 | if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 397 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 398 | "offset %u of %zu but the size of dex file header is %zu", |
| 399 | GetLocation().c_str(), |
| 400 | i, |
| 401 | dex_file_location.c_str(), |
| 402 | dex_file_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 403 | DexSize(), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 404 | sizeof(DexFile::Header)); |
| 405 | return false; |
| 406 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 407 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 408 | const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 409 | if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 410 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " |
| 411 | "dex file magic '%s'", |
| 412 | GetLocation().c_str(), |
| 413 | i, |
| 414 | dex_file_location.c_str(), |
| 415 | dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 416 | return false; |
| 417 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 418 | if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 419 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " |
| 420 | "dex file version '%s'", |
| 421 | GetLocation().c_str(), |
| 422 | i, |
| 423 | dex_file_location.c_str(), |
| 424 | dex_file_pointer); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 425 | return false; |
| 426 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 427 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 428 | if (DexSize() - dex_file_offset < header->file_size_) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 429 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " |
| 430 | "offset %u and size %u truncated at %zu", |
| 431 | GetLocation().c_str(), |
| 432 | i, |
| 433 | dex_file_location.c_str(), |
| 434 | dex_file_offset, |
| 435 | header->file_size_, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 436 | DexSize()); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 437 | return false; |
| 438 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 439 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 440 | uint32_t class_offsets_offset; |
| 441 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) { |
| 442 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " |
| 443 | "after class offsets offset", |
| 444 | GetLocation().c_str(), |
| 445 | i, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 446 | dex_file_location.c_str()); |
| 447 | return false; |
| 448 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 449 | if (UNLIKELY(class_offsets_offset > Size()) || |
| 450 | UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) { |
| 451 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " |
| 452 | "class offsets, offset %u of %zu, class defs %u", |
| 453 | GetLocation().c_str(), |
| 454 | i, |
| 455 | dex_file_location.c_str(), |
| 456 | class_offsets_offset, |
| 457 | Size(), |
| 458 | header->class_defs_size_); |
| 459 | return false; |
| 460 | } |
| 461 | if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) { |
| 462 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned " |
| 463 | "class offsets, offset %u", |
| 464 | GetLocation().c_str(), |
| 465 | i, |
| 466 | dex_file_location.c_str(), |
| 467 | class_offsets_offset); |
| 468 | return false; |
| 469 | } |
| 470 | const uint32_t* class_offsets_pointer = |
| 471 | reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset); |
| 472 | |
| 473 | uint32_t lookup_table_offset; |
| 474 | if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) { |
| 475 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " |
| 476 | "after lookup table offset", |
| 477 | GetLocation().c_str(), |
| 478 | i, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 479 | dex_file_location.c_str()); |
| 480 | return false; |
| 481 | } |
| 482 | const uint8_t* lookup_table_data = lookup_table_offset != 0u |
| 483 | ? Begin() + lookup_table_offset |
| 484 | : nullptr; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 485 | if (lookup_table_offset != 0u && |
| 486 | (UNLIKELY(lookup_table_offset > Size()) || |
| 487 | UNLIKELY(Size() - lookup_table_offset < |
| 488 | TypeLookupTable::RawDataLength(header->class_defs_size_)))) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 489 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 490 | "type lookup table, offset %u of %zu, class defs %u", |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 491 | GetLocation().c_str(), |
| 492 | i, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 493 | dex_file_location.c_str(), |
| 494 | lookup_table_offset, |
| 495 | Size(), |
| 496 | header->class_defs_size_); |
Brian Carlstrom | fb331d7 | 2013-07-25 22:00:16 -0700 | [diff] [blame] | 497 | return false; |
| 498 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 499 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 500 | uint8_t* current_dex_cache_arrays = nullptr; |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 501 | if (dex_cache_arrays != nullptr) { |
| 502 | DexCacheArraysLayout layout(pointer_size, *header); |
| 503 | if (layout.Size() != 0u) { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 504 | if (static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays) < layout.Size()) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 505 | *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with " |
| 506 | "truncated dex cache arrays, %zu < %zu.", |
| 507 | GetLocation().c_str(), |
| 508 | i, |
| 509 | dex_file_location.c_str(), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 510 | static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays), |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 511 | layout.Size()); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 512 | return false; |
| 513 | } |
| 514 | current_dex_cache_arrays = dex_cache_arrays; |
| 515 | dex_cache_arrays += layout.Size(); |
| 516 | } |
| 517 | } |
| 518 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 519 | std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str()); |
| 520 | |
| 521 | // Create the OatDexFile and add it to the owning container. |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 522 | OatDexFile* oat_dex_file = new OatDexFile(this, |
| 523 | dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 524 | canonical_location, |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 525 | dex_file_checksum, |
| 526 | dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 527 | lookup_table_data, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 528 | class_offsets_pointer, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 529 | current_dex_cache_arrays); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 530 | oat_dex_files_storage_.push_back(oat_dex_file); |
| 531 | |
| 532 | // 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] | 533 | StringPiece key(oat_dex_file->GetDexFileLocation()); |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 534 | oat_dex_files_.Put(key, oat_dex_file); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 535 | if (canonical_location != dex_file_location) { |
| 536 | StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation()); |
| 537 | oat_dex_files_.Put(canonical_key, oat_dex_file); |
| 538 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 539 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 540 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 541 | if (dex_cache_arrays != dex_cache_arrays_end) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 542 | // We expect the bss section to be either empty (dex_cache_arrays and bss_end_ |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 543 | // both null) or contain just the dex cache arrays and optionally some GC roots. |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 544 | *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.", |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 545 | GetLocation().c_str(), |
| 546 | static_cast<size_t>(bss_end_ - dex_cache_arrays)); |
| 547 | return false; |
| 548 | } |
Brian Carlstrom | f1b3030 | 2013-03-28 10:35:32 -0700 | [diff] [blame] | 549 | return true; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 552 | //////////////////////// |
| 553 | // OatFile via dlopen // |
| 554 | //////////////////////// |
| 555 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 556 | class DlOpenOatFile FINAL : public OatFileBase { |
| 557 | public: |
| 558 | DlOpenOatFile(const std::string& filename, bool executable) |
| 559 | : OatFileBase(filename, executable), |
| 560 | dlopen_handle_(nullptr), |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 561 | shared_objects_before_(0) { |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | ~DlOpenOatFile() { |
| 565 | if (dlopen_handle_ != nullptr) { |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 566 | if (!kIsTargetBuild) { |
| 567 | MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_); |
| 568 | host_dlopen_handles_.erase(dlopen_handle_); |
Mathieu Chartier | c7d3f4b | 2016-06-01 10:48:19 -0700 | [diff] [blame] | 569 | dlclose(dlopen_handle_); |
| 570 | } else { |
| 571 | dlclose(dlopen_handle_); |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 572 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 573 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | protected: |
| 577 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 578 | std::string* error_msg) const OVERRIDE { |
| 579 | const uint8_t* ptr = |
| 580 | reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str())); |
| 581 | if (ptr == nullptr) { |
| 582 | *error_msg = dlerror(); |
| 583 | } |
| 584 | return ptr; |
| 585 | } |
| 586 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 587 | void PreLoad() OVERRIDE; |
| 588 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 589 | bool Load(const std::string& elf_filename, |
| 590 | uint8_t* oat_file_begin, |
| 591 | bool writable, |
| 592 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 593 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 594 | std::string* error_msg) OVERRIDE; |
| 595 | |
| 596 | // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions. |
| 597 | void PreSetup(const std::string& elf_filename) OVERRIDE; |
| 598 | |
| 599 | private: |
| 600 | bool Dlopen(const std::string& elf_filename, |
| 601 | uint8_t* oat_file_begin, |
| 602 | std::string* error_msg); |
| 603 | |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 604 | // On the host, if the same library is loaded again with dlopen the same |
| 605 | // file handle is returned. This differs from the behavior of dlopen on the |
| 606 | // target, where dlopen reloads the library at a different address every |
| 607 | // time you load it. The runtime relies on the target behavior to ensure |
| 608 | // each instance of the loaded library has a unique dex cache. To avoid |
| 609 | // problems, we fall back to our own linker in the case when the same |
| 610 | // library is opened multiple times on host. dlopen_handles_ is used to |
| 611 | // detect that case. |
| 612 | // Guarded by host_dlopen_handles_lock_; |
| 613 | static std::unordered_set<void*> host_dlopen_handles_; |
| 614 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 615 | // dlopen handle during runtime. |
| 616 | void* dlopen_handle_; // TODO: Unique_ptr with custom deleter. |
| 617 | |
| 618 | // Dummy memory map objects corresponding to the regions mapped by dlopen. |
| 619 | std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_; |
| 620 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 621 | // The number of shared objects the linker told us about before loading. Used to |
| 622 | // (optimistically) optimize the PreSetup stage (see comment there). |
| 623 | size_t shared_objects_before_; |
| 624 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 625 | DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile); |
| 626 | }; |
| 627 | |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 628 | std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_; |
| 629 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 630 | void DlOpenOatFile::PreLoad() { |
| 631 | #ifdef __APPLE__ |
Andreas Gampe | 39004a6 | 2016-05-18 21:27:00 -0700 | [diff] [blame] | 632 | UNUSED(shared_objects_before_); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 633 | LOG(FATAL) << "Should not reach here."; |
| 634 | UNREACHABLE(); |
| 635 | #else |
| 636 | // Count the entries in dl_iterate_phdr we get at this point in time. |
| 637 | struct dl_iterate_context { |
| 638 | static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED, |
| 639 | size_t size ATTRIBUTE_UNUSED, |
| 640 | void *data) { |
| 641 | reinterpret_cast<dl_iterate_context*>(data)->count++; |
| 642 | return 0; // Continue iteration. |
| 643 | } |
| 644 | size_t count = 0; |
| 645 | } context; |
| 646 | |
| 647 | dl_iterate_phdr(dl_iterate_context::callback, &context); |
| 648 | shared_objects_before_ = context.count; |
| 649 | #endif |
| 650 | } |
| 651 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 652 | bool DlOpenOatFile::Load(const std::string& elf_filename, |
| 653 | uint8_t* oat_file_begin, |
| 654 | bool writable, |
| 655 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 656 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 657 | std::string* error_msg) { |
| 658 | // Use dlopen only when flagged to do so, and when it's OK to load things executable. |
| 659 | // TODO: Also try when not executable? The issue here could be re-mapping as writable (as |
| 660 | // !executable is a sign that we may want to patch), which may not be allowed for |
| 661 | // various reasons. |
| 662 | if (!kUseDlopen) { |
| 663 | *error_msg = "DlOpen is disabled."; |
| 664 | return false; |
| 665 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 666 | if (low_4gb) { |
| 667 | *error_msg = "DlOpen does not support low 4gb loading."; |
| 668 | return false; |
| 669 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 670 | if (writable) { |
| 671 | *error_msg = "DlOpen does not support writable loading."; |
| 672 | return false; |
| 673 | } |
| 674 | if (!executable) { |
| 675 | *error_msg = "DlOpen does not support non-executable loading."; |
| 676 | return false; |
| 677 | } |
| 678 | |
| 679 | // dlopen always returns the same library if it is already opened on the host. For this reason |
| 680 | // we only use dlopen if we are the target or we do not already have the dex file opened. Having |
| 681 | // the same library loaded multiple times at different addresses is required for class unloading |
| 682 | // and for having dex caches arrays in the .bss section. |
| 683 | if (!kIsTargetBuild) { |
| 684 | if (!kUseDlopenOnHost) { |
| 685 | *error_msg = "DlOpen disabled for host."; |
| 686 | return false; |
| 687 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | bool success = Dlopen(elf_filename, oat_file_begin, error_msg); |
| 691 | DCHECK(dlopen_handle_ != nullptr || !success); |
| 692 | |
| 693 | return success; |
| 694 | } |
| 695 | |
| 696 | bool DlOpenOatFile::Dlopen(const std::string& elf_filename, |
| 697 | uint8_t* oat_file_begin, |
| 698 | std::string* error_msg) { |
| 699 | #ifdef __APPLE__ |
| 700 | // The dl_iterate_phdr syscall is missing. There is similar API on OSX, |
| 701 | // but let's fallback to the custom loading code for the time being. |
| 702 | UNUSED(elf_filename, oat_file_begin); |
| 703 | *error_msg = "Dlopen unsupported on Mac."; |
| 704 | return false; |
| 705 | #else |
| 706 | { |
| 707 | UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr)); |
| 708 | if (absolute_path == nullptr) { |
| 709 | *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); |
| 710 | return false; |
| 711 | } |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 712 | #ifdef ART_TARGET_ANDROID |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 713 | android_dlextinfo extinfo; |
| 714 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle |
| 715 | // (open oat files multiple |
| 716 | // times). |
| 717 | ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute |
| 718 | // (non-pic boot image). |
Andreas Gampe | 226b91e | 2015-12-02 10:29:33 -0800 | [diff] [blame] | 719 | if (oat_file_begin != nullptr) { // |
| 720 | extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if |
| 721 | extinfo.reserved_addr = oat_file_begin; // vaddr = 0. |
| 722 | } // (pic boot image). |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 723 | dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo); |
| 724 | #else |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 725 | UNUSED(oat_file_begin); |
Julien Duraj | 1af0c4f | 2016-11-16 14:05:48 +0000 | [diff] [blame] | 726 | static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles"); |
Mathieu Chartier | c7d3f4b | 2016-06-01 10:48:19 -0700 | [diff] [blame] | 727 | MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_); |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 728 | dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW); |
| 729 | if (dlopen_handle_ != nullptr) { |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 730 | if (!host_dlopen_handles_.insert(dlopen_handle_).second) { |
| 731 | dlclose(dlopen_handle_); |
| 732 | dlopen_handle_ = nullptr; |
| 733 | *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str()); |
| 734 | return false; |
| 735 | } |
| 736 | } |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 737 | #endif // ART_TARGET_ANDROID |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 738 | } |
| 739 | if (dlopen_handle_ == nullptr) { |
| 740 | *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); |
| 741 | return false; |
| 742 | } |
| 743 | return true; |
| 744 | #endif |
| 745 | } |
| 746 | |
| 747 | void DlOpenOatFile::PreSetup(const std::string& elf_filename) { |
Andreas Gampe | 74f07b5 | 2015-12-02 11:53:26 -0800 | [diff] [blame] | 748 | #ifdef __APPLE__ |
| 749 | UNUSED(elf_filename); |
| 750 | LOG(FATAL) << "Should not reach here."; |
| 751 | UNREACHABLE(); |
| 752 | #else |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 753 | struct dl_iterate_context { |
| 754 | static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) { |
| 755 | auto* context = reinterpret_cast<dl_iterate_context*>(data); |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 756 | context->shared_objects_seen++; |
| 757 | if (context->shared_objects_seen < context->shared_objects_before) { |
| 758 | // We haven't been called yet for anything we haven't seen before. Just continue. |
| 759 | // Note: this is aggressively optimistic. If another thread was unloading a library, |
| 760 | // we may miss out here. However, this does not happen often in practice. |
| 761 | return 0; |
| 762 | } |
| 763 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 764 | // See whether this callback corresponds to the file which we have just loaded. |
| 765 | bool contains_begin = false; |
| 766 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 767 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 768 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 769 | info->dlpi_phdr[i].p_vaddr); |
| 770 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
| 771 | if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) { |
| 772 | contains_begin = true; |
| 773 | break; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | // Add dummy mmaps for this file. |
| 778 | if (contains_begin) { |
| 779 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 780 | if (info->dlpi_phdr[i].p_type == PT_LOAD) { |
| 781 | uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + |
| 782 | info->dlpi_phdr[i].p_vaddr); |
| 783 | size_t memsz = info->dlpi_phdr[i].p_memsz; |
| 784 | MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz); |
| 785 | context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap)); |
| 786 | } |
| 787 | } |
| 788 | return 1; // Stop iteration and return 1 from dl_iterate_phdr. |
| 789 | } |
| 790 | return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished. |
| 791 | } |
| 792 | const uint8_t* const begin_; |
| 793 | std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_; |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 794 | const size_t shared_objects_before; |
| 795 | size_t shared_objects_seen; |
| 796 | }; |
| 797 | dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0}; |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 798 | |
| 799 | if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) { |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 800 | // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0 |
| 801 | // before giving up. This should be unusual. |
| 802 | VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before=" |
| 803 | << shared_objects_before_; |
| 804 | dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0}; |
| 805 | if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) { |
| 806 | // OK, give up and print an error. |
| 807 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 808 | LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps."; |
| 809 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 810 | } |
Andreas Gampe | 74f07b5 | 2015-12-02 11:53:26 -0800 | [diff] [blame] | 811 | #endif |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | //////////////////////////////////////////////// |
| 815 | // OatFile via our own ElfFile implementation // |
| 816 | //////////////////////////////////////////////// |
| 817 | |
| 818 | class ElfOatFile FINAL : public OatFileBase { |
| 819 | public: |
| 820 | ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {} |
| 821 | |
| 822 | static ElfOatFile* OpenElfFile(File* file, |
| 823 | const std::string& location, |
| 824 | uint8_t* requested_base, |
| 825 | uint8_t* oat_file_begin, // Override base if not null |
| 826 | bool writable, |
| 827 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 828 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 829 | const char* abs_dex_location, |
| 830 | std::string* error_msg); |
| 831 | |
| 832 | bool InitializeFromElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 833 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 834 | const char* abs_dex_location, |
| 835 | std::string* error_msg); |
| 836 | |
| 837 | protected: |
| 838 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, |
| 839 | std::string* error_msg) const OVERRIDE { |
| 840 | const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name); |
| 841 | if (ptr == nullptr) { |
| 842 | *error_msg = "(Internal implementation could not find symbol)"; |
| 843 | } |
| 844 | return ptr; |
| 845 | } |
| 846 | |
Andreas Gampe | 4075f83 | 2016-05-18 13:09:54 -0700 | [diff] [blame] | 847 | void PreLoad() OVERRIDE { |
| 848 | } |
| 849 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 850 | bool Load(const std::string& elf_filename, |
| 851 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 852 | bool writable, |
| 853 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 854 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 855 | std::string* error_msg) OVERRIDE; |
| 856 | |
| 857 | void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE { |
| 858 | } |
| 859 | |
| 860 | private: |
| 861 | bool ElfFileOpen(File* file, |
| 862 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 863 | bool writable, |
| 864 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 865 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 866 | std::string* error_msg); |
| 867 | |
| 868 | private: |
| 869 | // Backing memory map for oat file during cross compilation. |
| 870 | std::unique_ptr<ElfFile> elf_file_; |
| 871 | |
| 872 | DISALLOW_COPY_AND_ASSIGN(ElfOatFile); |
| 873 | }; |
| 874 | |
| 875 | ElfOatFile* ElfOatFile::OpenElfFile(File* file, |
| 876 | const std::string& location, |
| 877 | uint8_t* requested_base, |
| 878 | uint8_t* oat_file_begin, // Override base if not null |
| 879 | bool writable, |
| 880 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 881 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 882 | const char* abs_dex_location, |
| 883 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 884 | ScopedTrace trace("Open elf file " + location); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 885 | std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable)); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 886 | bool success = oat_file->ElfFileOpen(file, |
| 887 | oat_file_begin, |
| 888 | writable, |
| 889 | low_4gb, |
| 890 | executable, |
| 891 | error_msg); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 892 | if (!success) { |
| 893 | CHECK(!error_msg->empty()); |
| 894 | return nullptr; |
| 895 | } |
| 896 | |
| 897 | // Complete the setup. |
| 898 | if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) { |
| 899 | return nullptr; |
| 900 | } |
| 901 | |
| 902 | if (!oat_file->Setup(abs_dex_location, error_msg)) { |
| 903 | return nullptr; |
| 904 | } |
| 905 | |
| 906 | return oat_file.release(); |
| 907 | } |
| 908 | |
| 909 | bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 910 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 911 | const char* abs_dex_location, |
| 912 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 913 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 914 | if (IsExecutable()) { |
| 915 | *error_msg = "Cannot initialize from elf file in executable mode."; |
| 916 | return false; |
| 917 | } |
| 918 | elf_file_.reset(elf_file); |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 919 | SetVdex(vdex_file); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 920 | uint64_t offset, size; |
| 921 | bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size); |
| 922 | CHECK(has_section); |
| 923 | SetBegin(elf_file->Begin() + offset); |
| 924 | SetEnd(elf_file->Begin() + size + offset); |
| 925 | // Ignore the optional .bss section when opening non-executable. |
| 926 | return Setup(abs_dex_location, error_msg); |
| 927 | } |
| 928 | |
| 929 | bool ElfOatFile::Load(const std::string& elf_filename, |
| 930 | uint8_t* oat_file_begin, // Override where the file is loaded to if not null |
| 931 | bool writable, |
| 932 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 933 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 934 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 935 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 936 | std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str())); |
| 937 | if (file == nullptr) { |
| 938 | *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); |
| 939 | return false; |
| 940 | } |
| 941 | return ElfOatFile::ElfFileOpen(file.get(), |
| 942 | oat_file_begin, |
| 943 | writable, |
| 944 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 945 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 946 | error_msg); |
| 947 | } |
| 948 | |
| 949 | bool ElfOatFile::ElfFileOpen(File* file, |
| 950 | uint8_t* oat_file_begin, |
| 951 | bool writable, |
| 952 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 953 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 954 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 955 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 956 | // TODO: rename requested_base to oat_data_begin |
| 957 | elf_file_.reset(ElfFile::Open(file, |
| 958 | writable, |
| 959 | /*program_header_only*/true, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 960 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 961 | error_msg, |
| 962 | oat_file_begin)); |
| 963 | if (elf_file_ == nullptr) { |
| 964 | DCHECK(!error_msg->empty()); |
| 965 | return false; |
| 966 | } |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 967 | bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg); |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 968 | DCHECK(loaded || !error_msg->empty()); |
| 969 | return loaded; |
| 970 | } |
| 971 | |
| 972 | ////////////////////////// |
| 973 | // General OatFile code // |
| 974 | ////////////////////////// |
| 975 | |
| 976 | std::string OatFile::ResolveRelativeEncodedDexLocation( |
| 977 | const char* abs_dex_location, const std::string& rel_dex_location) { |
| 978 | if (abs_dex_location != nullptr && rel_dex_location[0] != '/') { |
| 979 | // Strip :classes<N>.dex used for secondary multidex files. |
| 980 | std::string base = DexFile::GetBaseLocation(rel_dex_location); |
| 981 | std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location); |
| 982 | |
| 983 | // Check if the base is a suffix of the provided abs_dex_location. |
| 984 | std::string target_suffix = "/" + base; |
| 985 | std::string abs_location(abs_dex_location); |
| 986 | if (abs_location.size() > target_suffix.size()) { |
| 987 | size_t pos = abs_location.size() - target_suffix.size(); |
| 988 | if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) { |
| 989 | return abs_location + multidex_suffix; |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | return rel_dex_location; |
| 994 | } |
| 995 | |
| 996 | static void CheckLocation(const std::string& location) { |
| 997 | CHECK(!location.empty()); |
| 998 | } |
| 999 | |
| 1000 | OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file, |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 1001 | VdexFile* vdex_file, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1002 | const std::string& location, |
| 1003 | const char* abs_dex_location, |
| 1004 | std::string* error_msg) { |
| 1005 | std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */)); |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 1006 | return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg) |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1007 | ? oat_file.release() |
| 1008 | : nullptr; |
| 1009 | } |
| 1010 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1011 | OatFile* OatFile::Open(const std::string& oat_filename, |
| 1012 | const std::string& oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1013 | uint8_t* requested_base, |
| 1014 | uint8_t* oat_file_begin, |
| 1015 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1016 | bool low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1017 | const char* abs_dex_location, |
| 1018 | std::string* error_msg) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1019 | ScopedTrace trace("Open oat file " + oat_location); |
| 1020 | CHECK(!oat_filename.empty()) << oat_location; |
| 1021 | CheckLocation(oat_location); |
Andreas Gampe | 54315c7 | 2016-05-18 21:10:42 -0700 | [diff] [blame] | 1022 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1023 | std::string vdex_filename = ReplaceFileExtension(oat_filename, "vdex"); |
| 1024 | |
| 1025 | // Check that the files even exist, fast-fail. |
| 1026 | if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) { |
| 1027 | *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str()); |
| 1028 | return nullptr; |
| 1029 | } else if (!OS::FileExists(oat_filename.c_str())) { |
| 1030 | *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str()); |
Andreas Gampe | 54315c7 | 2016-05-18 21:10:42 -0700 | [diff] [blame] | 1031 | return nullptr; |
| 1032 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1033 | |
| 1034 | // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is |
| 1035 | // disabled. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1036 | OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename, |
| 1037 | oat_filename, |
| 1038 | oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1039 | requested_base, |
| 1040 | oat_file_begin, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1041 | false /* writable */, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1042 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1043 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1044 | abs_dex_location, |
| 1045 | error_msg); |
| 1046 | if (with_dlopen != nullptr) { |
| 1047 | return with_dlopen; |
| 1048 | } |
| 1049 | if (kPrintDlOpenErrorMessage) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1050 | LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg; |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1051 | } |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1052 | // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: |
| 1053 | // |
| 1054 | // On target, dlopen may fail when compiling due to selinux restrictions on installd. |
| 1055 | // |
| 1056 | // We use our own ELF loader for Quick to deal with legacy apps that |
| 1057 | // open a generated dex file by name, remove the file, then open |
| 1058 | // another generated dex file with the same name. http://b/10614658 |
| 1059 | // |
| 1060 | // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. |
| 1061 | // |
| 1062 | // |
| 1063 | // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually |
| 1064 | // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1065 | OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename, |
| 1066 | oat_filename, |
| 1067 | oat_location, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1068 | requested_base, |
| 1069 | oat_file_begin, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1070 | false /* writable */, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1071 | executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1072 | low_4gb, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1073 | abs_dex_location, |
| 1074 | error_msg); |
| 1075 | return with_internal; |
| 1076 | } |
| 1077 | |
| 1078 | OatFile* OatFile::OpenWritable(File* file, |
| 1079 | const std::string& location, |
| 1080 | const char* abs_dex_location, |
| 1081 | std::string* error_msg) { |
| 1082 | CheckLocation(location); |
| 1083 | return ElfOatFile::OpenElfFile(file, |
| 1084 | location, |
| 1085 | nullptr, |
| 1086 | nullptr, |
| 1087 | true, |
| 1088 | false, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1089 | /*low_4gb*/false, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1090 | abs_dex_location, |
| 1091 | error_msg); |
| 1092 | } |
| 1093 | |
| 1094 | OatFile* OatFile::OpenReadable(File* file, |
| 1095 | const std::string& location, |
| 1096 | const char* abs_dex_location, |
| 1097 | std::string* error_msg) { |
| 1098 | CheckLocation(location); |
| 1099 | return ElfOatFile::OpenElfFile(file, |
| 1100 | location, |
| 1101 | nullptr, |
| 1102 | nullptr, |
| 1103 | false, |
| 1104 | false, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1105 | /*low_4gb*/false, |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1106 | abs_dex_location, |
| 1107 | error_msg); |
| 1108 | } |
| 1109 | |
| 1110 | OatFile::OatFile(const std::string& location, bool is_executable) |
| 1111 | : location_(location), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1112 | vdex_(nullptr), |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1113 | begin_(nullptr), |
| 1114 | end_(nullptr), |
| 1115 | bss_begin_(nullptr), |
| 1116 | bss_end_(nullptr), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1117 | bss_roots_(nullptr), |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 1118 | is_executable_(is_executable), |
| 1119 | secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) { |
| 1120 | CHECK(!location_.empty()); |
| 1121 | } |
| 1122 | |
| 1123 | OatFile::~OatFile() { |
| 1124 | STLDeleteElements(&oat_dex_files_storage_); |
| 1125 | } |
| 1126 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1127 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1128 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1131 | const uint8_t* OatFile::Begin() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1132 | CHECK(begin_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1133 | return begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1136 | const uint8_t* OatFile::End() const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1137 | CHECK(end_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1138 | return end_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1141 | const uint8_t* OatFile::BssBegin() const { |
| 1142 | return bss_begin_; |
| 1143 | } |
| 1144 | |
| 1145 | const uint8_t* OatFile::BssEnd() const { |
| 1146 | return bss_end_; |
| 1147 | } |
| 1148 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1149 | const uint8_t* OatFile::DexBegin() const { |
| 1150 | return kIsVdexEnabled ? vdex_->Begin() : Begin(); |
| 1151 | } |
| 1152 | |
| 1153 | const uint8_t* OatFile::DexEnd() const { |
| 1154 | return kIsVdexEnabled ? vdex_->End() : End(); |
| 1155 | } |
| 1156 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 1157 | ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const { |
| 1158 | if (bss_roots_ != nullptr) { |
| 1159 | auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_); |
| 1160 | auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_); |
| 1161 | return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots); |
| 1162 | } else { |
| 1163 | return ArrayRef<GcRoot<mirror::Object>>(); |
| 1164 | } |
| 1165 | } |
| 1166 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1167 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location, |
| 1168 | const uint32_t* dex_location_checksum, |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1169 | std::string* error_msg) const { |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1170 | // NOTE: We assume here that the canonical location for a given dex_location never |
| 1171 | // changes. If it does (i.e. some symlink used by the filename changes) we may return |
| 1172 | // an incorrect OatDexFile. As long as we have a checksum to check, we shall return |
| 1173 | // an identical file or fail; otherwise we may see some unpredictable failures. |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1174 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1175 | // TODO: Additional analysis of usage patterns to see if this can be simplified |
| 1176 | // without any performance loss, for example by not doing the first lock-free lookup. |
| 1177 | |
| 1178 | const OatFile::OatDexFile* oat_dex_file = nullptr; |
| 1179 | StringPiece key(dex_location); |
| 1180 | // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations |
| 1181 | // directly mentioned in the oat file and doesn't require locking. |
| 1182 | auto primary_it = oat_dex_files_.find(key); |
| 1183 | if (primary_it != oat_dex_files_.end()) { |
| 1184 | oat_dex_file = primary_it->second; |
| 1185 | DCHECK(oat_dex_file != nullptr); |
| 1186 | } else { |
| 1187 | // This dex_location is not one of the dex locations directly mentioned in the |
| 1188 | // oat file. The correct lookup is via the canonical location but first see in |
| 1189 | // the secondary_oat_dex_files_ whether we've looked up this location before. |
| 1190 | MutexLock mu(Thread::Current(), secondary_lookup_lock_); |
| 1191 | auto secondary_lb = secondary_oat_dex_files_.lower_bound(key); |
| 1192 | if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1193 | oat_dex_file = secondary_lb->second; // May be null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1194 | } else { |
| 1195 | // 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] | 1196 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1197 | if (dex_canonical_location != dex_location) { |
| 1198 | StringPiece canonical_key(dex_canonical_location); |
| 1199 | auto canonical_it = oat_dex_files_.find(canonical_key); |
| 1200 | if (canonical_it != oat_dex_files_.end()) { |
| 1201 | oat_dex_file = canonical_it->second; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1202 | } // else keep null. |
| 1203 | } // else keep null. |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1204 | |
| 1205 | // Copy the key to the string_cache_ and store the result in secondary map. |
| 1206 | string_cache_.emplace_back(key.data(), key.length()); |
| 1207 | StringPiece key_copy(string_cache_.back()); |
| 1208 | secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1211 | |
| 1212 | if (oat_dex_file == nullptr) { |
| 1213 | if (error_msg != nullptr) { |
| 1214 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
| 1215 | *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location) |
| 1216 | + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation(); |
| 1217 | } |
| 1218 | return nullptr; |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 1219 | } |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 1220 | |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1221 | if (dex_location_checksum != nullptr && |
| 1222 | oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) { |
| 1223 | if (error_msg != nullptr) { |
| 1224 | std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location); |
| 1225 | std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum()); |
| 1226 | std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum); |
| 1227 | *error_msg = "OatDexFile for DexFile " + std::string(dex_location) |
| 1228 | + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation() |
| 1229 | + " has checksum " + checksum + " but " + required_checksum + " was required"; |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1230 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1231 | return nullptr; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1232 | } |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1233 | return oat_dex_file; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1236 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 1237 | const std::string& dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1238 | const std::string& canonical_dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1239 | uint32_t dex_file_location_checksum, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1240 | const uint8_t* dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1241 | const uint8_t* lookup_table_data, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1242 | const uint32_t* oat_class_offsets_pointer, |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 1243 | uint8_t* dex_cache_arrays) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1244 | : oat_file_(oat_file), |
| 1245 | dex_file_location_(dex_file_location), |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1246 | canonical_dex_file_location_(canonical_dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1247 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1248 | dex_file_pointer_(dex_file_pointer), |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1249 | lookup_table_data_(lookup_table_data), |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1250 | oat_class_offsets_pointer_(oat_class_offsets_pointer), |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1251 | dex_cache_arrays_(dex_cache_arrays) { |
| 1252 | // Initialize TypeLookupTable. |
| 1253 | if (lookup_table_data_ != nullptr) { |
| 1254 | // Peek the number of classes from the DexFile. |
| 1255 | const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_); |
| 1256 | const uint32_t num_class_defs = dex_header->class_defs_size_; |
| 1257 | if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) { |
| 1258 | LOG(WARNING) << "found truncated lookup table in " << dex_file_location_; |
| 1259 | } else { |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1260 | lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1264 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1265 | OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table) |
| 1266 | : lookup_table_(std::move(lookup_table)) {} |
| 1267 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1268 | OatFile::OatDexFile::~OatDexFile() {} |
| 1269 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 1270 | size_t OatFile::OatDexFile::FileSize() const { |
| 1271 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 1272 | } |
| 1273 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1274 | std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1275 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1276 | static constexpr bool kVerify = false; |
| 1277 | static constexpr bool kVerifyChecksum = false; |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 1278 | return DexFile::Open(dex_file_pointer_, |
| 1279 | FileSize(), |
| 1280 | dex_file_location_, |
| 1281 | dex_file_location_checksum_, |
| 1282 | this, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1283 | kVerify, |
| 1284 | kVerifyChecksum, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 1285 | error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1288 | uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const { |
| 1289 | return oat_class_offsets_pointer_[class_def_index]; |
| 1290 | } |
| 1291 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1292 | OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1293 | uint32_t oat_class_offset = GetOatClassOffset(class_def_index); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1294 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1295 | const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1296 | CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1297 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1298 | const uint8_t* status_pointer = oat_class_pointer; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1299 | CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1300 | mirror::Class::Status status = |
| 1301 | static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer)); |
| 1302 | CHECK_LT(status, mirror::Class::kStatusMax); |
| 1303 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1304 | const uint8_t* type_pointer = status_pointer + sizeof(uint16_t); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1305 | CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1306 | OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); |
| 1307 | CHECK_LT(type, kOatClassMax); |
| 1308 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1309 | const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1310 | CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1311 | |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1312 | uint32_t bitmap_size = 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1313 | const uint8_t* bitmap_pointer = nullptr; |
| 1314 | const uint8_t* methods_pointer = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1315 | if (type != kOatClassNoneCompiled) { |
| 1316 | if (type == kOatClassSomeCompiled) { |
| 1317 | bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); |
| 1318 | bitmap_pointer = after_type_pointer + sizeof(bitmap_size); |
| 1319 | CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
| 1320 | methods_pointer = bitmap_pointer + bitmap_size; |
| 1321 | } else { |
| 1322 | methods_pointer = after_type_pointer; |
| 1323 | } |
| 1324 | CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1325 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1326 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1327 | return OatFile::OatClass(oat_file_, |
| 1328 | status, |
| 1329 | type, |
| 1330 | bitmap_size, |
| 1331 | reinterpret_cast<const uint32_t*>(bitmap_pointer), |
| 1332 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1335 | const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file, |
| 1336 | const char* descriptor, |
| 1337 | size_t hash) { |
| 1338 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 1339 | DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash); |
| 1340 | if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) { |
| 1341 | const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash); |
| 1342 | return (class_def_idx != DexFile::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr; |
| 1343 | } |
| 1344 | // Fast path for rare no class defs case. |
| 1345 | const uint32_t num_class_defs = dex_file.NumClassDefs(); |
| 1346 | if (num_class_defs == 0) { |
| 1347 | return nullptr; |
| 1348 | } |
| 1349 | const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor); |
| 1350 | if (type_id != nullptr) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1351 | dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1352 | return dex_file.FindClassDef(type_idx); |
| 1353 | } |
| 1354 | return nullptr; |
| 1355 | } |
| 1356 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1357 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1358 | mirror::Class::Status status, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1359 | OatClassType type, |
| 1360 | uint32_t bitmap_size, |
| 1361 | const uint32_t* bitmap_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1362 | const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1363 | : oat_file_(oat_file), status_(status), type_(type), |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1364 | bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1365 | switch (type_) { |
| 1366 | case kOatClassAllCompiled: { |
| 1367 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1368 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1369 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1370 | break; |
| 1371 | } |
| 1372 | case kOatClassSomeCompiled: { |
| 1373 | CHECK_NE(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1374 | CHECK(bitmap_pointer != nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1375 | CHECK(methods_pointer != nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1376 | break; |
| 1377 | } |
| 1378 | case kOatClassNoneCompiled: { |
| 1379 | CHECK_EQ(0U, bitmap_size); |
Brian Carlstrom | cd937a9 | 2014-03-04 22:53:23 -0800 | [diff] [blame] | 1380 | CHECK(bitmap_pointer == nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 1381 | CHECK(methods_pointer_ == nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1382 | break; |
| 1383 | } |
| 1384 | case kOatClassMax: { |
| 1385 | LOG(FATAL) << "Invalid OatClassType " << type_; |
| 1386 | break; |
| 1387 | } |
| 1388 | } |
| 1389 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1390 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1391 | uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const { |
| 1392 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 1393 | if (oat_method_offsets == nullptr) { |
| 1394 | return 0u; |
| 1395 | } |
| 1396 | return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin(); |
| 1397 | } |
| 1398 | |
| 1399 | const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1400 | // 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] | 1401 | if (methods_pointer_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1402 | CHECK_EQ(kOatClassNoneCompiled, type_); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1403 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1404 | } |
| 1405 | size_t methods_pointer_index; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1406 | if (bitmap_ == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1407 | CHECK_EQ(kOatClassAllCompiled, type_); |
| 1408 | methods_pointer_index = method_index; |
| 1409 | } else { |
| 1410 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1411 | if (!BitVector::IsBitSet(bitmap_, method_index)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1412 | return nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1413 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1414 | size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); |
| 1415 | methods_pointer_index = num_set_bits; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1416 | } |
| 1417 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1418 | return &oat_method_offsets; |
| 1419 | } |
| 1420 | |
| 1421 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 1422 | const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); |
| 1423 | if (oat_method_offsets == nullptr) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1424 | return OatMethod(nullptr, 0); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1425 | } |
| 1426 | if (oat_file_->IsExecutable() || |
| 1427 | Runtime::Current() == nullptr || // This case applies for oatdump. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1428 | Runtime::Current()->IsAotCompiler()) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1429 | return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1430 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1431 | // We aren't allowed to use the compiled code. We just force it down the interpreted / jit |
| 1432 | // version. |
| 1433 | return OatMethod(oat_file_->Begin(), 0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1436 | void OatFile::OatMethod::LinkMethod(ArtMethod* method) const { |
Andreas Gampe | fa8429b | 2015-04-07 18:34:42 -0700 | [diff] [blame] | 1437 | CHECK(method != nullptr); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1438 | method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 1441 | bool OatFile::HasPatchInfo() const { |
| 1442 | return GetOatHeader().HasPatchInfo(); |
| 1443 | } |
| 1444 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1445 | bool OatFile::IsPic() const { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1446 | return GetOatHeader().IsPic(); |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1447 | // TODO: Check against oat_patches. b/18144996 |
| 1448 | } |
| 1449 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 1450 | bool OatFile::IsDebuggable() const { |
| 1451 | return GetOatHeader().IsDebuggable(); |
| 1452 | } |
| 1453 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1454 | CompilerFilter::Filter OatFile::GetCompilerFilter() const { |
| 1455 | return GetOatHeader().GetCompilerFilter(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1458 | static constexpr char kDexClassPathEncodingSeparator = '*'; |
| 1459 | |
| 1460 | std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) { |
| 1461 | std::ostringstream out; |
| 1462 | |
| 1463 | for (const DexFile* dex_file : dex_files) { |
| 1464 | out << dex_file->GetLocation().c_str(); |
| 1465 | out << kDexClassPathEncodingSeparator; |
| 1466 | out << dex_file->GetLocationChecksum(); |
| 1467 | out << kDexClassPathEncodingSeparator; |
| 1468 | } |
| 1469 | |
| 1470 | return out.str(); |
| 1471 | } |
| 1472 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1473 | bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1474 | if (dex_dependencies == nullptr || dex_dependencies[0] == 0) { |
| 1475 | // No dependencies. |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
| 1479 | // Assumption: this is not performance-critical. So it's OK to do this with a std::string and |
| 1480 | // Split() instead of manual parsing of the combined char*. |
| 1481 | std::vector<std::string> split; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1482 | Split(dex_dependencies, kDexClassPathEncodingSeparator, &split); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1483 | if (split.size() % 2 != 0) { |
| 1484 | // Expected pairs of location and checksum. |
| 1485 | *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies); |
| 1486 | return false; |
| 1487 | } |
| 1488 | |
| 1489 | for (auto it = split.begin(), end = split.end(); it != end; it += 2) { |
| 1490 | std::string& location = *it; |
| 1491 | std::string& checksum = *(it + 1); |
| 1492 | int64_t converted = strtoll(checksum.c_str(), nullptr, 10); |
| 1493 | if (converted == 0) { |
| 1494 | // Conversion error. |
| 1495 | *msg = StringPrintf("Conversion error for %s", checksum.c_str()); |
| 1496 | return false; |
| 1497 | } |
| 1498 | |
| 1499 | uint32_t dex_checksum; |
| 1500 | std::string error_msg; |
| 1501 | if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1502 | &dex_checksum, |
| 1503 | &error_msg)) { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1504 | if (converted != dex_checksum) { |
| 1505 | *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u", |
| 1506 | location.c_str(), converted, dex_checksum); |
| 1507 | return false; |
| 1508 | } |
| 1509 | } else { |
| 1510 | // Problem retrieving checksum. |
| 1511 | // TODO: odex files? |
| 1512 | *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(), |
| 1513 | error_msg.c_str()); |
| 1514 | return false; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | return true; |
| 1519 | } |
| 1520 | |
| 1521 | bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1522 | std::vector<std::string>* locations) { |
| 1523 | DCHECK(locations != nullptr); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1524 | if (dex_dependencies == nullptr || dex_dependencies[0] == 0) { |
| 1525 | return true; |
| 1526 | } |
| 1527 | |
| 1528 | // Assumption: this is not performance-critical. So it's OK to do this with a std::string and |
| 1529 | // Split() instead of manual parsing of the combined char*. |
| 1530 | std::vector<std::string> split; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1531 | Split(dex_dependencies, kDexClassPathEncodingSeparator, &split); |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 1532 | if (split.size() % 2 != 0) { |
| 1533 | // Expected pairs of location and checksum. |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | for (auto it = split.begin(), end = split.end(); it != end; it += 2) { |
| 1538 | locations->push_back(*it); |
| 1539 | } |
| 1540 | |
| 1541 | return true; |
| 1542 | } |
| 1543 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1544 | OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file, |
| 1545 | uint16_t class_def_idx, |
| 1546 | bool* found) { |
| 1547 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
| 1548 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1549 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1550 | *found = false; |
| 1551 | return OatFile::OatClass::Invalid(); |
| 1552 | } |
| 1553 | *found = true; |
| 1554 | return oat_dex_file->GetOatClass(class_def_idx); |
| 1555 | } |
| 1556 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1557 | void OatFile::OatDexFile::AssertAotCompiler() { |
| 1558 | CHECK(Runtime::Current()->IsAotCompiler()); |
| 1559 | } |
| 1560 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1561 | } // namespace art |