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