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