blob: 3f4cb942fc85288e70e222ec75a92c69b8968b38 [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"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070051#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010052#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070053#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070054#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070055#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000056#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070057#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058#include "utils.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
Mathieu Chartierbe8303d2017-08-17 17:39:39 -070077// If true, we advise the kernel about dex file mem map accesses.
Mathieu Chartier150d25d2017-08-28 09:52:55 -070078static constexpr bool kMadviseDexFileAccesses = true;
Mathieu Chartierbe8303d2017-08-17 17:39:39 -070079
Andreas Gampe049cff02015-12-01 23:27:12 -080080// Note for OatFileBase and descendents:
81//
82// These are used in OatFile::Open to try all our loaders.
83//
84// The process is simple:
85//
86// 1) Allocate an instance through the standard constructor (location, executable)
87// 2) Load() to try to open the file.
88// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
89// 4) PreSetup() for any steps that should be done before the final setup.
90// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070091
Andreas Gampe049cff02015-12-01 23:27:12 -080092class OatFileBase : public OatFile {
93 public:
94 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070095
Andreas Gampe049cff02015-12-01 23:27:12 -080096 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010097 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
98 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070099 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800100 uint8_t* requested_base,
101 uint8_t* oat_file_begin,
102 bool writable,
103 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800104 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700105 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800106 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800107
Andreas Gampe049cff02015-12-01 23:27:12 -0800108 protected:
109 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700110
Andreas Gampe049cff02015-12-01 23:27:12 -0800111 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
112 std::string* error_msg) const = 0;
113
Andreas Gampe4075f832016-05-18 13:09:54 -0700114 virtual void PreLoad() = 0;
115
David Brazdil7b49e6c2016-09-01 11:06:18 +0100116 bool LoadVdex(const std::string& vdex_filename,
117 bool writable,
118 bool low_4gb,
119 std::string* error_msg);
120
Andreas Gampe049cff02015-12-01 23:27:12 -0800121 virtual bool Load(const std::string& elf_filename,
122 uint8_t* oat_file_begin,
123 bool writable,
124 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800125 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800126 std::string* error_msg) = 0;
127
128 bool ComputeFields(uint8_t* requested_base,
129 const std::string& file_path,
130 std::string* error_msg);
131
132 virtual void PreSetup(const std::string& elf_filename) = 0;
133
134 bool Setup(const char* abs_dex_location, std::string* error_msg);
135
136 // Setters exposed for ElfOatFile.
137
138 void SetBegin(const uint8_t* begin) {
139 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700140 }
141
Andreas Gampe049cff02015-12-01 23:27:12 -0800142 void SetEnd(const uint8_t* end) {
143 end_ = end;
144 }
145
David Brazdilc93b3be2016-09-12 18:49:58 +0100146 void SetVdex(VdexFile* vdex) {
147 vdex_.reset(vdex);
148 }
149
Andreas Gampe049cff02015-12-01 23:27:12 -0800150 private:
151 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
152};
153
154template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100155OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
156 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800157 const std::string& location,
158 uint8_t* requested_base,
159 uint8_t* oat_file_begin,
160 bool writable,
161 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800162 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800163 const char* abs_dex_location,
164 std::string* error_msg) {
165 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700166
167 ret->PreLoad();
168
David Brazdil7b49e6c2016-09-01 11:06:18 +0100169 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
170 return nullptr;
171 }
172
Andreas Gampe049cff02015-12-01 23:27:12 -0800173 if (!ret->Load(elf_filename,
174 oat_file_begin,
175 writable,
176 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800177 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800178 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800179 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800180 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800181
Andreas Gampe049cff02015-12-01 23:27:12 -0800182 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
183 return nullptr;
184 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700185
Andreas Gampe049cff02015-12-01 23:27:12 -0800186 ret->PreSetup(elf_filename);
187
188 if (!ret->Setup(abs_dex_location, error_msg)) {
189 return nullptr;
190 }
191
Dave Allison69dfe512014-07-11 17:11:58 +0000192 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800193}
194
David Brazdil7b49e6c2016-09-01 11:06:18 +0100195bool OatFileBase::LoadVdex(const std::string& vdex_filename,
196 bool writable,
197 bool low_4gb,
198 std::string* error_msg) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100199 vdex_ = VdexFile::Open(vdex_filename, writable, low_4gb, /* unquicken*/ false, error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100200 if (vdex_.get() == nullptr) {
201 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
202 vdex_filename.c_str(),
203 error_msg->c_str());
204 return false;
205 }
206 return true;
207}
208
Andreas Gampe049cff02015-12-01 23:27:12 -0800209bool OatFileBase::ComputeFields(uint8_t* requested_base,
210 const std::string& file_path,
211 std::string* error_msg) {
212 std::string symbol_error_msg;
213 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700214 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800215 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
216 file_path.c_str(),
217 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700218 return false;
219 }
220 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000221 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700222 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000223 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
224 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700225 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800226 "oatdata=%p != expected=%p. See process maps in the log.",
227 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700228 return false;
229 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800230 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700231 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800232 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
233 file_path.c_str(),
234 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700235 return false;
236 }
237 // Readjust to be non-inclusive upper bound.
238 end_ += sizeof(uint32_t);
239
Andreas Gampe049cff02015-12-01 23:27:12 -0800240 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700241 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800242 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700243 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700244 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800245 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700246 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800247 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700248 return false;
249 }
250 // Readjust to be non-inclusive upper bound.
251 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100252 // Find bss methods if present.
253 bss_methods_ =
254 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000255 // Find bss roots if present.
256 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700257 }
258
Andreas Gampe049cff02015-12-01 23:27:12 -0800259 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800260}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800261
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100262// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
263// position by the number of bytes read, i.e. sizeof(T).
264// Return true on success, false if the read would go beyond the end of the OatFile.
265template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100266inline static bool ReadOatDexFileData(const OatFile& oat_file,
267 /*inout*/const uint8_t** oat,
268 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100269 DCHECK(oat != nullptr);
270 DCHECK(value != nullptr);
271 DCHECK_LE(*oat, oat_file.End());
272 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
273 return false;
274 }
275 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
276 typedef __attribute__((__aligned__(1))) T unaligned_type;
277 *value = *reinterpret_cast<const unaligned_type*>(*oat);
278 *oat += sizeof(T);
279 return true;
280}
281
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100282static inline bool MapConstantTables(const gc::space::ImageSpace* space,
283 uint8_t* address) {
284 // If MREMAP_DUP is ever merged to Linux kernel, use it to avoid the unnecessary open()/close().
285 // Note: The current approach relies on the filename still referencing the same inode.
286
287 File file(space->GetImageFilename(), O_RDONLY, /* checkUsage */ false);
288 if (!file.IsOpened()) {
289 LOG(ERROR) << "Failed to open boot image file " << space->GetImageFilename();
290 return false;
291 }
292
293 uint32_t offset = space->GetImageHeader().GetBootImageConstantTablesOffset();
294 uint32_t size = space->GetImageHeader().GetBootImageConstantTablesSize();
295 std::string error_msg;
296 std::unique_ptr<MemMap> mem_map(MemMap::MapFileAtAddress(address,
297 size,
298 PROT_READ,
299 MAP_PRIVATE,
300 file.Fd(),
301 offset,
302 /* low_4gb */ false,
303 /* reuse */ true,
304 file.GetPath().c_str(),
305 &error_msg));
306 if (mem_map == nullptr) {
307 LOG(ERROR) << "Failed to mmap boot image tables from file " << space->GetImageFilename();
308 return false;
309 }
310 return true;
311}
312
Andreas Gampe049cff02015-12-01 23:27:12 -0800313bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700314 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800315 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100316 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
317 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800318 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700319 return false;
320 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100321 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
322 size_t key_value_store_size =
323 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
324 if (Size() < sizeof(OatHeader) + key_value_store_size) {
325 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
326 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100327 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100328 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100329 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100330 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700331 return false;
332 }
333
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100334 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
335 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
336 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
337 "%zu is not in [%zu, %zu]",
338 GetLocation().c_str(),
339 oat_dex_files_offset,
340 GetOatHeader().GetHeaderSize(),
341 Size());
342 return false;
343 }
344 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
345
346 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
347 if (!IsAligned<kPageSize>(bss_begin_) ||
348 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
349 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000350 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
351 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100352 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000353 GetLocation().c_str(),
354 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100355 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000356 bss_roots_,
357 bss_end_);
358 return false;
359 }
360
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100361 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
362 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
363 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
364 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100365 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000366 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000367 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100368 bss_methods_,
369 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370 bss_end_);
371 return false;
372 }
373
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100374 uint8_t* after_tables =
375 (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_; // May be null.
376 uint8_t* boot_image_tables = (bss_begin_ == after_tables) ? nullptr : bss_begin_;
377 uint8_t* boot_image_tables_end =
378 (bss_begin_ == after_tables) ? nullptr : (after_tables != nullptr) ? after_tables : bss_end_;
379 DCHECK_EQ(boot_image_tables != nullptr, boot_image_tables_end != nullptr);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100380 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
381 oat_dex_files_storage_.reserve(dex_file_count);
382 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100383 uint32_t dex_file_location_size;
384 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
385 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
386 "location size",
387 GetLocation().c_str(),
388 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700389 return false;
390 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100391 if (UNLIKELY(dex_file_location_size == 0U)) {
392 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
393 GetLocation().c_str(),
394 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700395 return false;
396 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000397 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100398 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
399 "location",
400 GetLocation().c_str(),
401 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700402 return false;
403 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000404 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
405 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700406
Richard Uhlere5fed032015-03-18 08:21:11 -0700407 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
408 abs_dex_location,
409 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700410
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100411 uint32_t dex_file_checksum;
412 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
413 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
414 "dex file checksum",
415 GetLocation().c_str(),
416 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700417 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700418 return false;
419 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700420
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100421 uint32_t dex_file_offset;
422 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
423 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
424 "after dex file offsets",
425 GetLocation().c_str(),
426 i,
427 dex_file_location.c_str());
428 return false;
429 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700430 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100431 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
432 "file offset",
433 GetLocation().c_str(),
434 i,
435 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700436 return false;
437 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100438 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100439 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
440 "offset %u > %zu",
441 GetLocation().c_str(),
442 i,
443 dex_file_location.c_str(),
444 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100445 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700446 return false;
447 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100448 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000449 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
450 "offset %u of %zu but the size of dex file header is %zu",
451 GetLocation().c_str(),
452 i,
453 dex_file_location.c_str(),
454 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100455 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000456 sizeof(DexFile::Header));
457 return false;
458 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800459
David Brazdil7b49e6c2016-09-01 11:06:18 +0100460 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700461 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100462 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
463 "dex file magic '%s'",
464 GetLocation().c_str(),
465 i,
466 dex_file_location.c_str(),
467 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700468 return false;
469 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700470 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100471 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
472 "dex file version '%s'",
473 GetLocation().c_str(),
474 i,
475 dex_file_location.c_str(),
476 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700477 return false;
478 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800479 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100480 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000481 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
482 "offset %u and size %u truncated at %zu",
483 GetLocation().c_str(),
484 i,
485 dex_file_location.c_str(),
486 dex_file_offset,
487 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100488 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000489 return false;
490 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300491
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000492 uint32_t class_offsets_offset;
493 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
494 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
495 "after class offsets offset",
496 GetLocation().c_str(),
497 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300498 dex_file_location.c_str());
499 return false;
500 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000501 if (UNLIKELY(class_offsets_offset > Size()) ||
502 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
503 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
504 "class offsets, offset %u of %zu, class defs %u",
505 GetLocation().c_str(),
506 i,
507 dex_file_location.c_str(),
508 class_offsets_offset,
509 Size(),
510 header->class_defs_size_);
511 return false;
512 }
513 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
514 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
515 "class offsets, offset %u",
516 GetLocation().c_str(),
517 i,
518 dex_file_location.c_str(),
519 class_offsets_offset);
520 return false;
521 }
522 const uint32_t* class_offsets_pointer =
523 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
524
525 uint32_t lookup_table_offset;
526 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
527 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
528 "after lookup table offset",
529 GetLocation().c_str(),
530 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300531 dex_file_location.c_str());
532 return false;
533 }
534 const uint8_t* lookup_table_data = lookup_table_offset != 0u
535 ? Begin() + lookup_table_offset
536 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000537 if (lookup_table_offset != 0u &&
538 (UNLIKELY(lookup_table_offset > Size()) ||
539 UNLIKELY(Size() - lookup_table_offset <
540 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100541 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000542 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100543 GetLocation().c_str(),
544 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000545 dex_file_location.c_str(),
546 lookup_table_offset,
547 Size(),
548 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700549 return false;
550 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700551
Mathieu Chartier120aa282017-08-05 16:03:03 -0700552 uint32_t dex_layout_sections_offset;
553 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
554 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
555 "after dex layout sections offset",
556 GetLocation().c_str(),
557 i,
558 dex_file_location.c_str());
559 return false;
560 }
561 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
562 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
563 : nullptr;
564
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100565 uint32_t method_bss_mapping_offset;
566 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &method_bss_mapping_offset))) {
567 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
568 "after method bss mapping offset",
569 GetLocation().c_str(),
570 i,
571 dex_file_location.c_str());
572 return false;
573 }
574 const bool readable_method_bss_mapping_size =
575 method_bss_mapping_offset != 0u &&
576 method_bss_mapping_offset <= Size() &&
577 IsAligned<alignof(MethodBssMapping)>(method_bss_mapping_offset) &&
578 Size() - method_bss_mapping_offset >= MethodBssMapping::ComputeSize(0);
579 const MethodBssMapping* method_bss_mapping = readable_method_bss_mapping_size
580 ? reinterpret_cast<const MethodBssMapping*>(Begin() + method_bss_mapping_offset)
581 : nullptr;
582 if (method_bss_mapping_offset != 0u &&
583 (UNLIKELY(method_bss_mapping == nullptr) ||
584 UNLIKELY(method_bss_mapping->size() == 0u) ||
585 UNLIKELY(Size() - method_bss_mapping_offset <
586 MethodBssMapping::ComputeSize(method_bss_mapping->size())))) {
587 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
588 " truncated method bss mapping, offset %u of %zu, length %zu",
589 GetLocation().c_str(),
590 i,
591 dex_file_location.c_str(),
592 method_bss_mapping_offset,
593 Size(),
594 method_bss_mapping != nullptr ? method_bss_mapping->size() : 0u);
595 return false;
596 }
597 if (kIsDebugBuild && method_bss_mapping != nullptr) {
598 const MethodBssMappingEntry* prev_entry = nullptr;
599 for (const MethodBssMappingEntry& entry : *method_bss_mapping) {
600 CHECK_ALIGNED_PARAM(entry.bss_offset, static_cast<size_t>(pointer_size));
601 CHECK_LT(entry.bss_offset, BssSize());
602 CHECK_LE(POPCOUNT(entry.index_mask) * static_cast<size_t>(pointer_size), entry.bss_offset);
603 size_t index_mask_span = (entry.index_mask != 0u) ? 16u - CTZ(entry.index_mask) : 0u;
604 CHECK_LE(index_mask_span, entry.method_index);
605 if (prev_entry != nullptr) {
606 CHECK_LT(prev_entry->method_index, entry.method_index - index_mask_span);
607 }
608 prev_entry = &entry;
609 }
610 CHECK_LT(prev_entry->method_index,
611 reinterpret_cast<const DexFile::Header*>(dex_file_pointer)->method_ids_size_);
612 }
613
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100614 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
615
616 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100617 OatDexFile* oat_dex_file = new OatDexFile(this,
618 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100619 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100620 dex_file_checksum,
621 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300622 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100623 method_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000624 class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700625 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100626 oat_dex_files_storage_.push_back(oat_dex_file);
627
628 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100629 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100630 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100631 if (canonical_location != dex_file_location) {
632 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
633 oat_dex_files_.Put(canonical_key, oat_dex_file);
634 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700635 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100636
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100637 if (boot_image_tables != nullptr) {
Vladimir Marko667585a2017-09-29 10:42:31 +0100638 Runtime* runtime = Runtime::Current();
639 if (UNLIKELY(runtime == nullptr)) {
640 // This must be oatdump without boot image. Make sure the .bss is inaccessible.
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700641 CheckedCall(mprotect, "protect bss", const_cast<uint8_t*>(BssBegin()), BssSize(), PROT_NONE);
Vladimir Marko667585a2017-09-29 10:42:31 +0100642 } else {
643 // Map boot image tables into the .bss. The reserved size must match size of the tables.
644 size_t reserved_size = static_cast<size_t>(boot_image_tables_end - boot_image_tables);
645 size_t tables_size = 0u;
646 for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) {
647 tables_size += space->GetImageHeader().GetBootImageConstantTablesSize();
648 DCHECK_ALIGNED(tables_size, kPageSize);
649 }
650 if (tables_size != reserved_size) {
651 *error_msg = StringPrintf("In oat file '%s' found unexpected boot image table sizes, "
652 " %zu bytes, should be %zu.",
653 GetLocation().c_str(),
654 reserved_size,
655 tables_size);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100656 return false;
657 }
Vladimir Marko667585a2017-09-29 10:42:31 +0100658 for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) {
659 uint32_t current_tables_size = space->GetImageHeader().GetBootImageConstantTablesSize();
660 if (current_tables_size != 0u && !MapConstantTables(space, boot_image_tables)) {
661 return false;
662 }
663 boot_image_tables += current_tables_size;
664 }
665 DCHECK(boot_image_tables == boot_image_tables_end);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100666 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100667 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700668 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700669}
670
Andreas Gampe049cff02015-12-01 23:27:12 -0800671////////////////////////
672// OatFile via dlopen //
673////////////////////////
674
Andreas Gampe049cff02015-12-01 23:27:12 -0800675class DlOpenOatFile FINAL : public OatFileBase {
676 public:
677 DlOpenOatFile(const std::string& filename, bool executable)
678 : OatFileBase(filename, executable),
679 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700680 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800681 }
682
683 ~DlOpenOatFile() {
684 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700685 if (!kIsTargetBuild) {
686 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
687 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700688 dlclose(dlopen_handle_);
689 } else {
690 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700691 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800692 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800693 }
694
695 protected:
696 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
697 std::string* error_msg) const OVERRIDE {
698 const uint8_t* ptr =
699 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
700 if (ptr == nullptr) {
701 *error_msg = dlerror();
702 }
703 return ptr;
704 }
705
Andreas Gampe4075f832016-05-18 13:09:54 -0700706 void PreLoad() OVERRIDE;
707
Andreas Gampe049cff02015-12-01 23:27:12 -0800708 bool Load(const std::string& elf_filename,
709 uint8_t* oat_file_begin,
710 bool writable,
711 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800712 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800713 std::string* error_msg) OVERRIDE;
714
715 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
716 void PreSetup(const std::string& elf_filename) OVERRIDE;
717
718 private:
719 bool Dlopen(const std::string& elf_filename,
720 uint8_t* oat_file_begin,
721 std::string* error_msg);
722
Richard Uhlera206c742016-05-24 15:04:22 -0700723 // On the host, if the same library is loaded again with dlopen the same
724 // file handle is returned. This differs from the behavior of dlopen on the
725 // target, where dlopen reloads the library at a different address every
726 // time you load it. The runtime relies on the target behavior to ensure
727 // each instance of the loaded library has a unique dex cache. To avoid
728 // problems, we fall back to our own linker in the case when the same
729 // library is opened multiple times on host. dlopen_handles_ is used to
730 // detect that case.
731 // Guarded by host_dlopen_handles_lock_;
732 static std::unordered_set<void*> host_dlopen_handles_;
733
Andreas Gampe049cff02015-12-01 23:27:12 -0800734 // dlopen handle during runtime.
735 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
736
737 // Dummy memory map objects corresponding to the regions mapped by dlopen.
738 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
739
Andreas Gampe4075f832016-05-18 13:09:54 -0700740 // The number of shared objects the linker told us about before loading. Used to
741 // (optimistically) optimize the PreSetup stage (see comment there).
742 size_t shared_objects_before_;
743
Andreas Gampe049cff02015-12-01 23:27:12 -0800744 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
745};
746
Richard Uhlera206c742016-05-24 15:04:22 -0700747std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
748
Andreas Gampe4075f832016-05-18 13:09:54 -0700749void DlOpenOatFile::PreLoad() {
750#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700751 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700752 LOG(FATAL) << "Should not reach here.";
753 UNREACHABLE();
754#else
755 // Count the entries in dl_iterate_phdr we get at this point in time.
756 struct dl_iterate_context {
757 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
758 size_t size ATTRIBUTE_UNUSED,
759 void *data) {
760 reinterpret_cast<dl_iterate_context*>(data)->count++;
761 return 0; // Continue iteration.
762 }
763 size_t count = 0;
764 } context;
765
766 dl_iterate_phdr(dl_iterate_context::callback, &context);
767 shared_objects_before_ = context.count;
768#endif
769}
770
Andreas Gampe049cff02015-12-01 23:27:12 -0800771bool DlOpenOatFile::Load(const std::string& elf_filename,
772 uint8_t* oat_file_begin,
773 bool writable,
774 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800775 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800776 std::string* error_msg) {
777 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
778 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
779 // !executable is a sign that we may want to patch), which may not be allowed for
780 // various reasons.
781 if (!kUseDlopen) {
782 *error_msg = "DlOpen is disabled.";
783 return false;
784 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800785 if (low_4gb) {
786 *error_msg = "DlOpen does not support low 4gb loading.";
787 return false;
788 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800789 if (writable) {
790 *error_msg = "DlOpen does not support writable loading.";
791 return false;
792 }
793 if (!executable) {
794 *error_msg = "DlOpen does not support non-executable loading.";
795 return false;
796 }
797
798 // dlopen always returns the same library if it is already opened on the host. For this reason
799 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
800 // the same library loaded multiple times at different addresses is required for class unloading
801 // and for having dex caches arrays in the .bss section.
802 if (!kIsTargetBuild) {
803 if (!kUseDlopenOnHost) {
804 *error_msg = "DlOpen disabled for host.";
805 return false;
806 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800807 }
808
809 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
810 DCHECK(dlopen_handle_ != nullptr || !success);
811
812 return success;
813}
814
815bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
816 uint8_t* oat_file_begin,
817 std::string* error_msg) {
818#ifdef __APPLE__
819 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
820 // but let's fallback to the custom loading code for the time being.
821 UNUSED(elf_filename, oat_file_begin);
822 *error_msg = "Dlopen unsupported on Mac.";
823 return false;
824#else
825 {
826 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
827 if (absolute_path == nullptr) {
828 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
829 return false;
830 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100831#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +0000832 android_dlextinfo extinfo = {};
Andreas Gampe049cff02015-12-01 23:27:12 -0800833 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
834 // (open oat files multiple
835 // times).
836 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
837 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800838 if (oat_file_begin != nullptr) { //
839 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
840 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
841 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800842 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
843#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800844 UNUSED(oat_file_begin);
Julien Duraj1af0c4f2016-11-16 14:05:48 +0000845 static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700846 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700847 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
848 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700849 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
850 dlclose(dlopen_handle_);
851 dlopen_handle_ = nullptr;
852 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
853 return false;
854 }
855 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100856#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800857 }
858 if (dlopen_handle_ == nullptr) {
859 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
860 return false;
861 }
862 return true;
863#endif
864}
865
866void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800867#ifdef __APPLE__
868 UNUSED(elf_filename);
869 LOG(FATAL) << "Should not reach here.";
870 UNREACHABLE();
871#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800872 struct dl_iterate_context {
873 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
874 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700875 context->shared_objects_seen++;
876 if (context->shared_objects_seen < context->shared_objects_before) {
877 // We haven't been called yet for anything we haven't seen before. Just continue.
878 // Note: this is aggressively optimistic. If another thread was unloading a library,
879 // we may miss out here. However, this does not happen often in practice.
880 return 0;
881 }
882
Andreas Gampe049cff02015-12-01 23:27:12 -0800883 // See whether this callback corresponds to the file which we have just loaded.
884 bool contains_begin = false;
885 for (int i = 0; i < info->dlpi_phnum; i++) {
886 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
887 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
888 info->dlpi_phdr[i].p_vaddr);
889 size_t memsz = info->dlpi_phdr[i].p_memsz;
890 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
891 contains_begin = true;
892 break;
893 }
894 }
895 }
896 // Add dummy mmaps for this file.
897 if (contains_begin) {
898 for (int i = 0; i < info->dlpi_phnum; i++) {
899 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
900 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
901 info->dlpi_phdr[i].p_vaddr);
902 size_t memsz = info->dlpi_phdr[i].p_memsz;
903 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
904 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
905 }
906 }
907 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
908 }
909 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
910 }
911 const uint8_t* const begin_;
912 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700913 const size_t shared_objects_before;
914 size_t shared_objects_seen;
915 };
916 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800917
918 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700919 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
920 // before giving up. This should be unusual.
921 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
922 << shared_objects_before_;
923 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
924 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
925 // OK, give up and print an error.
926 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
927 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
928 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800929 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800930#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800931}
932
933////////////////////////////////////////////////
934// OatFile via our own ElfFile implementation //
935////////////////////////////////////////////////
936
937class ElfOatFile FINAL : public OatFileBase {
938 public:
939 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
940
941 static ElfOatFile* OpenElfFile(File* file,
942 const std::string& location,
943 uint8_t* requested_base,
944 uint8_t* oat_file_begin, // Override base if not null
945 bool writable,
946 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800947 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800948 const char* abs_dex_location,
949 std::string* error_msg);
950
951 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100952 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800953 const char* abs_dex_location,
954 std::string* error_msg);
955
956 protected:
957 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
958 std::string* error_msg) const OVERRIDE {
959 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
960 if (ptr == nullptr) {
961 *error_msg = "(Internal implementation could not find symbol)";
962 }
963 return ptr;
964 }
965
Andreas Gampe4075f832016-05-18 13:09:54 -0700966 void PreLoad() OVERRIDE {
967 }
968
Andreas Gampe049cff02015-12-01 23:27:12 -0800969 bool Load(const std::string& elf_filename,
970 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
971 bool writable,
972 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800973 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800974 std::string* error_msg) OVERRIDE;
975
976 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
977 }
978
979 private:
980 bool ElfFileOpen(File* file,
981 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
982 bool writable,
983 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800984 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800985 std::string* error_msg);
986
987 private:
988 // Backing memory map for oat file during cross compilation.
989 std::unique_ptr<ElfFile> elf_file_;
990
991 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
992};
993
994ElfOatFile* ElfOatFile::OpenElfFile(File* file,
995 const std::string& location,
996 uint8_t* requested_base,
997 uint8_t* oat_file_begin, // Override base if not null
998 bool writable,
999 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001000 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001001 const char* abs_dex_location,
1002 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001003 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001004 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001005 bool success = oat_file->ElfFileOpen(file,
1006 oat_file_begin,
1007 writable,
1008 low_4gb,
1009 executable,
1010 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001011 if (!success) {
1012 CHECK(!error_msg->empty());
1013 return nullptr;
1014 }
1015
1016 // Complete the setup.
1017 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
1018 return nullptr;
1019 }
1020
1021 if (!oat_file->Setup(abs_dex_location, error_msg)) {
1022 return nullptr;
1023 }
1024
1025 return oat_file.release();
1026}
1027
1028bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001029 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001030 const char* abs_dex_location,
1031 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001032 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001033 if (IsExecutable()) {
1034 *error_msg = "Cannot initialize from elf file in executable mode.";
1035 return false;
1036 }
1037 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001038 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001039 uint64_t offset, size;
1040 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1041 CHECK(has_section);
1042 SetBegin(elf_file->Begin() + offset);
1043 SetEnd(elf_file->Begin() + size + offset);
1044 // Ignore the optional .bss section when opening non-executable.
1045 return Setup(abs_dex_location, error_msg);
1046}
1047
1048bool ElfOatFile::Load(const std::string& elf_filename,
1049 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1050 bool writable,
1051 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001052 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001053 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001054 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001055 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1056 if (file == nullptr) {
1057 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1058 return false;
1059 }
1060 return ElfOatFile::ElfFileOpen(file.get(),
1061 oat_file_begin,
1062 writable,
1063 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001064 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001065 error_msg);
1066}
1067
1068bool ElfOatFile::ElfFileOpen(File* file,
1069 uint8_t* oat_file_begin,
1070 bool writable,
1071 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001072 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001073 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001074 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001075 // TODO: rename requested_base to oat_data_begin
1076 elf_file_.reset(ElfFile::Open(file,
1077 writable,
1078 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001079 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001080 error_msg,
1081 oat_file_begin));
1082 if (elf_file_ == nullptr) {
1083 DCHECK(!error_msg->empty());
1084 return false;
1085 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001086 bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001087 DCHECK(loaded || !error_msg->empty());
1088 return loaded;
1089}
1090
1091//////////////////////////
1092// General OatFile code //
1093//////////////////////////
1094
1095std::string OatFile::ResolveRelativeEncodedDexLocation(
1096 const char* abs_dex_location, const std::string& rel_dex_location) {
1097 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
1098 // Strip :classes<N>.dex used for secondary multidex files.
1099 std::string base = DexFile::GetBaseLocation(rel_dex_location);
1100 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
1101
1102 // Check if the base is a suffix of the provided abs_dex_location.
1103 std::string target_suffix = "/" + base;
1104 std::string abs_location(abs_dex_location);
1105 if (abs_location.size() > target_suffix.size()) {
1106 size_t pos = abs_location.size() - target_suffix.size();
1107 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1108 return abs_location + multidex_suffix;
1109 }
1110 }
1111 }
1112 return rel_dex_location;
1113}
1114
1115static void CheckLocation(const std::string& location) {
1116 CHECK(!location.empty());
1117}
1118
1119OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001120 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001121 const std::string& location,
1122 const char* abs_dex_location,
1123 std::string* error_msg) {
1124 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001125 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001126 ? oat_file.release()
1127 : nullptr;
1128}
1129
David Brazdil7b49e6c2016-09-01 11:06:18 +01001130OatFile* OatFile::Open(const std::string& oat_filename,
1131 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001132 uint8_t* requested_base,
1133 uint8_t* oat_file_begin,
1134 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001135 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001136 const char* abs_dex_location,
1137 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001138 ScopedTrace trace("Open oat file " + oat_location);
1139 CHECK(!oat_filename.empty()) << oat_location;
1140 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001141
Calin Juravle367b9d82017-05-15 18:18:39 -07001142 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001143
1144 // Check that the files even exist, fast-fail.
1145 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
1146 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1147 return nullptr;
1148 } else if (!OS::FileExists(oat_filename.c_str())) {
1149 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001150 return nullptr;
1151 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001152
1153 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1154 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001155 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1156 oat_filename,
1157 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001158 requested_base,
1159 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001160 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001161 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001162 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001163 abs_dex_location,
1164 error_msg);
1165 if (with_dlopen != nullptr) {
1166 return with_dlopen;
1167 }
1168 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001169 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001170 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001171 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1172 //
1173 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1174 //
1175 // We use our own ELF loader for Quick to deal with legacy apps that
1176 // open a generated dex file by name, remove the file, then open
1177 // another generated dex file with the same name. http://b/10614658
1178 //
1179 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1180 //
1181 //
1182 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1183 // 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 +01001184 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1185 oat_filename,
1186 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001187 requested_base,
1188 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001189 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001190 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001191 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001192 abs_dex_location,
1193 error_msg);
1194 return with_internal;
1195}
1196
1197OatFile* OatFile::OpenWritable(File* file,
1198 const std::string& location,
1199 const char* abs_dex_location,
1200 std::string* error_msg) {
1201 CheckLocation(location);
1202 return ElfOatFile::OpenElfFile(file,
1203 location,
1204 nullptr,
1205 nullptr,
1206 true,
1207 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001208 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001209 abs_dex_location,
1210 error_msg);
1211}
1212
1213OatFile* OatFile::OpenReadable(File* file,
1214 const std::string& location,
1215 const char* abs_dex_location,
1216 std::string* error_msg) {
1217 CheckLocation(location);
1218 return ElfOatFile::OpenElfFile(file,
1219 location,
1220 nullptr,
1221 nullptr,
1222 false,
1223 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001224 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001225 abs_dex_location,
1226 error_msg);
1227}
1228
1229OatFile::OatFile(const std::string& location, bool is_executable)
1230 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001231 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001232 begin_(nullptr),
1233 end_(nullptr),
1234 bss_begin_(nullptr),
1235 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001236 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001237 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001238 is_executable_(is_executable),
1239 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1240 CHECK(!location_.empty());
1241}
1242
1243OatFile::~OatFile() {
1244 STLDeleteElements(&oat_dex_files_storage_);
1245}
1246
Brian Carlstrome24fa612011-09-29 00:53:55 -07001247const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001248 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001249}
1250
Ian Rogers13735952014-10-08 12:43:28 -07001251const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001252 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001253 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001254}
1255
Ian Rogers13735952014-10-08 12:43:28 -07001256const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001257 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001258 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001259}
1260
Vladimir Marko5c42c292015-02-25 12:02:49 +00001261const uint8_t* OatFile::BssBegin() const {
1262 return bss_begin_;
1263}
1264
1265const uint8_t* OatFile::BssEnd() const {
1266 return bss_end_;
1267}
1268
David Brazdil7b49e6c2016-09-01 11:06:18 +01001269const uint8_t* OatFile::DexBegin() const {
1270 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1271}
1272
1273const uint8_t* OatFile::DexEnd() const {
1274 return kIsVdexEnabled ? vdex_->End() : End();
1275}
1276
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001277ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1278 if (bss_methods_ != nullptr) {
1279 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1280 ArtMethod** methods_end =
1281 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1282 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1283 } else {
1284 return ArrayRef<ArtMethod*>();
1285 }
1286}
1287
Vladimir Markoaad75c62016-10-03 08:46:48 +00001288ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1289 if (bss_roots_ != nullptr) {
1290 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1291 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1292 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1293 } else {
1294 return ArrayRef<GcRoot<mirror::Object>>();
1295 }
1296}
1297
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001298const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1299 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001300 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001301 // NOTE: We assume here that the canonical location for a given dex_location never
1302 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1303 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1304 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001305
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001306 // TODO: Additional analysis of usage patterns to see if this can be simplified
1307 // without any performance loss, for example by not doing the first lock-free lookup.
1308
1309 const OatFile::OatDexFile* oat_dex_file = nullptr;
1310 StringPiece key(dex_location);
1311 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1312 // directly mentioned in the oat file and doesn't require locking.
1313 auto primary_it = oat_dex_files_.find(key);
1314 if (primary_it != oat_dex_files_.end()) {
1315 oat_dex_file = primary_it->second;
1316 DCHECK(oat_dex_file != nullptr);
1317 } else {
1318 // This dex_location is not one of the dex locations directly mentioned in the
1319 // oat file. The correct lookup is via the canonical location but first see in
1320 // the secondary_oat_dex_files_ whether we've looked up this location before.
1321 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1322 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1323 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001324 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001325 } else {
1326 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001327 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001328 if (dex_canonical_location != dex_location) {
1329 StringPiece canonical_key(dex_canonical_location);
1330 auto canonical_it = oat_dex_files_.find(canonical_key);
1331 if (canonical_it != oat_dex_files_.end()) {
1332 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001333 } // else keep null.
1334 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001335
1336 // Copy the key to the string_cache_ and store the result in secondary map.
1337 string_cache_.emplace_back(key.data(), key.length());
1338 StringPiece key_copy(string_cache_.back());
1339 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001340 }
1341 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001342
1343 if (oat_dex_file == nullptr) {
1344 if (error_msg != nullptr) {
1345 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1346 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1347 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1348 }
1349 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001350 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001351
Richard Uhler9a37efc2016-08-05 16:32:55 -07001352 if (dex_location_checksum != nullptr &&
1353 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1354 if (error_msg != nullptr) {
1355 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1356 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1357 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1358 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1359 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1360 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001361 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001362 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001363 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001364 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001365}
1366
Brian Carlstrome24fa612011-09-29 00:53:55 -07001367OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001368 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001369 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001370 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001371 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001372 const uint8_t* lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001373 const MethodBssMapping* method_bss_mapping_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001374 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001375 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001376 : oat_file_(oat_file),
1377 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001378 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001379 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001380 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001381 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001382 method_bss_mapping_(method_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001383 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001384 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001385 // Initialize TypeLookupTable.
1386 if (lookup_table_data_ != nullptr) {
1387 // Peek the number of classes from the DexFile.
1388 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1389 const uint32_t num_class_defs = dex_header->class_defs_size_;
1390 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1391 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1392 } else {
Mathieu Chartier1b868492016-11-16 16:22:37 -08001393 lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001394 }
1395 }
1396}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001397
Mathieu Chartier1b868492016-11-16 16:22:37 -08001398OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table)
1399 : lookup_table_(std::move(lookup_table)) {}
1400
Brian Carlstrome24fa612011-09-29 00:53:55 -07001401OatFile::OatDexFile::~OatDexFile() {}
1402
Ian Rogers05f28c62012-10-23 18:12:13 -07001403size_t OatFile::OatDexFile::FileSize() const {
1404 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1405}
1406
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001407std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001408 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001409 static constexpr bool kVerify = false;
1410 static constexpr bool kVerifyChecksum = false;
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001411 return DexFile::Open(dex_file_pointer_,
1412 FileSize(),
1413 dex_file_location_,
1414 dex_file_location_checksum_,
1415 this,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001416 kVerify,
1417 kVerifyChecksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001418 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001419}
1420
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001421uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1422 return oat_class_offsets_pointer_[class_def_index];
1423}
1424
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001425OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001426 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001427
Ian Rogers13735952014-10-08 12:43:28 -07001428 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001429 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001430
Ian Rogers13735952014-10-08 12:43:28 -07001431 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001432 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1433 mirror::Class::Status status =
1434 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1435 CHECK_LT(status, mirror::Class::kStatusMax);
1436
Ian Rogers13735952014-10-08 12:43:28 -07001437 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001438 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1439 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1440 CHECK_LT(type, kOatClassMax);
1441
Ian Rogers13735952014-10-08 12:43:28 -07001442 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001443 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001444
Brian Carlstromcd937a92014-03-04 22:53:23 -08001445 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001446 const uint8_t* bitmap_pointer = nullptr;
1447 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001448 if (type != kOatClassNoneCompiled) {
1449 if (type == kOatClassSomeCompiled) {
1450 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1451 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1452 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1453 methods_pointer = bitmap_pointer + bitmap_size;
1454 } else {
1455 methods_pointer = after_type_pointer;
1456 }
1457 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001458 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001459
Richard Uhler07b3c232015-03-31 15:57:54 -07001460 return OatFile::OatClass(oat_file_,
1461 status,
1462 type,
1463 bitmap_size,
1464 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1465 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001466}
1467
David Sehr9aa352e2016-09-15 18:13:52 -07001468const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1469 const char* descriptor,
1470 size_t hash) {
1471 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1472 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1473 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1474 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
Andreas Gampee2abbc62017-09-15 11:59:26 -07001475 return (class_def_idx != dex::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
David Sehr9aa352e2016-09-15 18:13:52 -07001476 }
1477 // Fast path for rare no class defs case.
1478 const uint32_t num_class_defs = dex_file.NumClassDefs();
1479 if (num_class_defs == 0) {
1480 return nullptr;
1481 }
1482 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1483 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001484 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
David Sehr9aa352e2016-09-15 18:13:52 -07001485 return dex_file.FindClassDef(type_idx);
1486 }
1487 return nullptr;
1488}
1489
Mathieu Chartier120aa282017-08-05 16:03:03 -07001490// Madvise the dex file based on the state we are moving to.
1491void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001492 const bool low_ram = Runtime::Current()->GetHeap()->IsLowMemoryMode();
1493 // TODO: Also do madvise hints for non low ram devices.
1494 if (!kMadviseDexFileAccesses || !low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07001495 return;
1496 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001497 if (state == MadviseState::kMadviseStateAtLoad) {
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001498 if (low_ram) {
Mathieu Chartier4ec507d2017-08-15 15:21:40 -07001499 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
1500 // Other devices have enough page cache to get performance benefits from loading more pages
1501 // into the page cache.
1502 MadviseLargestPageAlignedRegion(dex_file.Begin(),
1503 dex_file.Begin() + dex_file.Size(),
1504 MADV_RANDOM);
1505 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001506 }
1507 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1508 if (oat_dex_file != nullptr) {
1509 // Should always be there.
1510 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
1511 CHECK(sections != nullptr);
1512 sections->Madvise(&dex_file, state);
1513 }
1514}
1515
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001516OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001517 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001518 OatClassType type,
1519 uint32_t bitmap_size,
1520 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001521 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001522 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001523 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001524 switch (type_) {
1525 case kOatClassAllCompiled: {
1526 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001527 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001528 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001529 break;
1530 }
1531 case kOatClassSomeCompiled: {
1532 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001533 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001534 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001535 break;
1536 }
1537 case kOatClassNoneCompiled: {
1538 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001539 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001540 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001541 break;
1542 }
1543 case kOatClassMax: {
1544 LOG(FATAL) << "Invalid OatClassType " << type_;
1545 break;
1546 }
1547 }
1548}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001549
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001550uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1551 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1552 if (oat_method_offsets == nullptr) {
1553 return 0u;
1554 }
1555 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1556}
1557
1558const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001559 // 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 -07001560 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001561 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001562 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001563 }
1564 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001565 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001566 CHECK_EQ(kOatClassAllCompiled, type_);
1567 methods_pointer_index = method_index;
1568 } else {
1569 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001570 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001571 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001572 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001573 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1574 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001575 }
1576 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001577 return &oat_method_offsets;
1578}
1579
1580const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1581 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1582 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001583 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001584 }
1585 if (oat_file_->IsExecutable() ||
1586 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001587 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001588 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001589 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001590 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1591 // version.
1592 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001593}
1594
Mathieu Chartiere401d142015-04-22 13:56:20 -07001595void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001596 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001597 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001598}
1599
Andreas Gampe7ba64962014-10-23 11:37:40 -07001600bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001601 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001602 // TODO: Check against oat_patches. b/18144996
1603}
1604
Sebastien Hertz0de11332015-05-13 12:14:05 +02001605bool OatFile::IsDebuggable() const {
1606 return GetOatHeader().IsDebuggable();
1607}
1608
Andreas Gampe29d38e72016-03-23 15:31:51 +00001609CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1610 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001611}
1612
Calin Juravle44e5efa2017-09-12 00:54:26 -07001613std::string OatFile::GetClassLoaderContext() const {
1614 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1615};
1616
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001617OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1618 uint16_t class_def_idx,
1619 bool* found) {
1620 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
1621 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001622 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001623 *found = false;
1624 return OatFile::OatClass::Invalid();
1625 }
1626 *found = true;
1627 return oat_dex_file->GetOatClass(class_def_idx);
1628}
1629
Mathieu Chartier1b868492016-11-16 16:22:37 -08001630void OatFile::OatDexFile::AssertAotCompiler() {
1631 CHECK(Runtime::Current()->IsAotCompiler());
1632}
1633
Brian Carlstrome24fa612011-09-29 00:53:55 -07001634} // namespace art