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