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