blob: d3d566f440ff8cf909ed7b774fd58a163d332d31 [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"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070044#include "dex_file_loader.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070046#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000047#include "gc_root.h"
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010048#include "gc/space/image_space.h"
David Srbecky1baabf02015-06-16 17:12:34 +000049#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070051#include "mirror/object-inl.h"
Mathieu Chartier7b074bf2017-09-25 16:22:36 -070052#include "native_dex_file.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070053#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070056#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070057#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000058#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070059#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060#include "utils.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010061#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070062
63namespace art {
64
Andreas Gampe46ee31b2016-12-14 10:11:49 -080065using android::base::StringPrintf;
66
Andreas Gampe049cff02015-12-01 23:27:12 -080067// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000068static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070069
Andreas Gampe049cff02015-12-01 23:27:12 -080070// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070071// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
72// multiple times). However, if/when we switch the above, we likely want to switch this, too,
73// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000074static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070075
76// For debugging, Open will print DlOpen error message if set to true.
77static constexpr bool kPrintDlOpenErrorMessage = false;
78
Mathieu Chartierbe8303d2017-08-17 17:39:39 -070079// If true, we advise the kernel about dex file mem map accesses.
Mathieu Chartier150d25d2017-08-28 09:52:55 -070080static constexpr bool kMadviseDexFileAccesses = true;
Mathieu Chartierbe8303d2017-08-17 17:39:39 -070081
Andreas Gampe049cff02015-12-01 23:27:12 -080082// Note for OatFileBase and descendents:
83//
84// These are used in OatFile::Open to try all our loaders.
85//
86// The process is simple:
87//
88// 1) Allocate an instance through the standard constructor (location, executable)
89// 2) Load() to try to open the file.
90// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
91// 4) PreSetup() for any steps that should be done before the final setup.
92// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070093
Andreas Gampe049cff02015-12-01 23:27:12 -080094class OatFileBase : public OatFile {
95 public:
96 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070097
Andreas Gampe049cff02015-12-01 23:27:12 -080098 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010099 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
100 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -0700101 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800102 uint8_t* requested_base,
103 uint8_t* oat_file_begin,
104 bool writable,
105 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800106 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700107 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800108 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800109
Andreas Gampe049cff02015-12-01 23:27:12 -0800110 protected:
111 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700112
Andreas Gampe049cff02015-12-01 23:27:12 -0800113 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
114 std::string* error_msg) const = 0;
115
Andreas Gampe4075f832016-05-18 13:09:54 -0700116 virtual void PreLoad() = 0;
117
David Brazdil7b49e6c2016-09-01 11:06:18 +0100118 bool LoadVdex(const std::string& vdex_filename,
119 bool writable,
120 bool low_4gb,
121 std::string* error_msg);
122
Andreas Gampe049cff02015-12-01 23:27:12 -0800123 virtual bool Load(const std::string& elf_filename,
124 uint8_t* oat_file_begin,
125 bool writable,
126 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800127 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800128 std::string* error_msg) = 0;
129
130 bool ComputeFields(uint8_t* requested_base,
131 const std::string& file_path,
132 std::string* error_msg);
133
134 virtual void PreSetup(const std::string& elf_filename) = 0;
135
136 bool Setup(const char* abs_dex_location, std::string* error_msg);
137
138 // Setters exposed for ElfOatFile.
139
140 void SetBegin(const uint8_t* begin) {
141 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700142 }
143
Andreas Gampe049cff02015-12-01 23:27:12 -0800144 void SetEnd(const uint8_t* end) {
145 end_ = end;
146 }
147
David Brazdilc93b3be2016-09-12 18:49:58 +0100148 void SetVdex(VdexFile* vdex) {
149 vdex_.reset(vdex);
150 }
151
Andreas Gampe049cff02015-12-01 23:27:12 -0800152 private:
153 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
154};
155
156template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100157OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
158 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800159 const std::string& location,
160 uint8_t* requested_base,
161 uint8_t* oat_file_begin,
162 bool writable,
163 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800164 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800165 const char* abs_dex_location,
166 std::string* error_msg) {
167 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700168
169 ret->PreLoad();
170
David Brazdil7b49e6c2016-09-01 11:06:18 +0100171 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
172 return nullptr;
173 }
174
Andreas Gampe049cff02015-12-01 23:27:12 -0800175 if (!ret->Load(elf_filename,
176 oat_file_begin,
177 writable,
178 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800179 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800180 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800181 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800182 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800183
Andreas Gampe049cff02015-12-01 23:27:12 -0800184 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
185 return nullptr;
186 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700187
Andreas Gampe049cff02015-12-01 23:27:12 -0800188 ret->PreSetup(elf_filename);
189
190 if (!ret->Setup(abs_dex_location, error_msg)) {
191 return nullptr;
192 }
193
Dave Allison69dfe512014-07-11 17:11:58 +0000194 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800195}
196
David Brazdil7b49e6c2016-09-01 11:06:18 +0100197bool OatFileBase::LoadVdex(const std::string& vdex_filename,
198 bool writable,
199 bool low_4gb,
200 std::string* error_msg) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100201 vdex_ = VdexFile::Open(vdex_filename, writable, low_4gb, /* unquicken*/ false, error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100202 if (vdex_.get() == nullptr) {
203 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
204 vdex_filename.c_str(),
205 error_msg->c_str());
206 return false;
207 }
208 return true;
209}
210
Andreas Gampe049cff02015-12-01 23:27:12 -0800211bool OatFileBase::ComputeFields(uint8_t* requested_base,
212 const std::string& file_path,
213 std::string* error_msg) {
214 std::string symbol_error_msg;
215 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700216 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800217 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
218 file_path.c_str(),
219 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700220 return false;
221 }
222 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000223 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700224 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000225 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
226 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700227 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800228 "oatdata=%p != expected=%p. See process maps in the log.",
229 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700230 return false;
231 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800232 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700233 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800234 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
235 file_path.c_str(),
236 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700237 return false;
238 }
239 // Readjust to be non-inclusive upper bound.
240 end_ += sizeof(uint32_t);
241
Andreas Gampe049cff02015-12-01 23:27:12 -0800242 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700243 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800244 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700245 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700246 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800247 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700248 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800249 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700250 return false;
251 }
252 // Readjust to be non-inclusive upper bound.
253 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100254 // Find bss methods if present.
255 bss_methods_ =
256 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000257 // Find bss roots if present.
258 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700259 }
260
Andreas Gampe049cff02015-12-01 23:27:12 -0800261 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800262}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800263
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100264// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
265// position by the number of bytes read, i.e. sizeof(T).
266// Return true on success, false if the read would go beyond the end of the OatFile.
267template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100268inline static bool ReadOatDexFileData(const OatFile& oat_file,
269 /*inout*/const uint8_t** oat,
270 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100271 DCHECK(oat != nullptr);
272 DCHECK(value != nullptr);
273 DCHECK_LE(*oat, oat_file.End());
274 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
275 return false;
276 }
277 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
278 typedef __attribute__((__aligned__(1))) T unaligned_type;
279 *value = *reinterpret_cast<const unaligned_type*>(*oat);
280 *oat += sizeof(T);
281 return true;
282}
283
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100284static inline bool MapConstantTables(const gc::space::ImageSpace* space,
285 uint8_t* address) {
286 // If MREMAP_DUP is ever merged to Linux kernel, use it to avoid the unnecessary open()/close().
287 // Note: The current approach relies on the filename still referencing the same inode.
288
289 File file(space->GetImageFilename(), O_RDONLY, /* checkUsage */ false);
290 if (!file.IsOpened()) {
291 LOG(ERROR) << "Failed to open boot image file " << space->GetImageFilename();
292 return false;
293 }
294
295 uint32_t offset = space->GetImageHeader().GetBootImageConstantTablesOffset();
296 uint32_t size = space->GetImageHeader().GetBootImageConstantTablesSize();
297 std::string error_msg;
298 std::unique_ptr<MemMap> mem_map(MemMap::MapFileAtAddress(address,
299 size,
300 PROT_READ,
301 MAP_PRIVATE,
302 file.Fd(),
303 offset,
304 /* low_4gb */ false,
305 /* reuse */ true,
306 file.GetPath().c_str(),
307 &error_msg));
308 if (mem_map == nullptr) {
309 LOG(ERROR) << "Failed to mmap boot image tables from file " << space->GetImageFilename();
310 return false;
311 }
312 return true;
313}
314
Andreas Gampe049cff02015-12-01 23:27:12 -0800315bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700316 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800317 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100318 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
319 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800320 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700321 return false;
322 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100323 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
324 size_t key_value_store_size =
325 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
326 if (Size() < sizeof(OatHeader) + key_value_store_size) {
327 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
328 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100329 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100330 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100331 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100332 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700333 return false;
334 }
335
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100336 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
337 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
338 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
339 "%zu is not in [%zu, %zu]",
340 GetLocation().c_str(),
341 oat_dex_files_offset,
342 GetOatHeader().GetHeaderSize(),
343 Size());
344 return false;
345 }
346 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
347
348 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
349 if (!IsAligned<kPageSize>(bss_begin_) ||
350 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
351 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000352 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
353 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100354 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000355 GetLocation().c_str(),
356 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100357 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000358 bss_roots_,
359 bss_end_);
360 return false;
361 }
362
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100363 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
364 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
365 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
366 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100367 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000368 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000369 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100370 bss_methods_,
371 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 bss_end_);
373 return false;
374 }
375
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100376 uint8_t* after_tables =
377 (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_; // May be null.
378 uint8_t* boot_image_tables = (bss_begin_ == after_tables) ? nullptr : bss_begin_;
379 uint8_t* boot_image_tables_end =
380 (bss_begin_ == after_tables) ? nullptr : (after_tables != nullptr) ? after_tables : bss_end_;
381 DCHECK_EQ(boot_image_tables != nullptr, boot_image_tables_end != nullptr);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100382 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
383 oat_dex_files_storage_.reserve(dex_file_count);
384 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100385 uint32_t dex_file_location_size;
386 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
387 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
388 "location size",
389 GetLocation().c_str(),
390 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700391 return false;
392 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100393 if (UNLIKELY(dex_file_location_size == 0U)) {
394 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
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 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100400 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
401 "location",
402 GetLocation().c_str(),
403 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700404 return false;
405 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000406 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
407 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700408
Richard Uhlere5fed032015-03-18 08:21:11 -0700409 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
410 abs_dex_location,
411 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700412
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100413 uint32_t dex_file_checksum;
414 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
415 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
416 "dex file checksum",
417 GetLocation().c_str(),
418 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700419 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700420 return false;
421 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100423 uint32_t dex_file_offset;
424 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
425 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
426 "after dex file offsets",
427 GetLocation().c_str(),
428 i,
429 dex_file_location.c_str());
430 return false;
431 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700432 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100433 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
434 "file offset",
435 GetLocation().c_str(),
436 i,
437 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700438 return false;
439 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100440 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100441 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
442 "offset %u > %zu",
443 GetLocation().c_str(),
444 i,
445 dex_file_location.c_str(),
446 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100447 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700448 return false;
449 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100450 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000451 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
452 "offset %u of %zu but the size of dex file header is %zu",
453 GetLocation().c_str(),
454 i,
455 dex_file_location.c_str(),
456 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100457 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000458 sizeof(DexFile::Header));
459 return false;
460 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800461
David Brazdil7b49e6c2016-09-01 11:06:18 +0100462 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700463
464 const bool valid_magic = NativeDexFile::IsMagicValid(dex_file_pointer);
465 if (UNLIKELY(!valid_magic)) {
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 magic '%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 }
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700474 if (UNLIKELY(!NativeDexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100475 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
476 "dex file version '%s'",
477 GetLocation().c_str(),
478 i,
479 dex_file_location.c_str(),
480 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700481 return false;
482 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800483 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100484 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000485 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
486 "offset %u and size %u truncated at %zu",
487 GetLocation().c_str(),
488 i,
489 dex_file_location.c_str(),
490 dex_file_offset,
491 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100492 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000493 return false;
494 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300495
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000496 uint32_t class_offsets_offset;
497 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
498 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
499 "after class offsets offset",
500 GetLocation().c_str(),
501 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300502 dex_file_location.c_str());
503 return false;
504 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000505 if (UNLIKELY(class_offsets_offset > Size()) ||
506 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
507 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
508 "class offsets, offset %u of %zu, class defs %u",
509 GetLocation().c_str(),
510 i,
511 dex_file_location.c_str(),
512 class_offsets_offset,
513 Size(),
514 header->class_defs_size_);
515 return false;
516 }
517 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
518 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
519 "class offsets, offset %u",
520 GetLocation().c_str(),
521 i,
522 dex_file_location.c_str(),
523 class_offsets_offset);
524 return false;
525 }
526 const uint32_t* class_offsets_pointer =
527 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
528
529 uint32_t lookup_table_offset;
530 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
531 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
532 "after lookup table offset",
533 GetLocation().c_str(),
534 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300535 dex_file_location.c_str());
536 return false;
537 }
538 const uint8_t* lookup_table_data = lookup_table_offset != 0u
539 ? Begin() + lookup_table_offset
540 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000541 if (lookup_table_offset != 0u &&
542 (UNLIKELY(lookup_table_offset > Size()) ||
543 UNLIKELY(Size() - lookup_table_offset <
544 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100545 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000546 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100547 GetLocation().c_str(),
548 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000549 dex_file_location.c_str(),
550 lookup_table_offset,
551 Size(),
552 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700553 return false;
554 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700555
Mathieu Chartier120aa282017-08-05 16:03:03 -0700556 uint32_t dex_layout_sections_offset;
557 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
558 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
559 "after dex layout sections offset",
560 GetLocation().c_str(),
561 i,
562 dex_file_location.c_str());
563 return false;
564 }
565 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
566 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
567 : nullptr;
568
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100569 uint32_t method_bss_mapping_offset;
570 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &method_bss_mapping_offset))) {
571 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
572 "after method bss mapping offset",
573 GetLocation().c_str(),
574 i,
575 dex_file_location.c_str());
576 return false;
577 }
578 const bool readable_method_bss_mapping_size =
579 method_bss_mapping_offset != 0u &&
580 method_bss_mapping_offset <= Size() &&
581 IsAligned<alignof(MethodBssMapping)>(method_bss_mapping_offset) &&
582 Size() - method_bss_mapping_offset >= MethodBssMapping::ComputeSize(0);
583 const MethodBssMapping* method_bss_mapping = readable_method_bss_mapping_size
584 ? reinterpret_cast<const MethodBssMapping*>(Begin() + method_bss_mapping_offset)
585 : nullptr;
586 if (method_bss_mapping_offset != 0u &&
587 (UNLIKELY(method_bss_mapping == nullptr) ||
588 UNLIKELY(method_bss_mapping->size() == 0u) ||
589 UNLIKELY(Size() - method_bss_mapping_offset <
590 MethodBssMapping::ComputeSize(method_bss_mapping->size())))) {
591 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
592 " truncated method bss mapping, offset %u of %zu, length %zu",
593 GetLocation().c_str(),
594 i,
595 dex_file_location.c_str(),
596 method_bss_mapping_offset,
597 Size(),
598 method_bss_mapping != nullptr ? method_bss_mapping->size() : 0u);
599 return false;
600 }
601 if (kIsDebugBuild && method_bss_mapping != nullptr) {
602 const MethodBssMappingEntry* prev_entry = nullptr;
603 for (const MethodBssMappingEntry& entry : *method_bss_mapping) {
604 CHECK_ALIGNED_PARAM(entry.bss_offset, static_cast<size_t>(pointer_size));
605 CHECK_LT(entry.bss_offset, BssSize());
606 CHECK_LE(POPCOUNT(entry.index_mask) * static_cast<size_t>(pointer_size), entry.bss_offset);
607 size_t index_mask_span = (entry.index_mask != 0u) ? 16u - CTZ(entry.index_mask) : 0u;
608 CHECK_LE(index_mask_span, entry.method_index);
609 if (prev_entry != nullptr) {
610 CHECK_LT(prev_entry->method_index, entry.method_index - index_mask_span);
611 }
612 prev_entry = &entry;
613 }
614 CHECK_LT(prev_entry->method_index,
615 reinterpret_cast<const DexFile::Header*>(dex_file_pointer)->method_ids_size_);
616 }
617
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700618 std::string canonical_location =
619 DexFileLoader::GetDexCanonicalLocation(dex_file_location.c_str());
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100620
621 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100622 OatDexFile* oat_dex_file = new OatDexFile(this,
623 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100624 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100625 dex_file_checksum,
626 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300627 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100628 method_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000629 class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700630 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100631 oat_dex_files_storage_.push_back(oat_dex_file);
632
633 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100634 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100635 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100636 if (canonical_location != dex_file_location) {
637 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
638 oat_dex_files_.Put(canonical_key, oat_dex_file);
639 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700640 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100641
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100642 if (boot_image_tables != nullptr) {
Vladimir Marko667585a2017-09-29 10:42:31 +0100643 Runtime* runtime = Runtime::Current();
644 if (UNLIKELY(runtime == nullptr)) {
645 // This must be oatdump without boot image. Make sure the .bss is inaccessible.
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700646 CheckedCall(mprotect, "protect bss", const_cast<uint8_t*>(BssBegin()), BssSize(), PROT_NONE);
Vladimir Marko667585a2017-09-29 10:42:31 +0100647 } else {
648 // Map boot image tables into the .bss. The reserved size must match size of the tables.
649 size_t reserved_size = static_cast<size_t>(boot_image_tables_end - boot_image_tables);
650 size_t tables_size = 0u;
651 for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) {
652 tables_size += space->GetImageHeader().GetBootImageConstantTablesSize();
653 DCHECK_ALIGNED(tables_size, kPageSize);
654 }
655 if (tables_size != reserved_size) {
656 *error_msg = StringPrintf("In oat file '%s' found unexpected boot image table sizes, "
657 " %zu bytes, should be %zu.",
658 GetLocation().c_str(),
659 reserved_size,
660 tables_size);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100661 return false;
662 }
Vladimir Marko667585a2017-09-29 10:42:31 +0100663 for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) {
664 uint32_t current_tables_size = space->GetImageHeader().GetBootImageConstantTablesSize();
665 if (current_tables_size != 0u && !MapConstantTables(space, boot_image_tables)) {
666 return false;
667 }
668 boot_image_tables += current_tables_size;
669 }
670 DCHECK(boot_image_tables == boot_image_tables_end);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100671 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100672 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700673 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700674}
675
Andreas Gampe049cff02015-12-01 23:27:12 -0800676////////////////////////
677// OatFile via dlopen //
678////////////////////////
679
Andreas Gampe049cff02015-12-01 23:27:12 -0800680class DlOpenOatFile FINAL : public OatFileBase {
681 public:
682 DlOpenOatFile(const std::string& filename, bool executable)
683 : OatFileBase(filename, executable),
684 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700685 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800686 }
687
688 ~DlOpenOatFile() {
689 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700690 if (!kIsTargetBuild) {
691 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
692 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700693 dlclose(dlopen_handle_);
694 } else {
695 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700696 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800697 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800698 }
699
700 protected:
701 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
702 std::string* error_msg) const OVERRIDE {
703 const uint8_t* ptr =
704 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
705 if (ptr == nullptr) {
706 *error_msg = dlerror();
707 }
708 return ptr;
709 }
710
Andreas Gampe4075f832016-05-18 13:09:54 -0700711 void PreLoad() OVERRIDE;
712
Andreas Gampe049cff02015-12-01 23:27:12 -0800713 bool Load(const std::string& elf_filename,
714 uint8_t* oat_file_begin,
715 bool writable,
716 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800717 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800718 std::string* error_msg) OVERRIDE;
719
720 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
721 void PreSetup(const std::string& elf_filename) OVERRIDE;
722
723 private:
724 bool Dlopen(const std::string& elf_filename,
725 uint8_t* oat_file_begin,
726 std::string* error_msg);
727
Richard Uhlera206c742016-05-24 15:04:22 -0700728 // On the host, if the same library is loaded again with dlopen the same
729 // file handle is returned. This differs from the behavior of dlopen on the
730 // target, where dlopen reloads the library at a different address every
731 // time you load it. The runtime relies on the target behavior to ensure
732 // each instance of the loaded library has a unique dex cache. To avoid
733 // problems, we fall back to our own linker in the case when the same
734 // library is opened multiple times on host. dlopen_handles_ is used to
735 // detect that case.
736 // Guarded by host_dlopen_handles_lock_;
737 static std::unordered_set<void*> host_dlopen_handles_;
738
Andreas Gampe049cff02015-12-01 23:27:12 -0800739 // dlopen handle during runtime.
740 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
741
742 // Dummy memory map objects corresponding to the regions mapped by dlopen.
743 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
744
Andreas Gampe4075f832016-05-18 13:09:54 -0700745 // The number of shared objects the linker told us about before loading. Used to
746 // (optimistically) optimize the PreSetup stage (see comment there).
747 size_t shared_objects_before_;
748
Andreas Gampe049cff02015-12-01 23:27:12 -0800749 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
750};
751
Richard Uhlera206c742016-05-24 15:04:22 -0700752std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
753
Andreas Gampe4075f832016-05-18 13:09:54 -0700754void DlOpenOatFile::PreLoad() {
755#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700756 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700757 LOG(FATAL) << "Should not reach here.";
758 UNREACHABLE();
759#else
760 // Count the entries in dl_iterate_phdr we get at this point in time.
761 struct dl_iterate_context {
762 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
763 size_t size ATTRIBUTE_UNUSED,
764 void *data) {
765 reinterpret_cast<dl_iterate_context*>(data)->count++;
766 return 0; // Continue iteration.
767 }
768 size_t count = 0;
769 } context;
770
771 dl_iterate_phdr(dl_iterate_context::callback, &context);
772 shared_objects_before_ = context.count;
773#endif
774}
775
Andreas Gampe049cff02015-12-01 23:27:12 -0800776bool DlOpenOatFile::Load(const std::string& elf_filename,
777 uint8_t* oat_file_begin,
778 bool writable,
779 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800780 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800781 std::string* error_msg) {
782 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
783 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
784 // !executable is a sign that we may want to patch), which may not be allowed for
785 // various reasons.
786 if (!kUseDlopen) {
787 *error_msg = "DlOpen is disabled.";
788 return false;
789 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800790 if (low_4gb) {
791 *error_msg = "DlOpen does not support low 4gb loading.";
792 return false;
793 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800794 if (writable) {
795 *error_msg = "DlOpen does not support writable loading.";
796 return false;
797 }
798 if (!executable) {
799 *error_msg = "DlOpen does not support non-executable loading.";
800 return false;
801 }
802
803 // dlopen always returns the same library if it is already opened on the host. For this reason
804 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
805 // the same library loaded multiple times at different addresses is required for class unloading
806 // and for having dex caches arrays in the .bss section.
807 if (!kIsTargetBuild) {
808 if (!kUseDlopenOnHost) {
809 *error_msg = "DlOpen disabled for host.";
810 return false;
811 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800812 }
813
814 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
815 DCHECK(dlopen_handle_ != nullptr || !success);
816
817 return success;
818}
819
820bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
821 uint8_t* oat_file_begin,
822 std::string* error_msg) {
823#ifdef __APPLE__
824 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
825 // but let's fallback to the custom loading code for the time being.
826 UNUSED(elf_filename, oat_file_begin);
827 *error_msg = "Dlopen unsupported on Mac.";
828 return false;
829#else
830 {
831 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
832 if (absolute_path == nullptr) {
833 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
834 return false;
835 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100836#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +0000837 android_dlextinfo extinfo = {};
Andreas Gampe049cff02015-12-01 23:27:12 -0800838 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
839 // (open oat files multiple
840 // times).
841 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
842 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800843 if (oat_file_begin != nullptr) { //
844 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
845 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
846 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800847 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
848#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800849 UNUSED(oat_file_begin);
Julien Duraj1af0c4f2016-11-16 14:05:48 +0000850 static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700851 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700852 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
853 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700854 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
855 dlclose(dlopen_handle_);
856 dlopen_handle_ = nullptr;
857 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
858 return false;
859 }
860 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100861#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800862 }
863 if (dlopen_handle_ == nullptr) {
864 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
865 return false;
866 }
867 return true;
868#endif
869}
870
871void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800872#ifdef __APPLE__
873 UNUSED(elf_filename);
874 LOG(FATAL) << "Should not reach here.";
875 UNREACHABLE();
876#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800877 struct dl_iterate_context {
878 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
879 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700880 context->shared_objects_seen++;
881 if (context->shared_objects_seen < context->shared_objects_before) {
882 // We haven't been called yet for anything we haven't seen before. Just continue.
883 // Note: this is aggressively optimistic. If another thread was unloading a library,
884 // we may miss out here. However, this does not happen often in practice.
885 return 0;
886 }
887
Andreas Gampe049cff02015-12-01 23:27:12 -0800888 // See whether this callback corresponds to the file which we have just loaded.
889 bool contains_begin = false;
890 for (int i = 0; i < info->dlpi_phnum; i++) {
891 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
892 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
893 info->dlpi_phdr[i].p_vaddr);
894 size_t memsz = info->dlpi_phdr[i].p_memsz;
895 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
896 contains_begin = true;
897 break;
898 }
899 }
900 }
901 // Add dummy mmaps for this file.
902 if (contains_begin) {
903 for (int i = 0; i < info->dlpi_phnum; i++) {
904 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
905 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
906 info->dlpi_phdr[i].p_vaddr);
907 size_t memsz = info->dlpi_phdr[i].p_memsz;
908 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
909 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
910 }
911 }
912 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
913 }
914 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
915 }
916 const uint8_t* const begin_;
917 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700918 const size_t shared_objects_before;
919 size_t shared_objects_seen;
920 };
921 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800922
923 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700924 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
925 // before giving up. This should be unusual.
926 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
927 << shared_objects_before_;
928 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
929 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
930 // OK, give up and print an error.
931 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
932 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
933 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800934 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800935#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800936}
937
938////////////////////////////////////////////////
939// OatFile via our own ElfFile implementation //
940////////////////////////////////////////////////
941
942class ElfOatFile FINAL : public OatFileBase {
943 public:
944 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
945
946 static ElfOatFile* OpenElfFile(File* file,
947 const std::string& location,
948 uint8_t* requested_base,
949 uint8_t* oat_file_begin, // Override base if not null
950 bool writable,
951 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800952 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800953 const char* abs_dex_location,
954 std::string* error_msg);
955
956 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100957 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800958 const char* abs_dex_location,
959 std::string* error_msg);
960
961 protected:
962 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
963 std::string* error_msg) const OVERRIDE {
964 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
965 if (ptr == nullptr) {
966 *error_msg = "(Internal implementation could not find symbol)";
967 }
968 return ptr;
969 }
970
Andreas Gampe4075f832016-05-18 13:09:54 -0700971 void PreLoad() OVERRIDE {
972 }
973
Andreas Gampe049cff02015-12-01 23:27:12 -0800974 bool Load(const std::string& elf_filename,
975 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
976 bool writable,
977 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800978 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800979 std::string* error_msg) OVERRIDE;
980
981 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
982 }
983
984 private:
985 bool ElfFileOpen(File* file,
986 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
987 bool writable,
988 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800989 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800990 std::string* error_msg);
991
992 private:
993 // Backing memory map for oat file during cross compilation.
994 std::unique_ptr<ElfFile> elf_file_;
995
996 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
997};
998
999ElfOatFile* ElfOatFile::OpenElfFile(File* file,
1000 const std::string& location,
1001 uint8_t* requested_base,
1002 uint8_t* oat_file_begin, // Override base if not null
1003 bool writable,
1004 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001005 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001006 const char* abs_dex_location,
1007 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001008 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001009 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001010 bool success = oat_file->ElfFileOpen(file,
1011 oat_file_begin,
1012 writable,
1013 low_4gb,
1014 executable,
1015 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001016 if (!success) {
1017 CHECK(!error_msg->empty());
1018 return nullptr;
1019 }
1020
1021 // Complete the setup.
1022 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
1023 return nullptr;
1024 }
1025
1026 if (!oat_file->Setup(abs_dex_location, error_msg)) {
1027 return nullptr;
1028 }
1029
1030 return oat_file.release();
1031}
1032
1033bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001034 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001035 const char* abs_dex_location,
1036 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001037 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001038 if (IsExecutable()) {
1039 *error_msg = "Cannot initialize from elf file in executable mode.";
1040 return false;
1041 }
1042 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001043 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001044 uint64_t offset, size;
1045 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1046 CHECK(has_section);
1047 SetBegin(elf_file->Begin() + offset);
1048 SetEnd(elf_file->Begin() + size + offset);
1049 // Ignore the optional .bss section when opening non-executable.
1050 return Setup(abs_dex_location, error_msg);
1051}
1052
1053bool ElfOatFile::Load(const std::string& elf_filename,
1054 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1055 bool writable,
1056 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001057 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001058 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001059 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001060 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1061 if (file == nullptr) {
1062 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1063 return false;
1064 }
1065 return ElfOatFile::ElfFileOpen(file.get(),
1066 oat_file_begin,
1067 writable,
1068 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001069 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001070 error_msg);
1071}
1072
1073bool ElfOatFile::ElfFileOpen(File* file,
1074 uint8_t* oat_file_begin,
1075 bool writable,
1076 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001077 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001078 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001079 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001080 // TODO: rename requested_base to oat_data_begin
1081 elf_file_.reset(ElfFile::Open(file,
1082 writable,
1083 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001084 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001085 error_msg,
1086 oat_file_begin));
1087 if (elf_file_ == nullptr) {
1088 DCHECK(!error_msg->empty());
1089 return false;
1090 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001091 bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001092 DCHECK(loaded || !error_msg->empty());
1093 return loaded;
1094}
1095
1096//////////////////////////
1097// General OatFile code //
1098//////////////////////////
1099
1100std::string OatFile::ResolveRelativeEncodedDexLocation(
1101 const char* abs_dex_location, const std::string& rel_dex_location) {
1102 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
1103 // Strip :classes<N>.dex used for secondary multidex files.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001104 std::string base = DexFileLoader::GetBaseLocation(rel_dex_location);
1105 std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(rel_dex_location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001106
1107 // Check if the base is a suffix of the provided abs_dex_location.
1108 std::string target_suffix = "/" + base;
1109 std::string abs_location(abs_dex_location);
1110 if (abs_location.size() > target_suffix.size()) {
1111 size_t pos = abs_location.size() - target_suffix.size();
1112 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1113 return abs_location + multidex_suffix;
1114 }
1115 }
1116 }
1117 return rel_dex_location;
1118}
1119
1120static void CheckLocation(const std::string& location) {
1121 CHECK(!location.empty());
1122}
1123
1124OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001125 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001126 const std::string& location,
1127 const char* abs_dex_location,
1128 std::string* error_msg) {
1129 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001130 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001131 ? oat_file.release()
1132 : nullptr;
1133}
1134
David Brazdil7b49e6c2016-09-01 11:06:18 +01001135OatFile* OatFile::Open(const std::string& oat_filename,
1136 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001137 uint8_t* requested_base,
1138 uint8_t* oat_file_begin,
1139 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001140 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001141 const char* abs_dex_location,
1142 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001143 ScopedTrace trace("Open oat file " + oat_location);
1144 CHECK(!oat_filename.empty()) << oat_location;
1145 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001146
Calin Juravle367b9d82017-05-15 18:18:39 -07001147 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001148
1149 // Check that the files even exist, fast-fail.
1150 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
1151 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1152 return nullptr;
1153 } else if (!OS::FileExists(oat_filename.c_str())) {
1154 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001155 return nullptr;
1156 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001157
1158 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1159 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001160 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1161 oat_filename,
1162 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001163 requested_base,
1164 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001165 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001166 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001167 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001168 abs_dex_location,
1169 error_msg);
1170 if (with_dlopen != nullptr) {
1171 return with_dlopen;
1172 }
1173 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001174 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001175 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001176 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1177 //
1178 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1179 //
1180 // We use our own ELF loader for Quick to deal with legacy apps that
1181 // open a generated dex file by name, remove the file, then open
1182 // another generated dex file with the same name. http://b/10614658
1183 //
1184 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1185 //
1186 //
1187 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1188 // 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 +01001189 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1190 oat_filename,
1191 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001192 requested_base,
1193 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001194 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001195 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001196 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001197 abs_dex_location,
1198 error_msg);
1199 return with_internal;
1200}
1201
1202OatFile* OatFile::OpenWritable(File* file,
1203 const std::string& location,
1204 const char* abs_dex_location,
1205 std::string* error_msg) {
1206 CheckLocation(location);
1207 return ElfOatFile::OpenElfFile(file,
1208 location,
1209 nullptr,
1210 nullptr,
1211 true,
1212 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001213 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001214 abs_dex_location,
1215 error_msg);
1216}
1217
1218OatFile* OatFile::OpenReadable(File* file,
1219 const std::string& location,
1220 const char* abs_dex_location,
1221 std::string* error_msg) {
1222 CheckLocation(location);
1223 return ElfOatFile::OpenElfFile(file,
1224 location,
1225 nullptr,
1226 nullptr,
1227 false,
1228 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001229 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001230 abs_dex_location,
1231 error_msg);
1232}
1233
1234OatFile::OatFile(const std::string& location, bool is_executable)
1235 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001236 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001237 begin_(nullptr),
1238 end_(nullptr),
1239 bss_begin_(nullptr),
1240 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001241 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001242 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001243 is_executable_(is_executable),
1244 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1245 CHECK(!location_.empty());
1246}
1247
1248OatFile::~OatFile() {
1249 STLDeleteElements(&oat_dex_files_storage_);
1250}
1251
Brian Carlstrome24fa612011-09-29 00:53:55 -07001252const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001253 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001254}
1255
Ian Rogers13735952014-10-08 12:43:28 -07001256const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001257 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001258 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001259}
1260
Ian Rogers13735952014-10-08 12:43:28 -07001261const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001262 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001263 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001264}
1265
Vladimir Marko5c42c292015-02-25 12:02:49 +00001266const uint8_t* OatFile::BssBegin() const {
1267 return bss_begin_;
1268}
1269
1270const uint8_t* OatFile::BssEnd() const {
1271 return bss_end_;
1272}
1273
David Brazdil7b49e6c2016-09-01 11:06:18 +01001274const uint8_t* OatFile::DexBegin() const {
1275 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1276}
1277
1278const uint8_t* OatFile::DexEnd() const {
1279 return kIsVdexEnabled ? vdex_->End() : End();
1280}
1281
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001282ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1283 if (bss_methods_ != nullptr) {
1284 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1285 ArtMethod** methods_end =
1286 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1287 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1288 } else {
1289 return ArrayRef<ArtMethod*>();
1290 }
1291}
1292
Vladimir Markoaad75c62016-10-03 08:46:48 +00001293ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1294 if (bss_roots_ != nullptr) {
1295 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1296 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1297 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1298 } else {
1299 return ArrayRef<GcRoot<mirror::Object>>();
1300 }
1301}
1302
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001303const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1304 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001305 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001306 // NOTE: We assume here that the canonical location for a given dex_location never
1307 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1308 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1309 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001310
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001311 // TODO: Additional analysis of usage patterns to see if this can be simplified
1312 // without any performance loss, for example by not doing the first lock-free lookup.
1313
1314 const OatFile::OatDexFile* oat_dex_file = nullptr;
1315 StringPiece key(dex_location);
1316 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1317 // directly mentioned in the oat file and doesn't require locking.
1318 auto primary_it = oat_dex_files_.find(key);
1319 if (primary_it != oat_dex_files_.end()) {
1320 oat_dex_file = primary_it->second;
1321 DCHECK(oat_dex_file != nullptr);
1322 } else {
1323 // This dex_location is not one of the dex locations directly mentioned in the
1324 // oat file. The correct lookup is via the canonical location but first see in
1325 // the secondary_oat_dex_files_ whether we've looked up this location before.
1326 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1327 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1328 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001329 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001330 } else {
1331 // We haven't seen this dex_location before, we must check the canonical location.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001332 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001333 if (dex_canonical_location != dex_location) {
1334 StringPiece canonical_key(dex_canonical_location);
1335 auto canonical_it = oat_dex_files_.find(canonical_key);
1336 if (canonical_it != oat_dex_files_.end()) {
1337 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001338 } // else keep null.
1339 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001340
1341 // Copy the key to the string_cache_ and store the result in secondary map.
1342 string_cache_.emplace_back(key.data(), key.length());
1343 StringPiece key_copy(string_cache_.back());
1344 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001345 }
1346 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001347
1348 if (oat_dex_file == nullptr) {
1349 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001350 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001351 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1352 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1353 }
1354 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001355 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001356
Richard Uhler9a37efc2016-08-05 16:32:55 -07001357 if (dex_location_checksum != nullptr &&
1358 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1359 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001360 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001361 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1362 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1363 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1364 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1365 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001366 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001367 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001368 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001369 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001370}
1371
Brian Carlstrome24fa612011-09-29 00:53:55 -07001372OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001373 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001374 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001375 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001376 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001377 const uint8_t* lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001378 const MethodBssMapping* method_bss_mapping_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001379 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001380 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001381 : oat_file_(oat_file),
1382 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001383 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001384 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001385 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001386 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001387 method_bss_mapping_(method_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001388 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001389 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001390 // Initialize TypeLookupTable.
1391 if (lookup_table_data_ != nullptr) {
1392 // Peek the number of classes from the DexFile.
1393 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1394 const uint32_t num_class_defs = dex_header->class_defs_size_;
1395 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1396 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1397 } else {
Mathieu Chartier1b868492016-11-16 16:22:37 -08001398 lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001399 }
1400 }
1401}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001402
Mathieu Chartier1b868492016-11-16 16:22:37 -08001403OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table)
1404 : lookup_table_(std::move(lookup_table)) {}
1405
Brian Carlstrome24fa612011-09-29 00:53:55 -07001406OatFile::OatDexFile::~OatDexFile() {}
1407
Ian Rogers05f28c62012-10-23 18:12:13 -07001408size_t OatFile::OatDexFile::FileSize() const {
1409 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1410}
1411
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001412std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001413 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001414 static constexpr bool kVerify = false;
1415 static constexpr bool kVerifyChecksum = false;
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001416 return DexFileLoader::Open(dex_file_pointer_,
1417 FileSize(),
1418 dex_file_location_,
1419 dex_file_location_checksum_,
1420 this,
1421 kVerify,
1422 kVerifyChecksum,
1423 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001424}
1425
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001426uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1427 return oat_class_offsets_pointer_[class_def_index];
1428}
1429
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001430OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001431 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001432
Ian Rogers13735952014-10-08 12:43:28 -07001433 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001434 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001435
Ian Rogers13735952014-10-08 12:43:28 -07001436 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001437 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1438 mirror::Class::Status status =
1439 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1440 CHECK_LT(status, mirror::Class::kStatusMax);
1441
Ian Rogers13735952014-10-08 12:43:28 -07001442 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001443 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1444 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1445 CHECK_LT(type, kOatClassMax);
1446
Ian Rogers13735952014-10-08 12:43:28 -07001447 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001448 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001449
Brian Carlstromcd937a92014-03-04 22:53:23 -08001450 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001451 const uint8_t* bitmap_pointer = nullptr;
1452 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001453 if (type != kOatClassNoneCompiled) {
1454 if (type == kOatClassSomeCompiled) {
1455 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1456 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1457 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1458 methods_pointer = bitmap_pointer + bitmap_size;
1459 } else {
1460 methods_pointer = after_type_pointer;
1461 }
1462 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001463 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001464
Richard Uhler07b3c232015-03-31 15:57:54 -07001465 return OatFile::OatClass(oat_file_,
1466 status,
1467 type,
1468 bitmap_size,
1469 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1470 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001471}
1472
David Sehr9aa352e2016-09-15 18:13:52 -07001473const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1474 const char* descriptor,
1475 size_t hash) {
1476 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1477 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1478 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1479 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
Andreas Gampee2abbc62017-09-15 11:59:26 -07001480 return (class_def_idx != dex::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
David Sehr9aa352e2016-09-15 18:13:52 -07001481 }
1482 // Fast path for rare no class defs case.
1483 const uint32_t num_class_defs = dex_file.NumClassDefs();
1484 if (num_class_defs == 0) {
1485 return nullptr;
1486 }
1487 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1488 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001489 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
David Sehr9aa352e2016-09-15 18:13:52 -07001490 return dex_file.FindClassDef(type_idx);
1491 }
1492 return nullptr;
1493}
1494
Mathieu Chartier120aa282017-08-05 16:03:03 -07001495// Madvise the dex file based on the state we are moving to.
1496void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001497 const bool low_ram = Runtime::Current()->GetHeap()->IsLowMemoryMode();
1498 // TODO: Also do madvise hints for non low ram devices.
1499 if (!kMadviseDexFileAccesses || !low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07001500 return;
1501 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001502 if (state == MadviseState::kMadviseStateAtLoad) {
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001503 if (low_ram) {
Mathieu Chartier4ec507d2017-08-15 15:21:40 -07001504 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
1505 // Other devices have enough page cache to get performance benefits from loading more pages
1506 // into the page cache.
1507 MadviseLargestPageAlignedRegion(dex_file.Begin(),
1508 dex_file.Begin() + dex_file.Size(),
1509 MADV_RANDOM);
1510 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001511 }
1512 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1513 if (oat_dex_file != nullptr) {
1514 // Should always be there.
1515 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
1516 CHECK(sections != nullptr);
1517 sections->Madvise(&dex_file, state);
1518 }
1519}
1520
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001521OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001522 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001523 OatClassType type,
1524 uint32_t bitmap_size,
1525 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001526 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001527 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001528 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001529 switch (type_) {
1530 case kOatClassAllCompiled: {
1531 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001532 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001533 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001534 break;
1535 }
1536 case kOatClassSomeCompiled: {
1537 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001538 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001539 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001540 break;
1541 }
1542 case kOatClassNoneCompiled: {
1543 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001544 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001545 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001546 break;
1547 }
1548 case kOatClassMax: {
1549 LOG(FATAL) << "Invalid OatClassType " << type_;
1550 break;
1551 }
1552 }
1553}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001554
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001555uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1556 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1557 if (oat_method_offsets == nullptr) {
1558 return 0u;
1559 }
1560 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1561}
1562
1563const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001564 // 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 -07001565 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001566 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001567 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001568 }
1569 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001570 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001571 CHECK_EQ(kOatClassAllCompiled, type_);
1572 methods_pointer_index = method_index;
1573 } else {
1574 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001575 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001576 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001577 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001578 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1579 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001580 }
1581 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001582 return &oat_method_offsets;
1583}
1584
1585const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1586 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1587 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001588 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001589 }
1590 if (oat_file_->IsExecutable() ||
1591 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001592 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001593 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001594 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001595 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1596 // version.
1597 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001598}
1599
Mathieu Chartiere401d142015-04-22 13:56:20 -07001600void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001601 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001602 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001603}
1604
Andreas Gampe7ba64962014-10-23 11:37:40 -07001605bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001606 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001607 // TODO: Check against oat_patches. b/18144996
1608}
1609
Sebastien Hertz0de11332015-05-13 12:14:05 +02001610bool OatFile::IsDebuggable() const {
1611 return GetOatHeader().IsDebuggable();
1612}
1613
Andreas Gampe29d38e72016-03-23 15:31:51 +00001614CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1615 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001616}
1617
Calin Juravle44e5efa2017-09-12 00:54:26 -07001618std::string OatFile::GetClassLoaderContext() const {
1619 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1620};
1621
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001622OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1623 uint16_t class_def_idx,
1624 bool* found) {
1625 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
1626 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001627 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001628 *found = false;
1629 return OatFile::OatClass::Invalid();
1630 }
1631 *found = true;
1632 return oat_dex_file->GetOatClass(class_def_idx);
1633}
1634
Mathieu Chartier1b868492016-11-16 16:22:37 -08001635void OatFile::OatDexFile::AssertAotCompiler() {
1636 CHECK(Runtime::Current()->IsAotCompiler());
1637}
1638
Brian Carlstrome24fa612011-09-29 00:53:55 -07001639} // namespace art