blob: 67803b61214d5dd751cb8f8de8ed235c125c3b5a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_file.h"
18
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019#include <dlfcn.h>
David Srbecky1baabf02015-06-16 17:12:34 +000020#ifndef __APPLE__
21#include <link.h> // for dl_iterate_phdr.
22#endif
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include <unistd.h>
24
25#include <cstdlib>
26#include <cstring>
Richard Uhlere5fed032015-03-18 08:21:11 -070027#include <sstream>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include <type_traits>
Shubham Ajmerab22dea02017-10-04 18:36:41 -070029#include <sys/stat.h>
Richard Uhlere5fed032015-03-18 08:21:11 -070030
Andreas Gampefa8429b2015-04-07 18:34:42 -070031// dlopen_ext support from bionic.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010032#ifdef ART_TARGET_ANDROID
Andreas Gampefa8429b2015-04-07 18:34:42 -070033#include "android/dlext.h"
34#endif
35
Andreas Gampe875b4f22018-11-19 12:59:15 -080036#include <android-base/logging.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080037#include "android-base/stringprintf.h"
38
Andreas Gampec6ea7d02017-02-01 16:46:28 -080039#include "art_method.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070040#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070041#include "base/enums.h"
David Sehr891a50e2017-10-27 17:01:07 -070042#include "base/file_utils.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080043#include "base/logging.h" // For VLOG_IS_ON.
David Sehr79e26072018-04-06 17:58:50 -070044#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080045#include "base/os.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080046#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080047#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080048#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080049#include "base/utils.h"
David Sehr013fd802018-01-11 22:55:24 -080050#include "dex/art_dex_file_loader.h"
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080051#include "dex/dex_file.h"
David Sehr9e734c72018-01-04 17:56:19 -080052#include "dex/dex_file_loader.h"
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080053#include "dex/dex_file_structs.h"
David Sehr9e734c72018-01-04 17:56:19 -080054#include "dex/dex_file_types.h"
55#include "dex/standard_dex_file.h"
David Sehr9c4a0152018-04-05 12:23:54 -070056#include "dex/type_lookup_table.h"
David Sehr0225f8e2018-01-31 08:52:24 +000057#include "dex/utf-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070059#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000060#include "gc_root.h"
Vladimir Marko1cedb4a2019-02-06 14:13:28 +000061#include "gc/heap.h"
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010062#include "gc/space/image_space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070064#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070065#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010066#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070067#include "oat_file_manager.h"
Vladimir Marko1cedb4a2019-02-06 14:13:28 +000068#include "runtime-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010069#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070070
71namespace art {
72
Andreas Gampe46ee31b2016-12-14 10:11:49 -080073using android::base::StringPrintf;
74
Andreas Gampe049cff02015-12-01 23:27:12 -080075// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000076static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070077
Andreas Gampe049cff02015-12-01 23:27:12 -080078// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070079// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
80// multiple times). However, if/when we switch the above, we likely want to switch this, too,
81// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000082static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070083
84// For debugging, Open will print DlOpen error message if set to true.
85static constexpr bool kPrintDlOpenErrorMessage = false;
86
Andreas Gampe049cff02015-12-01 23:27:12 -080087// Note for OatFileBase and descendents:
88//
89// These are used in OatFile::Open to try all our loaders.
90//
91// The process is simple:
92//
93// 1) Allocate an instance through the standard constructor (location, executable)
94// 2) Load() to try to open the file.
95// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
96// 4) PreSetup() for any steps that should be done before the final setup.
97// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070098
Andreas Gampe049cff02015-12-01 23:27:12 -080099class OatFileBase : public OatFile {
100 public:
101 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -0700102
Andreas Gampe049cff02015-12-01 23:27:12 -0800103 template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100104 static OatFileBase* OpenOatFile(int zip_fd,
105 const std::string& vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100106 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -0700107 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800108 bool writable,
109 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800110 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700111 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100112 /*inout*/MemMap* reservation, // Where to load if not null.
113 /*out*/std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800114
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700115 template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100116 static OatFileBase* OpenOatFile(int zip_fd,
117 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700118 int oat_fd,
119 const std::string& vdex_filename,
120 const std::string& oat_filename,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700121 bool writable,
122 bool executable,
123 bool low_4gb,
124 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100125 /*inout*/MemMap* reservation, // Where to load if not null.
126 /*out*/std::string* error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700127
Andreas Gampe049cff02015-12-01 23:27:12 -0800128 protected:
129 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700130
Andreas Gampe049cff02015-12-01 23:27:12 -0800131 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
132 std::string* error_msg) const = 0;
133
Andreas Gampe4075f832016-05-18 13:09:54 -0700134 virtual void PreLoad() = 0;
135
David Brazdil7b49e6c2016-09-01 11:06:18 +0100136 bool LoadVdex(const std::string& vdex_filename,
137 bool writable,
138 bool low_4gb,
139 std::string* error_msg);
140
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700141 bool LoadVdex(int vdex_fd,
142 const std::string& vdex_filename,
143 bool writable,
144 bool low_4gb,
145 std::string* error_msg);
146
Andreas Gampe049cff02015-12-01 23:27:12 -0800147 virtual bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800148 bool writable,
149 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800150 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100151 /*inout*/MemMap* reservation, // Where to load if not null.
152 /*out*/std::string* error_msg) = 0;
Andreas Gampe049cff02015-12-01 23:27:12 -0800153
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700154 virtual bool Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700155 bool writable,
156 bool executable,
157 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100158 /*inout*/MemMap* reservation, // Where to load if not null.
159 /*out*/std::string* error_msg) = 0;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700160
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100161 bool ComputeFields(const std::string& file_path, std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800162
163 virtual void PreSetup(const std::string& elf_filename) = 0;
164
Nicolas Geoffray30025092018-04-19 14:43:29 +0100165 bool Setup(int zip_fd, const char* abs_dex_location, std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800166
167 // Setters exposed for ElfOatFile.
168
169 void SetBegin(const uint8_t* begin) {
170 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700171 }
172
Andreas Gampe049cff02015-12-01 23:27:12 -0800173 void SetEnd(const uint8_t* end) {
174 end_ = end;
175 }
176
David Brazdilc93b3be2016-09-12 18:49:58 +0100177 void SetVdex(VdexFile* vdex) {
178 vdex_.reset(vdex);
179 }
180
Andreas Gampe049cff02015-12-01 23:27:12 -0800181 private:
182 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
183};
184
185template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100186OatFileBase* OatFileBase::OpenOatFile(int zip_fd,
187 const std::string& vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100188 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800189 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800190 bool writable,
191 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800192 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800193 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100194 /*inout*/MemMap* reservation,
195 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800196 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700197
198 ret->PreLoad();
199
Andreas Gampe049cff02015-12-01 23:27:12 -0800200 if (!ret->Load(elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800201 writable,
202 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800203 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100204 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -0800205 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800206 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800207 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800208
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100209 if (!ret->ComputeFields(elf_filename, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800210 return nullptr;
211 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700212
David Sehr2300b2d2018-05-10 14:20:10 -0700213 ret->PreSetup(elf_filename);
214
David Srbeckyec2cdf42017-12-08 16:21:25 +0000215 if (!ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
216 return nullptr;
217 }
218
Nicolas Geoffray30025092018-04-19 14:43:29 +0100219 if (!ret->Setup(zip_fd, abs_dex_location, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800220 return nullptr;
221 }
222
Dave Allison69dfe512014-07-11 17:11:58 +0000223 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800224}
225
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700226template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100227OatFileBase* OatFileBase::OpenOatFile(int zip_fd,
228 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700229 int oat_fd,
230 const std::string& vdex_location,
231 const std::string& oat_location,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700232 bool writable,
233 bool executable,
234 bool low_4gb,
235 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100236 /*inout*/MemMap* reservation,
237 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700238 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(oat_location, executable));
239
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700240 if (!ret->Load(oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700241 writable,
242 executable,
243 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100244 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700245 error_msg)) {
246 return nullptr;
247 }
248
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100249 if (!ret->ComputeFields(oat_location, error_msg)) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700250 return nullptr;
251 }
252
David Sehr2300b2d2018-05-10 14:20:10 -0700253 ret->PreSetup(oat_location);
254
David Srbeckyec2cdf42017-12-08 16:21:25 +0000255 if (!ret->LoadVdex(vdex_fd, vdex_location, writable, low_4gb, error_msg)) {
256 return nullptr;
257 }
258
Nicolas Geoffray30025092018-04-19 14:43:29 +0100259 if (!ret->Setup(zip_fd, abs_dex_location, error_msg)) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700260 return nullptr;
261 }
262
263 return ret.release();
264}
265
David Brazdil7b49e6c2016-09-01 11:06:18 +0100266bool OatFileBase::LoadVdex(const std::string& vdex_filename,
267 bool writable,
268 bool low_4gb,
269 std::string* error_msg) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000270 vdex_ = VdexFile::OpenAtAddress(vdex_begin_,
271 vdex_end_ - vdex_begin_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100272 /*mmap_reuse=*/ vdex_begin_ != nullptr,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000273 vdex_filename,
274 writable,
275 low_4gb,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700276 /* unquicken=*/ false,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000277 error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100278 if (vdex_.get() == nullptr) {
279 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
280 vdex_filename.c_str(),
281 error_msg->c_str());
282 return false;
283 }
284 return true;
285}
286
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700287bool OatFileBase::LoadVdex(int vdex_fd,
288 const std::string& vdex_filename,
289 bool writable,
290 bool low_4gb,
291 std::string* error_msg) {
292 if (vdex_fd != -1) {
293 struct stat s;
294 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd, &s));
295 if (rc == -1) {
296 PLOG(WARNING) << "Failed getting length of vdex file";
297 } else {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000298 vdex_ = VdexFile::OpenAtAddress(vdex_begin_,
299 vdex_end_ - vdex_begin_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100300 /*mmap_reuse=*/ vdex_begin_ != nullptr,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000301 vdex_fd,
302 s.st_size,
303 vdex_filename,
304 writable,
305 low_4gb,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100306 /*unquicken=*/ false,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000307 error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700308 if (vdex_.get() == nullptr) {
309 *error_msg = "Failed opening vdex file.";
310 return false;
311 }
312 }
313 }
314 return true;
315}
316
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100317bool OatFileBase::ComputeFields(const std::string& file_path, std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800318 std::string symbol_error_msg;
319 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700320 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800321 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
322 file_path.c_str(),
323 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700324 return false;
325 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800326 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700327 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800328 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
329 file_path.c_str(),
330 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700331 return false;
332 }
333 // Readjust to be non-inclusive upper bound.
334 end_ += sizeof(uint32_t);
335
Vladimir Markob066d432018-01-03 13:14:37 +0000336 data_bimg_rel_ro_begin_ = FindDynamicSymbolAddress("oatdatabimgrelro", &symbol_error_msg);
337 if (data_bimg_rel_ro_begin_ != nullptr) {
338 data_bimg_rel_ro_end_ =
339 FindDynamicSymbolAddress("oatdatabimgrelrolastword", &symbol_error_msg);
340 if (data_bimg_rel_ro_end_ == nullptr) {
341 *error_msg =
342 StringPrintf("Failed to find oatdatabimgrelrolastword symbol in '%s'", file_path.c_str());
343 return false;
344 }
345 // Readjust to be non-inclusive upper bound.
346 data_bimg_rel_ro_end_ += sizeof(uint32_t);
347 }
348
Andreas Gampe049cff02015-12-01 23:27:12 -0800349 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700350 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800351 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700352 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700353 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800354 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700355 if (bss_end_ == nullptr) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000356 *error_msg = StringPrintf("Failed to find oatbsslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700357 return false;
358 }
359 // Readjust to be non-inclusive upper bound.
360 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100361 // Find bss methods if present.
362 bss_methods_ =
363 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000364 // Find bss roots if present.
365 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700366 }
367
David Srbeckyec2cdf42017-12-08 16:21:25 +0000368 vdex_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdex", &symbol_error_msg));
369 if (vdex_begin_ == nullptr) {
370 // No .vdex section.
371 vdex_end_ = nullptr;
372 } else {
373 vdex_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdexlastword", &symbol_error_msg));
374 if (vdex_end_ == nullptr) {
375 *error_msg = StringPrintf("Failed to find oatdexlastword symbol in '%s'", file_path.c_str());
376 return false;
377 }
378 // Readjust to be non-inclusive upper bound.
379 vdex_end_ += sizeof(uint32_t);
380 }
381
Andreas Gampe049cff02015-12-01 23:27:12 -0800382 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800383}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800384
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100385// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
386// position by the number of bytes read, i.e. sizeof(T).
387// Return true on success, false if the read would go beyond the end of the OatFile.
388template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100389inline static bool ReadOatDexFileData(const OatFile& oat_file,
390 /*inout*/const uint8_t** oat,
391 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100392 DCHECK(oat != nullptr);
393 DCHECK(value != nullptr);
394 DCHECK_LE(*oat, oat_file.End());
395 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
396 return false;
397 }
398 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
Andreas Gampec55bb392018-09-21 00:02:02 +0000399 using unaligned_type __attribute__((__aligned__(1))) = T;
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100400 *value = *reinterpret_cast<const unaligned_type*>(*oat);
401 *oat += sizeof(T);
402 return true;
403}
404
Vladimir Markof3c52b42017-11-17 17:32:12 +0000405static bool ReadIndexBssMapping(OatFile* oat_file,
406 /*inout*/const uint8_t** oat,
407 size_t dex_file_index,
408 const std::string& dex_file_location,
409 const char* tag,
410 /*out*/const IndexBssMapping** mapping,
411 std::string* error_msg) {
412 uint32_t index_bss_mapping_offset;
413 if (UNLIKELY(!ReadOatDexFileData(*oat_file, oat, &index_bss_mapping_offset))) {
414 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
415 "after %s bss mapping offset",
416 oat_file->GetLocation().c_str(),
417 dex_file_index,
418 dex_file_location.c_str(),
419 tag);
420 return false;
421 }
422 const bool readable_index_bss_mapping_size =
423 index_bss_mapping_offset != 0u &&
424 index_bss_mapping_offset <= oat_file->Size() &&
425 IsAligned<alignof(IndexBssMapping)>(index_bss_mapping_offset) &&
426 oat_file->Size() - index_bss_mapping_offset >= IndexBssMapping::ComputeSize(0);
427 const IndexBssMapping* index_bss_mapping = readable_index_bss_mapping_size
428 ? reinterpret_cast<const IndexBssMapping*>(oat_file->Begin() + index_bss_mapping_offset)
429 : nullptr;
430 if (index_bss_mapping_offset != 0u &&
431 (UNLIKELY(index_bss_mapping == nullptr) ||
432 UNLIKELY(index_bss_mapping->size() == 0u) ||
433 UNLIKELY(oat_file->Size() - index_bss_mapping_offset <
434 IndexBssMapping::ComputeSize(index_bss_mapping->size())))) {
435 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
436 " truncated %s bss mapping, offset %u of %zu, length %zu",
437 oat_file->GetLocation().c_str(),
438 dex_file_index,
439 dex_file_location.c_str(),
440 tag,
441 index_bss_mapping_offset,
442 oat_file->Size(),
443 index_bss_mapping != nullptr ? index_bss_mapping->size() : 0u);
444 return false;
445 }
446
447 *mapping = index_bss_mapping;
448 return true;
449}
450
Nicolas Geoffray30025092018-04-19 14:43:29 +0100451bool OatFileBase::Setup(int zip_fd, const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700452 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800453 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100454 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
455 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800456 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700457 return false;
458 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100459 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
460 size_t key_value_store_size =
461 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
462 if (Size() < sizeof(OatHeader) + key_value_store_size) {
463 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
464 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100465 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100466 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100467 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100468 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700469 return false;
470 }
471
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100472 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
473 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
474 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
475 "%zu is not in [%zu, %zu]",
476 GetLocation().c_str(),
477 oat_dex_files_offset,
478 GetOatHeader().GetHeaderSize(),
479 Size());
480 return false;
481 }
482 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
483
Vladimir Markob066d432018-01-03 13:14:37 +0000484 if (!IsAligned<sizeof(uint32_t)>(data_bimg_rel_ro_begin_) ||
485 !IsAligned<sizeof(uint32_t)>(data_bimg_rel_ro_end_) ||
486 data_bimg_rel_ro_begin_ > data_bimg_rel_ro_end_) {
487 *error_msg = StringPrintf("In oat file '%s' found unaligned or unordered databimgrelro "
488 "symbol(s): begin = %p, end = %p",
489 GetLocation().c_str(),
490 data_bimg_rel_ro_begin_,
491 data_bimg_rel_ro_end_);
492 return false;
493 }
494
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100495 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
496 if (!IsAligned<kPageSize>(bss_begin_) ||
497 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
498 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000499 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
500 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100501 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000502 GetLocation().c_str(),
503 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100504 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000505 bss_roots_,
506 bss_end_);
507 return false;
508 }
509
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100510 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
511 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
512 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
513 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100514 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000515 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000516 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100517 bss_methods_,
518 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000519 bss_end_);
520 return false;
521 }
522
Vladimir Markoe47f60c2018-02-21 13:43:28 +0000523 if (bss_methods_ != nullptr && bss_methods_ != bss_begin_) {
524 *error_msg = StringPrintf("In oat file '%s' found unexpected .bss gap before 'oatbssmethods': "
525 "begin = %p, methods = %p",
526 GetLocation().c_str(),
527 bss_begin_,
528 bss_methods_);
529 return false;
530 }
531
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100532 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
533 oat_dex_files_storage_.reserve(dex_file_count);
534 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100535 uint32_t dex_file_location_size;
536 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
537 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
538 "location size",
539 GetLocation().c_str(),
540 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700541 return false;
542 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100543 if (UNLIKELY(dex_file_location_size == 0U)) {
544 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
545 GetLocation().c_str(),
546 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700547 return false;
548 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000549 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100550 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
551 "location",
552 GetLocation().c_str(),
553 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700554 return false;
555 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000556 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
557 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700558
Vladimir Markoc4a924d2018-12-19 18:02:47 +0000559 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
560 std::string dex_file_name =
561 ResolveRelativeEncodedDexLocation(abs_dex_location, dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700562
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100563 uint32_t dex_file_checksum;
564 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
565 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
566 "dex file checksum",
567 GetLocation().c_str(),
568 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700569 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700570 return false;
571 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700572
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100573 uint32_t dex_file_offset;
574 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
575 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
576 "after dex file offsets",
577 GetLocation().c_str(),
578 i,
579 dex_file_location.c_str());
580 return false;
581 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100582 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100583 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
584 "offset %u > %zu",
585 GetLocation().c_str(),
586 i,
587 dex_file_location.c_str(),
588 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100589 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700590 return false;
591 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000592 const uint8_t* dex_file_pointer = nullptr;
593 if (UNLIKELY(dex_file_offset == 0U)) {
594 if (uncompressed_dex_files_ == nullptr) {
Andreas Gampefc604a72018-02-08 15:43:37 -0800595 // Do not support mixed-mode oat files.
596 if (i > 0) {
597 *error_msg = StringPrintf("In oat file '%s', unsupported uncompressed-dex-file for dex "
598 "file %zu (%s)",
599 GetLocation().c_str(),
600 i,
601 dex_file_location.c_str());
602 return false;
603 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000604 uncompressed_dex_files_.reset(new std::vector<std::unique_ptr<const DexFile>>());
605 // No dex files, load it from location.
606 const ArtDexFileLoader dex_file_loader;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100607 bool loaded = false;
608 if (zip_fd != -1) {
609 loaded = dex_file_loader.OpenZip(zip_fd,
610 dex_file_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100611 /*verify=*/ false,
612 /*verify_checksum=*/ false,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100613 error_msg,
614 uncompressed_dex_files_.get());
615 } else {
Vladimir Markoc4a924d2018-12-19 18:02:47 +0000616 loaded = dex_file_loader.Open(dex_file_name.c_str(),
Nicolas Geoffray30025092018-04-19 14:43:29 +0100617 dex_file_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100618 /*verify=*/ false,
619 /*verify_checksum=*/ false,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100620 error_msg,
621 uncompressed_dex_files_.get());
622 }
623 if (!loaded) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000624 if (Runtime::Current() == nullptr) {
625 // If there's no runtime, we're running oatdump, so return
626 // a half constructed oat file that oatdump knows how to deal with.
627 LOG(WARNING) << "Could not find associated dex files of oat file. "
628 << "Oatdump will only dump the header.";
629 return true;
630 } else {
631 return false;
632 }
633 }
Andreas Gampefc604a72018-02-08 15:43:37 -0800634 // The oat file may be out of date wrt/ the dex-file location. We need to be defensive
635 // here and ensure that at least the number of dex files still matches.
636 // Note: actual checksum comparisons are the duty of the OatFileAssistant and will be
637 // done after loading the OatFile.
638 if (uncompressed_dex_files_->size() != dex_file_count) {
639 *error_msg = StringPrintf("In oat file '%s', expected %u uncompressed dex files, but "
640 "found %zu in '%s'",
641 GetLocation().c_str(),
642 dex_file_count,
643 uncompressed_dex_files_->size(),
644 dex_file_location.c_str());
645 return false;
646 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000647 }
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100648 dex_file_pointer = (*uncompressed_dex_files_)[i]->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000649 } else {
Andreas Gampefc604a72018-02-08 15:43:37 -0800650 // Do not support mixed-mode oat files.
651 if (uncompressed_dex_files_ != nullptr) {
652 *error_msg = StringPrintf("In oat file '%s', unsupported embedded dex-file for dex file "
653 "%zu (%s)",
654 GetLocation().c_str(),
655 i,
656 dex_file_location.c_str());
657 return false;
658 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000659 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
660 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
661 "offset %u of %zu but the size of dex file header is %zu",
662 GetLocation().c_str(),
663 i,
664 dex_file_location.c_str(),
665 dex_file_offset,
666 DexSize(),
667 sizeof(DexFile::Header));
668 return false;
669 }
670 dex_file_pointer = DexBegin() + dex_file_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000671 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800672
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700673 const bool valid_magic = DexFileLoader::IsMagicValid(dex_file_pointer);
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700674 if (UNLIKELY(!valid_magic)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100675 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
676 "dex file magic '%s'",
677 GetLocation().c_str(),
678 i,
679 dex_file_location.c_str(),
680 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700681 return false;
682 }
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700683 if (UNLIKELY(!DexFileLoader::IsVersionAndMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100684 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
685 "dex file version '%s'",
686 GetLocation().c_str(),
687 i,
688 dex_file_location.c_str(),
689 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700690 return false;
691 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800692 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000693 if (dex_file_offset != 0 && (DexSize() - dex_file_offset < header->file_size_)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000694 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
695 "offset %u and size %u truncated at %zu",
696 GetLocation().c_str(),
697 i,
698 dex_file_location.c_str(),
699 dex_file_offset,
700 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100701 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000702 return false;
703 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300704
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000705 uint32_t class_offsets_offset;
706 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
707 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
708 "after class offsets offset",
709 GetLocation().c_str(),
710 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300711 dex_file_location.c_str());
712 return false;
713 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000714 if (UNLIKELY(class_offsets_offset > Size()) ||
715 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
716 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
717 "class offsets, offset %u of %zu, class defs %u",
718 GetLocation().c_str(),
719 i,
720 dex_file_location.c_str(),
721 class_offsets_offset,
722 Size(),
723 header->class_defs_size_);
724 return false;
725 }
726 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
727 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
728 "class offsets, offset %u",
729 GetLocation().c_str(),
730 i,
731 dex_file_location.c_str(),
732 class_offsets_offset);
733 return false;
734 }
735 const uint32_t* class_offsets_pointer =
736 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
737
738 uint32_t lookup_table_offset;
739 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
740 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
741 "after lookup table offset",
742 GetLocation().c_str(),
743 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300744 dex_file_location.c_str());
745 return false;
746 }
747 const uint8_t* lookup_table_data = lookup_table_offset != 0u
748 ? Begin() + lookup_table_offset
749 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000750 if (lookup_table_offset != 0u &&
751 (UNLIKELY(lookup_table_offset > Size()) ||
752 UNLIKELY(Size() - lookup_table_offset <
753 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100754 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000755 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100756 GetLocation().c_str(),
757 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000758 dex_file_location.c_str(),
759 lookup_table_offset,
760 Size(),
761 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700762 return false;
763 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700764
Mathieu Chartier120aa282017-08-05 16:03:03 -0700765 uint32_t dex_layout_sections_offset;
766 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
767 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
768 "after dex layout sections offset",
769 GetLocation().c_str(),
770 i,
771 dex_file_location.c_str());
772 return false;
773 }
774 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
775 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
776 : nullptr;
777
Vladimir Markof3c52b42017-11-17 17:32:12 +0000778 const IndexBssMapping* method_bss_mapping;
779 const IndexBssMapping* type_bss_mapping;
780 const IndexBssMapping* string_bss_mapping;
781 if (!ReadIndexBssMapping(
782 this, &oat, i, dex_file_location, "method", &method_bss_mapping, error_msg) ||
783 !ReadIndexBssMapping(
784 this, &oat, i, dex_file_location, "type", &type_bss_mapping, error_msg) ||
785 !ReadIndexBssMapping(
786 this, &oat, i, dex_file_location, "string", &string_bss_mapping, error_msg)) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100787 return false;
788 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100789
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700790 std::string canonical_location =
Vladimir Markoc4a924d2018-12-19 18:02:47 +0000791 DexFileLoader::GetDexCanonicalLocation(dex_file_name.c_str());
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100792
793 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100794 OatDexFile* oat_dex_file = new OatDexFile(this,
795 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100796 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100797 dex_file_checksum,
798 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300799 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100800 method_bss_mapping,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000801 type_bss_mapping,
802 string_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000803 class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700804 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100805 oat_dex_files_storage_.push_back(oat_dex_file);
806
807 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko59ae4f92019-02-04 14:06:02 +0000808 std::string_view key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100809 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100810 if (canonical_location != dex_file_location) {
Vladimir Marko59ae4f92019-02-04 14:06:02 +0000811 std::string_view canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100812 oat_dex_files_.Put(canonical_key, oat_dex_file);
813 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700814 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100815
Vladimir Markob066d432018-01-03 13:14:37 +0000816 Runtime* runtime = Runtime::Current();
817
818 if (DataBimgRelRoBegin() != nullptr) {
819 // Make .data.bimg.rel.ro read only. ClassLinker shall make it writable for relocation.
820 uint8_t* reloc_begin = const_cast<uint8_t*>(DataBimgRelRoBegin());
821 CheckedCall(mprotect, "protect relocations", reloc_begin, DataBimgRelRoSize(), PROT_READ);
822 if (UNLIKELY(runtime == nullptr)) {
823 // This must be oatdump without boot image.
824 } else if (!IsExecutable()) {
825 // Do not check whether we have a boot image if the oat file is not executable.
826 } else if (UNLIKELY(runtime->GetHeap()->GetBootImageSpaces().empty())) {
827 *error_msg = StringPrintf("Cannot load oat file '%s' with .data.bimg.rel.ro as executable "
828 "without boot image.",
829 GetLocation().c_str());
830 return false;
831 } else {
832 // ClassLinker shall perform the relocation when we register a dex file from
833 // this oat file. We do not do the relocation here to avoid dirtying the pages
834 // if the code is never actually ready to be executed.
835 }
836 }
837
Brian Carlstromf1b30302013-03-28 10:35:32 -0700838 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700839}
840
Andreas Gampe049cff02015-12-01 23:27:12 -0800841////////////////////////
842// OatFile via dlopen //
843////////////////////////
844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100845class DlOpenOatFile final : public OatFileBase {
Andreas Gampe049cff02015-12-01 23:27:12 -0800846 public:
847 DlOpenOatFile(const std::string& filename, bool executable)
848 : OatFileBase(filename, executable),
849 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700850 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800851 }
852
853 ~DlOpenOatFile() {
854 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700855 if (!kIsTargetBuild) {
856 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
857 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700858 dlclose(dlopen_handle_);
859 } else {
860 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700861 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800862 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800863 }
864
865 protected:
866 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100867 std::string* error_msg) const override {
Andreas Gampe049cff02015-12-01 23:27:12 -0800868 const uint8_t* ptr =
869 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
870 if (ptr == nullptr) {
871 *error_msg = dlerror();
872 }
873 return ptr;
874 }
875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100876 void PreLoad() override;
Andreas Gampe4075f832016-05-18 13:09:54 -0700877
Andreas Gampe049cff02015-12-01 23:27:12 -0800878 bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800879 bool writable,
880 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800881 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100882 /*inout*/MemMap* reservation, // Where to load if not null.
883 /*out*/std::string* error_msg) override;
Andreas Gampe049cff02015-12-01 23:27:12 -0800884
Vladimir Markoc09cd052018-08-23 16:36:36 +0100885 bool Load(int oat_fd ATTRIBUTE_UNUSED,
886 bool writable ATTRIBUTE_UNUSED,
887 bool executable ATTRIBUTE_UNUSED,
888 bool low_4gb ATTRIBUTE_UNUSED,
889 /*inout*/MemMap* reservation ATTRIBUTE_UNUSED,
890 /*out*/std::string* error_msg ATTRIBUTE_UNUSED) override {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700891 return false;
892 }
893
Andreas Gampe049cff02015-12-01 23:27:12 -0800894 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100895 void PreSetup(const std::string& elf_filename) override;
Andreas Gampe049cff02015-12-01 23:27:12 -0800896
897 private:
898 bool Dlopen(const std::string& elf_filename,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100899 /*inout*/MemMap* reservation, // Where to load if not null.
900 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800901
Richard Uhlera206c742016-05-24 15:04:22 -0700902 // On the host, if the same library is loaded again with dlopen the same
903 // file handle is returned. This differs from the behavior of dlopen on the
904 // target, where dlopen reloads the library at a different address every
905 // time you load it. The runtime relies on the target behavior to ensure
906 // each instance of the loaded library has a unique dex cache. To avoid
907 // problems, we fall back to our own linker in the case when the same
908 // library is opened multiple times on host. dlopen_handles_ is used to
909 // detect that case.
910 // Guarded by host_dlopen_handles_lock_;
911 static std::unordered_set<void*> host_dlopen_handles_;
912
Vladimir Markoc09cd052018-08-23 16:36:36 +0100913 // Reservation and dummy memory map objects corresponding to the regions mapped by dlopen.
914 // Note: Must be destroyed after dlclose() as it can hold the owning reservation.
915 std::vector<MemMap> dlopen_mmaps_;
916
Andreas Gampe049cff02015-12-01 23:27:12 -0800917 // dlopen handle during runtime.
918 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
919
Andreas Gampe4075f832016-05-18 13:09:54 -0700920 // The number of shared objects the linker told us about before loading. Used to
921 // (optimistically) optimize the PreSetup stage (see comment there).
922 size_t shared_objects_before_;
923
Andreas Gampe049cff02015-12-01 23:27:12 -0800924 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
925};
926
Richard Uhlera206c742016-05-24 15:04:22 -0700927std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
928
Andreas Gampe4075f832016-05-18 13:09:54 -0700929void DlOpenOatFile::PreLoad() {
930#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700931 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700932 LOG(FATAL) << "Should not reach here.";
933 UNREACHABLE();
934#else
935 // Count the entries in dl_iterate_phdr we get at this point in time.
936 struct dl_iterate_context {
Vladimir Markoc09cd052018-08-23 16:36:36 +0100937 static int callback(dl_phdr_info* info ATTRIBUTE_UNUSED,
Andreas Gampe4075f832016-05-18 13:09:54 -0700938 size_t size ATTRIBUTE_UNUSED,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100939 void* data) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700940 reinterpret_cast<dl_iterate_context*>(data)->count++;
941 return 0; // Continue iteration.
942 }
943 size_t count = 0;
944 } context;
945
946 dl_iterate_phdr(dl_iterate_context::callback, &context);
947 shared_objects_before_ = context.count;
948#endif
949}
950
Andreas Gampe049cff02015-12-01 23:27:12 -0800951bool DlOpenOatFile::Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800952 bool writable,
953 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800954 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100955 /*inout*/MemMap* reservation, // Where to load if not null.
956 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800957 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
958 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
959 // !executable is a sign that we may want to patch), which may not be allowed for
960 // various reasons.
961 if (!kUseDlopen) {
962 *error_msg = "DlOpen is disabled.";
963 return false;
964 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800965 if (low_4gb) {
966 *error_msg = "DlOpen does not support low 4gb loading.";
967 return false;
968 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800969 if (writable) {
970 *error_msg = "DlOpen does not support writable loading.";
971 return false;
972 }
973 if (!executable) {
974 *error_msg = "DlOpen does not support non-executable loading.";
975 return false;
976 }
977
978 // dlopen always returns the same library if it is already opened on the host. For this reason
979 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
980 // the same library loaded multiple times at different addresses is required for class unloading
981 // and for having dex caches arrays in the .bss section.
982 if (!kIsTargetBuild) {
983 if (!kUseDlopenOnHost) {
984 *error_msg = "DlOpen disabled for host.";
985 return false;
986 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800987 }
988
Vladimir Markoc09cd052018-08-23 16:36:36 +0100989 bool success = Dlopen(elf_filename, reservation, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800990 DCHECK(dlopen_handle_ != nullptr || !success);
991
992 return success;
993}
994
995bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100996 /*inout*/MemMap* reservation,
997 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800998#ifdef __APPLE__
999 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
1000 // but let's fallback to the custom loading code for the time being.
Vladimir Marko3ec8fb62018-08-31 17:47:38 +01001001 UNUSED(elf_filename, reservation);
Andreas Gampe049cff02015-12-01 23:27:12 -08001002 *error_msg = "Dlopen unsupported on Mac.";
1003 return false;
1004#else
1005 {
1006 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
1007 if (absolute_path == nullptr) {
1008 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
1009 return false;
1010 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +01001011#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +00001012 android_dlextinfo extinfo = {};
Vladimir Markof6cfd002018-11-01 16:53:31 +00001013 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; // Force-load, don't reuse handle
1014 // (open oat files multiple times).
Vladimir Markoc09cd052018-08-23 16:36:36 +01001015 if (reservation != nullptr) {
1016 if (!reservation->IsValid()) {
1017 *error_msg = StringPrintf("Invalid reservation for %s", elf_filename.c_str());
1018 return false;
1019 }
1020 extinfo.flags |= ANDROID_DLEXT_RESERVED_ADDRESS; // Use the reserved memory range.
1021 extinfo.reserved_addr = reservation->Begin();
1022 extinfo.reserved_size = reservation->Size();
1023 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001024 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
Vladimir Markoc09cd052018-08-23 16:36:36 +01001025 if (reservation != nullptr && dlopen_handle_ != nullptr) {
1026 // Find used pages from the reservation.
1027 struct dl_iterate_context {
1028 static int callback(dl_phdr_info* info, size_t size ATTRIBUTE_UNUSED, void* data) {
1029 auto* context = reinterpret_cast<dl_iterate_context*>(data);
1030 static_assert(std::is_same<Elf32_Half, Elf64_Half>::value, "Half must match");
1031 using Elf_Half = Elf64_Half;
1032
1033 // See whether this callback corresponds to the file which we have just loaded.
1034 uint8_t* reservation_begin = context->reservation->Begin();
1035 bool contained_in_reservation = false;
1036 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
1037 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1038 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1039 info->dlpi_phdr[i].p_vaddr);
1040 size_t memsz = info->dlpi_phdr[i].p_memsz;
1041 size_t offset = static_cast<size_t>(vaddr - reservation_begin);
1042 if (offset < context->reservation->Size()) {
1043 contained_in_reservation = true;
1044 DCHECK_LE(memsz, context->reservation->Size() - offset);
1045 } else if (vaddr < reservation_begin) {
1046 // Check that there's no overlap with the reservation.
1047 DCHECK_LE(memsz, static_cast<size_t>(reservation_begin - vaddr));
1048 }
1049 break; // It is sufficient to check the first PT_LOAD header.
1050 }
1051 }
1052
1053 if (contained_in_reservation) {
1054 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
1055 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1056 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1057 info->dlpi_phdr[i].p_vaddr);
1058 size_t memsz = info->dlpi_phdr[i].p_memsz;
1059 size_t offset = static_cast<size_t>(vaddr - reservation_begin);
1060 DCHECK_LT(offset, context->reservation->Size());
1061 DCHECK_LE(memsz, context->reservation->Size() - offset);
1062 context->max_size = std::max(context->max_size, offset + memsz);
1063 }
1064 }
1065
1066 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1067 }
1068 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1069 }
1070
1071 const MemMap* const reservation;
1072 size_t max_size = 0u;
1073 };
1074 dl_iterate_context context = { reservation };
1075
1076 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
1077 LOG(FATAL) << "Could not find the shared object mmapped to the reservation.";
1078 UNREACHABLE();
1079 }
1080
1081 // Take ownership of the memory used by the shared object. dlopen() does not assume
1082 // full ownership of this memory and dlclose() shall just remap it as zero pages with
1083 // PROT_NONE. We need to unmap the memory when destroying this oat file.
1084 dlopen_mmaps_.push_back(reservation->TakeReservedMemory(context.max_size));
1085 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001086#else
Steve Austin882ed6b2018-06-08 11:40:38 -07001087 static_assert(!kIsTargetBuild || kIsTargetLinux || kIsTargetFuchsia,
1088 "host_dlopen_handles_ will leak handles");
Vladimir Markoc09cd052018-08-23 16:36:36 +01001089 if (reservation != nullptr) {
1090 *error_msg = StringPrintf("dlopen() into reserved memory is unsupported on host for '%s'.",
1091 elf_filename.c_str());
1092 return false;
1093 }
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -07001094 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -07001095 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
1096 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -07001097 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
1098 dlclose(dlopen_handle_);
1099 dlopen_handle_ = nullptr;
1100 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
1101 return false;
1102 }
1103 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +01001104#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -08001105 }
1106 if (dlopen_handle_ == nullptr) {
1107 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
1108 return false;
1109 }
1110 return true;
1111#endif
1112}
1113
1114void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -08001115#ifdef __APPLE__
1116 UNUSED(elf_filename);
1117 LOG(FATAL) << "Should not reach here.";
1118 UNREACHABLE();
1119#else
Andreas Gampe049cff02015-12-01 23:27:12 -08001120 struct dl_iterate_context {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001121 static int callback(dl_phdr_info* info, size_t size ATTRIBUTE_UNUSED, void* data) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001122 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Vladimir Markoc09cd052018-08-23 16:36:36 +01001123 static_assert(std::is_same<Elf32_Half, Elf64_Half>::value, "Half must match");
1124 using Elf_Half = Elf64_Half;
1125
Andreas Gampe4075f832016-05-18 13:09:54 -07001126 context->shared_objects_seen++;
1127 if (context->shared_objects_seen < context->shared_objects_before) {
1128 // We haven't been called yet for anything we haven't seen before. Just continue.
1129 // Note: this is aggressively optimistic. If another thread was unloading a library,
1130 // we may miss out here. However, this does not happen often in practice.
1131 return 0;
1132 }
1133
Andreas Gampe049cff02015-12-01 23:27:12 -08001134 // See whether this callback corresponds to the file which we have just loaded.
1135 bool contains_begin = false;
Vladimir Markoc09cd052018-08-23 16:36:36 +01001136 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001137 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1138 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1139 info->dlpi_phdr[i].p_vaddr);
1140 size_t memsz = info->dlpi_phdr[i].p_memsz;
1141 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
1142 contains_begin = true;
1143 break;
1144 }
1145 }
1146 }
1147 // Add dummy mmaps for this file.
1148 if (contains_begin) {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001149 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001150 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1151 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1152 info->dlpi_phdr[i].p_vaddr);
1153 size_t memsz = info->dlpi_phdr[i].p_memsz;
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001154 MemMap mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
1155 context->dlopen_mmaps_->push_back(std::move(mmap));
Andreas Gampe049cff02015-12-01 23:27:12 -08001156 }
1157 }
1158 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1159 }
1160 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1161 }
1162 const uint8_t* const begin_;
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001163 std::vector<MemMap>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -07001164 const size_t shared_objects_before;
1165 size_t shared_objects_seen;
1166 };
1167 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -08001168
1169 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -07001170 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
1171 // before giving up. This should be unusual.
1172 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
1173 << shared_objects_before_;
1174 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
1175 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
1176 // OK, give up and print an error.
Andreas Gampe170331f2017-12-07 18:41:03 -08001177 PrintFileToLog("/proc/self/maps", android::base::LogSeverity::WARNING);
Andreas Gampe4075f832016-05-18 13:09:54 -07001178 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
1179 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001180 }
Andreas Gampe74f07b52015-12-02 11:53:26 -08001181#endif
Andreas Gampe049cff02015-12-01 23:27:12 -08001182}
1183
1184////////////////////////////////////////////////
1185// OatFile via our own ElfFile implementation //
1186////////////////////////////////////////////////
1187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001188class ElfOatFile final : public OatFileBase {
Andreas Gampe049cff02015-12-01 23:27:12 -08001189 public:
1190 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
1191
Nicolas Geoffray30025092018-04-19 14:43:29 +01001192 static ElfOatFile* OpenElfFile(int zip_fd,
1193 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001194 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001195 bool writable,
1196 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001197 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001198 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001199 /*inout*/MemMap* reservation, // Where to load if not null.
1200 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001201
Nicolas Geoffray30025092018-04-19 14:43:29 +01001202 bool InitializeFromElfFile(int zip_fd,
1203 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001204 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001205 const char* abs_dex_location,
1206 std::string* error_msg);
1207
1208 protected:
1209 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001210 std::string* error_msg) const override {
Andreas Gampe049cff02015-12-01 23:27:12 -08001211 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
1212 if (ptr == nullptr) {
1213 *error_msg = "(Internal implementation could not find symbol)";
1214 }
1215 return ptr;
1216 }
1217
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001218 void PreLoad() override {
Andreas Gampe4075f832016-05-18 13:09:54 -07001219 }
1220
Andreas Gampe049cff02015-12-01 23:27:12 -08001221 bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -08001222 bool writable,
1223 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001224 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001225 /*inout*/MemMap* reservation, // Where to load if not null.
1226 /*out*/std::string* error_msg) override;
Andreas Gampe049cff02015-12-01 23:27:12 -08001227
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001228 bool Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001229 bool writable,
1230 bool executable,
1231 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001232 /*inout*/MemMap* reservation, // Where to load if not null.
1233 /*out*/std::string* error_msg) override;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001234
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001235 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) override {
Andreas Gampe049cff02015-12-01 23:27:12 -08001236 }
1237
1238 private:
1239 bool ElfFileOpen(File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001240 bool writable,
1241 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001242 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001243 /*inout*/MemMap* reservation, // Where to load if not null.
1244 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001245
1246 private:
1247 // Backing memory map for oat file during cross compilation.
1248 std::unique_ptr<ElfFile> elf_file_;
1249
1250 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
1251};
1252
Nicolas Geoffray30025092018-04-19 14:43:29 +01001253ElfOatFile* ElfOatFile::OpenElfFile(int zip_fd,
1254 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001255 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001256 bool writable,
1257 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001258 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001259 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001260 /*inout*/MemMap* reservation, // Where to load if not null.
1261 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001262 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001263 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001264 bool success = oat_file->ElfFileOpen(file,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001265 writable,
1266 low_4gb,
1267 executable,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001268 reservation,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001269 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001270 if (!success) {
1271 CHECK(!error_msg->empty());
1272 return nullptr;
1273 }
1274
1275 // Complete the setup.
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001276 if (!oat_file->ComputeFields(file->GetPath(), error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001277 return nullptr;
1278 }
1279
Nicolas Geoffray30025092018-04-19 14:43:29 +01001280 if (!oat_file->Setup(zip_fd, abs_dex_location, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001281 return nullptr;
1282 }
1283
1284 return oat_file.release();
1285}
1286
Nicolas Geoffray30025092018-04-19 14:43:29 +01001287bool ElfOatFile::InitializeFromElfFile(int zip_fd,
1288 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001289 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001290 const char* abs_dex_location,
1291 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001292 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001293 if (IsExecutable()) {
1294 *error_msg = "Cannot initialize from elf file in executable mode.";
1295 return false;
1296 }
1297 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001298 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001299 uint64_t offset, size;
1300 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1301 CHECK(has_section);
1302 SetBegin(elf_file->Begin() + offset);
1303 SetEnd(elf_file->Begin() + size + offset);
1304 // Ignore the optional .bss section when opening non-executable.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001305 return Setup(zip_fd, abs_dex_location, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001306}
1307
1308bool ElfOatFile::Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -08001309 bool writable,
1310 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001311 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001312 /*inout*/MemMap* reservation,
1313 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001314 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001315 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1316 if (file == nullptr) {
1317 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1318 return false;
1319 }
1320 return ElfOatFile::ElfFileOpen(file.get(),
Andreas Gampe049cff02015-12-01 23:27:12 -08001321 writable,
1322 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001323 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001324 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001325 error_msg);
1326}
1327
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001328bool ElfOatFile::Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001329 bool writable,
1330 bool executable,
1331 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001332 /*inout*/MemMap* reservation,
1333 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001334 ScopedTrace trace(__PRETTY_FUNCTION__);
1335 if (oat_fd != -1) {
Josh Gaoafeec9f2018-08-30 14:05:56 -07001336 int duped_fd = DupCloexec(oat_fd);
1337 std::unique_ptr<File> file = std::make_unique<File>(duped_fd, false);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001338 if (file == nullptr) {
1339 *error_msg = StringPrintf("Failed to open oat filename for reading: %s",
1340 strerror(errno));
1341 return false;
1342 }
1343 return ElfOatFile::ElfFileOpen(file.get(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001344 writable,
1345 executable,
1346 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001347 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001348 error_msg);
1349 }
1350 return false;
1351}
1352
Andreas Gampe049cff02015-12-01 23:27:12 -08001353bool ElfOatFile::ElfFileOpen(File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001354 bool writable,
1355 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001356 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001357 /*inout*/MemMap* reservation,
1358 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001359 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001360 elf_file_.reset(ElfFile::Open(file,
1361 writable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001362 /*program_header_only=*/ true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001363 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001364 error_msg));
Andreas Gampe049cff02015-12-01 23:27:12 -08001365 if (elf_file_ == nullptr) {
1366 DCHECK(!error_msg->empty());
1367 return false;
1368 }
Vladimir Markoc09cd052018-08-23 16:36:36 +01001369 bool loaded = elf_file_->Load(file, executable, low_4gb, reservation, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001370 DCHECK(loaded || !error_msg->empty());
1371 return loaded;
1372}
1373
1374//////////////////////////
1375// General OatFile code //
1376//////////////////////////
1377
1378std::string OatFile::ResolveRelativeEncodedDexLocation(
1379 const char* abs_dex_location, const std::string& rel_dex_location) {
Nicolas Geoffray8baa11a2019-01-24 10:30:03 +00001380 if (abs_dex_location != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001381 std::string base = DexFileLoader::GetBaseLocation(rel_dex_location);
Nicolas Geoffray8baa11a2019-01-24 10:30:03 +00001382 // Strip :classes<N>.dex used for secondary multidex files.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001383 std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(rel_dex_location);
Nicolas Geoffray8baa11a2019-01-24 10:30:03 +00001384 if (!kIsTargetBuild) {
1385 // For host, we still do resolution as the rel_dex_location might be absolute
1386 // for a target dex (for example /system/foo/foo.apk).
1387 return std::string(abs_dex_location) + multidex_suffix;
1388 } else if (rel_dex_location[0] != '/') {
1389 // Check if the base is a suffix of the provided abs_dex_location.
1390 std::string target_suffix = ((rel_dex_location[0] != '/') ? "/" : "") + base;
1391 std::string abs_location(abs_dex_location);
1392 if (abs_location.size() > target_suffix.size()) {
1393 size_t pos = abs_location.size() - target_suffix.size();
1394 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1395 return abs_location + multidex_suffix;
1396 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001397 }
1398 }
1399 }
1400 return rel_dex_location;
1401}
1402
1403static void CheckLocation(const std::string& location) {
1404 CHECK(!location.empty());
1405}
1406
Nicolas Geoffray30025092018-04-19 14:43:29 +01001407OatFile* OatFile::OpenWithElfFile(int zip_fd,
1408 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001409 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001410 const std::string& location,
1411 const char* abs_dex_location,
1412 std::string* error_msg) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001413 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, /*executable=*/ false));
Nicolas Geoffray30025092018-04-19 14:43:29 +01001414 return oat_file->InitializeFromElfFile(zip_fd, elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001415 ? oat_file.release()
1416 : nullptr;
1417}
1418
Nicolas Geoffray30025092018-04-19 14:43:29 +01001419OatFile* OatFile::Open(int zip_fd,
1420 const std::string& oat_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001421 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001422 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001423 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001424 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001425 /*inout*/MemMap* reservation,
1426 /*out*/std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001427 ScopedTrace trace("Open oat file " + oat_location);
1428 CHECK(!oat_filename.empty()) << oat_location;
1429 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001430
Calin Juravle367b9d82017-05-15 18:18:39 -07001431 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001432
1433 // Check that the files even exist, fast-fail.
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001434 if (!OS::FileExists(vdex_filename.c_str())) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001435 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1436 return nullptr;
1437 } else if (!OS::FileExists(oat_filename.c_str())) {
1438 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001439 return nullptr;
1440 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001441
1442 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1443 // disabled.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001444 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(zip_fd,
1445 vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001446 oat_filename,
1447 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001448 /*writable=*/ false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001449 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001450 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001451 abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001452 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001453 error_msg);
1454 if (with_dlopen != nullptr) {
1455 return with_dlopen;
1456 }
1457 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001458 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001459 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001460 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1461 //
1462 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1463 //
1464 // We use our own ELF loader for Quick to deal with legacy apps that
1465 // open a generated dex file by name, remove the file, then open
1466 // another generated dex file with the same name. http://b/10614658
1467 //
1468 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1469 //
1470 //
1471 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1472 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001473 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(zip_fd,
1474 vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001475 oat_filename,
1476 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001477 /*writable=*/ false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001478 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001479 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001480 abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001481 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001482 error_msg);
1483 return with_internal;
1484}
1485
Nicolas Geoffray30025092018-04-19 14:43:29 +01001486OatFile* OatFile::Open(int zip_fd,
1487 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001488 int oat_fd,
1489 const std::string& oat_location,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001490 bool executable,
1491 bool low_4gb,
1492 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001493 /*inout*/MemMap* reservation,
1494 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001495 CHECK(!oat_location.empty()) << oat_location;
1496
1497 std::string vdex_location = GetVdexFilename(oat_location);
1498
Nicolas Geoffray30025092018-04-19 14:43:29 +01001499 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(zip_fd,
1500 vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001501 oat_fd,
1502 vdex_location,
1503 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001504 /*writable=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001505 executable,
1506 low_4gb,
1507 abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001508 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001509 error_msg);
1510 return with_internal;
1511}
1512
Nicolas Geoffray30025092018-04-19 14:43:29 +01001513OatFile* OatFile::OpenWritable(int zip_fd,
1514 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001515 const std::string& location,
1516 const char* abs_dex_location,
1517 std::string* error_msg) {
1518 CheckLocation(location);
Nicolas Geoffray30025092018-04-19 14:43:29 +01001519 return ElfOatFile::OpenElfFile(zip_fd,
1520 file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001521 location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001522 /*writable=*/ true,
1523 /*executable=*/ false,
1524 /*low_4gb=*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001525 abs_dex_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001526 /*reservation=*/ nullptr,
Andreas Gampe049cff02015-12-01 23:27:12 -08001527 error_msg);
1528}
1529
Nicolas Geoffray30025092018-04-19 14:43:29 +01001530OatFile* OatFile::OpenReadable(int zip_fd,
1531 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001532 const std::string& location,
1533 const char* abs_dex_location,
1534 std::string* error_msg) {
1535 CheckLocation(location);
Nicolas Geoffray30025092018-04-19 14:43:29 +01001536 return ElfOatFile::OpenElfFile(zip_fd,
1537 file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001538 location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001539 /*writable=*/ false,
1540 /*executable=*/ false,
1541 /*low_4gb=*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001542 abs_dex_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001543 /*reservation=*/ nullptr,
Andreas Gampe049cff02015-12-01 23:27:12 -08001544 error_msg);
1545}
1546
1547OatFile::OatFile(const std::string& location, bool is_executable)
1548 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001549 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001550 begin_(nullptr),
1551 end_(nullptr),
Vladimir Markob066d432018-01-03 13:14:37 +00001552 data_bimg_rel_ro_begin_(nullptr),
1553 data_bimg_rel_ro_end_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001554 bss_begin_(nullptr),
1555 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001556 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001557 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001558 is_executable_(is_executable),
David Srbeckyec2cdf42017-12-08 16:21:25 +00001559 vdex_begin_(nullptr),
1560 vdex_end_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001561 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1562 CHECK(!location_.empty());
1563}
1564
1565OatFile::~OatFile() {
1566 STLDeleteElements(&oat_dex_files_storage_);
1567}
1568
Brian Carlstrome24fa612011-09-29 00:53:55 -07001569const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001570 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001571}
1572
Ian Rogers13735952014-10-08 12:43:28 -07001573const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001574 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001575 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001576}
1577
Ian Rogers13735952014-10-08 12:43:28 -07001578const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001579 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001580 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001581}
1582
David Brazdil7b49e6c2016-09-01 11:06:18 +01001583const uint8_t* OatFile::DexBegin() const {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001584 return vdex_->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001585}
1586
1587const uint8_t* OatFile::DexEnd() const {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001588 return vdex_->End();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001589}
1590
Vladimir Markob066d432018-01-03 13:14:37 +00001591ArrayRef<const uint32_t> OatFile::GetBootImageRelocations() const {
1592 if (data_bimg_rel_ro_begin_ != nullptr) {
1593 const uint32_t* relocations = reinterpret_cast<const uint32_t*>(data_bimg_rel_ro_begin_);
1594 const uint32_t* relocations_end = reinterpret_cast<const uint32_t*>(data_bimg_rel_ro_end_);
1595 return ArrayRef<const uint32_t>(relocations, relocations_end - relocations);
1596 } else {
1597 return ArrayRef<const uint32_t>();
1598 }
1599}
1600
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001601ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1602 if (bss_methods_ != nullptr) {
1603 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1604 ArtMethod** methods_end =
1605 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1606 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1607 } else {
1608 return ArrayRef<ArtMethod*>();
1609 }
1610}
1611
Vladimir Markoaad75c62016-10-03 08:46:48 +00001612ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1613 if (bss_roots_ != nullptr) {
1614 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1615 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1616 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1617 } else {
1618 return ArrayRef<GcRoot<mirror::Object>>();
1619 }
1620}
1621
Andreas Gampeb40d3612018-06-26 15:49:42 -07001622const OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1623 const uint32_t* dex_location_checksum,
1624 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001625 // NOTE: We assume here that the canonical location for a given dex_location never
1626 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1627 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1628 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001629
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001630 // TODO: Additional analysis of usage patterns to see if this can be simplified
1631 // without any performance loss, for example by not doing the first lock-free lookup.
1632
Andreas Gampeb40d3612018-06-26 15:49:42 -07001633 const OatDexFile* oat_dex_file = nullptr;
Vladimir Marko59ae4f92019-02-04 14:06:02 +00001634 std::string_view key(dex_location);
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001635 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1636 // directly mentioned in the oat file and doesn't require locking.
1637 auto primary_it = oat_dex_files_.find(key);
1638 if (primary_it != oat_dex_files_.end()) {
1639 oat_dex_file = primary_it->second;
1640 DCHECK(oat_dex_file != nullptr);
1641 } else {
1642 // This dex_location is not one of the dex locations directly mentioned in the
1643 // oat file. The correct lookup is via the canonical location but first see in
1644 // the secondary_oat_dex_files_ whether we've looked up this location before.
1645 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1646 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1647 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001648 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001649 } else {
1650 // We haven't seen this dex_location before, we must check the canonical location.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001651 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001652 if (dex_canonical_location != dex_location) {
Vladimir Marko59ae4f92019-02-04 14:06:02 +00001653 std::string_view canonical_key(dex_canonical_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001654 auto canonical_it = oat_dex_files_.find(canonical_key);
1655 if (canonical_it != oat_dex_files_.end()) {
1656 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001657 } // else keep null.
1658 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001659
1660 // Copy the key to the string_cache_ and store the result in secondary map.
1661 string_cache_.emplace_back(key.data(), key.length());
Vladimir Marko59ae4f92019-02-04 14:06:02 +00001662 std::string_view key_copy(string_cache_.back());
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001663 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001664 }
1665 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001666
1667 if (oat_dex_file == nullptr) {
1668 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001669 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001670 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1671 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1672 }
1673 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001674 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001675
Richard Uhler9a37efc2016-08-05 16:32:55 -07001676 if (dex_location_checksum != nullptr &&
1677 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1678 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001679 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001680 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1681 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1682 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1683 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1684 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001685 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001686 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001687 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001688 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001689}
1690
Andreas Gampeb40d3612018-06-26 15:49:42 -07001691OatDexFile::OatDexFile(const OatFile* oat_file,
1692 const std::string& dex_file_location,
1693 const std::string& canonical_dex_file_location,
1694 uint32_t dex_file_location_checksum,
1695 const uint8_t* dex_file_pointer,
1696 const uint8_t* lookup_table_data,
1697 const IndexBssMapping* method_bss_mapping_data,
1698 const IndexBssMapping* type_bss_mapping_data,
1699 const IndexBssMapping* string_bss_mapping_data,
1700 const uint32_t* oat_class_offsets_pointer,
1701 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001702 : oat_file_(oat_file),
1703 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001704 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001705 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001706 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001707 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001708 method_bss_mapping_(method_bss_mapping_data),
Vladimir Markof3c52b42017-11-17 17:32:12 +00001709 type_bss_mapping_(type_bss_mapping_data),
1710 string_bss_mapping_(string_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001711 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Vladimir Markoea341d22018-05-11 10:33:37 +01001712 lookup_table_(),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001713 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001714 // Initialize TypeLookupTable.
1715 if (lookup_table_data_ != nullptr) {
1716 // Peek the number of classes from the DexFile.
1717 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1718 const uint32_t num_class_defs = dex_header->class_defs_size_;
1719 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1720 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1721 } else {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001722 const uint8_t* dex_data = dex_file_pointer_;
1723 // TODO: Clean this up to create the type lookup table after the dex file has been created?
1724 if (CompactDexFile::IsMagicValid(dex_header->magic_)) {
1725 dex_data += dex_header->data_off_;
1726 }
1727 lookup_table_ = TypeLookupTable::Open(dex_data, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001728 }
1729 }
1730}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001731
Andreas Gampe875b4f22018-11-19 12:59:15 -08001732OatDexFile::OatDexFile(TypeLookupTable&& lookup_table) : lookup_table_(std::move(lookup_table)) {
1733 // Stripped-down OatDexFile only allowed in the compiler.
1734 CHECK(Runtime::Current() == nullptr || Runtime::Current()->IsAotCompiler());
1735}
Mathieu Chartier1b868492016-11-16 16:22:37 -08001736
Andreas Gampeb40d3612018-06-26 15:49:42 -07001737OatDexFile::~OatDexFile() {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001738
Andreas Gampeb40d3612018-06-26 15:49:42 -07001739size_t OatDexFile::FileSize() const {
Andreas Gampe875b4f22018-11-19 12:59:15 -08001740 DCHECK(dex_file_pointer_ != nullptr);
Ian Rogers05f28c62012-10-23 18:12:13 -07001741 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1742}
1743
Andreas Gampeb40d3612018-06-26 15:49:42 -07001744std::unique_ptr<const DexFile> OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001745 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001746 static constexpr bool kVerify = false;
1747 static constexpr bool kVerifyChecksum = false;
David Sehr013fd802018-01-11 22:55:24 -08001748 const ArtDexFileLoader dex_file_loader;
1749 return dex_file_loader.Open(dex_file_pointer_,
1750 FileSize(),
1751 dex_file_location_,
1752 dex_file_location_checksum_,
1753 this,
1754 kVerify,
1755 kVerifyChecksum,
1756 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001757}
1758
Andreas Gampeb40d3612018-06-26 15:49:42 -07001759uint32_t OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
Andreas Gampe875b4f22018-11-19 12:59:15 -08001760 DCHECK(oat_class_offsets_pointer_ != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001761 return oat_class_offsets_pointer_[class_def_index];
1762}
1763
Andreas Gampeb40d3612018-06-26 15:49:42 -07001764OatFile::OatClass OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001765 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001766
Ian Rogers13735952014-10-08 12:43:28 -07001767 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001768 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001769
Ian Rogers13735952014-10-08 12:43:28 -07001770 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001771 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
Vladimir Marko2c64a832018-01-04 11:31:56 +00001772 ClassStatus status = enum_cast<ClassStatus>(*reinterpret_cast<const int16_t*>(status_pointer));
1773 CHECK_LE(status, ClassStatus::kLast);
Brian Carlstromba150c32013-08-27 17:31:03 -07001774
Ian Rogers13735952014-10-08 12:43:28 -07001775 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001776 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1777 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1778 CHECK_LT(type, kOatClassMax);
1779
Ian Rogers13735952014-10-08 12:43:28 -07001780 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001781 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001782
Brian Carlstromcd937a92014-03-04 22:53:23 -08001783 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001784 const uint8_t* bitmap_pointer = nullptr;
1785 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001786 if (type != kOatClassNoneCompiled) {
1787 if (type == kOatClassSomeCompiled) {
1788 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1789 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1790 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1791 methods_pointer = bitmap_pointer + bitmap_size;
1792 } else {
1793 methods_pointer = after_type_pointer;
1794 }
1795 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001796 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001797
Richard Uhler07b3c232015-03-31 15:57:54 -07001798 return OatFile::OatClass(oat_file_,
1799 status,
1800 type,
1801 bitmap_size,
1802 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1803 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001804}
1805
Hans Boehm206348c2018-12-05 11:11:33 -08001806ArrayRef<const uint8_t> OatDexFile::GetQuickenedInfoOf(const DexFile& dex_file,
1807 uint32_t dex_method_idx) const {
1808 const OatFile* oat_file = GetOatFile();
1809 if (oat_file == nullptr) {
1810 return ArrayRef<const uint8_t>();
1811 } else {
1812 return oat_file->GetVdexFile()->GetQuickenedInfoOf(dex_file, dex_method_idx);
1813 }
1814}
1815
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001816const dex::ClassDef* OatDexFile::FindClassDef(const DexFile& dex_file,
1817 const char* descriptor,
1818 size_t hash) {
Andreas Gampeb40d3612018-06-26 15:49:42 -07001819 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
David Sehr9aa352e2016-09-15 18:13:52 -07001820 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001821 bool used_lookup_table = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001822 const dex::ClassDef* lookup_table_classdef = nullptr;
Vladimir Markoea341d22018-05-11 10:33:37 +01001823 if (LIKELY((oat_dex_file != nullptr) && oat_dex_file->GetTypeLookupTable().Valid())) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001824 used_lookup_table = true;
Vladimir Markoea341d22018-05-11 10:33:37 +01001825 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable().Lookup(descriptor, hash);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001826 lookup_table_classdef = (class_def_idx != dex::kDexNoIndex)
1827 ? &dex_file.GetClassDef(class_def_idx)
1828 : nullptr;
1829 if (!kIsDebugBuild) {
1830 return lookup_table_classdef;
1831 }
David Sehr9aa352e2016-09-15 18:13:52 -07001832 }
1833 // Fast path for rare no class defs case.
1834 const uint32_t num_class_defs = dex_file.NumClassDefs();
1835 if (num_class_defs == 0) {
Vladimir Markoea341d22018-05-11 10:33:37 +01001836 DCHECK(!used_lookup_table);
David Sehr9aa352e2016-09-15 18:13:52 -07001837 return nullptr;
1838 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001839 const dex::TypeId* type_id = dex_file.FindTypeId(descriptor);
David Sehr9aa352e2016-09-15 18:13:52 -07001840 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001841 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001842 const dex::ClassDef* found_class_def = dex_file.FindClassDef(type_idx);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001843 if (kIsDebugBuild && used_lookup_table) {
1844 DCHECK_EQ(found_class_def, lookup_table_classdef);
1845 }
1846 return found_class_def;
David Sehr9aa352e2016-09-15 18:13:52 -07001847 }
1848 return nullptr;
1849}
1850
Mathieu Chartier120aa282017-08-05 16:03:03 -07001851// Madvise the dex file based on the state we are moving to.
1852void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001853 Runtime* const runtime = Runtime::Current();
1854 const bool low_ram = runtime->GetHeap()->IsLowMemoryMode();
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001855 // TODO: Also do madvise hints for non low ram devices.
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001856 if (!low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07001857 return;
1858 }
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001859 if (state == MadviseState::kMadviseStateAtLoad && runtime->MAdviseRandomAccess()) {
1860 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
1861 // Other devices have enough page cache to get performance benefits from loading more pages
1862 // into the page cache.
David Sehrc431b9d2018-03-02 12:01:51 -08001863 DexLayoutSection::MadviseLargestPageAlignedRegion(dex_file.Begin(),
1864 dex_file.Begin() + dex_file.Size(),
1865 MADV_RANDOM);
Mathieu Chartier120aa282017-08-05 16:03:03 -07001866 }
Andreas Gampeb40d3612018-06-26 15:49:42 -07001867 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier120aa282017-08-05 16:03:03 -07001868 if (oat_dex_file != nullptr) {
1869 // Should always be there.
1870 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
1871 CHECK(sections != nullptr);
1872 sections->Madvise(&dex_file, state);
1873 }
1874}
1875
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001876OatFile::OatClass::OatClass(const OatFile* oat_file,
Vladimir Marko2c64a832018-01-04 11:31:56 +00001877 ClassStatus status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001878 OatClassType type,
1879 uint32_t bitmap_size,
1880 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001881 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001882 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001883 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001884 switch (type_) {
1885 case kOatClassAllCompiled: {
1886 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001887 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001888 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001889 break;
1890 }
1891 case kOatClassSomeCompiled: {
1892 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001893 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001894 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001895 break;
1896 }
1897 case kOatClassNoneCompiled: {
1898 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001899 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001900 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001901 break;
1902 }
1903 case kOatClassMax: {
1904 LOG(FATAL) << "Invalid OatClassType " << type_;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001905 UNREACHABLE();
Brian Carlstromba150c32013-08-27 17:31:03 -07001906 }
1907 }
1908}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001909
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001910uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1911 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1912 if (oat_method_offsets == nullptr) {
1913 return 0u;
1914 }
1915 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1916}
1917
1918const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001919 // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001920 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001921 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001922 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001923 }
1924 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001925 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001926 CHECK_EQ(kOatClassAllCompiled, type_);
1927 methods_pointer_index = method_index;
1928 } else {
1929 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001930 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001931 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001932 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001933 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1934 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001935 }
1936 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001937 return &oat_method_offsets;
1938}
1939
1940const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1941 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1942 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001943 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001944 }
1945 if (oat_file_->IsExecutable() ||
1946 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001947 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001948 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001949 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001950 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1951 // version.
1952 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001953}
1954
Mathieu Chartiere401d142015-04-22 13:56:20 -07001955void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001956 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001957 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001958}
1959
Sebastien Hertz0de11332015-05-13 12:14:05 +02001960bool OatFile::IsDebuggable() const {
1961 return GetOatHeader().IsDebuggable();
1962}
1963
Andreas Gampe29d38e72016-03-23 15:31:51 +00001964CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1965 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001966}
1967
Calin Juravle44e5efa2017-09-12 00:54:26 -07001968std::string OatFile::GetClassLoaderContext() const {
1969 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001970}
Calin Juravle44e5efa2017-09-12 00:54:26 -07001971
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001972const char* OatFile::GetCompilationReason() const {
1973 return GetOatHeader().GetStoreValueByKey(OatHeader::kCompilationReasonKey);
1974}
1975
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001976OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1977 uint16_t class_def_idx,
1978 bool* found) {
1979 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
Andreas Gampeb40d3612018-06-26 15:49:42 -07001980 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001981 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001982 *found = false;
1983 return OatFile::OatClass::Invalid();
1984 }
1985 *found = true;
1986 return oat_dex_file->GetOatClass(class_def_idx);
1987}
1988
Vladimir Marko1cedb4a2019-02-06 14:13:28 +00001989static void DCheckIndexToBssMapping(const OatFile* oat_file,
1990 uint32_t number_of_indexes,
1991 size_t slot_size,
1992 const IndexBssMapping* index_bss_mapping) {
1993 if (kIsDebugBuild && index_bss_mapping != nullptr) {
1994 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1995 const IndexBssMappingEntry* prev_entry = nullptr;
1996 for (const IndexBssMappingEntry& entry : *index_bss_mapping) {
1997 CHECK_ALIGNED_PARAM(entry.bss_offset, slot_size);
1998 CHECK_LT(entry.bss_offset, oat_file->BssSize());
1999 uint32_t mask = entry.GetMask(index_bits);
2000 CHECK_LE(POPCOUNT(mask) * slot_size, entry.bss_offset);
2001 size_t index_mask_span = (mask != 0u) ? 32u - index_bits - CTZ(mask) : 0u;
2002 CHECK_LE(index_mask_span, entry.GetIndex(index_bits));
2003 if (prev_entry != nullptr) {
2004 CHECK_LT(prev_entry->GetIndex(index_bits), entry.GetIndex(index_bits) - index_mask_span);
2005 }
2006 prev_entry = &entry;
2007 }
2008 CHECK(prev_entry != nullptr);
2009 CHECK_LT(prev_entry->GetIndex(index_bits), number_of_indexes);
2010 }
2011}
2012
2013void OatFile::InitializeRelocations() const {
2014 DCHECK(IsExecutable());
2015
2016 // Initialize the .data.bimg.rel.ro section.
2017 if (!GetBootImageRelocations().empty()) {
2018 uint8_t* reloc_begin = const_cast<uint8_t*>(DataBimgRelRoBegin());
2019 CheckedCall(mprotect,
2020 "un-protect boot image relocations",
2021 reloc_begin,
2022 DataBimgRelRoSize(),
2023 PROT_READ | PROT_WRITE);
2024 uint32_t boot_image_begin = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(
2025 Runtime::Current()->GetHeap()->GetBootImageSpaces().front()->Begin()));
2026 for (const uint32_t& relocation : GetBootImageRelocations()) {
2027 const_cast<uint32_t&>(relocation) += boot_image_begin;
2028 }
2029 CheckedCall(mprotect,
2030 "protect boot image relocations",
2031 reloc_begin,
2032 DataBimgRelRoSize(),
2033 PROT_READ);
2034 }
2035
2036 // Before initializing .bss, check the .bss mappings in debug mode.
2037 if (kIsDebugBuild) {
2038 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
2039 for (const OatDexFile* odf : GetOatDexFiles()) {
2040 const DexFile::Header* header =
2041 reinterpret_cast<const DexFile::Header*>(odf->GetDexFilePointer());
2042 DCheckIndexToBssMapping(this,
2043 header->method_ids_size_,
2044 static_cast<size_t>(pointer_size),
2045 odf->GetMethodBssMapping());
2046 DCheckIndexToBssMapping(this,
2047 header->type_ids_size_,
2048 sizeof(GcRoot<mirror::Class>),
2049 odf->GetTypeBssMapping());
2050 DCheckIndexToBssMapping(this,
2051 header->string_ids_size_,
2052 sizeof(GcRoot<mirror::String>),
2053 odf->GetStringBssMapping());
2054 }
2055 }
2056
2057 // Initialize the .bss section.
2058 // TODO: Pre-initialize from boot/app image?
2059 ArtMethod* resolution_method = Runtime::Current()->GetResolutionMethod();
2060 for (ArtMethod*& entry : GetBssMethods()) {
2061 entry = resolution_method;
2062 }
2063}
2064
Andreas Gampeb40d3612018-06-26 15:49:42 -07002065void OatDexFile::AssertAotCompiler() {
Mathieu Chartier1b868492016-11-16 16:22:37 -08002066 CHECK(Runtime::Current()->IsAotCompiler());
2067}
2068
Brian Carlstrome24fa612011-09-29 00:53:55 -07002069} // namespace art