blob: 1c1189d7de845b955b251e2f0ede948183d2be9d [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>
Calin Juravle4e1d5792014-07-15 23:56:47 +010020#include <string.h>
Vladimir Marko06d7aaa2015-10-16 11:23:41 +010021#include <type_traits>
Andreas Gampedaab38c2014-09-12 18:38:24 -070022#include <unistd.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080023
Andreas Gampe7848da42015-04-09 11:15:04 -070024#include <cstdlib>
David Srbecky1baabf02015-06-16 17:12:34 +000025#ifndef __APPLE__
26#include <link.h> // for dl_iterate_phdr.
27#endif
Richard Uhlere5fed032015-03-18 08:21:11 -070028#include <sstream>
29
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"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047#include "oat.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"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010051#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070052#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070053#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070054#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000055#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070056#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010058#include "utils/dex_cache_arrays_layout-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010059#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070060
61namespace art {
62
Andreas Gampe46ee31b2016-12-14 10:11:49 -080063using android::base::StringPrintf;
64
Andreas Gampe049cff02015-12-01 23:27:12 -080065// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000066static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070067
Andreas Gampe049cff02015-12-01 23:27:12 -080068// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070069// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
70// multiple times). However, if/when we switch the above, we likely want to switch this, too,
71// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000072static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070073
74// For debugging, Open will print DlOpen error message if set to true.
75static constexpr bool kPrintDlOpenErrorMessage = false;
76
Andreas Gampe049cff02015-12-01 23:27:12 -080077// Note for OatFileBase and descendents:
78//
79// These are used in OatFile::Open to try all our loaders.
80//
81// The process is simple:
82//
83// 1) Allocate an instance through the standard constructor (location, executable)
84// 2) Load() to try to open the file.
85// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
86// 4) PreSetup() for any steps that should be done before the final setup.
87// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070088
Andreas Gampe049cff02015-12-01 23:27:12 -080089class OatFileBase : public OatFile {
90 public:
91 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070092
Andreas Gampe049cff02015-12-01 23:27:12 -080093 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010094 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
95 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070096 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080097 uint8_t* requested_base,
98 uint8_t* oat_file_begin,
99 bool writable,
100 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800101 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700102 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800103 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104
Andreas Gampe049cff02015-12-01 23:27:12 -0800105 protected:
106 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700107
Andreas Gampe049cff02015-12-01 23:27:12 -0800108 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
109 std::string* error_msg) const = 0;
110
Andreas Gampe4075f832016-05-18 13:09:54 -0700111 virtual void PreLoad() = 0;
112
David Brazdil7b49e6c2016-09-01 11:06:18 +0100113 bool LoadVdex(const std::string& vdex_filename,
114 bool writable,
115 bool low_4gb,
116 std::string* error_msg);
117
Andreas Gampe049cff02015-12-01 23:27:12 -0800118 virtual bool Load(const std::string& elf_filename,
119 uint8_t* oat_file_begin,
120 bool writable,
121 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800122 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800123 std::string* error_msg) = 0;
124
125 bool ComputeFields(uint8_t* requested_base,
126 const std::string& file_path,
127 std::string* error_msg);
128
129 virtual void PreSetup(const std::string& elf_filename) = 0;
130
131 bool Setup(const char* abs_dex_location, std::string* error_msg);
132
133 // Setters exposed for ElfOatFile.
134
135 void SetBegin(const uint8_t* begin) {
136 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700137 }
138
Andreas Gampe049cff02015-12-01 23:27:12 -0800139 void SetEnd(const uint8_t* end) {
140 end_ = end;
141 }
142
David Brazdilc93b3be2016-09-12 18:49:58 +0100143 void SetVdex(VdexFile* vdex) {
144 vdex_.reset(vdex);
145 }
146
Andreas Gampe049cff02015-12-01 23:27:12 -0800147 private:
148 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
149};
150
151template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100152OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
153 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800154 const std::string& location,
155 uint8_t* requested_base,
156 uint8_t* oat_file_begin,
157 bool writable,
158 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800159 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800160 const char* abs_dex_location,
161 std::string* error_msg) {
162 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700163
164 ret->PreLoad();
165
David Brazdil7b49e6c2016-09-01 11:06:18 +0100166 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
167 return nullptr;
168 }
169
Andreas Gampe049cff02015-12-01 23:27:12 -0800170 if (!ret->Load(elf_filename,
171 oat_file_begin,
172 writable,
173 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800174 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800175 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800176 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800177 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800178
Andreas Gampe049cff02015-12-01 23:27:12 -0800179 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
180 return nullptr;
181 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700182
Andreas Gampe049cff02015-12-01 23:27:12 -0800183 ret->PreSetup(elf_filename);
184
185 if (!ret->Setup(abs_dex_location, error_msg)) {
186 return nullptr;
187 }
188
Dave Allison69dfe512014-07-11 17:11:58 +0000189 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800190}
191
David Brazdil7b49e6c2016-09-01 11:06:18 +0100192bool OatFileBase::LoadVdex(const std::string& vdex_filename,
193 bool writable,
194 bool low_4gb,
195 std::string* error_msg) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100196 vdex_ = VdexFile::Open(vdex_filename, writable, low_4gb, /* unquicken*/ false, error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100197 if (vdex_.get() == nullptr) {
198 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
199 vdex_filename.c_str(),
200 error_msg->c_str());
201 return false;
202 }
203 return true;
204}
205
Andreas Gampe049cff02015-12-01 23:27:12 -0800206bool OatFileBase::ComputeFields(uint8_t* requested_base,
207 const std::string& file_path,
208 std::string* error_msg) {
209 std::string symbol_error_msg;
210 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700211 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800212 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
213 file_path.c_str(),
214 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700215 return false;
216 }
217 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000218 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700219 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000220 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
221 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700222 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800223 "oatdata=%p != expected=%p. See process maps in the log.",
224 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700225 return false;
226 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800227 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700228 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800229 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
230 file_path.c_str(),
231 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700232 return false;
233 }
234 // Readjust to be non-inclusive upper bound.
235 end_ += sizeof(uint32_t);
236
Andreas Gampe049cff02015-12-01 23:27:12 -0800237 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700238 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800239 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700240 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700241 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800242 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700243 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800244 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700245 return false;
246 }
247 // Readjust to be non-inclusive upper bound.
248 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100249 // Find bss methods if present.
250 bss_methods_ =
251 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000252 // Find bss roots if present.
253 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700254 }
255
Andreas Gampe049cff02015-12-01 23:27:12 -0800256 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800257}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800258
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100259// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
260// position by the number of bytes read, i.e. sizeof(T).
261// Return true on success, false if the read would go beyond the end of the OatFile.
262template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100263inline static bool ReadOatDexFileData(const OatFile& oat_file,
264 /*inout*/const uint8_t** oat,
265 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100266 DCHECK(oat != nullptr);
267 DCHECK(value != nullptr);
268 DCHECK_LE(*oat, oat_file.End());
269 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
270 return false;
271 }
272 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
273 typedef __attribute__((__aligned__(1))) T unaligned_type;
274 *value = *reinterpret_cast<const unaligned_type*>(*oat);
275 *oat += sizeof(T);
276 return true;
277}
278
Orion Hodsonc069a302017-01-18 09:23:12 +0000279static bool FindDexFileMapItem(const uint8_t* dex_begin,
280 const uint8_t* dex_end,
281 DexFile::MapItemType map_item_type,
282 const DexFile::MapItem** result_item) {
283 *result_item = nullptr;
284
285 const DexFile::Header* header =
286 BoundsCheckedCast<const DexFile::Header*>(dex_begin, dex_begin, dex_end);
287 if (nullptr == header) return false;
288
289 if (!DexFile::IsMagicValid(header->magic_)) return true; // Not a dex file, not an error.
290
291 const DexFile::MapList* map_list =
292 BoundsCheckedCast<const DexFile::MapList*>(dex_begin + header->map_off_, dex_begin, dex_end);
293 if (nullptr == map_list) return false;
294
295 const DexFile::MapItem* map_item = map_list->list_;
296 size_t count = map_list->size_;
297 while (count--) {
298 if (map_item->type_ == static_cast<uint16_t>(map_item_type)) {
299 *result_item = map_item;
300 break;
301 }
302 map_item = BoundsCheckedCast<const DexFile::MapItem*>(map_item + 1, dex_begin, dex_end);
303 if (nullptr == map_item) return false;
304 }
305
306 return true;
307}
308
Andreas Gampe049cff02015-12-01 23:27:12 -0800309bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700310 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800311 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100312 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
313 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800314 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700315 return false;
316 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100317 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
318 size_t key_value_store_size =
319 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
320 if (Size() < sizeof(OatHeader) + key_value_store_size) {
321 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
322 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100323 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100324 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100325 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100326 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700327 return false;
328 }
329
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100330 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
331 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
332 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
333 "%zu is not in [%zu, %zu]",
334 GetLocation().c_str(),
335 oat_dex_files_offset,
336 GetOatHeader().GetHeaderSize(),
337 Size());
338 return false;
339 }
340 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
341
342 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
343 if (!IsAligned<kPageSize>(bss_begin_) ||
344 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
345 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000346 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
347 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100348 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000349 GetLocation().c_str(),
350 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100351 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000352 bss_roots_,
353 bss_end_);
354 return false;
355 }
356
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100357 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
358 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
359 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
360 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
361 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000363 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100364 bss_methods_,
365 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000366 bss_end_);
367 return false;
368 }
369
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100370 uint8_t* after_arrays = (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_; // May be null.
371 uint8_t* dex_cache_arrays = (bss_begin_ == after_arrays) ? nullptr : bss_begin_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000372 uint8_t* dex_cache_arrays_end =
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100373 (bss_begin_ == after_arrays) ? nullptr : (after_arrays != nullptr) ? after_arrays : bss_end_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000374 DCHECK_EQ(dex_cache_arrays != nullptr, dex_cache_arrays_end != nullptr);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100375 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
376 oat_dex_files_storage_.reserve(dex_file_count);
377 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100378 uint32_t dex_file_location_size;
379 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
380 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
381 "location size",
382 GetLocation().c_str(),
383 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700384 return false;
385 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100386 if (UNLIKELY(dex_file_location_size == 0U)) {
387 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
388 GetLocation().c_str(),
389 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700390 return false;
391 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000392 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100393 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
394 "location",
395 GetLocation().c_str(),
396 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700397 return false;
398 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000399 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
400 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401
Richard Uhlere5fed032015-03-18 08:21:11 -0700402 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
403 abs_dex_location,
404 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700405
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100406 uint32_t dex_file_checksum;
407 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
408 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
409 "dex file checksum",
410 GetLocation().c_str(),
411 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700412 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700413 return false;
414 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700415
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100416 uint32_t dex_file_offset;
417 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
418 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
419 "after dex file offsets",
420 GetLocation().c_str(),
421 i,
422 dex_file_location.c_str());
423 return false;
424 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700425 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100426 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
427 "file offset",
428 GetLocation().c_str(),
429 i,
430 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700431 return false;
432 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100433 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100434 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
435 "offset %u > %zu",
436 GetLocation().c_str(),
437 i,
438 dex_file_location.c_str(),
439 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100440 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700441 return false;
442 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100443 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000444 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
445 "offset %u of %zu but the size of dex file header is %zu",
446 GetLocation().c_str(),
447 i,
448 dex_file_location.c_str(),
449 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100450 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000451 sizeof(DexFile::Header));
452 return false;
453 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800454
David Brazdil7b49e6c2016-09-01 11:06:18 +0100455 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700456 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100457 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
458 "dex file magic '%s'",
459 GetLocation().c_str(),
460 i,
461 dex_file_location.c_str(),
462 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700463 return false;
464 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700465 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100466 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
467 "dex file version '%s'",
468 GetLocation().c_str(),
469 i,
470 dex_file_location.c_str(),
471 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700472 return false;
473 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800474 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100475 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000476 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
477 "offset %u and size %u truncated at %zu",
478 GetLocation().c_str(),
479 i,
480 dex_file_location.c_str(),
481 dex_file_offset,
482 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100483 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000484 return false;
485 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300486
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000487 uint32_t class_offsets_offset;
488 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
489 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
490 "after class offsets offset",
491 GetLocation().c_str(),
492 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300493 dex_file_location.c_str());
494 return false;
495 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000496 if (UNLIKELY(class_offsets_offset > Size()) ||
497 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
498 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
499 "class offsets, offset %u of %zu, class defs %u",
500 GetLocation().c_str(),
501 i,
502 dex_file_location.c_str(),
503 class_offsets_offset,
504 Size(),
505 header->class_defs_size_);
506 return false;
507 }
508 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
509 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
510 "class offsets, offset %u",
511 GetLocation().c_str(),
512 i,
513 dex_file_location.c_str(),
514 class_offsets_offset);
515 return false;
516 }
517 const uint32_t* class_offsets_pointer =
518 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
519
520 uint32_t lookup_table_offset;
521 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
522 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
523 "after lookup table offset",
524 GetLocation().c_str(),
525 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300526 dex_file_location.c_str());
527 return false;
528 }
529 const uint8_t* lookup_table_data = lookup_table_offset != 0u
530 ? Begin() + lookup_table_offset
531 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000532 if (lookup_table_offset != 0u &&
533 (UNLIKELY(lookup_table_offset > Size()) ||
534 UNLIKELY(Size() - lookup_table_offset <
535 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100536 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000537 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100538 GetLocation().c_str(),
539 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000540 dex_file_location.c_str(),
541 lookup_table_offset,
542 Size(),
543 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700544 return false;
545 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700546
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100547 uint32_t method_bss_mapping_offset;
548 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &method_bss_mapping_offset))) {
549 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
550 "after method bss mapping offset",
551 GetLocation().c_str(),
552 i,
553 dex_file_location.c_str());
554 return false;
555 }
556 const bool readable_method_bss_mapping_size =
557 method_bss_mapping_offset != 0u &&
558 method_bss_mapping_offset <= Size() &&
559 IsAligned<alignof(MethodBssMapping)>(method_bss_mapping_offset) &&
560 Size() - method_bss_mapping_offset >= MethodBssMapping::ComputeSize(0);
561 const MethodBssMapping* method_bss_mapping = readable_method_bss_mapping_size
562 ? reinterpret_cast<const MethodBssMapping*>(Begin() + method_bss_mapping_offset)
563 : nullptr;
564 if (method_bss_mapping_offset != 0u &&
565 (UNLIKELY(method_bss_mapping == nullptr) ||
566 UNLIKELY(method_bss_mapping->size() == 0u) ||
567 UNLIKELY(Size() - method_bss_mapping_offset <
568 MethodBssMapping::ComputeSize(method_bss_mapping->size())))) {
569 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
570 " truncated method bss mapping, offset %u of %zu, length %zu",
571 GetLocation().c_str(),
572 i,
573 dex_file_location.c_str(),
574 method_bss_mapping_offset,
575 Size(),
576 method_bss_mapping != nullptr ? method_bss_mapping->size() : 0u);
577 return false;
578 }
579 if (kIsDebugBuild && method_bss_mapping != nullptr) {
580 const MethodBssMappingEntry* prev_entry = nullptr;
581 for (const MethodBssMappingEntry& entry : *method_bss_mapping) {
582 CHECK_ALIGNED_PARAM(entry.bss_offset, static_cast<size_t>(pointer_size));
583 CHECK_LT(entry.bss_offset, BssSize());
584 CHECK_LE(POPCOUNT(entry.index_mask) * static_cast<size_t>(pointer_size), entry.bss_offset);
585 size_t index_mask_span = (entry.index_mask != 0u) ? 16u - CTZ(entry.index_mask) : 0u;
586 CHECK_LE(index_mask_span, entry.method_index);
587 if (prev_entry != nullptr) {
588 CHECK_LT(prev_entry->method_index, entry.method_index - index_mask_span);
589 }
590 prev_entry = &entry;
591 }
592 CHECK_LT(prev_entry->method_index,
593 reinterpret_cast<const DexFile::Header*>(dex_file_pointer)->method_ids_size_);
594 }
595
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100596 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100597 if (dex_cache_arrays != nullptr) {
Orion Hodsonc069a302017-01-18 09:23:12 +0000598 // All DexCache types except for CallSite have their instance counts in the
599 // DexFile header. For CallSites, we need to read the info from the MapList.
600 const DexFile::MapItem* call_sites_item = nullptr;
601 if (!FindDexFileMapItem(DexBegin(),
602 DexEnd(),
603 DexFile::MapItemType::kDexTypeCallSiteIdItem,
604 &call_sites_item)) {
605 *error_msg = StringPrintf("In oat file '%s' could not read data from truncated DexFile map",
606 GetLocation().c_str());
607 return false;
608 }
609 size_t num_call_sites = call_sites_item == nullptr ? 0 : call_sites_item->size_;
610 DexCacheArraysLayout layout(pointer_size, *header, num_call_sites);
Vladimir Marko09d09432015-09-08 13:47:48 +0100611 if (layout.Size() != 0u) {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000612 if (static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100613 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
614 "truncated dex cache arrays, %zu < %zu.",
615 GetLocation().c_str(),
616 i,
617 dex_file_location.c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000618 static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100619 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100620 return false;
621 }
622 current_dex_cache_arrays = dex_cache_arrays;
623 dex_cache_arrays += layout.Size();
624 }
625 }
626
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100627 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
628
629 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100630 OatDexFile* oat_dex_file = new OatDexFile(this,
631 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100632 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100633 dex_file_checksum,
634 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300635 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100636 method_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000637 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100638 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100639 oat_dex_files_storage_.push_back(oat_dex_file);
640
641 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100642 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100643 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100644 if (canonical_location != dex_file_location) {
645 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
646 oat_dex_files_.Put(canonical_key, oat_dex_file);
647 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700648 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100649
Vladimir Markoaad75c62016-10-03 08:46:48 +0000650 if (dex_cache_arrays != dex_cache_arrays_end) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100651 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
Vladimir Markoaad75c62016-10-03 08:46:48 +0000652 // both null) or contain just the dex cache arrays and optionally some GC roots.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100653 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100654 GetLocation().c_str(),
655 static_cast<size_t>(bss_end_ - dex_cache_arrays));
656 return false;
657 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700658 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700659}
660
Andreas Gampe049cff02015-12-01 23:27:12 -0800661////////////////////////
662// OatFile via dlopen //
663////////////////////////
664
Andreas Gampe049cff02015-12-01 23:27:12 -0800665class DlOpenOatFile FINAL : public OatFileBase {
666 public:
667 DlOpenOatFile(const std::string& filename, bool executable)
668 : OatFileBase(filename, executable),
669 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700670 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800671 }
672
673 ~DlOpenOatFile() {
674 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700675 if (!kIsTargetBuild) {
676 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
677 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700678 dlclose(dlopen_handle_);
679 } else {
680 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700681 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800682 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800683 }
684
685 protected:
686 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
687 std::string* error_msg) const OVERRIDE {
688 const uint8_t* ptr =
689 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
690 if (ptr == nullptr) {
691 *error_msg = dlerror();
692 }
693 return ptr;
694 }
695
Andreas Gampe4075f832016-05-18 13:09:54 -0700696 void PreLoad() OVERRIDE;
697
Andreas Gampe049cff02015-12-01 23:27:12 -0800698 bool Load(const std::string& elf_filename,
699 uint8_t* oat_file_begin,
700 bool writable,
701 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800702 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800703 std::string* error_msg) OVERRIDE;
704
705 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
706 void PreSetup(const std::string& elf_filename) OVERRIDE;
707
708 private:
709 bool Dlopen(const std::string& elf_filename,
710 uint8_t* oat_file_begin,
711 std::string* error_msg);
712
Richard Uhlera206c742016-05-24 15:04:22 -0700713 // On the host, if the same library is loaded again with dlopen the same
714 // file handle is returned. This differs from the behavior of dlopen on the
715 // target, where dlopen reloads the library at a different address every
716 // time you load it. The runtime relies on the target behavior to ensure
717 // each instance of the loaded library has a unique dex cache. To avoid
718 // problems, we fall back to our own linker in the case when the same
719 // library is opened multiple times on host. dlopen_handles_ is used to
720 // detect that case.
721 // Guarded by host_dlopen_handles_lock_;
722 static std::unordered_set<void*> host_dlopen_handles_;
723
Andreas Gampe049cff02015-12-01 23:27:12 -0800724 // dlopen handle during runtime.
725 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
726
727 // Dummy memory map objects corresponding to the regions mapped by dlopen.
728 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
729
Andreas Gampe4075f832016-05-18 13:09:54 -0700730 // The number of shared objects the linker told us about before loading. Used to
731 // (optimistically) optimize the PreSetup stage (see comment there).
732 size_t shared_objects_before_;
733
Andreas Gampe049cff02015-12-01 23:27:12 -0800734 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
735};
736
Richard Uhlera206c742016-05-24 15:04:22 -0700737std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
738
Andreas Gampe4075f832016-05-18 13:09:54 -0700739void DlOpenOatFile::PreLoad() {
740#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700741 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700742 LOG(FATAL) << "Should not reach here.";
743 UNREACHABLE();
744#else
745 // Count the entries in dl_iterate_phdr we get at this point in time.
746 struct dl_iterate_context {
747 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
748 size_t size ATTRIBUTE_UNUSED,
749 void *data) {
750 reinterpret_cast<dl_iterate_context*>(data)->count++;
751 return 0; // Continue iteration.
752 }
753 size_t count = 0;
754 } context;
755
756 dl_iterate_phdr(dl_iterate_context::callback, &context);
757 shared_objects_before_ = context.count;
758#endif
759}
760
Andreas Gampe049cff02015-12-01 23:27:12 -0800761bool DlOpenOatFile::Load(const std::string& elf_filename,
762 uint8_t* oat_file_begin,
763 bool writable,
764 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800765 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800766 std::string* error_msg) {
767 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
768 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
769 // !executable is a sign that we may want to patch), which may not be allowed for
770 // various reasons.
771 if (!kUseDlopen) {
772 *error_msg = "DlOpen is disabled.";
773 return false;
774 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800775 if (low_4gb) {
776 *error_msg = "DlOpen does not support low 4gb loading.";
777 return false;
778 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800779 if (writable) {
780 *error_msg = "DlOpen does not support writable loading.";
781 return false;
782 }
783 if (!executable) {
784 *error_msg = "DlOpen does not support non-executable loading.";
785 return false;
786 }
787
788 // dlopen always returns the same library if it is already opened on the host. For this reason
789 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
790 // the same library loaded multiple times at different addresses is required for class unloading
791 // and for having dex caches arrays in the .bss section.
792 if (!kIsTargetBuild) {
793 if (!kUseDlopenOnHost) {
794 *error_msg = "DlOpen disabled for host.";
795 return false;
796 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800797 }
798
799 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
800 DCHECK(dlopen_handle_ != nullptr || !success);
801
802 return success;
803}
804
805bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
806 uint8_t* oat_file_begin,
807 std::string* error_msg) {
808#ifdef __APPLE__
809 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
810 // but let's fallback to the custom loading code for the time being.
811 UNUSED(elf_filename, oat_file_begin);
812 *error_msg = "Dlopen unsupported on Mac.";
813 return false;
814#else
815 {
816 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
817 if (absolute_path == nullptr) {
818 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
819 return false;
820 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100821#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +0000822 android_dlextinfo extinfo = {};
Andreas Gampe049cff02015-12-01 23:27:12 -0800823 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
824 // (open oat files multiple
825 // times).
826 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
827 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800828 if (oat_file_begin != nullptr) { //
829 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
830 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
831 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800832 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
833#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800834 UNUSED(oat_file_begin);
Julien Duraj1af0c4f2016-11-16 14:05:48 +0000835 static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700836 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700837 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
838 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700839 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
840 dlclose(dlopen_handle_);
841 dlopen_handle_ = nullptr;
842 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
843 return false;
844 }
845 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100846#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800847 }
848 if (dlopen_handle_ == nullptr) {
849 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
850 return false;
851 }
852 return true;
853#endif
854}
855
856void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800857#ifdef __APPLE__
858 UNUSED(elf_filename);
859 LOG(FATAL) << "Should not reach here.";
860 UNREACHABLE();
861#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800862 struct dl_iterate_context {
863 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
864 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700865 context->shared_objects_seen++;
866 if (context->shared_objects_seen < context->shared_objects_before) {
867 // We haven't been called yet for anything we haven't seen before. Just continue.
868 // Note: this is aggressively optimistic. If another thread was unloading a library,
869 // we may miss out here. However, this does not happen often in practice.
870 return 0;
871 }
872
Andreas Gampe049cff02015-12-01 23:27:12 -0800873 // See whether this callback corresponds to the file which we have just loaded.
874 bool contains_begin = false;
875 for (int i = 0; i < info->dlpi_phnum; i++) {
876 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
877 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
878 info->dlpi_phdr[i].p_vaddr);
879 size_t memsz = info->dlpi_phdr[i].p_memsz;
880 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
881 contains_begin = true;
882 break;
883 }
884 }
885 }
886 // Add dummy mmaps for this file.
887 if (contains_begin) {
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 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
894 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
895 }
896 }
897 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
898 }
899 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
900 }
901 const uint8_t* const begin_;
902 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700903 const size_t shared_objects_before;
904 size_t shared_objects_seen;
905 };
906 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800907
908 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700909 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
910 // before giving up. This should be unusual.
911 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
912 << shared_objects_before_;
913 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
914 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
915 // OK, give up and print an error.
916 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
917 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
918 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800919 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800920#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800921}
922
923////////////////////////////////////////////////
924// OatFile via our own ElfFile implementation //
925////////////////////////////////////////////////
926
927class ElfOatFile FINAL : public OatFileBase {
928 public:
929 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
930
931 static ElfOatFile* OpenElfFile(File* file,
932 const std::string& location,
933 uint8_t* requested_base,
934 uint8_t* oat_file_begin, // Override base if not null
935 bool writable,
936 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800937 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800938 const char* abs_dex_location,
939 std::string* error_msg);
940
941 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100942 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800943 const char* abs_dex_location,
944 std::string* error_msg);
945
946 protected:
947 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
948 std::string* error_msg) const OVERRIDE {
949 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
950 if (ptr == nullptr) {
951 *error_msg = "(Internal implementation could not find symbol)";
952 }
953 return ptr;
954 }
955
Andreas Gampe4075f832016-05-18 13:09:54 -0700956 void PreLoad() OVERRIDE {
957 }
958
Andreas Gampe049cff02015-12-01 23:27:12 -0800959 bool Load(const std::string& elf_filename,
960 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
961 bool writable,
962 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800963 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800964 std::string* error_msg) OVERRIDE;
965
966 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
967 }
968
969 private:
970 bool ElfFileOpen(File* file,
971 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
972 bool writable,
973 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800974 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800975 std::string* error_msg);
976
977 private:
978 // Backing memory map for oat file during cross compilation.
979 std::unique_ptr<ElfFile> elf_file_;
980
981 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
982};
983
984ElfOatFile* ElfOatFile::OpenElfFile(File* file,
985 const std::string& location,
986 uint8_t* requested_base,
987 uint8_t* oat_file_begin, // Override base if not null
988 bool writable,
989 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800990 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800991 const char* abs_dex_location,
992 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800993 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800994 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800995 bool success = oat_file->ElfFileOpen(file,
996 oat_file_begin,
997 writable,
998 low_4gb,
999 executable,
1000 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001001 if (!success) {
1002 CHECK(!error_msg->empty());
1003 return nullptr;
1004 }
1005
1006 // Complete the setup.
1007 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
1008 return nullptr;
1009 }
1010
1011 if (!oat_file->Setup(abs_dex_location, error_msg)) {
1012 return nullptr;
1013 }
1014
1015 return oat_file.release();
1016}
1017
1018bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001019 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001020 const char* abs_dex_location,
1021 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001022 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001023 if (IsExecutable()) {
1024 *error_msg = "Cannot initialize from elf file in executable mode.";
1025 return false;
1026 }
1027 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001028 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001029 uint64_t offset, size;
1030 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1031 CHECK(has_section);
1032 SetBegin(elf_file->Begin() + offset);
1033 SetEnd(elf_file->Begin() + size + offset);
1034 // Ignore the optional .bss section when opening non-executable.
1035 return Setup(abs_dex_location, error_msg);
1036}
1037
1038bool ElfOatFile::Load(const std::string& elf_filename,
1039 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1040 bool writable,
1041 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001042 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001043 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001044 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001045 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1046 if (file == nullptr) {
1047 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1048 return false;
1049 }
1050 return ElfOatFile::ElfFileOpen(file.get(),
1051 oat_file_begin,
1052 writable,
1053 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001054 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001055 error_msg);
1056}
1057
1058bool ElfOatFile::ElfFileOpen(File* file,
1059 uint8_t* oat_file_begin,
1060 bool writable,
1061 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001062 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001063 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001064 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001065 // TODO: rename requested_base to oat_data_begin
1066 elf_file_.reset(ElfFile::Open(file,
1067 writable,
1068 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001069 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001070 error_msg,
1071 oat_file_begin));
1072 if (elf_file_ == nullptr) {
1073 DCHECK(!error_msg->empty());
1074 return false;
1075 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001076 bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001077 DCHECK(loaded || !error_msg->empty());
1078 return loaded;
1079}
1080
1081//////////////////////////
1082// General OatFile code //
1083//////////////////////////
1084
1085std::string OatFile::ResolveRelativeEncodedDexLocation(
1086 const char* abs_dex_location, const std::string& rel_dex_location) {
1087 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
1088 // Strip :classes<N>.dex used for secondary multidex files.
1089 std::string base = DexFile::GetBaseLocation(rel_dex_location);
1090 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
1091
1092 // Check if the base is a suffix of the provided abs_dex_location.
1093 std::string target_suffix = "/" + base;
1094 std::string abs_location(abs_dex_location);
1095 if (abs_location.size() > target_suffix.size()) {
1096 size_t pos = abs_location.size() - target_suffix.size();
1097 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1098 return abs_location + multidex_suffix;
1099 }
1100 }
1101 }
1102 return rel_dex_location;
1103}
1104
1105static void CheckLocation(const std::string& location) {
1106 CHECK(!location.empty());
1107}
1108
1109OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001110 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001111 const std::string& location,
1112 const char* abs_dex_location,
1113 std::string* error_msg) {
1114 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001115 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001116 ? oat_file.release()
1117 : nullptr;
1118}
1119
David Brazdil7b49e6c2016-09-01 11:06:18 +01001120OatFile* OatFile::Open(const std::string& oat_filename,
1121 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001122 uint8_t* requested_base,
1123 uint8_t* oat_file_begin,
1124 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001125 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001126 const char* abs_dex_location,
1127 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001128 ScopedTrace trace("Open oat file " + oat_location);
1129 CHECK(!oat_filename.empty()) << oat_location;
1130 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001131
Calin Juravle367b9d82017-05-15 18:18:39 -07001132 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001133
1134 // Check that the files even exist, fast-fail.
1135 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
1136 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1137 return nullptr;
1138 } else if (!OS::FileExists(oat_filename.c_str())) {
1139 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001140 return nullptr;
1141 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001142
1143 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1144 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001145 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1146 oat_filename,
1147 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001148 requested_base,
1149 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001150 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001151 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001152 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001153 abs_dex_location,
1154 error_msg);
1155 if (with_dlopen != nullptr) {
1156 return with_dlopen;
1157 }
1158 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001159 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001160 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001161 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1162 //
1163 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1164 //
1165 // We use our own ELF loader for Quick to deal with legacy apps that
1166 // open a generated dex file by name, remove the file, then open
1167 // another generated dex file with the same name. http://b/10614658
1168 //
1169 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1170 //
1171 //
1172 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1173 // 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 +01001174 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1175 oat_filename,
1176 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001177 requested_base,
1178 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001179 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001180 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001181 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001182 abs_dex_location,
1183 error_msg);
1184 return with_internal;
1185}
1186
1187OatFile* OatFile::OpenWritable(File* file,
1188 const std::string& location,
1189 const char* abs_dex_location,
1190 std::string* error_msg) {
1191 CheckLocation(location);
1192 return ElfOatFile::OpenElfFile(file,
1193 location,
1194 nullptr,
1195 nullptr,
1196 true,
1197 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001198 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001199 abs_dex_location,
1200 error_msg);
1201}
1202
1203OatFile* OatFile::OpenReadable(File* file,
1204 const std::string& location,
1205 const char* abs_dex_location,
1206 std::string* error_msg) {
1207 CheckLocation(location);
1208 return ElfOatFile::OpenElfFile(file,
1209 location,
1210 nullptr,
1211 nullptr,
1212 false,
1213 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001214 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001215 abs_dex_location,
1216 error_msg);
1217}
1218
1219OatFile::OatFile(const std::string& location, bool is_executable)
1220 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001221 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001222 begin_(nullptr),
1223 end_(nullptr),
1224 bss_begin_(nullptr),
1225 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001226 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001227 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001228 is_executable_(is_executable),
1229 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1230 CHECK(!location_.empty());
1231}
1232
1233OatFile::~OatFile() {
1234 STLDeleteElements(&oat_dex_files_storage_);
1235}
1236
Brian Carlstrome24fa612011-09-29 00:53:55 -07001237const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001238 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001239}
1240
Ian Rogers13735952014-10-08 12:43:28 -07001241const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001242 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001243 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001244}
1245
Ian Rogers13735952014-10-08 12:43:28 -07001246const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001247 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001248 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001249}
1250
Vladimir Marko5c42c292015-02-25 12:02:49 +00001251const uint8_t* OatFile::BssBegin() const {
1252 return bss_begin_;
1253}
1254
1255const uint8_t* OatFile::BssEnd() const {
1256 return bss_end_;
1257}
1258
David Brazdil7b49e6c2016-09-01 11:06:18 +01001259const uint8_t* OatFile::DexBegin() const {
1260 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1261}
1262
1263const uint8_t* OatFile::DexEnd() const {
1264 return kIsVdexEnabled ? vdex_->End() : End();
1265}
1266
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001267ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1268 if (bss_methods_ != nullptr) {
1269 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1270 ArtMethod** methods_end =
1271 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1272 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1273 } else {
1274 return ArrayRef<ArtMethod*>();
1275 }
1276}
1277
Vladimir Markoaad75c62016-10-03 08:46:48 +00001278ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1279 if (bss_roots_ != nullptr) {
1280 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1281 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1282 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1283 } else {
1284 return ArrayRef<GcRoot<mirror::Object>>();
1285 }
1286}
1287
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001288const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1289 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001290 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001291 // NOTE: We assume here that the canonical location for a given dex_location never
1292 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1293 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1294 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001295
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001296 // TODO: Additional analysis of usage patterns to see if this can be simplified
1297 // without any performance loss, for example by not doing the first lock-free lookup.
1298
1299 const OatFile::OatDexFile* oat_dex_file = nullptr;
1300 StringPiece key(dex_location);
1301 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1302 // directly mentioned in the oat file and doesn't require locking.
1303 auto primary_it = oat_dex_files_.find(key);
1304 if (primary_it != oat_dex_files_.end()) {
1305 oat_dex_file = primary_it->second;
1306 DCHECK(oat_dex_file != nullptr);
1307 } else {
1308 // This dex_location is not one of the dex locations directly mentioned in the
1309 // oat file. The correct lookup is via the canonical location but first see in
1310 // the secondary_oat_dex_files_ whether we've looked up this location before.
1311 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1312 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1313 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001314 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001315 } else {
1316 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001317 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001318 if (dex_canonical_location != dex_location) {
1319 StringPiece canonical_key(dex_canonical_location);
1320 auto canonical_it = oat_dex_files_.find(canonical_key);
1321 if (canonical_it != oat_dex_files_.end()) {
1322 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001323 } // else keep null.
1324 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001325
1326 // Copy the key to the string_cache_ and store the result in secondary map.
1327 string_cache_.emplace_back(key.data(), key.length());
1328 StringPiece key_copy(string_cache_.back());
1329 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001330 }
1331 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001332
1333 if (oat_dex_file == nullptr) {
1334 if (error_msg != nullptr) {
1335 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1336 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1337 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1338 }
1339 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001340 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001341
Richard Uhler9a37efc2016-08-05 16:32:55 -07001342 if (dex_location_checksum != nullptr &&
1343 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1344 if (error_msg != nullptr) {
1345 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1346 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1347 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1348 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1349 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1350 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001351 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001352 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001353 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001354 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001355}
1356
Brian Carlstrome24fa612011-09-29 00:53:55 -07001357OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001358 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001359 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001360 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001361 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001362 const uint8_t* lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001363 const MethodBssMapping* method_bss_mapping_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001364 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001365 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001366 : oat_file_(oat_file),
1367 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001368 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001369 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001370 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001371 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001372 method_bss_mapping_(method_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001373 oat_class_offsets_pointer_(oat_class_offsets_pointer),
David Sehr9aa352e2016-09-15 18:13:52 -07001374 dex_cache_arrays_(dex_cache_arrays) {
1375 // Initialize TypeLookupTable.
1376 if (lookup_table_data_ != nullptr) {
1377 // Peek the number of classes from the DexFile.
1378 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1379 const uint32_t num_class_defs = dex_header->class_defs_size_;
1380 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1381 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1382 } else {
Mathieu Chartier1b868492016-11-16 16:22:37 -08001383 lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001384 }
1385 }
1386}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001387
Mathieu Chartier1b868492016-11-16 16:22:37 -08001388OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table)
1389 : lookup_table_(std::move(lookup_table)) {}
1390
Brian Carlstrome24fa612011-09-29 00:53:55 -07001391OatFile::OatDexFile::~OatDexFile() {}
1392
Ian Rogers05f28c62012-10-23 18:12:13 -07001393size_t OatFile::OatDexFile::FileSize() const {
1394 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1395}
1396
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001397std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001398 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001399 static constexpr bool kVerify = false;
1400 static constexpr bool kVerifyChecksum = false;
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001401 return DexFile::Open(dex_file_pointer_,
1402 FileSize(),
1403 dex_file_location_,
1404 dex_file_location_checksum_,
1405 this,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001406 kVerify,
1407 kVerifyChecksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001408 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001409}
1410
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001411uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1412 return oat_class_offsets_pointer_[class_def_index];
1413}
1414
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001415OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001416 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001417
Ian Rogers13735952014-10-08 12:43:28 -07001418 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001419 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001420
Ian Rogers13735952014-10-08 12:43:28 -07001421 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001422 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1423 mirror::Class::Status status =
1424 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1425 CHECK_LT(status, mirror::Class::kStatusMax);
1426
Ian Rogers13735952014-10-08 12:43:28 -07001427 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001428 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1429 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1430 CHECK_LT(type, kOatClassMax);
1431
Ian Rogers13735952014-10-08 12:43:28 -07001432 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001433 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001434
Brian Carlstromcd937a92014-03-04 22:53:23 -08001435 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001436 const uint8_t* bitmap_pointer = nullptr;
1437 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001438 if (type != kOatClassNoneCompiled) {
1439 if (type == kOatClassSomeCompiled) {
1440 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1441 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1442 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1443 methods_pointer = bitmap_pointer + bitmap_size;
1444 } else {
1445 methods_pointer = after_type_pointer;
1446 }
1447 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001448 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001449
Richard Uhler07b3c232015-03-31 15:57:54 -07001450 return OatFile::OatClass(oat_file_,
1451 status,
1452 type,
1453 bitmap_size,
1454 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1455 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001456}
1457
David Sehr9aa352e2016-09-15 18:13:52 -07001458const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1459 const char* descriptor,
1460 size_t hash) {
1461 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1462 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1463 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1464 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
1465 return (class_def_idx != DexFile::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
1466 }
1467 // Fast path for rare no class defs case.
1468 const uint32_t num_class_defs = dex_file.NumClassDefs();
1469 if (num_class_defs == 0) {
1470 return nullptr;
1471 }
1472 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1473 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001474 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
David Sehr9aa352e2016-09-15 18:13:52 -07001475 return dex_file.FindClassDef(type_idx);
1476 }
1477 return nullptr;
1478}
1479
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001480OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001481 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001482 OatClassType type,
1483 uint32_t bitmap_size,
1484 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001485 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001486 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001487 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001488 switch (type_) {
1489 case kOatClassAllCompiled: {
1490 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001491 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001492 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001493 break;
1494 }
1495 case kOatClassSomeCompiled: {
1496 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001497 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001498 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001499 break;
1500 }
1501 case kOatClassNoneCompiled: {
1502 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001503 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001504 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001505 break;
1506 }
1507 case kOatClassMax: {
1508 LOG(FATAL) << "Invalid OatClassType " << type_;
1509 break;
1510 }
1511 }
1512}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001513
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001514uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1515 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1516 if (oat_method_offsets == nullptr) {
1517 return 0u;
1518 }
1519 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1520}
1521
1522const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001523 // 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 -07001524 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001525 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001526 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001527 }
1528 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001529 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001530 CHECK_EQ(kOatClassAllCompiled, type_);
1531 methods_pointer_index = method_index;
1532 } else {
1533 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001534 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001535 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001536 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001537 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1538 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001539 }
1540 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001541 return &oat_method_offsets;
1542}
1543
1544const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1545 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1546 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001547 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001548 }
1549 if (oat_file_->IsExecutable() ||
1550 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001551 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001552 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001553 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001554 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1555 // version.
1556 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001557}
1558
Mathieu Chartiere401d142015-04-22 13:56:20 -07001559void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001560 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001561 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001562}
1563
Andreas Gampe7ba64962014-10-23 11:37:40 -07001564bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001565 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001566 // TODO: Check against oat_patches. b/18144996
1567}
1568
Sebastien Hertz0de11332015-05-13 12:14:05 +02001569bool OatFile::IsDebuggable() const {
1570 return GetOatHeader().IsDebuggable();
1571}
1572
Andreas Gampe29d38e72016-03-23 15:31:51 +00001573CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1574 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001575}
1576
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001577OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1578 uint16_t class_def_idx,
1579 bool* found) {
1580 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
1581 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001582 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001583 *found = false;
1584 return OatFile::OatClass::Invalid();
1585 }
1586 *found = true;
1587 return oat_dex_file->GetOatClass(class_def_idx);
1588}
1589
Mathieu Chartier1b868492016-11-16 16:22:37 -08001590void OatFile::OatDexFile::AssertAotCompiler() {
1591 CHECK(Runtime::Current()->IsAotCompiler());
1592}
1593
Brian Carlstrome24fa612011-09-29 00:53:55 -07001594} // namespace art