blob: c122830eaf8dcb2178302343dff09a7024da3d35 [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>
Richard Uhlere5fed032015-03-18 08:21:11 -070029
Andreas Gampefa8429b2015-04-07 18:34:42 -070030// dlopen_ext support from bionic.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010031#ifdef ART_TARGET_ANDROID
Andreas Gampefa8429b2015-04-07 18:34:42 -070032#include "android/dlext.h"
33#endif
34
Andreas Gampe46ee31b2016-12-14 10:11:49 -080035#include "android-base/stringprintf.h"
36
Andreas Gampec6ea7d02017-02-01 16:46:28 -080037#include "art_method.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070038#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070039#include "base/enums.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080040#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080041#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080042#include "base/unix_file/fd_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080043#include "dex_file_types.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070045#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000046#include "gc_root.h"
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010047#include "gc/space/image_space.h"
David Srbecky1baabf02015-06-16 17:12:34 +000048#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070050#include "mirror/object-inl.h"
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070051#include "native_dex_file.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070052#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010053#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070054#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070055#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070056#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000057#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070058#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059#include "utils.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010060#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070061
62namespace art {
63
Andreas Gampe46ee31b2016-12-14 10:11:49 -080064using android::base::StringPrintf;
65
Andreas Gampe049cff02015-12-01 23:27:12 -080066// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000067static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070068
Andreas Gampe049cff02015-12-01 23:27:12 -080069// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070070// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
71// multiple times). However, if/when we switch the above, we likely want to switch this, too,
72// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000073static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070074
75// For debugging, Open will print DlOpen error message if set to true.
76static constexpr bool kPrintDlOpenErrorMessage = false;
77
Mathieu Chartierbe8303d2017-08-17 17:39:39 -070078// If true, we advise the kernel about dex file mem map accesses.
Mathieu Chartier150d25d2017-08-28 09:52:55 -070079static constexpr bool kMadviseDexFileAccesses = true;
Mathieu Chartierbe8303d2017-08-17 17:39:39 -070080
Andreas Gampe049cff02015-12-01 23:27:12 -080081// Note for OatFileBase and descendents:
82//
83// These are used in OatFile::Open to try all our loaders.
84//
85// The process is simple:
86//
87// 1) Allocate an instance through the standard constructor (location, executable)
88// 2) Load() to try to open the file.
89// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
90// 4) PreSetup() for any steps that should be done before the final setup.
91// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070092
Andreas Gampe049cff02015-12-01 23:27:12 -080093class OatFileBase : public OatFile {
94 public:
95 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070096
Andreas Gampe049cff02015-12-01 23:27:12 -080097 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010098 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
99 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -0700100 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800101 uint8_t* requested_base,
102 uint8_t* oat_file_begin,
103 bool writable,
104 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800105 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700106 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800107 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800108
Andreas Gampe049cff02015-12-01 23:27:12 -0800109 protected:
110 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700111
Andreas Gampe049cff02015-12-01 23:27:12 -0800112 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
113 std::string* error_msg) const = 0;
114
Andreas Gampe4075f832016-05-18 13:09:54 -0700115 virtual void PreLoad() = 0;
116
David Brazdil7b49e6c2016-09-01 11:06:18 +0100117 bool LoadVdex(const std::string& vdex_filename,
118 bool writable,
119 bool low_4gb,
120 std::string* error_msg);
121
Andreas Gampe049cff02015-12-01 23:27:12 -0800122 virtual bool Load(const std::string& elf_filename,
123 uint8_t* oat_file_begin,
124 bool writable,
125 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800126 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800127 std::string* error_msg) = 0;
128
129 bool ComputeFields(uint8_t* requested_base,
130 const std::string& file_path,
131 std::string* error_msg);
132
133 virtual void PreSetup(const std::string& elf_filename) = 0;
134
135 bool Setup(const char* abs_dex_location, std::string* error_msg);
136
137 // Setters exposed for ElfOatFile.
138
139 void SetBegin(const uint8_t* begin) {
140 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700141 }
142
Andreas Gampe049cff02015-12-01 23:27:12 -0800143 void SetEnd(const uint8_t* end) {
144 end_ = end;
145 }
146
David Brazdilc93b3be2016-09-12 18:49:58 +0100147 void SetVdex(VdexFile* vdex) {
148 vdex_.reset(vdex);
149 }
150
Andreas Gampe049cff02015-12-01 23:27:12 -0800151 private:
152 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
153};
154
155template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100156OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
157 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800158 const std::string& location,
159 uint8_t* requested_base,
160 uint8_t* oat_file_begin,
161 bool writable,
162 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800163 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800164 const char* abs_dex_location,
165 std::string* error_msg) {
166 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700167
168 ret->PreLoad();
169
David Brazdil7b49e6c2016-09-01 11:06:18 +0100170 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
171 return nullptr;
172 }
173
Andreas Gampe049cff02015-12-01 23:27:12 -0800174 if (!ret->Load(elf_filename,
175 oat_file_begin,
176 writable,
177 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800178 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800179 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800180 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800182
Andreas Gampe049cff02015-12-01 23:27:12 -0800183 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
184 return nullptr;
185 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700186
Andreas Gampe049cff02015-12-01 23:27:12 -0800187 ret->PreSetup(elf_filename);
188
189 if (!ret->Setup(abs_dex_location, error_msg)) {
190 return nullptr;
191 }
192
Dave Allison69dfe512014-07-11 17:11:58 +0000193 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800194}
195
David Brazdil7b49e6c2016-09-01 11:06:18 +0100196bool OatFileBase::LoadVdex(const std::string& vdex_filename,
197 bool writable,
198 bool low_4gb,
199 std::string* error_msg) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100200 vdex_ = VdexFile::Open(vdex_filename, writable, low_4gb, /* unquicken*/ false, error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100201 if (vdex_.get() == nullptr) {
202 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
203 vdex_filename.c_str(),
204 error_msg->c_str());
205 return false;
206 }
207 return true;
208}
209
Andreas Gampe049cff02015-12-01 23:27:12 -0800210bool OatFileBase::ComputeFields(uint8_t* requested_base,
211 const std::string& file_path,
212 std::string* error_msg) {
213 std::string symbol_error_msg;
214 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700215 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800216 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
217 file_path.c_str(),
218 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700219 return false;
220 }
221 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000222 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700223 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000224 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
225 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700226 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800227 "oatdata=%p != expected=%p. See process maps in the log.",
228 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700229 return false;
230 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800231 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700232 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800233 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
234 file_path.c_str(),
235 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700236 return false;
237 }
238 // Readjust to be non-inclusive upper bound.
239 end_ += sizeof(uint32_t);
240
Andreas Gampe049cff02015-12-01 23:27:12 -0800241 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700242 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800243 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700244 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700245 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800246 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700247 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800248 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700249 return false;
250 }
251 // Readjust to be non-inclusive upper bound.
252 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100253 // Find bss methods if present.
254 bss_methods_ =
255 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000256 // Find bss roots if present.
257 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700258 }
259
Andreas Gampe049cff02015-12-01 23:27:12 -0800260 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800261}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800262
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100263// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
264// position by the number of bytes read, i.e. sizeof(T).
265// Return true on success, false if the read would go beyond the end of the OatFile.
266template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100267inline static bool ReadOatDexFileData(const OatFile& oat_file,
268 /*inout*/const uint8_t** oat,
269 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100270 DCHECK(oat != nullptr);
271 DCHECK(value != nullptr);
272 DCHECK_LE(*oat, oat_file.End());
273 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
274 return false;
275 }
276 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
277 typedef __attribute__((__aligned__(1))) T unaligned_type;
278 *value = *reinterpret_cast<const unaligned_type*>(*oat);
279 *oat += sizeof(T);
280 return true;
281}
282
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100283static inline bool MapConstantTables(const gc::space::ImageSpace* space,
284 uint8_t* address) {
285 // If MREMAP_DUP is ever merged to Linux kernel, use it to avoid the unnecessary open()/close().
286 // Note: The current approach relies on the filename still referencing the same inode.
287
288 File file(space->GetImageFilename(), O_RDONLY, /* checkUsage */ false);
289 if (!file.IsOpened()) {
290 LOG(ERROR) << "Failed to open boot image file " << space->GetImageFilename();
291 return false;
292 }
293
294 uint32_t offset = space->GetImageHeader().GetBootImageConstantTablesOffset();
295 uint32_t size = space->GetImageHeader().GetBootImageConstantTablesSize();
296 std::string error_msg;
297 std::unique_ptr<MemMap> mem_map(MemMap::MapFileAtAddress(address,
298 size,
299 PROT_READ,
300 MAP_PRIVATE,
301 file.Fd(),
302 offset,
303 /* low_4gb */ false,
304 /* reuse */ true,
305 file.GetPath().c_str(),
306 &error_msg));
307 if (mem_map == nullptr) {
308 LOG(ERROR) << "Failed to mmap boot image tables from file " << space->GetImageFilename();
309 return false;
310 }
311 return true;
312}
313
Andreas Gampe049cff02015-12-01 23:27:12 -0800314bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700315 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800316 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100317 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
318 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800319 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700320 return false;
321 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100322 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
323 size_t key_value_store_size =
324 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
325 if (Size() < sizeof(OatHeader) + key_value_store_size) {
326 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
327 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100328 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100329 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100330 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100331 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700332 return false;
333 }
334
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100335 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
336 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
337 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
338 "%zu is not in [%zu, %zu]",
339 GetLocation().c_str(),
340 oat_dex_files_offset,
341 GetOatHeader().GetHeaderSize(),
342 Size());
343 return false;
344 }
345 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
346
347 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
348 if (!IsAligned<kPageSize>(bss_begin_) ||
349 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
350 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000351 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
352 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100353 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000354 GetLocation().c_str(),
355 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100356 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000357 bss_roots_,
358 bss_end_);
359 return false;
360 }
361
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100362 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
363 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
364 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
365 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100366 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000367 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000368 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100369 bss_methods_,
370 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000371 bss_end_);
372 return false;
373 }
374
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100375 uint8_t* after_tables =
376 (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_; // May be null.
377 uint8_t* boot_image_tables = (bss_begin_ == after_tables) ? nullptr : bss_begin_;
378 uint8_t* boot_image_tables_end =
379 (bss_begin_ == after_tables) ? nullptr : (after_tables != nullptr) ? after_tables : bss_end_;
380 DCHECK_EQ(boot_image_tables != nullptr, boot_image_tables_end != nullptr);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100381 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
382 oat_dex_files_storage_.reserve(dex_file_count);
383 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100384 uint32_t dex_file_location_size;
385 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
386 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
387 "location size",
388 GetLocation().c_str(),
389 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700390 return false;
391 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100392 if (UNLIKELY(dex_file_location_size == 0U)) {
393 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
394 GetLocation().c_str(),
395 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700396 return false;
397 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000398 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100399 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
400 "location",
401 GetLocation().c_str(),
402 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700403 return false;
404 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000405 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
406 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700407
Richard Uhlere5fed032015-03-18 08:21:11 -0700408 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
409 abs_dex_location,
410 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700411
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100412 uint32_t dex_file_checksum;
413 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
414 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
415 "dex file checksum",
416 GetLocation().c_str(),
417 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700418 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700419 return false;
420 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700421
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100422 uint32_t dex_file_offset;
423 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
424 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
425 "after dex file offsets",
426 GetLocation().c_str(),
427 i,
428 dex_file_location.c_str());
429 return false;
430 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700431 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100432 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
433 "file offset",
434 GetLocation().c_str(),
435 i,
436 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700437 return false;
438 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100439 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100440 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
441 "offset %u > %zu",
442 GetLocation().c_str(),
443 i,
444 dex_file_location.c_str(),
445 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100446 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700447 return false;
448 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100449 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000450 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
451 "offset %u of %zu but the size of dex file header is %zu",
452 GetLocation().c_str(),
453 i,
454 dex_file_location.c_str(),
455 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100456 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000457 sizeof(DexFile::Header));
458 return false;
459 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800460
David Brazdil7b49e6c2016-09-01 11:06:18 +0100461 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700462
463 const bool valid_magic = NativeDexFile::IsMagicValid(dex_file_pointer);
464 if (UNLIKELY(!valid_magic)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100465 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
466 "dex file magic '%s'",
467 GetLocation().c_str(),
468 i,
469 dex_file_location.c_str(),
470 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700471 return false;
472 }
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700473 if (UNLIKELY(!NativeDexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100474 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
475 "dex file version '%s'",
476 GetLocation().c_str(),
477 i,
478 dex_file_location.c_str(),
479 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700480 return false;
481 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800482 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100483 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000484 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
485 "offset %u and size %u truncated at %zu",
486 GetLocation().c_str(),
487 i,
488 dex_file_location.c_str(),
489 dex_file_offset,
490 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100491 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000492 return false;
493 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300494
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000495 uint32_t class_offsets_offset;
496 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
497 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
498 "after class offsets offset",
499 GetLocation().c_str(),
500 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300501 dex_file_location.c_str());
502 return false;
503 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000504 if (UNLIKELY(class_offsets_offset > Size()) ||
505 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
506 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
507 "class offsets, offset %u of %zu, class defs %u",
508 GetLocation().c_str(),
509 i,
510 dex_file_location.c_str(),
511 class_offsets_offset,
512 Size(),
513 header->class_defs_size_);
514 return false;
515 }
516 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
517 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
518 "class offsets, offset %u",
519 GetLocation().c_str(),
520 i,
521 dex_file_location.c_str(),
522 class_offsets_offset);
523 return false;
524 }
525 const uint32_t* class_offsets_pointer =
526 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
527
528 uint32_t lookup_table_offset;
529 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
530 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
531 "after lookup table offset",
532 GetLocation().c_str(),
533 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300534 dex_file_location.c_str());
535 return false;
536 }
537 const uint8_t* lookup_table_data = lookup_table_offset != 0u
538 ? Begin() + lookup_table_offset
539 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000540 if (lookup_table_offset != 0u &&
541 (UNLIKELY(lookup_table_offset > Size()) ||
542 UNLIKELY(Size() - lookup_table_offset <
543 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100544 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000545 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100546 GetLocation().c_str(),
547 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000548 dex_file_location.c_str(),
549 lookup_table_offset,
550 Size(),
551 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700552 return false;
553 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700554
Mathieu Chartier120aa282017-08-05 16:03:03 -0700555 uint32_t dex_layout_sections_offset;
556 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
557 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
558 "after dex layout sections offset",
559 GetLocation().c_str(),
560 i,
561 dex_file_location.c_str());
562 return false;
563 }
564 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
565 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
566 : nullptr;
567
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100568 uint32_t method_bss_mapping_offset;
569 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &method_bss_mapping_offset))) {
570 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
571 "after method bss mapping offset",
572 GetLocation().c_str(),
573 i,
574 dex_file_location.c_str());
575 return false;
576 }
577 const bool readable_method_bss_mapping_size =
578 method_bss_mapping_offset != 0u &&
579 method_bss_mapping_offset <= Size() &&
580 IsAligned<alignof(MethodBssMapping)>(method_bss_mapping_offset) &&
581 Size() - method_bss_mapping_offset >= MethodBssMapping::ComputeSize(0);
582 const MethodBssMapping* method_bss_mapping = readable_method_bss_mapping_size
583 ? reinterpret_cast<const MethodBssMapping*>(Begin() + method_bss_mapping_offset)
584 : nullptr;
585 if (method_bss_mapping_offset != 0u &&
586 (UNLIKELY(method_bss_mapping == nullptr) ||
587 UNLIKELY(method_bss_mapping->size() == 0u) ||
588 UNLIKELY(Size() - method_bss_mapping_offset <
589 MethodBssMapping::ComputeSize(method_bss_mapping->size())))) {
590 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
591 " truncated method bss mapping, offset %u of %zu, length %zu",
592 GetLocation().c_str(),
593 i,
594 dex_file_location.c_str(),
595 method_bss_mapping_offset,
596 Size(),
597 method_bss_mapping != nullptr ? method_bss_mapping->size() : 0u);
598 return false;
599 }
600 if (kIsDebugBuild && method_bss_mapping != nullptr) {
601 const MethodBssMappingEntry* prev_entry = nullptr;
602 for (const MethodBssMappingEntry& entry : *method_bss_mapping) {
603 CHECK_ALIGNED_PARAM(entry.bss_offset, static_cast<size_t>(pointer_size));
604 CHECK_LT(entry.bss_offset, BssSize());
605 CHECK_LE(POPCOUNT(entry.index_mask) * static_cast<size_t>(pointer_size), entry.bss_offset);
606 size_t index_mask_span = (entry.index_mask != 0u) ? 16u - CTZ(entry.index_mask) : 0u;
607 CHECK_LE(index_mask_span, entry.method_index);
608 if (prev_entry != nullptr) {
609 CHECK_LT(prev_entry->method_index, entry.method_index - index_mask_span);
610 }
611 prev_entry = &entry;
612 }
613 CHECK_LT(prev_entry->method_index,
614 reinterpret_cast<const DexFile::Header*>(dex_file_pointer)->method_ids_size_);
615 }
616
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100617 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
618
619 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100620 OatDexFile* oat_dex_file = new OatDexFile(this,
621 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100622 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100623 dex_file_checksum,
624 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300625 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100626 method_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000627 class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700628 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100629 oat_dex_files_storage_.push_back(oat_dex_file);
630
631 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100632 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100633 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100634 if (canonical_location != dex_file_location) {
635 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
636 oat_dex_files_.Put(canonical_key, oat_dex_file);
637 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700638 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100639
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100640 if (boot_image_tables != nullptr) {
Vladimir Marko667585a2017-09-29 10:42:31 +0100641 Runtime* runtime = Runtime::Current();
642 if (UNLIKELY(runtime == nullptr)) {
643 // This must be oatdump without boot image. Make sure the .bss is inaccessible.
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700644 CheckedCall(mprotect, "protect bss", const_cast<uint8_t*>(BssBegin()), BssSize(), PROT_NONE);
Vladimir Marko667585a2017-09-29 10:42:31 +0100645 } else {
646 // Map boot image tables into the .bss. The reserved size must match size of the tables.
647 size_t reserved_size = static_cast<size_t>(boot_image_tables_end - boot_image_tables);
648 size_t tables_size = 0u;
649 for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) {
650 tables_size += space->GetImageHeader().GetBootImageConstantTablesSize();
651 DCHECK_ALIGNED(tables_size, kPageSize);
652 }
653 if (tables_size != reserved_size) {
654 *error_msg = StringPrintf("In oat file '%s' found unexpected boot image table sizes, "
655 " %zu bytes, should be %zu.",
656 GetLocation().c_str(),
657 reserved_size,
658 tables_size);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100659 return false;
660 }
Vladimir Marko667585a2017-09-29 10:42:31 +0100661 for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) {
662 uint32_t current_tables_size = space->GetImageHeader().GetBootImageConstantTablesSize();
663 if (current_tables_size != 0u && !MapConstantTables(space, boot_image_tables)) {
664 return false;
665 }
666 boot_image_tables += current_tables_size;
667 }
668 DCHECK(boot_image_tables == boot_image_tables_end);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100669 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100670 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700671 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700672}
673
Andreas Gampe049cff02015-12-01 23:27:12 -0800674////////////////////////
675// OatFile via dlopen //
676////////////////////////
677
Andreas Gampe049cff02015-12-01 23:27:12 -0800678class DlOpenOatFile FINAL : public OatFileBase {
679 public:
680 DlOpenOatFile(const std::string& filename, bool executable)
681 : OatFileBase(filename, executable),
682 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700683 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800684 }
685
686 ~DlOpenOatFile() {
687 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700688 if (!kIsTargetBuild) {
689 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
690 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700691 dlclose(dlopen_handle_);
692 } else {
693 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700694 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800695 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800696 }
697
698 protected:
699 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
700 std::string* error_msg) const OVERRIDE {
701 const uint8_t* ptr =
702 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
703 if (ptr == nullptr) {
704 *error_msg = dlerror();
705 }
706 return ptr;
707 }
708
Andreas Gampe4075f832016-05-18 13:09:54 -0700709 void PreLoad() OVERRIDE;
710
Andreas Gampe049cff02015-12-01 23:27:12 -0800711 bool Load(const std::string& elf_filename,
712 uint8_t* oat_file_begin,
713 bool writable,
714 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800715 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800716 std::string* error_msg) OVERRIDE;
717
718 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
719 void PreSetup(const std::string& elf_filename) OVERRIDE;
720
721 private:
722 bool Dlopen(const std::string& elf_filename,
723 uint8_t* oat_file_begin,
724 std::string* error_msg);
725
Richard Uhlera206c742016-05-24 15:04:22 -0700726 // On the host, if the same library is loaded again with dlopen the same
727 // file handle is returned. This differs from the behavior of dlopen on the
728 // target, where dlopen reloads the library at a different address every
729 // time you load it. The runtime relies on the target behavior to ensure
730 // each instance of the loaded library has a unique dex cache. To avoid
731 // problems, we fall back to our own linker in the case when the same
732 // library is opened multiple times on host. dlopen_handles_ is used to
733 // detect that case.
734 // Guarded by host_dlopen_handles_lock_;
735 static std::unordered_set<void*> host_dlopen_handles_;
736
Andreas Gampe049cff02015-12-01 23:27:12 -0800737 // dlopen handle during runtime.
738 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
739
740 // Dummy memory map objects corresponding to the regions mapped by dlopen.
741 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
742
Andreas Gampe4075f832016-05-18 13:09:54 -0700743 // The number of shared objects the linker told us about before loading. Used to
744 // (optimistically) optimize the PreSetup stage (see comment there).
745 size_t shared_objects_before_;
746
Andreas Gampe049cff02015-12-01 23:27:12 -0800747 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
748};
749
Richard Uhlera206c742016-05-24 15:04:22 -0700750std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
751
Andreas Gampe4075f832016-05-18 13:09:54 -0700752void DlOpenOatFile::PreLoad() {
753#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700754 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700755 LOG(FATAL) << "Should not reach here.";
756 UNREACHABLE();
757#else
758 // Count the entries in dl_iterate_phdr we get at this point in time.
759 struct dl_iterate_context {
760 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
761 size_t size ATTRIBUTE_UNUSED,
762 void *data) {
763 reinterpret_cast<dl_iterate_context*>(data)->count++;
764 return 0; // Continue iteration.
765 }
766 size_t count = 0;
767 } context;
768
769 dl_iterate_phdr(dl_iterate_context::callback, &context);
770 shared_objects_before_ = context.count;
771#endif
772}
773
Andreas Gampe049cff02015-12-01 23:27:12 -0800774bool DlOpenOatFile::Load(const std::string& elf_filename,
775 uint8_t* oat_file_begin,
776 bool writable,
777 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800778 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800779 std::string* error_msg) {
780 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
781 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
782 // !executable is a sign that we may want to patch), which may not be allowed for
783 // various reasons.
784 if (!kUseDlopen) {
785 *error_msg = "DlOpen is disabled.";
786 return false;
787 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800788 if (low_4gb) {
789 *error_msg = "DlOpen does not support low 4gb loading.";
790 return false;
791 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800792 if (writable) {
793 *error_msg = "DlOpen does not support writable loading.";
794 return false;
795 }
796 if (!executable) {
797 *error_msg = "DlOpen does not support non-executable loading.";
798 return false;
799 }
800
801 // dlopen always returns the same library if it is already opened on the host. For this reason
802 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
803 // the same library loaded multiple times at different addresses is required for class unloading
804 // and for having dex caches arrays in the .bss section.
805 if (!kIsTargetBuild) {
806 if (!kUseDlopenOnHost) {
807 *error_msg = "DlOpen disabled for host.";
808 return false;
809 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800810 }
811
812 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
813 DCHECK(dlopen_handle_ != nullptr || !success);
814
815 return success;
816}
817
818bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
819 uint8_t* oat_file_begin,
820 std::string* error_msg) {
821#ifdef __APPLE__
822 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
823 // but let's fallback to the custom loading code for the time being.
824 UNUSED(elf_filename, oat_file_begin);
825 *error_msg = "Dlopen unsupported on Mac.";
826 return false;
827#else
828 {
829 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
830 if (absolute_path == nullptr) {
831 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
832 return false;
833 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100834#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +0000835 android_dlextinfo extinfo = {};
Andreas Gampe049cff02015-12-01 23:27:12 -0800836 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
837 // (open oat files multiple
838 // times).
839 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
840 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800841 if (oat_file_begin != nullptr) { //
842 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
843 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
844 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800845 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
846#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800847 UNUSED(oat_file_begin);
Julien Duraj1af0c4f2016-11-16 14:05:48 +0000848 static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700849 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700850 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
851 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700852 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
853 dlclose(dlopen_handle_);
854 dlopen_handle_ = nullptr;
855 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
856 return false;
857 }
858 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100859#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800860 }
861 if (dlopen_handle_ == nullptr) {
862 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
863 return false;
864 }
865 return true;
866#endif
867}
868
869void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800870#ifdef __APPLE__
871 UNUSED(elf_filename);
872 LOG(FATAL) << "Should not reach here.";
873 UNREACHABLE();
874#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800875 struct dl_iterate_context {
876 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
877 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700878 context->shared_objects_seen++;
879 if (context->shared_objects_seen < context->shared_objects_before) {
880 // We haven't been called yet for anything we haven't seen before. Just continue.
881 // Note: this is aggressively optimistic. If another thread was unloading a library,
882 // we may miss out here. However, this does not happen often in practice.
883 return 0;
884 }
885
Andreas Gampe049cff02015-12-01 23:27:12 -0800886 // See whether this callback corresponds to the file which we have just loaded.
887 bool contains_begin = false;
888 for (int i = 0; i < info->dlpi_phnum; i++) {
889 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
890 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
891 info->dlpi_phdr[i].p_vaddr);
892 size_t memsz = info->dlpi_phdr[i].p_memsz;
893 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
894 contains_begin = true;
895 break;
896 }
897 }
898 }
899 // Add dummy mmaps for this file.
900 if (contains_begin) {
901 for (int i = 0; i < info->dlpi_phnum; i++) {
902 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
903 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
904 info->dlpi_phdr[i].p_vaddr);
905 size_t memsz = info->dlpi_phdr[i].p_memsz;
906 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
907 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
908 }
909 }
910 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
911 }
912 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
913 }
914 const uint8_t* const begin_;
915 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700916 const size_t shared_objects_before;
917 size_t shared_objects_seen;
918 };
919 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800920
921 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700922 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
923 // before giving up. This should be unusual.
924 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
925 << shared_objects_before_;
926 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
927 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
928 // OK, give up and print an error.
929 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
930 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
931 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800932 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800933#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800934}
935
936////////////////////////////////////////////////
937// OatFile via our own ElfFile implementation //
938////////////////////////////////////////////////
939
940class ElfOatFile FINAL : public OatFileBase {
941 public:
942 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
943
944 static ElfOatFile* OpenElfFile(File* file,
945 const std::string& location,
946 uint8_t* requested_base,
947 uint8_t* oat_file_begin, // Override base if not null
948 bool writable,
949 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800950 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800951 const char* abs_dex_location,
952 std::string* error_msg);
953
954 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100955 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800956 const char* abs_dex_location,
957 std::string* error_msg);
958
959 protected:
960 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
961 std::string* error_msg) const OVERRIDE {
962 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
963 if (ptr == nullptr) {
964 *error_msg = "(Internal implementation could not find symbol)";
965 }
966 return ptr;
967 }
968
Andreas Gampe4075f832016-05-18 13:09:54 -0700969 void PreLoad() OVERRIDE {
970 }
971
Andreas Gampe049cff02015-12-01 23:27:12 -0800972 bool Load(const std::string& elf_filename,
973 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
974 bool writable,
975 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800976 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800977 std::string* error_msg) OVERRIDE;
978
979 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
980 }
981
982 private:
983 bool ElfFileOpen(File* file,
984 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
985 bool writable,
986 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800987 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800988 std::string* error_msg);
989
990 private:
991 // Backing memory map for oat file during cross compilation.
992 std::unique_ptr<ElfFile> elf_file_;
993
994 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
995};
996
997ElfOatFile* ElfOatFile::OpenElfFile(File* file,
998 const std::string& location,
999 uint8_t* requested_base,
1000 uint8_t* oat_file_begin, // Override base if not null
1001 bool writable,
1002 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001003 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001004 const char* abs_dex_location,
1005 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001006 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001007 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001008 bool success = oat_file->ElfFileOpen(file,
1009 oat_file_begin,
1010 writable,
1011 low_4gb,
1012 executable,
1013 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001014 if (!success) {
1015 CHECK(!error_msg->empty());
1016 return nullptr;
1017 }
1018
1019 // Complete the setup.
1020 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
1021 return nullptr;
1022 }
1023
1024 if (!oat_file->Setup(abs_dex_location, error_msg)) {
1025 return nullptr;
1026 }
1027
1028 return oat_file.release();
1029}
1030
1031bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001032 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001033 const char* abs_dex_location,
1034 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001035 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001036 if (IsExecutable()) {
1037 *error_msg = "Cannot initialize from elf file in executable mode.";
1038 return false;
1039 }
1040 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001041 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001042 uint64_t offset, size;
1043 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1044 CHECK(has_section);
1045 SetBegin(elf_file->Begin() + offset);
1046 SetEnd(elf_file->Begin() + size + offset);
1047 // Ignore the optional .bss section when opening non-executable.
1048 return Setup(abs_dex_location, error_msg);
1049}
1050
1051bool ElfOatFile::Load(const std::string& elf_filename,
1052 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1053 bool writable,
1054 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001055 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001056 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001057 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001058 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1059 if (file == nullptr) {
1060 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1061 return false;
1062 }
1063 return ElfOatFile::ElfFileOpen(file.get(),
1064 oat_file_begin,
1065 writable,
1066 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001067 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001068 error_msg);
1069}
1070
1071bool ElfOatFile::ElfFileOpen(File* file,
1072 uint8_t* oat_file_begin,
1073 bool writable,
1074 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001075 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001076 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001077 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001078 // TODO: rename requested_base to oat_data_begin
1079 elf_file_.reset(ElfFile::Open(file,
1080 writable,
1081 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001082 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001083 error_msg,
1084 oat_file_begin));
1085 if (elf_file_ == nullptr) {
1086 DCHECK(!error_msg->empty());
1087 return false;
1088 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001089 bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001090 DCHECK(loaded || !error_msg->empty());
1091 return loaded;
1092}
1093
1094//////////////////////////
1095// General OatFile code //
1096//////////////////////////
1097
1098std::string OatFile::ResolveRelativeEncodedDexLocation(
1099 const char* abs_dex_location, const std::string& rel_dex_location) {
1100 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
1101 // Strip :classes<N>.dex used for secondary multidex files.
1102 std::string base = DexFile::GetBaseLocation(rel_dex_location);
1103 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
1104
1105 // Check if the base is a suffix of the provided abs_dex_location.
1106 std::string target_suffix = "/" + base;
1107 std::string abs_location(abs_dex_location);
1108 if (abs_location.size() > target_suffix.size()) {
1109 size_t pos = abs_location.size() - target_suffix.size();
1110 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1111 return abs_location + multidex_suffix;
1112 }
1113 }
1114 }
1115 return rel_dex_location;
1116}
1117
1118static void CheckLocation(const std::string& location) {
1119 CHECK(!location.empty());
1120}
1121
1122OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001123 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001124 const std::string& location,
1125 const char* abs_dex_location,
1126 std::string* error_msg) {
1127 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001128 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001129 ? oat_file.release()
1130 : nullptr;
1131}
1132
David Brazdil7b49e6c2016-09-01 11:06:18 +01001133OatFile* OatFile::Open(const std::string& oat_filename,
1134 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001135 uint8_t* requested_base,
1136 uint8_t* oat_file_begin,
1137 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001138 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001139 const char* abs_dex_location,
1140 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001141 ScopedTrace trace("Open oat file " + oat_location);
1142 CHECK(!oat_filename.empty()) << oat_location;
1143 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001144
Calin Juravle367b9d82017-05-15 18:18:39 -07001145 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001146
1147 // Check that the files even exist, fast-fail.
1148 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
1149 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1150 return nullptr;
1151 } else if (!OS::FileExists(oat_filename.c_str())) {
1152 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001153 return nullptr;
1154 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001155
1156 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1157 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001158 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1159 oat_filename,
1160 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001161 requested_base,
1162 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001163 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001164 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001165 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001166 abs_dex_location,
1167 error_msg);
1168 if (with_dlopen != nullptr) {
1169 return with_dlopen;
1170 }
1171 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001172 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001173 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001174 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1175 //
1176 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1177 //
1178 // We use our own ELF loader for Quick to deal with legacy apps that
1179 // open a generated dex file by name, remove the file, then open
1180 // another generated dex file with the same name. http://b/10614658
1181 //
1182 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1183 //
1184 //
1185 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1186 // 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 +01001187 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1188 oat_filename,
1189 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001190 requested_base,
1191 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001192 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001193 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001194 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001195 abs_dex_location,
1196 error_msg);
1197 return with_internal;
1198}
1199
1200OatFile* OatFile::OpenWritable(File* file,
1201 const std::string& location,
1202 const char* abs_dex_location,
1203 std::string* error_msg) {
1204 CheckLocation(location);
1205 return ElfOatFile::OpenElfFile(file,
1206 location,
1207 nullptr,
1208 nullptr,
1209 true,
1210 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001211 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001212 abs_dex_location,
1213 error_msg);
1214}
1215
1216OatFile* OatFile::OpenReadable(File* file,
1217 const std::string& location,
1218 const char* abs_dex_location,
1219 std::string* error_msg) {
1220 CheckLocation(location);
1221 return ElfOatFile::OpenElfFile(file,
1222 location,
1223 nullptr,
1224 nullptr,
1225 false,
1226 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001227 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001228 abs_dex_location,
1229 error_msg);
1230}
1231
1232OatFile::OatFile(const std::string& location, bool is_executable)
1233 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001234 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001235 begin_(nullptr),
1236 end_(nullptr),
1237 bss_begin_(nullptr),
1238 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001239 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001240 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001241 is_executable_(is_executable),
1242 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1243 CHECK(!location_.empty());
1244}
1245
1246OatFile::~OatFile() {
1247 STLDeleteElements(&oat_dex_files_storage_);
1248}
1249
Brian Carlstrome24fa612011-09-29 00:53:55 -07001250const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001251 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001252}
1253
Ian Rogers13735952014-10-08 12:43:28 -07001254const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001255 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001256 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001257}
1258
Ian Rogers13735952014-10-08 12:43:28 -07001259const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001260 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001261 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001262}
1263
Vladimir Marko5c42c292015-02-25 12:02:49 +00001264const uint8_t* OatFile::BssBegin() const {
1265 return bss_begin_;
1266}
1267
1268const uint8_t* OatFile::BssEnd() const {
1269 return bss_end_;
1270}
1271
David Brazdil7b49e6c2016-09-01 11:06:18 +01001272const uint8_t* OatFile::DexBegin() const {
1273 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1274}
1275
1276const uint8_t* OatFile::DexEnd() const {
1277 return kIsVdexEnabled ? vdex_->End() : End();
1278}
1279
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001280ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1281 if (bss_methods_ != nullptr) {
1282 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1283 ArtMethod** methods_end =
1284 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1285 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1286 } else {
1287 return ArrayRef<ArtMethod*>();
1288 }
1289}
1290
Vladimir Markoaad75c62016-10-03 08:46:48 +00001291ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1292 if (bss_roots_ != nullptr) {
1293 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1294 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1295 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1296 } else {
1297 return ArrayRef<GcRoot<mirror::Object>>();
1298 }
1299}
1300
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001301const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1302 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001303 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001304 // NOTE: We assume here that the canonical location for a given dex_location never
1305 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1306 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1307 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001308
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001309 // TODO: Additional analysis of usage patterns to see if this can be simplified
1310 // without any performance loss, for example by not doing the first lock-free lookup.
1311
1312 const OatFile::OatDexFile* oat_dex_file = nullptr;
1313 StringPiece key(dex_location);
1314 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1315 // directly mentioned in the oat file and doesn't require locking.
1316 auto primary_it = oat_dex_files_.find(key);
1317 if (primary_it != oat_dex_files_.end()) {
1318 oat_dex_file = primary_it->second;
1319 DCHECK(oat_dex_file != nullptr);
1320 } else {
1321 // This dex_location is not one of the dex locations directly mentioned in the
1322 // oat file. The correct lookup is via the canonical location but first see in
1323 // the secondary_oat_dex_files_ whether we've looked up this location before.
1324 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1325 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1326 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001327 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001328 } else {
1329 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001330 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001331 if (dex_canonical_location != dex_location) {
1332 StringPiece canonical_key(dex_canonical_location);
1333 auto canonical_it = oat_dex_files_.find(canonical_key);
1334 if (canonical_it != oat_dex_files_.end()) {
1335 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001336 } // else keep null.
1337 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001338
1339 // Copy the key to the string_cache_ and store the result in secondary map.
1340 string_cache_.emplace_back(key.data(), key.length());
1341 StringPiece key_copy(string_cache_.back());
1342 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001343 }
1344 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001345
1346 if (oat_dex_file == nullptr) {
1347 if (error_msg != nullptr) {
1348 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1349 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1350 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1351 }
1352 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001353 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001354
Richard Uhler9a37efc2016-08-05 16:32:55 -07001355 if (dex_location_checksum != nullptr &&
1356 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1357 if (error_msg != nullptr) {
1358 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1359 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1360 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1361 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1362 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1363 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001364 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001365 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001366 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001367 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001368}
1369
Brian Carlstrome24fa612011-09-29 00:53:55 -07001370OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001371 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001372 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001373 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001374 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001375 const uint8_t* lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001376 const MethodBssMapping* method_bss_mapping_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001377 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001378 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001379 : oat_file_(oat_file),
1380 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001381 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001382 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001383 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001384 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001385 method_bss_mapping_(method_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001386 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001387 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001388 // Initialize TypeLookupTable.
1389 if (lookup_table_data_ != nullptr) {
1390 // Peek the number of classes from the DexFile.
1391 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1392 const uint32_t num_class_defs = dex_header->class_defs_size_;
1393 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1394 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1395 } else {
Mathieu Chartier1b868492016-11-16 16:22:37 -08001396 lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001397 }
1398 }
1399}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001400
Mathieu Chartier1b868492016-11-16 16:22:37 -08001401OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table)
1402 : lookup_table_(std::move(lookup_table)) {}
1403
Brian Carlstrome24fa612011-09-29 00:53:55 -07001404OatFile::OatDexFile::~OatDexFile() {}
1405
Ian Rogers05f28c62012-10-23 18:12:13 -07001406size_t OatFile::OatDexFile::FileSize() const {
1407 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1408}
1409
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001410std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001411 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001412 static constexpr bool kVerify = false;
1413 static constexpr bool kVerifyChecksum = false;
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001414 return DexFile::Open(dex_file_pointer_,
1415 FileSize(),
1416 dex_file_location_,
1417 dex_file_location_checksum_,
1418 this,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001419 kVerify,
1420 kVerifyChecksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001421 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001422}
1423
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001424uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1425 return oat_class_offsets_pointer_[class_def_index];
1426}
1427
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001428OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001429 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001430
Ian Rogers13735952014-10-08 12:43:28 -07001431 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001432 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001433
Ian Rogers13735952014-10-08 12:43:28 -07001434 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001435 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1436 mirror::Class::Status status =
1437 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1438 CHECK_LT(status, mirror::Class::kStatusMax);
1439
Ian Rogers13735952014-10-08 12:43:28 -07001440 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001441 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1442 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1443 CHECK_LT(type, kOatClassMax);
1444
Ian Rogers13735952014-10-08 12:43:28 -07001445 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001446 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001447
Brian Carlstromcd937a92014-03-04 22:53:23 -08001448 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001449 const uint8_t* bitmap_pointer = nullptr;
1450 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001451 if (type != kOatClassNoneCompiled) {
1452 if (type == kOatClassSomeCompiled) {
1453 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1454 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1455 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1456 methods_pointer = bitmap_pointer + bitmap_size;
1457 } else {
1458 methods_pointer = after_type_pointer;
1459 }
1460 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001461 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001462
Richard Uhler07b3c232015-03-31 15:57:54 -07001463 return OatFile::OatClass(oat_file_,
1464 status,
1465 type,
1466 bitmap_size,
1467 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1468 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001469}
1470
David Sehr9aa352e2016-09-15 18:13:52 -07001471const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1472 const char* descriptor,
1473 size_t hash) {
1474 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1475 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1476 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1477 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
Andreas Gampee2abbc62017-09-15 11:59:26 -07001478 return (class_def_idx != dex::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
David Sehr9aa352e2016-09-15 18:13:52 -07001479 }
1480 // Fast path for rare no class defs case.
1481 const uint32_t num_class_defs = dex_file.NumClassDefs();
1482 if (num_class_defs == 0) {
1483 return nullptr;
1484 }
1485 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1486 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001487 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
David Sehr9aa352e2016-09-15 18:13:52 -07001488 return dex_file.FindClassDef(type_idx);
1489 }
1490 return nullptr;
1491}
1492
Mathieu Chartier120aa282017-08-05 16:03:03 -07001493// Madvise the dex file based on the state we are moving to.
1494void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001495 const bool low_ram = Runtime::Current()->GetHeap()->IsLowMemoryMode();
1496 // TODO: Also do madvise hints for non low ram devices.
1497 if (!kMadviseDexFileAccesses || !low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07001498 return;
1499 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001500 if (state == MadviseState::kMadviseStateAtLoad) {
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001501 if (low_ram) {
Mathieu Chartier4ec507d2017-08-15 15:21:40 -07001502 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
1503 // Other devices have enough page cache to get performance benefits from loading more pages
1504 // into the page cache.
1505 MadviseLargestPageAlignedRegion(dex_file.Begin(),
1506 dex_file.Begin() + dex_file.Size(),
1507 MADV_RANDOM);
1508 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001509 }
1510 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1511 if (oat_dex_file != nullptr) {
1512 // Should always be there.
1513 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
1514 CHECK(sections != nullptr);
1515 sections->Madvise(&dex_file, state);
1516 }
1517}
1518
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001519OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001520 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001521 OatClassType type,
1522 uint32_t bitmap_size,
1523 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001524 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001525 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001526 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001527 switch (type_) {
1528 case kOatClassAllCompiled: {
1529 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001530 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001531 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001532 break;
1533 }
1534 case kOatClassSomeCompiled: {
1535 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001536 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001537 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001538 break;
1539 }
1540 case kOatClassNoneCompiled: {
1541 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001542 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001543 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001544 break;
1545 }
1546 case kOatClassMax: {
1547 LOG(FATAL) << "Invalid OatClassType " << type_;
1548 break;
1549 }
1550 }
1551}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001552
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001553uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1554 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1555 if (oat_method_offsets == nullptr) {
1556 return 0u;
1557 }
1558 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1559}
1560
1561const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001562 // 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 -07001563 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001564 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001565 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001566 }
1567 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001568 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001569 CHECK_EQ(kOatClassAllCompiled, type_);
1570 methods_pointer_index = method_index;
1571 } else {
1572 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001573 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001574 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001575 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001576 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1577 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001578 }
1579 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001580 return &oat_method_offsets;
1581}
1582
1583const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1584 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1585 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001586 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001587 }
1588 if (oat_file_->IsExecutable() ||
1589 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001590 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001591 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001592 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001593 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1594 // version.
1595 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001596}
1597
Mathieu Chartiere401d142015-04-22 13:56:20 -07001598void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001599 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001600 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001601}
1602
Andreas Gampe7ba64962014-10-23 11:37:40 -07001603bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001604 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001605 // TODO: Check against oat_patches. b/18144996
1606}
1607
Sebastien Hertz0de11332015-05-13 12:14:05 +02001608bool OatFile::IsDebuggable() const {
1609 return GetOatHeader().IsDebuggable();
1610}
1611
Andreas Gampe29d38e72016-03-23 15:31:51 +00001612CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1613 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001614}
1615
Calin Juravle44e5efa2017-09-12 00:54:26 -07001616std::string OatFile::GetClassLoaderContext() const {
1617 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1618};
1619
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001620OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1621 uint16_t class_def_idx,
1622 bool* found) {
1623 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
1624 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001625 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001626 *found = false;
1627 return OatFile::OatClass::Invalid();
1628 }
1629 *found = true;
1630 return oat_dex_file->GetOatClass(class_def_idx);
1631}
1632
Mathieu Chartier1b868492016-11-16 16:22:37 -08001633void OatFile::OatDexFile::AssertAotCompiler() {
1634 CHECK(Runtime::Current()->IsAotCompiler());
1635}
1636
Brian Carlstrome24fa612011-09-29 00:53:55 -07001637} // namespace art