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