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