blob: b3c3aa05f3a29ecea460fc94cf8e1287d585c027 [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 Gampe46ee31b2016-12-14 10:11:49 -080036#include "android-base/stringprintf.h"
37
Andreas Gampec6ea7d02017-02-01 16:46:28 -080038#include "art_method.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070039#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070040#include "base/enums.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080041#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080042#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080043#include "base/unix_file/fd_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080044#include "dex_file_types.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070045#include "dex_file_loader.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070047#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000048#include "gc_root.h"
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010049#include "gc/space/image_space.h"
David Srbecky1baabf02015-06-16 17:12:34 +000050#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070052#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070053#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070056#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070057#include "runtime.h"
Mathieu Chartier292567e2017-10-12 13:24:38 -070058#include "standard_dex_file.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000059#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070060#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061#include "utils.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010062#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070063
64namespace art {
65
Andreas Gampe46ee31b2016-12-14 10:11:49 -080066using android::base::StringPrintf;
67
Andreas Gampe049cff02015-12-01 23:27:12 -080068// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000069static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070070
Andreas Gampe049cff02015-12-01 23:27:12 -080071// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070072// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
73// multiple times). However, if/when we switch the above, we likely want to switch this, too,
74// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000075static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070076
77// For debugging, Open will print DlOpen error message if set to true.
78static constexpr bool kPrintDlOpenErrorMessage = false;
79
Mathieu Chartierbe8303d2017-08-17 17:39:39 -070080// If true, we advise the kernel about dex file mem map accesses.
Mathieu Chartier150d25d2017-08-28 09:52:55 -070081static constexpr bool kMadviseDexFileAccesses = true;
Mathieu Chartierbe8303d2017-08-17 17:39:39 -070082
Andreas Gampe049cff02015-12-01 23:27:12 -080083// Note for OatFileBase and descendents:
84//
85// These are used in OatFile::Open to try all our loaders.
86//
87// The process is simple:
88//
89// 1) Allocate an instance through the standard constructor (location, executable)
90// 2) Load() to try to open the file.
91// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
92// 4) PreSetup() for any steps that should be done before the final setup.
93// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070094
Andreas Gampe049cff02015-12-01 23:27:12 -080095class OatFileBase : public OatFile {
96 public:
97 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070098
Andreas Gampe049cff02015-12-01 23:27:12 -080099 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100100 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
101 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -0700102 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800103 uint8_t* requested_base,
104 uint8_t* oat_file_begin,
105 bool writable,
106 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800107 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700108 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800109 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800110
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700111 template <typename kOatFileBaseSubType>
112 static OatFileBase* OpenOatFile(int vdex_fd,
113 int oat_fd,
114 const std::string& vdex_filename,
115 const std::string& oat_filename,
116 uint8_t* requested_base,
117 uint8_t* oat_file_begin,
118 bool writable,
119 bool executable,
120 bool low_4gb,
121 const char* abs_dex_location,
122 std::string* error_msg);
123
Andreas Gampe049cff02015-12-01 23:27:12 -0800124 protected:
125 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700126
Andreas Gampe049cff02015-12-01 23:27:12 -0800127 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
128 std::string* error_msg) const = 0;
129
Andreas Gampe4075f832016-05-18 13:09:54 -0700130 virtual void PreLoad() = 0;
131
David Brazdil7b49e6c2016-09-01 11:06:18 +0100132 bool LoadVdex(const std::string& vdex_filename,
133 bool writable,
134 bool low_4gb,
135 std::string* error_msg);
136
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700137 bool LoadVdex(int vdex_fd,
138 const std::string& vdex_filename,
139 bool writable,
140 bool low_4gb,
141 std::string* error_msg);
142
Andreas Gampe049cff02015-12-01 23:27:12 -0800143 virtual bool Load(const std::string& elf_filename,
144 uint8_t* oat_file_begin,
145 bool writable,
146 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800147 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800148 std::string* error_msg) = 0;
149
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700150 virtual bool Load(int oat_fd,
151 uint8_t* oat_file_begin,
152 bool writable,
153 bool executable,
154 bool low_4gb,
155 std::string* error_msg) = 0;
156
Andreas Gampe049cff02015-12-01 23:27:12 -0800157 bool ComputeFields(uint8_t* requested_base,
158 const std::string& file_path,
159 std::string* error_msg);
160
161 virtual void PreSetup(const std::string& elf_filename) = 0;
162
163 bool Setup(const char* abs_dex_location, std::string* error_msg);
164
165 // Setters exposed for ElfOatFile.
166
167 void SetBegin(const uint8_t* begin) {
168 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700169 }
170
Andreas Gampe049cff02015-12-01 23:27:12 -0800171 void SetEnd(const uint8_t* end) {
172 end_ = end;
173 }
174
David Brazdilc93b3be2016-09-12 18:49:58 +0100175 void SetVdex(VdexFile* vdex) {
176 vdex_.reset(vdex);
177 }
178
Andreas Gampe049cff02015-12-01 23:27:12 -0800179 private:
180 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
181};
182
183template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100184OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
185 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800186 const std::string& location,
187 uint8_t* requested_base,
188 uint8_t* oat_file_begin,
189 bool writable,
190 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800191 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800192 const char* abs_dex_location,
193 std::string* error_msg) {
194 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700195
196 ret->PreLoad();
197
David Brazdil7b49e6c2016-09-01 11:06:18 +0100198 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
199 return nullptr;
200 }
201
Andreas Gampe049cff02015-12-01 23:27:12 -0800202 if (!ret->Load(elf_filename,
203 oat_file_begin,
204 writable,
205 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800206 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800207 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800208 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800209 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800210
Andreas Gampe049cff02015-12-01 23:27:12 -0800211 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
212 return nullptr;
213 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700214
Andreas Gampe049cff02015-12-01 23:27:12 -0800215 ret->PreSetup(elf_filename);
216
217 if (!ret->Setup(abs_dex_location, error_msg)) {
218 return nullptr;
219 }
220
Dave Allison69dfe512014-07-11 17:11:58 +0000221 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800222}
223
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700224template <typename kOatFileBaseSubType>
225OatFileBase* OatFileBase::OpenOatFile(int vdex_fd,
226 int oat_fd,
227 const std::string& vdex_location,
228 const std::string& oat_location,
229 uint8_t* requested_base,
230 uint8_t* oat_file_begin,
231 bool writable,
232 bool executable,
233 bool low_4gb,
234 const char* abs_dex_location,
235 std::string* error_msg) {
236 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(oat_location, executable));
237
238 if (kIsVdexEnabled && !ret->LoadVdex(vdex_fd, vdex_location, writable, low_4gb, error_msg)) {
239 return nullptr;
240 }
241
242 if (!ret->Load(oat_fd,
243 oat_file_begin,
244 writable,
245 executable,
246 low_4gb,
247 error_msg)) {
248 return nullptr;
249 }
250
251 if (!ret->ComputeFields(requested_base, oat_location, error_msg)) {
252 return nullptr;
253 }
254
255 ret->PreSetup(oat_location);
256
257 if (!ret->Setup(abs_dex_location, error_msg)) {
258 return nullptr;
259 }
260
261 return ret.release();
262}
263
David Brazdil7b49e6c2016-09-01 11:06:18 +0100264bool OatFileBase::LoadVdex(const std::string& vdex_filename,
265 bool writable,
266 bool low_4gb,
267 std::string* error_msg) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100268 vdex_ = VdexFile::Open(vdex_filename, writable, low_4gb, /* unquicken*/ false, error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100269 if (vdex_.get() == nullptr) {
270 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
271 vdex_filename.c_str(),
272 error_msg->c_str());
273 return false;
274 }
275 return true;
276}
277
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700278bool OatFileBase::LoadVdex(int vdex_fd,
279 const std::string& vdex_filename,
280 bool writable,
281 bool low_4gb,
282 std::string* error_msg) {
283 if (vdex_fd != -1) {
284 struct stat s;
285 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd, &s));
286 if (rc == -1) {
287 PLOG(WARNING) << "Failed getting length of vdex file";
288 } else {
289 vdex_ = VdexFile::Open(vdex_fd,
290 s.st_size,
291 vdex_filename,
292 writable,
293 low_4gb,
294 false /* unquicken */,
295 error_msg);
296 if (vdex_.get() == nullptr) {
297 *error_msg = "Failed opening vdex file.";
298 return false;
299 }
300 }
301 }
302 return true;
303}
304
Andreas Gampe049cff02015-12-01 23:27:12 -0800305bool OatFileBase::ComputeFields(uint8_t* requested_base,
306 const std::string& file_path,
307 std::string* error_msg) {
308 std::string symbol_error_msg;
309 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700310 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800311 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
312 file_path.c_str(),
313 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700314 return false;
315 }
316 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000317 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700318 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000319 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
320 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700321 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800322 "oatdata=%p != expected=%p. See process maps in the log.",
323 begin_, requested_base);
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
Andreas Gampe049cff02015-12-01 23:27:12 -0800336 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700337 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800338 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700339 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700340 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800341 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700342 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800343 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700344 return false;
345 }
346 // Readjust to be non-inclusive upper bound.
347 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100348 // Find bss methods if present.
349 bss_methods_ =
350 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000351 // Find bss roots if present.
352 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700353 }
354
Andreas Gampe049cff02015-12-01 23:27:12 -0800355 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800356}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800357
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100358// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
359// position by the number of bytes read, i.e. sizeof(T).
360// Return true on success, false if the read would go beyond the end of the OatFile.
361template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100362inline static bool ReadOatDexFileData(const OatFile& oat_file,
363 /*inout*/const uint8_t** oat,
364 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100365 DCHECK(oat != nullptr);
366 DCHECK(value != nullptr);
367 DCHECK_LE(*oat, oat_file.End());
368 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
369 return false;
370 }
371 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
372 typedef __attribute__((__aligned__(1))) T unaligned_type;
373 *value = *reinterpret_cast<const unaligned_type*>(*oat);
374 *oat += sizeof(T);
375 return true;
376}
377
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100378static inline bool MapConstantTables(const gc::space::ImageSpace* space,
379 uint8_t* address) {
380 // If MREMAP_DUP is ever merged to Linux kernel, use it to avoid the unnecessary open()/close().
381 // Note: The current approach relies on the filename still referencing the same inode.
382
383 File file(space->GetImageFilename(), O_RDONLY, /* checkUsage */ false);
384 if (!file.IsOpened()) {
385 LOG(ERROR) << "Failed to open boot image file " << space->GetImageFilename();
386 return false;
387 }
388
389 uint32_t offset = space->GetImageHeader().GetBootImageConstantTablesOffset();
390 uint32_t size = space->GetImageHeader().GetBootImageConstantTablesSize();
391 std::string error_msg;
392 std::unique_ptr<MemMap> mem_map(MemMap::MapFileAtAddress(address,
393 size,
394 PROT_READ,
395 MAP_PRIVATE,
396 file.Fd(),
397 offset,
398 /* low_4gb */ false,
399 /* reuse */ true,
400 file.GetPath().c_str(),
401 &error_msg));
402 if (mem_map == nullptr) {
403 LOG(ERROR) << "Failed to mmap boot image tables from file " << space->GetImageFilename();
404 return false;
405 }
406 return true;
407}
408
Andreas Gampe049cff02015-12-01 23:27:12 -0800409bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700410 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800411 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100412 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
413 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800414 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700415 return false;
416 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100417 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
418 size_t key_value_store_size =
419 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
420 if (Size() < sizeof(OatHeader) + key_value_store_size) {
421 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
422 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100423 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100424 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100425 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100426 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700427 return false;
428 }
429
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100430 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
431 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
432 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
433 "%zu is not in [%zu, %zu]",
434 GetLocation().c_str(),
435 oat_dex_files_offset,
436 GetOatHeader().GetHeaderSize(),
437 Size());
438 return false;
439 }
440 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
441
442 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
443 if (!IsAligned<kPageSize>(bss_begin_) ||
444 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
445 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000446 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
447 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100448 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000449 GetLocation().c_str(),
450 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100451 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000452 bss_roots_,
453 bss_end_);
454 return false;
455 }
456
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100457 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
458 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
459 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
460 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100461 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000462 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000463 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100464 bss_methods_,
465 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000466 bss_end_);
467 return false;
468 }
469
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100470 uint8_t* after_tables =
471 (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_; // May be null.
472 uint8_t* boot_image_tables = (bss_begin_ == after_tables) ? nullptr : bss_begin_;
473 uint8_t* boot_image_tables_end =
474 (bss_begin_ == after_tables) ? nullptr : (after_tables != nullptr) ? after_tables : bss_end_;
475 DCHECK_EQ(boot_image_tables != nullptr, boot_image_tables_end != nullptr);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100476 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
477 oat_dex_files_storage_.reserve(dex_file_count);
478 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100479 uint32_t dex_file_location_size;
480 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
481 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
482 "location size",
483 GetLocation().c_str(),
484 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700485 return false;
486 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100487 if (UNLIKELY(dex_file_location_size == 0U)) {
488 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
489 GetLocation().c_str(),
490 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700491 return false;
492 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000493 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100494 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
495 "location",
496 GetLocation().c_str(),
497 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700498 return false;
499 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000500 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
501 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700502
Richard Uhlere5fed032015-03-18 08:21:11 -0700503 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
504 abs_dex_location,
505 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700506
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100507 uint32_t dex_file_checksum;
508 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
509 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
510 "dex file checksum",
511 GetLocation().c_str(),
512 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700513 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700514 return false;
515 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700516
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100517 uint32_t dex_file_offset;
518 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
519 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
520 "after dex file offsets",
521 GetLocation().c_str(),
522 i,
523 dex_file_location.c_str());
524 return false;
525 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700526 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100527 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
528 "file offset",
529 GetLocation().c_str(),
530 i,
531 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700532 return false;
533 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100534 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100535 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
536 "offset %u > %zu",
537 GetLocation().c_str(),
538 i,
539 dex_file_location.c_str(),
540 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100541 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700542 return false;
543 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100544 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000545 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
546 "offset %u of %zu but the size of dex file header is %zu",
547 GetLocation().c_str(),
548 i,
549 dex_file_location.c_str(),
550 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100551 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000552 sizeof(DexFile::Header));
553 return false;
554 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800555
David Brazdil7b49e6c2016-09-01 11:06:18 +0100556 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700557
Mathieu Chartier292567e2017-10-12 13:24:38 -0700558 const bool valid_magic = StandardDexFile::IsMagicValid(dex_file_pointer);
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700559 if (UNLIKELY(!valid_magic)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100560 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
561 "dex file magic '%s'",
562 GetLocation().c_str(),
563 i,
564 dex_file_location.c_str(),
565 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700566 return false;
567 }
Mathieu Chartier292567e2017-10-12 13:24:38 -0700568 if (UNLIKELY(!StandardDexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100569 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
570 "dex file version '%s'",
571 GetLocation().c_str(),
572 i,
573 dex_file_location.c_str(),
574 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700575 return false;
576 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800577 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100578 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000579 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
580 "offset %u and size %u truncated at %zu",
581 GetLocation().c_str(),
582 i,
583 dex_file_location.c_str(),
584 dex_file_offset,
585 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100586 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000587 return false;
588 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300589
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000590 uint32_t class_offsets_offset;
591 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
592 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
593 "after class offsets offset",
594 GetLocation().c_str(),
595 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300596 dex_file_location.c_str());
597 return false;
598 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000599 if (UNLIKELY(class_offsets_offset > Size()) ||
600 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
601 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
602 "class offsets, offset %u of %zu, class defs %u",
603 GetLocation().c_str(),
604 i,
605 dex_file_location.c_str(),
606 class_offsets_offset,
607 Size(),
608 header->class_defs_size_);
609 return false;
610 }
611 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
612 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
613 "class offsets, offset %u",
614 GetLocation().c_str(),
615 i,
616 dex_file_location.c_str(),
617 class_offsets_offset);
618 return false;
619 }
620 const uint32_t* class_offsets_pointer =
621 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
622
623 uint32_t lookup_table_offset;
624 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
625 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
626 "after lookup table offset",
627 GetLocation().c_str(),
628 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300629 dex_file_location.c_str());
630 return false;
631 }
632 const uint8_t* lookup_table_data = lookup_table_offset != 0u
633 ? Begin() + lookup_table_offset
634 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000635 if (lookup_table_offset != 0u &&
636 (UNLIKELY(lookup_table_offset > Size()) ||
637 UNLIKELY(Size() - lookup_table_offset <
638 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100639 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000640 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100641 GetLocation().c_str(),
642 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000643 dex_file_location.c_str(),
644 lookup_table_offset,
645 Size(),
646 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700647 return false;
648 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700649
Mathieu Chartier120aa282017-08-05 16:03:03 -0700650 uint32_t dex_layout_sections_offset;
651 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
652 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
653 "after dex layout sections offset",
654 GetLocation().c_str(),
655 i,
656 dex_file_location.c_str());
657 return false;
658 }
659 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
660 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
661 : nullptr;
662
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100663 uint32_t method_bss_mapping_offset;
664 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &method_bss_mapping_offset))) {
665 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
666 "after method bss mapping offset",
667 GetLocation().c_str(),
668 i,
669 dex_file_location.c_str());
670 return false;
671 }
672 const bool readable_method_bss_mapping_size =
673 method_bss_mapping_offset != 0u &&
674 method_bss_mapping_offset <= Size() &&
675 IsAligned<alignof(MethodBssMapping)>(method_bss_mapping_offset) &&
676 Size() - method_bss_mapping_offset >= MethodBssMapping::ComputeSize(0);
677 const MethodBssMapping* method_bss_mapping = readable_method_bss_mapping_size
678 ? reinterpret_cast<const MethodBssMapping*>(Begin() + method_bss_mapping_offset)
679 : nullptr;
680 if (method_bss_mapping_offset != 0u &&
681 (UNLIKELY(method_bss_mapping == nullptr) ||
682 UNLIKELY(method_bss_mapping->size() == 0u) ||
683 UNLIKELY(Size() - method_bss_mapping_offset <
684 MethodBssMapping::ComputeSize(method_bss_mapping->size())))) {
685 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
686 " truncated method bss mapping, offset %u of %zu, length %zu",
687 GetLocation().c_str(),
688 i,
689 dex_file_location.c_str(),
690 method_bss_mapping_offset,
691 Size(),
692 method_bss_mapping != nullptr ? method_bss_mapping->size() : 0u);
693 return false;
694 }
695 if (kIsDebugBuild && method_bss_mapping != nullptr) {
696 const MethodBssMappingEntry* prev_entry = nullptr;
697 for (const MethodBssMappingEntry& entry : *method_bss_mapping) {
698 CHECK_ALIGNED_PARAM(entry.bss_offset, static_cast<size_t>(pointer_size));
699 CHECK_LT(entry.bss_offset, BssSize());
700 CHECK_LE(POPCOUNT(entry.index_mask) * static_cast<size_t>(pointer_size), entry.bss_offset);
701 size_t index_mask_span = (entry.index_mask != 0u) ? 16u - CTZ(entry.index_mask) : 0u;
702 CHECK_LE(index_mask_span, entry.method_index);
703 if (prev_entry != nullptr) {
704 CHECK_LT(prev_entry->method_index, entry.method_index - index_mask_span);
705 }
706 prev_entry = &entry;
707 }
708 CHECK_LT(prev_entry->method_index,
709 reinterpret_cast<const DexFile::Header*>(dex_file_pointer)->method_ids_size_);
710 }
711
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700712 std::string canonical_location =
713 DexFileLoader::GetDexCanonicalLocation(dex_file_location.c_str());
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100714
715 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100716 OatDexFile* oat_dex_file = new OatDexFile(this,
717 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100718 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100719 dex_file_checksum,
720 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300721 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100722 method_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000723 class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700724 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100725 oat_dex_files_storage_.push_back(oat_dex_file);
726
727 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100728 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100729 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100730 if (canonical_location != dex_file_location) {
731 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
732 oat_dex_files_.Put(canonical_key, oat_dex_file);
733 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700734 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100735
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100736 if (boot_image_tables != nullptr) {
Vladimir Marko667585a2017-09-29 10:42:31 +0100737 Runtime* runtime = Runtime::Current();
738 if (UNLIKELY(runtime == nullptr)) {
739 // This must be oatdump without boot image. Make sure the .bss is inaccessible.
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700740 CheckedCall(mprotect, "protect bss", const_cast<uint8_t*>(BssBegin()), BssSize(), PROT_NONE);
Vladimir Marko667585a2017-09-29 10:42:31 +0100741 } else {
742 // Map boot image tables into the .bss. The reserved size must match size of the tables.
743 size_t reserved_size = static_cast<size_t>(boot_image_tables_end - boot_image_tables);
744 size_t tables_size = 0u;
745 for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) {
746 tables_size += space->GetImageHeader().GetBootImageConstantTablesSize();
747 DCHECK_ALIGNED(tables_size, kPageSize);
748 }
749 if (tables_size != reserved_size) {
750 *error_msg = StringPrintf("In oat file '%s' found unexpected boot image table sizes, "
751 " %zu bytes, should be %zu.",
752 GetLocation().c_str(),
753 reserved_size,
754 tables_size);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100755 return false;
756 }
Vladimir Marko667585a2017-09-29 10:42:31 +0100757 for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) {
758 uint32_t current_tables_size = space->GetImageHeader().GetBootImageConstantTablesSize();
759 if (current_tables_size != 0u && !MapConstantTables(space, boot_image_tables)) {
760 return false;
761 }
762 boot_image_tables += current_tables_size;
763 }
764 DCHECK(boot_image_tables == boot_image_tables_end);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100765 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100766 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700767 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700768}
769
Andreas Gampe049cff02015-12-01 23:27:12 -0800770////////////////////////
771// OatFile via dlopen //
772////////////////////////
773
Andreas Gampe049cff02015-12-01 23:27:12 -0800774class DlOpenOatFile FINAL : public OatFileBase {
775 public:
776 DlOpenOatFile(const std::string& filename, bool executable)
777 : OatFileBase(filename, executable),
778 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700779 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800780 }
781
782 ~DlOpenOatFile() {
783 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700784 if (!kIsTargetBuild) {
785 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
786 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700787 dlclose(dlopen_handle_);
788 } else {
789 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700790 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800791 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800792 }
793
794 protected:
795 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
796 std::string* error_msg) const OVERRIDE {
797 const uint8_t* ptr =
798 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
799 if (ptr == nullptr) {
800 *error_msg = dlerror();
801 }
802 return ptr;
803 }
804
Andreas Gampe4075f832016-05-18 13:09:54 -0700805 void PreLoad() OVERRIDE;
806
Andreas Gampe049cff02015-12-01 23:27:12 -0800807 bool Load(const std::string& elf_filename,
808 uint8_t* oat_file_begin,
809 bool writable,
810 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800811 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800812 std::string* error_msg) OVERRIDE;
813
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700814 bool Load(int, uint8_t*, bool, bool, bool, std::string*) {
815 return false;
816 }
817
Andreas Gampe049cff02015-12-01 23:27:12 -0800818 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
819 void PreSetup(const std::string& elf_filename) OVERRIDE;
820
821 private:
822 bool Dlopen(const std::string& elf_filename,
823 uint8_t* oat_file_begin,
824 std::string* error_msg);
825
Richard Uhlera206c742016-05-24 15:04:22 -0700826 // On the host, if the same library is loaded again with dlopen the same
827 // file handle is returned. This differs from the behavior of dlopen on the
828 // target, where dlopen reloads the library at a different address every
829 // time you load it. The runtime relies on the target behavior to ensure
830 // each instance of the loaded library has a unique dex cache. To avoid
831 // problems, we fall back to our own linker in the case when the same
832 // library is opened multiple times on host. dlopen_handles_ is used to
833 // detect that case.
834 // Guarded by host_dlopen_handles_lock_;
835 static std::unordered_set<void*> host_dlopen_handles_;
836
Andreas Gampe049cff02015-12-01 23:27:12 -0800837 // dlopen handle during runtime.
838 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
839
840 // Dummy memory map objects corresponding to the regions mapped by dlopen.
841 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
842
Andreas Gampe4075f832016-05-18 13:09:54 -0700843 // The number of shared objects the linker told us about before loading. Used to
844 // (optimistically) optimize the PreSetup stage (see comment there).
845 size_t shared_objects_before_;
846
Andreas Gampe049cff02015-12-01 23:27:12 -0800847 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
848};
849
Richard Uhlera206c742016-05-24 15:04:22 -0700850std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
851
Andreas Gampe4075f832016-05-18 13:09:54 -0700852void DlOpenOatFile::PreLoad() {
853#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700854 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700855 LOG(FATAL) << "Should not reach here.";
856 UNREACHABLE();
857#else
858 // Count the entries in dl_iterate_phdr we get at this point in time.
859 struct dl_iterate_context {
860 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
861 size_t size ATTRIBUTE_UNUSED,
862 void *data) {
863 reinterpret_cast<dl_iterate_context*>(data)->count++;
864 return 0; // Continue iteration.
865 }
866 size_t count = 0;
867 } context;
868
869 dl_iterate_phdr(dl_iterate_context::callback, &context);
870 shared_objects_before_ = context.count;
871#endif
872}
873
Andreas Gampe049cff02015-12-01 23:27:12 -0800874bool DlOpenOatFile::Load(const std::string& elf_filename,
875 uint8_t* oat_file_begin,
876 bool writable,
877 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800878 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800879 std::string* error_msg) {
880 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
881 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
882 // !executable is a sign that we may want to patch), which may not be allowed for
883 // various reasons.
884 if (!kUseDlopen) {
885 *error_msg = "DlOpen is disabled.";
886 return false;
887 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800888 if (low_4gb) {
889 *error_msg = "DlOpen does not support low 4gb loading.";
890 return false;
891 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800892 if (writable) {
893 *error_msg = "DlOpen does not support writable loading.";
894 return false;
895 }
896 if (!executable) {
897 *error_msg = "DlOpen does not support non-executable loading.";
898 return false;
899 }
900
901 // dlopen always returns the same library if it is already opened on the host. For this reason
902 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
903 // the same library loaded multiple times at different addresses is required for class unloading
904 // and for having dex caches arrays in the .bss section.
905 if (!kIsTargetBuild) {
906 if (!kUseDlopenOnHost) {
907 *error_msg = "DlOpen disabled for host.";
908 return false;
909 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800910 }
911
912 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
913 DCHECK(dlopen_handle_ != nullptr || !success);
914
915 return success;
916}
917
918bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
919 uint8_t* oat_file_begin,
920 std::string* error_msg) {
921#ifdef __APPLE__
922 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
923 // but let's fallback to the custom loading code for the time being.
924 UNUSED(elf_filename, oat_file_begin);
925 *error_msg = "Dlopen unsupported on Mac.";
926 return false;
927#else
928 {
929 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
930 if (absolute_path == nullptr) {
931 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
932 return false;
933 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100934#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +0000935 android_dlextinfo extinfo = {};
Andreas Gampe049cff02015-12-01 23:27:12 -0800936 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
937 // (open oat files multiple
938 // times).
939 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
940 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800941 if (oat_file_begin != nullptr) { //
942 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
943 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
944 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800945 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
946#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800947 UNUSED(oat_file_begin);
Julien Duraj1af0c4f2016-11-16 14:05:48 +0000948 static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700949 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700950 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
951 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700952 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
953 dlclose(dlopen_handle_);
954 dlopen_handle_ = nullptr;
955 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
956 return false;
957 }
958 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100959#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800960 }
961 if (dlopen_handle_ == nullptr) {
962 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
963 return false;
964 }
965 return true;
966#endif
967}
968
969void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800970#ifdef __APPLE__
971 UNUSED(elf_filename);
972 LOG(FATAL) << "Should not reach here.";
973 UNREACHABLE();
974#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800975 struct dl_iterate_context {
976 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
977 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700978 context->shared_objects_seen++;
979 if (context->shared_objects_seen < context->shared_objects_before) {
980 // We haven't been called yet for anything we haven't seen before. Just continue.
981 // Note: this is aggressively optimistic. If another thread was unloading a library,
982 // we may miss out here. However, this does not happen often in practice.
983 return 0;
984 }
985
Andreas Gampe049cff02015-12-01 23:27:12 -0800986 // See whether this callback corresponds to the file which we have just loaded.
987 bool contains_begin = false;
988 for (int i = 0; i < info->dlpi_phnum; i++) {
989 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
990 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
991 info->dlpi_phdr[i].p_vaddr);
992 size_t memsz = info->dlpi_phdr[i].p_memsz;
993 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
994 contains_begin = true;
995 break;
996 }
997 }
998 }
999 // Add dummy mmaps for this file.
1000 if (contains_begin) {
1001 for (int i = 0; i < info->dlpi_phnum; i++) {
1002 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1003 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1004 info->dlpi_phdr[i].p_vaddr);
1005 size_t memsz = info->dlpi_phdr[i].p_memsz;
1006 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
1007 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
1008 }
1009 }
1010 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1011 }
1012 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1013 }
1014 const uint8_t* const begin_;
1015 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -07001016 const size_t shared_objects_before;
1017 size_t shared_objects_seen;
1018 };
1019 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -08001020
1021 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -07001022 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
1023 // before giving up. This should be unusual.
1024 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
1025 << shared_objects_before_;
1026 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
1027 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
1028 // OK, give up and print an error.
1029 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
1030 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
1031 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001032 }
Andreas Gampe74f07b52015-12-02 11:53:26 -08001033#endif
Andreas Gampe049cff02015-12-01 23:27:12 -08001034}
1035
1036////////////////////////////////////////////////
1037// OatFile via our own ElfFile implementation //
1038////////////////////////////////////////////////
1039
1040class ElfOatFile FINAL : public OatFileBase {
1041 public:
1042 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
1043
1044 static ElfOatFile* OpenElfFile(File* file,
1045 const std::string& location,
1046 uint8_t* requested_base,
1047 uint8_t* oat_file_begin, // Override base if not null
1048 bool writable,
1049 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001050 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001051 const char* abs_dex_location,
1052 std::string* error_msg);
1053
1054 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001055 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001056 const char* abs_dex_location,
1057 std::string* error_msg);
1058
1059 protected:
1060 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
1061 std::string* error_msg) const OVERRIDE {
1062 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
1063 if (ptr == nullptr) {
1064 *error_msg = "(Internal implementation could not find symbol)";
1065 }
1066 return ptr;
1067 }
1068
Andreas Gampe4075f832016-05-18 13:09:54 -07001069 void PreLoad() OVERRIDE {
1070 }
1071
Andreas Gampe049cff02015-12-01 23:27:12 -08001072 bool Load(const std::string& elf_filename,
1073 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1074 bool writable,
1075 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001076 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001077 std::string* error_msg) OVERRIDE;
1078
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001079 bool Load(int oat_fd,
1080 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1081 bool writable,
1082 bool executable,
1083 bool low_4gb,
1084 std::string* error_msg) OVERRIDE;
1085
Andreas Gampe049cff02015-12-01 23:27:12 -08001086 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
1087 }
1088
1089 private:
1090 bool ElfFileOpen(File* file,
1091 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1092 bool writable,
1093 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001094 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001095 std::string* error_msg);
1096
1097 private:
1098 // Backing memory map for oat file during cross compilation.
1099 std::unique_ptr<ElfFile> elf_file_;
1100
1101 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
1102};
1103
1104ElfOatFile* ElfOatFile::OpenElfFile(File* file,
1105 const std::string& location,
1106 uint8_t* requested_base,
1107 uint8_t* oat_file_begin, // Override base if not null
1108 bool writable,
1109 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001110 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001111 const char* abs_dex_location,
1112 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001113 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001114 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001115 bool success = oat_file->ElfFileOpen(file,
1116 oat_file_begin,
1117 writable,
1118 low_4gb,
1119 executable,
1120 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001121 if (!success) {
1122 CHECK(!error_msg->empty());
1123 return nullptr;
1124 }
1125
1126 // Complete the setup.
1127 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
1128 return nullptr;
1129 }
1130
1131 if (!oat_file->Setup(abs_dex_location, error_msg)) {
1132 return nullptr;
1133 }
1134
1135 return oat_file.release();
1136}
1137
1138bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001139 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001140 const char* abs_dex_location,
1141 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001142 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001143 if (IsExecutable()) {
1144 *error_msg = "Cannot initialize from elf file in executable mode.";
1145 return false;
1146 }
1147 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001148 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001149 uint64_t offset, size;
1150 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1151 CHECK(has_section);
1152 SetBegin(elf_file->Begin() + offset);
1153 SetEnd(elf_file->Begin() + size + offset);
1154 // Ignore the optional .bss section when opening non-executable.
1155 return Setup(abs_dex_location, error_msg);
1156}
1157
1158bool ElfOatFile::Load(const std::string& elf_filename,
1159 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1160 bool writable,
1161 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001162 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001163 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001164 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001165 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1166 if (file == nullptr) {
1167 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1168 return false;
1169 }
1170 return ElfOatFile::ElfFileOpen(file.get(),
1171 oat_file_begin,
1172 writable,
1173 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001174 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001175 error_msg);
1176}
1177
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001178bool ElfOatFile::Load(int oat_fd,
1179 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1180 bool writable,
1181 bool executable,
1182 bool low_4gb,
1183 std::string* error_msg) {
1184 ScopedTrace trace(__PRETTY_FUNCTION__);
1185 if (oat_fd != -1) {
1186 std::unique_ptr<File> file = std::make_unique<File>(oat_fd, false);
1187 file->DisableAutoClose();
1188 if (file == nullptr) {
1189 *error_msg = StringPrintf("Failed to open oat filename for reading: %s",
1190 strerror(errno));
1191 return false;
1192 }
1193 return ElfOatFile::ElfFileOpen(file.get(),
1194 oat_file_begin,
1195 writable,
1196 executable,
1197 low_4gb,
1198 error_msg);
1199 }
1200 return false;
1201}
1202
Andreas Gampe049cff02015-12-01 23:27:12 -08001203bool ElfOatFile::ElfFileOpen(File* file,
1204 uint8_t* oat_file_begin,
1205 bool writable,
1206 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001207 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001208 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001209 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001210 // TODO: rename requested_base to oat_data_begin
1211 elf_file_.reset(ElfFile::Open(file,
1212 writable,
1213 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001214 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001215 error_msg,
1216 oat_file_begin));
1217 if (elf_file_ == nullptr) {
1218 DCHECK(!error_msg->empty());
1219 return false;
1220 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001221 bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001222 DCHECK(loaded || !error_msg->empty());
1223 return loaded;
1224}
1225
1226//////////////////////////
1227// General OatFile code //
1228//////////////////////////
1229
1230std::string OatFile::ResolveRelativeEncodedDexLocation(
1231 const char* abs_dex_location, const std::string& rel_dex_location) {
1232 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
1233 // Strip :classes<N>.dex used for secondary multidex files.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001234 std::string base = DexFileLoader::GetBaseLocation(rel_dex_location);
1235 std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(rel_dex_location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001236
1237 // Check if the base is a suffix of the provided abs_dex_location.
1238 std::string target_suffix = "/" + base;
1239 std::string abs_location(abs_dex_location);
1240 if (abs_location.size() > target_suffix.size()) {
1241 size_t pos = abs_location.size() - target_suffix.size();
1242 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1243 return abs_location + multidex_suffix;
1244 }
1245 }
1246 }
1247 return rel_dex_location;
1248}
1249
1250static void CheckLocation(const std::string& location) {
1251 CHECK(!location.empty());
1252}
1253
1254OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001255 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001256 const std::string& location,
1257 const char* abs_dex_location,
1258 std::string* error_msg) {
1259 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001260 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001261 ? oat_file.release()
1262 : nullptr;
1263}
1264
David Brazdil7b49e6c2016-09-01 11:06:18 +01001265OatFile* OatFile::Open(const std::string& oat_filename,
1266 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001267 uint8_t* requested_base,
1268 uint8_t* oat_file_begin,
1269 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001270 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001271 const char* abs_dex_location,
1272 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001273 ScopedTrace trace("Open oat file " + oat_location);
1274 CHECK(!oat_filename.empty()) << oat_location;
1275 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001276
Calin Juravle367b9d82017-05-15 18:18:39 -07001277 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001278
1279 // Check that the files even exist, fast-fail.
1280 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
1281 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1282 return nullptr;
1283 } else if (!OS::FileExists(oat_filename.c_str())) {
1284 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001285 return nullptr;
1286 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001287
1288 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1289 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001290 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1291 oat_filename,
1292 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001293 requested_base,
1294 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001295 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001296 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001297 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001298 abs_dex_location,
1299 error_msg);
1300 if (with_dlopen != nullptr) {
1301 return with_dlopen;
1302 }
1303 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001304 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001305 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001306 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1307 //
1308 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1309 //
1310 // We use our own ELF loader for Quick to deal with legacy apps that
1311 // open a generated dex file by name, remove the file, then open
1312 // another generated dex file with the same name. http://b/10614658
1313 //
1314 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1315 //
1316 //
1317 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1318 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001319 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1320 oat_filename,
1321 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001322 requested_base,
1323 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001324 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001325 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001326 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001327 abs_dex_location,
1328 error_msg);
1329 return with_internal;
1330}
1331
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001332OatFile* OatFile::Open(int vdex_fd,
1333 int oat_fd,
1334 const std::string& oat_location,
1335 uint8_t* requested_base,
1336 uint8_t* oat_file_begin,
1337 bool executable,
1338 bool low_4gb,
1339 const char* abs_dex_location,
1340 std::string* error_msg) {
1341 CHECK(!oat_location.empty()) << oat_location;
1342
1343 std::string vdex_location = GetVdexFilename(oat_location);
1344
1345 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_fd,
1346 oat_fd,
1347 vdex_location,
1348 oat_location,
1349 requested_base,
1350 oat_file_begin,
1351 false /* writable */,
1352 executable,
1353 low_4gb,
1354 abs_dex_location,
1355 error_msg);
1356 return with_internal;
1357}
1358
Andreas Gampe049cff02015-12-01 23:27:12 -08001359OatFile* OatFile::OpenWritable(File* file,
1360 const std::string& location,
1361 const char* abs_dex_location,
1362 std::string* error_msg) {
1363 CheckLocation(location);
1364 return ElfOatFile::OpenElfFile(file,
1365 location,
1366 nullptr,
1367 nullptr,
1368 true,
1369 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001370 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001371 abs_dex_location,
1372 error_msg);
1373}
1374
1375OatFile* OatFile::OpenReadable(File* file,
1376 const std::string& location,
1377 const char* abs_dex_location,
1378 std::string* error_msg) {
1379 CheckLocation(location);
1380 return ElfOatFile::OpenElfFile(file,
1381 location,
1382 nullptr,
1383 nullptr,
1384 false,
1385 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001386 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001387 abs_dex_location,
1388 error_msg);
1389}
1390
1391OatFile::OatFile(const std::string& location, bool is_executable)
1392 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001393 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001394 begin_(nullptr),
1395 end_(nullptr),
1396 bss_begin_(nullptr),
1397 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001398 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001399 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001400 is_executable_(is_executable),
1401 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1402 CHECK(!location_.empty());
1403}
1404
1405OatFile::~OatFile() {
1406 STLDeleteElements(&oat_dex_files_storage_);
1407}
1408
Brian Carlstrome24fa612011-09-29 00:53:55 -07001409const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001410 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001411}
1412
Ian Rogers13735952014-10-08 12:43:28 -07001413const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001414 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001415 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001416}
1417
Ian Rogers13735952014-10-08 12:43:28 -07001418const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001419 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001420 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001421}
1422
Vladimir Marko5c42c292015-02-25 12:02:49 +00001423const uint8_t* OatFile::BssBegin() const {
1424 return bss_begin_;
1425}
1426
1427const uint8_t* OatFile::BssEnd() const {
1428 return bss_end_;
1429}
1430
David Brazdil7b49e6c2016-09-01 11:06:18 +01001431const uint8_t* OatFile::DexBegin() const {
1432 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1433}
1434
1435const uint8_t* OatFile::DexEnd() const {
1436 return kIsVdexEnabled ? vdex_->End() : End();
1437}
1438
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001439ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1440 if (bss_methods_ != nullptr) {
1441 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1442 ArtMethod** methods_end =
1443 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1444 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1445 } else {
1446 return ArrayRef<ArtMethod*>();
1447 }
1448}
1449
Vladimir Markoaad75c62016-10-03 08:46:48 +00001450ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1451 if (bss_roots_ != nullptr) {
1452 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1453 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1454 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1455 } else {
1456 return ArrayRef<GcRoot<mirror::Object>>();
1457 }
1458}
1459
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001460const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1461 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001462 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001463 // NOTE: We assume here that the canonical location for a given dex_location never
1464 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1465 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1466 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001467
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001468 // TODO: Additional analysis of usage patterns to see if this can be simplified
1469 // without any performance loss, for example by not doing the first lock-free lookup.
1470
1471 const OatFile::OatDexFile* oat_dex_file = nullptr;
1472 StringPiece key(dex_location);
1473 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1474 // directly mentioned in the oat file and doesn't require locking.
1475 auto primary_it = oat_dex_files_.find(key);
1476 if (primary_it != oat_dex_files_.end()) {
1477 oat_dex_file = primary_it->second;
1478 DCHECK(oat_dex_file != nullptr);
1479 } else {
1480 // This dex_location is not one of the dex locations directly mentioned in the
1481 // oat file. The correct lookup is via the canonical location but first see in
1482 // the secondary_oat_dex_files_ whether we've looked up this location before.
1483 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1484 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1485 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001486 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001487 } else {
1488 // We haven't seen this dex_location before, we must check the canonical location.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001489 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001490 if (dex_canonical_location != dex_location) {
1491 StringPiece canonical_key(dex_canonical_location);
1492 auto canonical_it = oat_dex_files_.find(canonical_key);
1493 if (canonical_it != oat_dex_files_.end()) {
1494 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001495 } // else keep null.
1496 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001497
1498 // Copy the key to the string_cache_ and store the result in secondary map.
1499 string_cache_.emplace_back(key.data(), key.length());
1500 StringPiece key_copy(string_cache_.back());
1501 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001502 }
1503 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001504
1505 if (oat_dex_file == nullptr) {
1506 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001507 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001508 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1509 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1510 }
1511 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001512 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001513
Richard Uhler9a37efc2016-08-05 16:32:55 -07001514 if (dex_location_checksum != nullptr &&
1515 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1516 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001517 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001518 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1519 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1520 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1521 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1522 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001523 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001524 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001525 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001526 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001527}
1528
Brian Carlstrome24fa612011-09-29 00:53:55 -07001529OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001530 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001531 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001532 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001533 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001534 const uint8_t* lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001535 const MethodBssMapping* method_bss_mapping_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001536 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001537 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001538 : oat_file_(oat_file),
1539 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001540 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001541 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001542 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001543 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001544 method_bss_mapping_(method_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001545 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001546 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001547 // Initialize TypeLookupTable.
1548 if (lookup_table_data_ != nullptr) {
1549 // Peek the number of classes from the DexFile.
1550 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1551 const uint32_t num_class_defs = dex_header->class_defs_size_;
1552 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1553 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1554 } else {
Mathieu Chartier1b868492016-11-16 16:22:37 -08001555 lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001556 }
1557 }
1558}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001559
Mathieu Chartier1b868492016-11-16 16:22:37 -08001560OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table)
1561 : lookup_table_(std::move(lookup_table)) {}
1562
Brian Carlstrome24fa612011-09-29 00:53:55 -07001563OatFile::OatDexFile::~OatDexFile() {}
1564
Ian Rogers05f28c62012-10-23 18:12:13 -07001565size_t OatFile::OatDexFile::FileSize() const {
1566 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1567}
1568
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001569std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001570 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001571 static constexpr bool kVerify = false;
1572 static constexpr bool kVerifyChecksum = false;
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001573 return DexFileLoader::Open(dex_file_pointer_,
1574 FileSize(),
1575 dex_file_location_,
1576 dex_file_location_checksum_,
1577 this,
1578 kVerify,
1579 kVerifyChecksum,
1580 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001581}
1582
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001583uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1584 return oat_class_offsets_pointer_[class_def_index];
1585}
1586
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001587OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001588 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001589
Ian Rogers13735952014-10-08 12:43:28 -07001590 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001591 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001592
Ian Rogers13735952014-10-08 12:43:28 -07001593 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001594 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1595 mirror::Class::Status status =
1596 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1597 CHECK_LT(status, mirror::Class::kStatusMax);
1598
Ian Rogers13735952014-10-08 12:43:28 -07001599 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001600 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1601 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1602 CHECK_LT(type, kOatClassMax);
1603
Ian Rogers13735952014-10-08 12:43:28 -07001604 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001605 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001606
Brian Carlstromcd937a92014-03-04 22:53:23 -08001607 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001608 const uint8_t* bitmap_pointer = nullptr;
1609 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001610 if (type != kOatClassNoneCompiled) {
1611 if (type == kOatClassSomeCompiled) {
1612 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1613 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1614 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1615 methods_pointer = bitmap_pointer + bitmap_size;
1616 } else {
1617 methods_pointer = after_type_pointer;
1618 }
1619 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001620 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001621
Richard Uhler07b3c232015-03-31 15:57:54 -07001622 return OatFile::OatClass(oat_file_,
1623 status,
1624 type,
1625 bitmap_size,
1626 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1627 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001628}
1629
David Sehr9aa352e2016-09-15 18:13:52 -07001630const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1631 const char* descriptor,
1632 size_t hash) {
1633 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1634 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1635 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1636 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
Andreas Gampee2abbc62017-09-15 11:59:26 -07001637 return (class_def_idx != dex::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
David Sehr9aa352e2016-09-15 18:13:52 -07001638 }
1639 // Fast path for rare no class defs case.
1640 const uint32_t num_class_defs = dex_file.NumClassDefs();
1641 if (num_class_defs == 0) {
1642 return nullptr;
1643 }
1644 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1645 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001646 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
David Sehr9aa352e2016-09-15 18:13:52 -07001647 return dex_file.FindClassDef(type_idx);
1648 }
1649 return nullptr;
1650}
1651
Mathieu Chartier120aa282017-08-05 16:03:03 -07001652// Madvise the dex file based on the state we are moving to.
1653void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001654 const bool low_ram = Runtime::Current()->GetHeap()->IsLowMemoryMode();
1655 // TODO: Also do madvise hints for non low ram devices.
1656 if (!kMadviseDexFileAccesses || !low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07001657 return;
1658 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001659 if (state == MadviseState::kMadviseStateAtLoad) {
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001660 if (low_ram) {
Mathieu Chartier4ec507d2017-08-15 15:21:40 -07001661 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
1662 // Other devices have enough page cache to get performance benefits from loading more pages
1663 // into the page cache.
1664 MadviseLargestPageAlignedRegion(dex_file.Begin(),
1665 dex_file.Begin() + dex_file.Size(),
1666 MADV_RANDOM);
1667 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001668 }
1669 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1670 if (oat_dex_file != nullptr) {
1671 // Should always be there.
1672 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
1673 CHECK(sections != nullptr);
1674 sections->Madvise(&dex_file, state);
1675 }
1676}
1677
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001678OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001679 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001680 OatClassType type,
1681 uint32_t bitmap_size,
1682 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001683 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001684 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001685 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001686 switch (type_) {
1687 case kOatClassAllCompiled: {
1688 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001689 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001690 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001691 break;
1692 }
1693 case kOatClassSomeCompiled: {
1694 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001695 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001696 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001697 break;
1698 }
1699 case kOatClassNoneCompiled: {
1700 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001701 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001702 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001703 break;
1704 }
1705 case kOatClassMax: {
1706 LOG(FATAL) << "Invalid OatClassType " << type_;
1707 break;
1708 }
1709 }
1710}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001711
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001712uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1713 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1714 if (oat_method_offsets == nullptr) {
1715 return 0u;
1716 }
1717 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1718}
1719
1720const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001721 // 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 -07001722 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001723 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001724 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001725 }
1726 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001727 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001728 CHECK_EQ(kOatClassAllCompiled, type_);
1729 methods_pointer_index = method_index;
1730 } else {
1731 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001732 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001733 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001734 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001735 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1736 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001737 }
1738 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001739 return &oat_method_offsets;
1740}
1741
1742const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1743 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1744 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001745 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001746 }
1747 if (oat_file_->IsExecutable() ||
1748 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001749 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001750 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001751 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001752 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1753 // version.
1754 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001755}
1756
Mathieu Chartiere401d142015-04-22 13:56:20 -07001757void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001758 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001759 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001760}
1761
Andreas Gampe7ba64962014-10-23 11:37:40 -07001762bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001763 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001764 // TODO: Check against oat_patches. b/18144996
1765}
1766
Sebastien Hertz0de11332015-05-13 12:14:05 +02001767bool OatFile::IsDebuggable() const {
1768 return GetOatHeader().IsDebuggable();
1769}
1770
Andreas Gampe29d38e72016-03-23 15:31:51 +00001771CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1772 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001773}
1774
Calin Juravle44e5efa2017-09-12 00:54:26 -07001775std::string OatFile::GetClassLoaderContext() const {
1776 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1777};
1778
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001779OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1780 uint16_t class_def_idx,
1781 bool* found) {
1782 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
1783 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001784 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001785 *found = false;
1786 return OatFile::OatClass::Invalid();
1787 }
1788 *found = true;
1789 return oat_dex_file->GetOatClass(class_def_idx);
1790}
1791
Mathieu Chartier1b868492016-11-16 16:22:37 -08001792void OatFile::OatDexFile::AssertAotCompiler() {
1793 CHECK(Runtime::Current()->IsAotCompiler());
1794}
1795
Brian Carlstrome24fa612011-09-29 00:53:55 -07001796} // namespace art