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