blob: d64986e76d5f7a83ef9a42aee3682c643dc21b91 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_file.h"
18
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019#include <dlfcn.h>
David Srbecky1baabf02015-06-16 17:12:34 +000020#ifndef __APPLE__
21#include <link.h> // for dl_iterate_phdr.
22#endif
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include <unistd.h>
24
25#include <cstdlib>
26#include <cstring>
Richard Uhlere5fed032015-03-18 08:21:11 -070027#include <sstream>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include <type_traits>
Shubham Ajmerab22dea02017-10-04 18:36:41 -070029#include <sys/stat.h>
Richard Uhlere5fed032015-03-18 08:21:11 -070030
Andreas Gampefa8429b2015-04-07 18:34:42 -070031// dlopen_ext support from bionic.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010032#ifdef ART_TARGET_ANDROID
Andreas Gampefa8429b2015-04-07 18:34:42 -070033#include "android/dlext.h"
34#endif
35
Andreas Gampe46ee31b2016-12-14 10:11:49 -080036#include "android-base/stringprintf.h"
37
Andreas Gampec6ea7d02017-02-01 16:46:28 -080038#include "art_method.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070039#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070040#include "base/enums.h"
David Sehr891a50e2017-10-27 17:01:07 -070041#include "base/file_utils.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080042#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080043#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080044#include "base/unix_file/fd_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080045#include "dex_file_types.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070046#include "dex_file_loader.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070048#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000049#include "gc_root.h"
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010050#include "gc/space/image_space.h"
David Srbecky1baabf02015-06-16 17:12:34 +000051#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070053#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070054#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010055#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070056#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070057#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070058#include "runtime.h"
Mathieu Chartier292567e2017-10-12 13:24:38 -070059#include "standard_dex_file.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000060#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070061#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062#include "utils.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010063#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070064
65namespace art {
66
Andreas Gampe46ee31b2016-12-14 10:11:49 -080067using android::base::StringPrintf;
68
Andreas Gampe049cff02015-12-01 23:27:12 -080069// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000070static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070071
Andreas Gampe049cff02015-12-01 23:27:12 -080072// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070073// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
74// multiple times). However, if/when we switch the above, we likely want to switch this, too,
75// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000076static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070077
78// For debugging, Open will print DlOpen error message if set to true.
79static constexpr bool kPrintDlOpenErrorMessage = false;
80
Andreas Gampe049cff02015-12-01 23:27:12 -080081// Note for OatFileBase and descendents:
82//
83// These are used in OatFile::Open to try all our loaders.
84//
85// The process is simple:
86//
87// 1) Allocate an instance through the standard constructor (location, executable)
88// 2) Load() to try to open the file.
89// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
90// 4) PreSetup() for any steps that should be done before the final setup.
91// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070092
Andreas Gampe049cff02015-12-01 23:27:12 -080093class OatFileBase : public OatFile {
94 public:
95 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070096
Andreas Gampe049cff02015-12-01 23:27:12 -080097 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010098 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
99 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -0700100 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800101 uint8_t* requested_base,
102 uint8_t* oat_file_begin,
103 bool writable,
104 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800105 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700106 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800107 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800108
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700109 template <typename kOatFileBaseSubType>
110 static OatFileBase* OpenOatFile(int vdex_fd,
111 int oat_fd,
112 const std::string& vdex_filename,
113 const std::string& oat_filename,
114 uint8_t* requested_base,
115 uint8_t* oat_file_begin,
116 bool writable,
117 bool executable,
118 bool low_4gb,
119 const char* abs_dex_location,
120 std::string* error_msg);
121
Andreas Gampe049cff02015-12-01 23:27:12 -0800122 protected:
123 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700124
Andreas Gampe049cff02015-12-01 23:27:12 -0800125 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
126 std::string* error_msg) const = 0;
127
Andreas Gampe4075f832016-05-18 13:09:54 -0700128 virtual void PreLoad() = 0;
129
David Brazdil7b49e6c2016-09-01 11:06:18 +0100130 bool LoadVdex(const std::string& vdex_filename,
131 bool writable,
132 bool low_4gb,
133 std::string* error_msg);
134
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700135 bool LoadVdex(int vdex_fd,
136 const std::string& vdex_filename,
137 bool writable,
138 bool low_4gb,
139 std::string* error_msg);
140
Andreas Gampe049cff02015-12-01 23:27:12 -0800141 virtual bool Load(const std::string& elf_filename,
142 uint8_t* oat_file_begin,
143 bool writable,
144 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800145 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800146 std::string* error_msg) = 0;
147
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700148 virtual bool Load(int oat_fd,
149 uint8_t* oat_file_begin,
150 bool writable,
151 bool executable,
152 bool low_4gb,
153 std::string* error_msg) = 0;
154
Andreas Gampe049cff02015-12-01 23:27:12 -0800155 bool ComputeFields(uint8_t* requested_base,
156 const std::string& file_path,
157 std::string* error_msg);
158
159 virtual void PreSetup(const std::string& elf_filename) = 0;
160
161 bool Setup(const char* abs_dex_location, std::string* error_msg);
162
163 // Setters exposed for ElfOatFile.
164
165 void SetBegin(const uint8_t* begin) {
166 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700167 }
168
Andreas Gampe049cff02015-12-01 23:27:12 -0800169 void SetEnd(const uint8_t* end) {
170 end_ = end;
171 }
172
David Brazdilc93b3be2016-09-12 18:49:58 +0100173 void SetVdex(VdexFile* vdex) {
174 vdex_.reset(vdex);
175 }
176
Andreas Gampe049cff02015-12-01 23:27:12 -0800177 private:
178 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
179};
180
181template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100182OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
183 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800184 const std::string& location,
185 uint8_t* requested_base,
186 uint8_t* oat_file_begin,
187 bool writable,
188 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800189 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800190 const char* abs_dex_location,
191 std::string* error_msg) {
192 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700193
194 ret->PreLoad();
195
David Brazdil7b49e6c2016-09-01 11:06:18 +0100196 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
197 return nullptr;
198 }
199
Andreas Gampe049cff02015-12-01 23:27:12 -0800200 if (!ret->Load(elf_filename,
201 oat_file_begin,
202 writable,
203 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800204 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800205 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800206 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800207 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800208
Andreas Gampe049cff02015-12-01 23:27:12 -0800209 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
210 return nullptr;
211 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700212
Andreas Gampe049cff02015-12-01 23:27:12 -0800213 ret->PreSetup(elf_filename);
214
215 if (!ret->Setup(abs_dex_location, error_msg)) {
216 return nullptr;
217 }
218
Dave Allison69dfe512014-07-11 17:11:58 +0000219 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800220}
221
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700222template <typename kOatFileBaseSubType>
223OatFileBase* OatFileBase::OpenOatFile(int vdex_fd,
224 int oat_fd,
225 const std::string& vdex_location,
226 const std::string& oat_location,
227 uint8_t* requested_base,
228 uint8_t* oat_file_begin,
229 bool writable,
230 bool executable,
231 bool low_4gb,
232 const char* abs_dex_location,
233 std::string* error_msg) {
234 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(oat_location, executable));
235
236 if (kIsVdexEnabled && !ret->LoadVdex(vdex_fd, vdex_location, writable, low_4gb, error_msg)) {
237 return nullptr;
238 }
239
240 if (!ret->Load(oat_fd,
241 oat_file_begin,
242 writable,
243 executable,
244 low_4gb,
245 error_msg)) {
246 return nullptr;
247 }
248
249 if (!ret->ComputeFields(requested_base, oat_location, error_msg)) {
250 return nullptr;
251 }
252
253 ret->PreSetup(oat_location);
254
255 if (!ret->Setup(abs_dex_location, error_msg)) {
256 return nullptr;
257 }
258
259 return ret.release();
260}
261
David Brazdil7b49e6c2016-09-01 11:06:18 +0100262bool OatFileBase::LoadVdex(const std::string& vdex_filename,
263 bool writable,
264 bool low_4gb,
265 std::string* error_msg) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100266 vdex_ = VdexFile::Open(vdex_filename, writable, low_4gb, /* unquicken*/ false, error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100267 if (vdex_.get() == nullptr) {
268 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
269 vdex_filename.c_str(),
270 error_msg->c_str());
271 return false;
272 }
273 return true;
274}
275
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700276bool OatFileBase::LoadVdex(int vdex_fd,
277 const std::string& vdex_filename,
278 bool writable,
279 bool low_4gb,
280 std::string* error_msg) {
281 if (vdex_fd != -1) {
282 struct stat s;
283 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd, &s));
284 if (rc == -1) {
285 PLOG(WARNING) << "Failed getting length of vdex file";
286 } else {
287 vdex_ = VdexFile::Open(vdex_fd,
288 s.st_size,
289 vdex_filename,
290 writable,
291 low_4gb,
292 false /* unquicken */,
293 error_msg);
294 if (vdex_.get() == nullptr) {
295 *error_msg = "Failed opening vdex file.";
296 return false;
297 }
298 }
299 }
300 return true;
301}
302
Andreas Gampe049cff02015-12-01 23:27:12 -0800303bool OatFileBase::ComputeFields(uint8_t* requested_base,
304 const std::string& file_path,
305 std::string* error_msg) {
306 std::string symbol_error_msg;
307 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700308 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800309 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
310 file_path.c_str(),
311 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700312 return false;
313 }
314 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000315 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700316 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000317 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
318 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700319 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800320 "oatdata=%p != expected=%p. See process maps in the log.",
321 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700322 return false;
323 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800324 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700325 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800326 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
327 file_path.c_str(),
328 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700329 return false;
330 }
331 // Readjust to be non-inclusive upper bound.
332 end_ += sizeof(uint32_t);
333
Andreas Gampe049cff02015-12-01 23:27:12 -0800334 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700335 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800336 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700337 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700338 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800339 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700340 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800341 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700342 return false;
343 }
344 // Readjust to be non-inclusive upper bound.
345 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100346 // Find bss methods if present.
347 bss_methods_ =
348 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000349 // Find bss roots if present.
350 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700351 }
352
Andreas Gampe049cff02015-12-01 23:27:12 -0800353 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800354}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800355
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100356// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
357// position by the number of bytes read, i.e. sizeof(T).
358// Return true on success, false if the read would go beyond the end of the OatFile.
359template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100360inline static bool ReadOatDexFileData(const OatFile& oat_file,
361 /*inout*/const uint8_t** oat,
362 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100363 DCHECK(oat != nullptr);
364 DCHECK(value != nullptr);
365 DCHECK_LE(*oat, oat_file.End());
366 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
367 return false;
368 }
369 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
370 typedef __attribute__((__aligned__(1))) T unaligned_type;
371 *value = *reinterpret_cast<const unaligned_type*>(*oat);
372 *oat += sizeof(T);
373 return true;
374}
375
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100376static inline bool MapConstantTables(const gc::space::ImageSpace* space,
377 uint8_t* address) {
378 // If MREMAP_DUP is ever merged to Linux kernel, use it to avoid the unnecessary open()/close().
379 // Note: The current approach relies on the filename still referencing the same inode.
380
381 File file(space->GetImageFilename(), O_RDONLY, /* checkUsage */ false);
382 if (!file.IsOpened()) {
383 LOG(ERROR) << "Failed to open boot image file " << space->GetImageFilename();
384 return false;
385 }
386
387 uint32_t offset = space->GetImageHeader().GetBootImageConstantTablesOffset();
388 uint32_t size = space->GetImageHeader().GetBootImageConstantTablesSize();
389 std::string error_msg;
390 std::unique_ptr<MemMap> mem_map(MemMap::MapFileAtAddress(address,
391 size,
392 PROT_READ,
393 MAP_PRIVATE,
394 file.Fd(),
395 offset,
396 /* low_4gb */ false,
397 /* reuse */ true,
398 file.GetPath().c_str(),
399 &error_msg));
400 if (mem_map == nullptr) {
401 LOG(ERROR) << "Failed to mmap boot image tables from file " << space->GetImageFilename();
402 return false;
403 }
404 return true;
405}
406
Andreas Gampe049cff02015-12-01 23:27:12 -0800407bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700408 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800409 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100410 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
411 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800412 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700413 return false;
414 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100415 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
416 size_t key_value_store_size =
417 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
418 if (Size() < sizeof(OatHeader) + key_value_store_size) {
419 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
420 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100421 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100422 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100423 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100424 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700425 return false;
426 }
427
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100428 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
429 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
430 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
431 "%zu is not in [%zu, %zu]",
432 GetLocation().c_str(),
433 oat_dex_files_offset,
434 GetOatHeader().GetHeaderSize(),
435 Size());
436 return false;
437 }
438 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
439
440 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
441 if (!IsAligned<kPageSize>(bss_begin_) ||
442 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
443 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000444 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
445 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100446 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000447 GetLocation().c_str(),
448 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100449 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000450 bss_roots_,
451 bss_end_);
452 return false;
453 }
454
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100455 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
456 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
457 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
458 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100459 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000460 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000461 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100462 bss_methods_,
463 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000464 bss_end_);
465 return false;
466 }
467
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100468 uint8_t* after_tables =
469 (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_; // May be null.
470 uint8_t* boot_image_tables = (bss_begin_ == after_tables) ? nullptr : bss_begin_;
471 uint8_t* boot_image_tables_end =
472 (bss_begin_ == after_tables) ? nullptr : (after_tables != nullptr) ? after_tables : bss_end_;
473 DCHECK_EQ(boot_image_tables != nullptr, boot_image_tables_end != nullptr);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100474 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
475 oat_dex_files_storage_.reserve(dex_file_count);
476 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100477 uint32_t dex_file_location_size;
478 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
479 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
480 "location size",
481 GetLocation().c_str(),
482 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700483 return false;
484 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100485 if (UNLIKELY(dex_file_location_size == 0U)) {
486 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
487 GetLocation().c_str(),
488 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700489 return false;
490 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000491 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100492 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
493 "location",
494 GetLocation().c_str(),
495 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700496 return false;
497 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000498 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
499 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700500
Richard Uhlere5fed032015-03-18 08:21:11 -0700501 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
502 abs_dex_location,
503 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700504
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100505 uint32_t dex_file_checksum;
506 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
507 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
508 "dex file checksum",
509 GetLocation().c_str(),
510 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700511 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700512 return false;
513 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700514
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100515 uint32_t dex_file_offset;
516 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
517 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
518 "after dex file offsets",
519 GetLocation().c_str(),
520 i,
521 dex_file_location.c_str());
522 return false;
523 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700524 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100525 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
526 "file offset",
527 GetLocation().c_str(),
528 i,
529 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700530 return false;
531 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100532 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100533 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
534 "offset %u > %zu",
535 GetLocation().c_str(),
536 i,
537 dex_file_location.c_str(),
538 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100539 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700540 return false;
541 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100542 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000543 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
544 "offset %u of %zu but the size of dex file header is %zu",
545 GetLocation().c_str(),
546 i,
547 dex_file_location.c_str(),
548 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100549 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000550 sizeof(DexFile::Header));
551 return false;
552 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800553
David Brazdil7b49e6c2016-09-01 11:06:18 +0100554 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700555
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700556 const bool valid_magic = DexFileLoader::IsMagicValid(dex_file_pointer);
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700557 if (UNLIKELY(!valid_magic)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100558 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
559 "dex file magic '%s'",
560 GetLocation().c_str(),
561 i,
562 dex_file_location.c_str(),
563 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700564 return false;
565 }
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700566 if (UNLIKELY(!DexFileLoader::IsVersionAndMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100567 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
568 "dex file version '%s'",
569 GetLocation().c_str(),
570 i,
571 dex_file_location.c_str(),
572 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700573 return false;
574 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800575 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100576 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000577 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
578 "offset %u and size %u truncated at %zu",
579 GetLocation().c_str(),
580 i,
581 dex_file_location.c_str(),
582 dex_file_offset,
583 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100584 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000585 return false;
586 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300587
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000588 uint32_t class_offsets_offset;
589 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
590 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
591 "after class offsets offset",
592 GetLocation().c_str(),
593 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300594 dex_file_location.c_str());
595 return false;
596 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000597 if (UNLIKELY(class_offsets_offset > Size()) ||
598 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
599 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
600 "class offsets, offset %u of %zu, class defs %u",
601 GetLocation().c_str(),
602 i,
603 dex_file_location.c_str(),
604 class_offsets_offset,
605 Size(),
606 header->class_defs_size_);
607 return false;
608 }
609 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
610 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
611 "class offsets, offset %u",
612 GetLocation().c_str(),
613 i,
614 dex_file_location.c_str(),
615 class_offsets_offset);
616 return false;
617 }
618 const uint32_t* class_offsets_pointer =
619 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
620
621 uint32_t lookup_table_offset;
622 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
623 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
624 "after lookup table offset",
625 GetLocation().c_str(),
626 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300627 dex_file_location.c_str());
628 return false;
629 }
630 const uint8_t* lookup_table_data = lookup_table_offset != 0u
631 ? Begin() + lookup_table_offset
632 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000633 if (lookup_table_offset != 0u &&
634 (UNLIKELY(lookup_table_offset > Size()) ||
635 UNLIKELY(Size() - lookup_table_offset <
636 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100637 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000638 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100639 GetLocation().c_str(),
640 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000641 dex_file_location.c_str(),
642 lookup_table_offset,
643 Size(),
644 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700645 return false;
646 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700647
Mathieu Chartier120aa282017-08-05 16:03:03 -0700648 uint32_t dex_layout_sections_offset;
649 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
650 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
651 "after dex layout sections offset",
652 GetLocation().c_str(),
653 i,
654 dex_file_location.c_str());
655 return false;
656 }
657 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
658 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
659 : nullptr;
660
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100661 uint32_t method_bss_mapping_offset;
662 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &method_bss_mapping_offset))) {
663 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
664 "after method bss mapping offset",
665 GetLocation().c_str(),
666 i,
667 dex_file_location.c_str());
668 return false;
669 }
670 const bool readable_method_bss_mapping_size =
671 method_bss_mapping_offset != 0u &&
672 method_bss_mapping_offset <= Size() &&
673 IsAligned<alignof(MethodBssMapping)>(method_bss_mapping_offset) &&
674 Size() - method_bss_mapping_offset >= MethodBssMapping::ComputeSize(0);
675 const MethodBssMapping* method_bss_mapping = readable_method_bss_mapping_size
676 ? reinterpret_cast<const MethodBssMapping*>(Begin() + method_bss_mapping_offset)
677 : nullptr;
678 if (method_bss_mapping_offset != 0u &&
679 (UNLIKELY(method_bss_mapping == nullptr) ||
680 UNLIKELY(method_bss_mapping->size() == 0u) ||
681 UNLIKELY(Size() - method_bss_mapping_offset <
682 MethodBssMapping::ComputeSize(method_bss_mapping->size())))) {
683 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
684 " truncated method bss mapping, offset %u of %zu, length %zu",
685 GetLocation().c_str(),
686 i,
687 dex_file_location.c_str(),
688 method_bss_mapping_offset,
689 Size(),
690 method_bss_mapping != nullptr ? method_bss_mapping->size() : 0u);
691 return false;
692 }
693 if (kIsDebugBuild && method_bss_mapping != nullptr) {
694 const MethodBssMappingEntry* prev_entry = nullptr;
695 for (const MethodBssMappingEntry& entry : *method_bss_mapping) {
696 CHECK_ALIGNED_PARAM(entry.bss_offset, static_cast<size_t>(pointer_size));
697 CHECK_LT(entry.bss_offset, BssSize());
698 CHECK_LE(POPCOUNT(entry.index_mask) * static_cast<size_t>(pointer_size), entry.bss_offset);
699 size_t index_mask_span = (entry.index_mask != 0u) ? 16u - CTZ(entry.index_mask) : 0u;
700 CHECK_LE(index_mask_span, entry.method_index);
701 if (prev_entry != nullptr) {
702 CHECK_LT(prev_entry->method_index, entry.method_index - index_mask_span);
703 }
704 prev_entry = &entry;
705 }
706 CHECK_LT(prev_entry->method_index,
707 reinterpret_cast<const DexFile::Header*>(dex_file_pointer)->method_ids_size_);
708 }
709
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700710 std::string canonical_location =
711 DexFileLoader::GetDexCanonicalLocation(dex_file_location.c_str());
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100712
713 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100714 OatDexFile* oat_dex_file = new OatDexFile(this,
715 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100716 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100717 dex_file_checksum,
718 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300719 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100720 method_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000721 class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700722 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100723 oat_dex_files_storage_.push_back(oat_dex_file);
724
725 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100726 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100727 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100728 if (canonical_location != dex_file_location) {
729 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
730 oat_dex_files_.Put(canonical_key, oat_dex_file);
731 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700732 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100733
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100734 if (boot_image_tables != nullptr) {
Vladimir Marko667585a2017-09-29 10:42:31 +0100735 Runtime* runtime = Runtime::Current();
736 if (UNLIKELY(runtime == nullptr)) {
737 // This must be oatdump without boot image. Make sure the .bss is inaccessible.
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700738 CheckedCall(mprotect, "protect bss", const_cast<uint8_t*>(BssBegin()), BssSize(), PROT_NONE);
Vladimir Marko667585a2017-09-29 10:42:31 +0100739 } else {
740 // Map boot image tables into the .bss. The reserved size must match size of the tables.
741 size_t reserved_size = static_cast<size_t>(boot_image_tables_end - boot_image_tables);
742 size_t tables_size = 0u;
743 for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) {
744 tables_size += space->GetImageHeader().GetBootImageConstantTablesSize();
745 DCHECK_ALIGNED(tables_size, kPageSize);
746 }
747 if (tables_size != reserved_size) {
748 *error_msg = StringPrintf("In oat file '%s' found unexpected boot image table sizes, "
749 " %zu bytes, should be %zu.",
750 GetLocation().c_str(),
751 reserved_size,
752 tables_size);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100753 return false;
754 }
Vladimir Marko667585a2017-09-29 10:42:31 +0100755 for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) {
756 uint32_t current_tables_size = space->GetImageHeader().GetBootImageConstantTablesSize();
757 if (current_tables_size != 0u && !MapConstantTables(space, boot_image_tables)) {
758 return false;
759 }
760 boot_image_tables += current_tables_size;
761 }
762 DCHECK(boot_image_tables == boot_image_tables_end);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100763 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100764 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700765 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700766}
767
Andreas Gampe049cff02015-12-01 23:27:12 -0800768////////////////////////
769// OatFile via dlopen //
770////////////////////////
771
Andreas Gampe049cff02015-12-01 23:27:12 -0800772class DlOpenOatFile FINAL : public OatFileBase {
773 public:
774 DlOpenOatFile(const std::string& filename, bool executable)
775 : OatFileBase(filename, executable),
776 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700777 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800778 }
779
780 ~DlOpenOatFile() {
781 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700782 if (!kIsTargetBuild) {
783 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
784 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700785 dlclose(dlopen_handle_);
786 } else {
787 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700788 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800789 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800790 }
791
792 protected:
793 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
794 std::string* error_msg) const OVERRIDE {
795 const uint8_t* ptr =
796 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
797 if (ptr == nullptr) {
798 *error_msg = dlerror();
799 }
800 return ptr;
801 }
802
Andreas Gampe4075f832016-05-18 13:09:54 -0700803 void PreLoad() OVERRIDE;
804
Andreas Gampe049cff02015-12-01 23:27:12 -0800805 bool Load(const std::string& elf_filename,
806 uint8_t* oat_file_begin,
807 bool writable,
808 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800809 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800810 std::string* error_msg) OVERRIDE;
811
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700812 bool Load(int, uint8_t*, bool, bool, bool, std::string*) {
813 return false;
814 }
815
Andreas Gampe049cff02015-12-01 23:27:12 -0800816 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
817 void PreSetup(const std::string& elf_filename) OVERRIDE;
818
819 private:
820 bool Dlopen(const std::string& elf_filename,
821 uint8_t* oat_file_begin,
822 std::string* error_msg);
823
Richard Uhlera206c742016-05-24 15:04:22 -0700824 // On the host, if the same library is loaded again with dlopen the same
825 // file handle is returned. This differs from the behavior of dlopen on the
826 // target, where dlopen reloads the library at a different address every
827 // time you load it. The runtime relies on the target behavior to ensure
828 // each instance of the loaded library has a unique dex cache. To avoid
829 // problems, we fall back to our own linker in the case when the same
830 // library is opened multiple times on host. dlopen_handles_ is used to
831 // detect that case.
832 // Guarded by host_dlopen_handles_lock_;
833 static std::unordered_set<void*> host_dlopen_handles_;
834
Andreas Gampe049cff02015-12-01 23:27:12 -0800835 // dlopen handle during runtime.
836 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
837
838 // Dummy memory map objects corresponding to the regions mapped by dlopen.
839 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
840
Andreas Gampe4075f832016-05-18 13:09:54 -0700841 // The number of shared objects the linker told us about before loading. Used to
842 // (optimistically) optimize the PreSetup stage (see comment there).
843 size_t shared_objects_before_;
844
Andreas Gampe049cff02015-12-01 23:27:12 -0800845 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
846};
847
Richard Uhlera206c742016-05-24 15:04:22 -0700848std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
849
Andreas Gampe4075f832016-05-18 13:09:54 -0700850void DlOpenOatFile::PreLoad() {
851#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700852 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700853 LOG(FATAL) << "Should not reach here.";
854 UNREACHABLE();
855#else
856 // Count the entries in dl_iterate_phdr we get at this point in time.
857 struct dl_iterate_context {
858 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
859 size_t size ATTRIBUTE_UNUSED,
860 void *data) {
861 reinterpret_cast<dl_iterate_context*>(data)->count++;
862 return 0; // Continue iteration.
863 }
864 size_t count = 0;
865 } context;
866
867 dl_iterate_phdr(dl_iterate_context::callback, &context);
868 shared_objects_before_ = context.count;
869#endif
870}
871
Andreas Gampe049cff02015-12-01 23:27:12 -0800872bool DlOpenOatFile::Load(const std::string& elf_filename,
873 uint8_t* oat_file_begin,
874 bool writable,
875 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800876 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800877 std::string* error_msg) {
878 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
879 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
880 // !executable is a sign that we may want to patch), which may not be allowed for
881 // various reasons.
882 if (!kUseDlopen) {
883 *error_msg = "DlOpen is disabled.";
884 return false;
885 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800886 if (low_4gb) {
887 *error_msg = "DlOpen does not support low 4gb loading.";
888 return false;
889 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800890 if (writable) {
891 *error_msg = "DlOpen does not support writable loading.";
892 return false;
893 }
894 if (!executable) {
895 *error_msg = "DlOpen does not support non-executable loading.";
896 return false;
897 }
898
899 // dlopen always returns the same library if it is already opened on the host. For this reason
900 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
901 // the same library loaded multiple times at different addresses is required for class unloading
902 // and for having dex caches arrays in the .bss section.
903 if (!kIsTargetBuild) {
904 if (!kUseDlopenOnHost) {
905 *error_msg = "DlOpen disabled for host.";
906 return false;
907 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800908 }
909
910 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
911 DCHECK(dlopen_handle_ != nullptr || !success);
912
913 return success;
914}
915
916bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
917 uint8_t* oat_file_begin,
918 std::string* error_msg) {
919#ifdef __APPLE__
920 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
921 // but let's fallback to the custom loading code for the time being.
922 UNUSED(elf_filename, oat_file_begin);
923 *error_msg = "Dlopen unsupported on Mac.";
924 return false;
925#else
926 {
927 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
928 if (absolute_path == nullptr) {
929 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
930 return false;
931 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100932#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +0000933 android_dlextinfo extinfo = {};
Andreas Gampe049cff02015-12-01 23:27:12 -0800934 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
935 // (open oat files multiple
936 // times).
937 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
938 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800939 if (oat_file_begin != nullptr) { //
940 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
941 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
942 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800943 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
944#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800945 UNUSED(oat_file_begin);
Julien Duraj1af0c4f2016-11-16 14:05:48 +0000946 static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700947 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700948 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
949 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700950 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
951 dlclose(dlopen_handle_);
952 dlopen_handle_ = nullptr;
953 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
954 return false;
955 }
956 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100957#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800958 }
959 if (dlopen_handle_ == nullptr) {
960 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
961 return false;
962 }
963 return true;
964#endif
965}
966
967void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800968#ifdef __APPLE__
969 UNUSED(elf_filename);
970 LOG(FATAL) << "Should not reach here.";
971 UNREACHABLE();
972#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800973 struct dl_iterate_context {
974 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
975 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700976 context->shared_objects_seen++;
977 if (context->shared_objects_seen < context->shared_objects_before) {
978 // We haven't been called yet for anything we haven't seen before. Just continue.
979 // Note: this is aggressively optimistic. If another thread was unloading a library,
980 // we may miss out here. However, this does not happen often in practice.
981 return 0;
982 }
983
Andreas Gampe049cff02015-12-01 23:27:12 -0800984 // See whether this callback corresponds to the file which we have just loaded.
985 bool contains_begin = false;
986 for (int i = 0; i < info->dlpi_phnum; i++) {
987 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
988 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
989 info->dlpi_phdr[i].p_vaddr);
990 size_t memsz = info->dlpi_phdr[i].p_memsz;
991 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
992 contains_begin = true;
993 break;
994 }
995 }
996 }
997 // Add dummy mmaps for this file.
998 if (contains_begin) {
999 for (int i = 0; i < info->dlpi_phnum; i++) {
1000 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1001 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1002 info->dlpi_phdr[i].p_vaddr);
1003 size_t memsz = info->dlpi_phdr[i].p_memsz;
1004 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
1005 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
1006 }
1007 }
1008 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1009 }
1010 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1011 }
1012 const uint8_t* const begin_;
1013 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -07001014 const size_t shared_objects_before;
1015 size_t shared_objects_seen;
1016 };
1017 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -08001018
1019 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -07001020 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
1021 // before giving up. This should be unusual.
1022 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
1023 << shared_objects_before_;
1024 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
1025 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
1026 // OK, give up and print an error.
1027 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
1028 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
1029 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001030 }
Andreas Gampe74f07b52015-12-02 11:53:26 -08001031#endif
Andreas Gampe049cff02015-12-01 23:27:12 -08001032}
1033
1034////////////////////////////////////////////////
1035// OatFile via our own ElfFile implementation //
1036////////////////////////////////////////////////
1037
1038class ElfOatFile FINAL : public OatFileBase {
1039 public:
1040 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
1041
1042 static ElfOatFile* OpenElfFile(File* file,
1043 const std::string& location,
1044 uint8_t* requested_base,
1045 uint8_t* oat_file_begin, // Override base if not null
1046 bool writable,
1047 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001048 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001049 const char* abs_dex_location,
1050 std::string* error_msg);
1051
1052 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001053 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001054 const char* abs_dex_location,
1055 std::string* error_msg);
1056
1057 protected:
1058 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
1059 std::string* error_msg) const OVERRIDE {
1060 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
1061 if (ptr == nullptr) {
1062 *error_msg = "(Internal implementation could not find symbol)";
1063 }
1064 return ptr;
1065 }
1066
Andreas Gampe4075f832016-05-18 13:09:54 -07001067 void PreLoad() OVERRIDE {
1068 }
1069
Andreas Gampe049cff02015-12-01 23:27:12 -08001070 bool Load(const std::string& elf_filename,
1071 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1072 bool writable,
1073 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001074 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001075 std::string* error_msg) OVERRIDE;
1076
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001077 bool Load(int oat_fd,
1078 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1079 bool writable,
1080 bool executable,
1081 bool low_4gb,
1082 std::string* error_msg) OVERRIDE;
1083
Andreas Gampe049cff02015-12-01 23:27:12 -08001084 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
1085 }
1086
1087 private:
1088 bool ElfFileOpen(File* file,
1089 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1090 bool writable,
1091 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001092 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001093 std::string* error_msg);
1094
1095 private:
1096 // Backing memory map for oat file during cross compilation.
1097 std::unique_ptr<ElfFile> elf_file_;
1098
1099 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
1100};
1101
1102ElfOatFile* ElfOatFile::OpenElfFile(File* file,
1103 const std::string& location,
1104 uint8_t* requested_base,
1105 uint8_t* oat_file_begin, // Override base if not null
1106 bool writable,
1107 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001108 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001109 const char* abs_dex_location,
1110 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001111 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001112 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001113 bool success = oat_file->ElfFileOpen(file,
1114 oat_file_begin,
1115 writable,
1116 low_4gb,
1117 executable,
1118 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001119 if (!success) {
1120 CHECK(!error_msg->empty());
1121 return nullptr;
1122 }
1123
1124 // Complete the setup.
1125 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
1126 return nullptr;
1127 }
1128
1129 if (!oat_file->Setup(abs_dex_location, error_msg)) {
1130 return nullptr;
1131 }
1132
1133 return oat_file.release();
1134}
1135
1136bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001137 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001138 const char* abs_dex_location,
1139 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001140 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001141 if (IsExecutable()) {
1142 *error_msg = "Cannot initialize from elf file in executable mode.";
1143 return false;
1144 }
1145 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001146 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001147 uint64_t offset, size;
1148 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1149 CHECK(has_section);
1150 SetBegin(elf_file->Begin() + offset);
1151 SetEnd(elf_file->Begin() + size + offset);
1152 // Ignore the optional .bss section when opening non-executable.
1153 return Setup(abs_dex_location, error_msg);
1154}
1155
1156bool ElfOatFile::Load(const std::string& elf_filename,
1157 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1158 bool writable,
1159 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001160 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001161 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001162 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001163 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1164 if (file == nullptr) {
1165 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1166 return false;
1167 }
1168 return ElfOatFile::ElfFileOpen(file.get(),
1169 oat_file_begin,
1170 writable,
1171 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001172 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001173 error_msg);
1174}
1175
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001176bool ElfOatFile::Load(int oat_fd,
1177 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1178 bool writable,
1179 bool executable,
1180 bool low_4gb,
1181 std::string* error_msg) {
1182 ScopedTrace trace(__PRETTY_FUNCTION__);
1183 if (oat_fd != -1) {
1184 std::unique_ptr<File> file = std::make_unique<File>(oat_fd, false);
1185 file->DisableAutoClose();
1186 if (file == nullptr) {
1187 *error_msg = StringPrintf("Failed to open oat filename for reading: %s",
1188 strerror(errno));
1189 return false;
1190 }
1191 return ElfOatFile::ElfFileOpen(file.get(),
1192 oat_file_begin,
1193 writable,
1194 executable,
1195 low_4gb,
1196 error_msg);
1197 }
1198 return false;
1199}
1200
Andreas Gampe049cff02015-12-01 23:27:12 -08001201bool ElfOatFile::ElfFileOpen(File* file,
1202 uint8_t* oat_file_begin,
1203 bool writable,
1204 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001205 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001206 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001207 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001208 // TODO: rename requested_base to oat_data_begin
1209 elf_file_.reset(ElfFile::Open(file,
1210 writable,
1211 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001212 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001213 error_msg,
1214 oat_file_begin));
1215 if (elf_file_ == nullptr) {
1216 DCHECK(!error_msg->empty());
1217 return false;
1218 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001219 bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001220 DCHECK(loaded || !error_msg->empty());
1221 return loaded;
1222}
1223
1224//////////////////////////
1225// General OatFile code //
1226//////////////////////////
1227
1228std::string OatFile::ResolveRelativeEncodedDexLocation(
1229 const char* abs_dex_location, const std::string& rel_dex_location) {
1230 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
1231 // Strip :classes<N>.dex used for secondary multidex files.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001232 std::string base = DexFileLoader::GetBaseLocation(rel_dex_location);
1233 std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(rel_dex_location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001234
1235 // Check if the base is a suffix of the provided abs_dex_location.
1236 std::string target_suffix = "/" + base;
1237 std::string abs_location(abs_dex_location);
1238 if (abs_location.size() > target_suffix.size()) {
1239 size_t pos = abs_location.size() - target_suffix.size();
1240 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1241 return abs_location + multidex_suffix;
1242 }
1243 }
1244 }
1245 return rel_dex_location;
1246}
1247
1248static void CheckLocation(const std::string& location) {
1249 CHECK(!location.empty());
1250}
1251
1252OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001253 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001254 const std::string& location,
1255 const char* abs_dex_location,
1256 std::string* error_msg) {
1257 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001258 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001259 ? oat_file.release()
1260 : nullptr;
1261}
1262
David Brazdil7b49e6c2016-09-01 11:06:18 +01001263OatFile* OatFile::Open(const std::string& oat_filename,
1264 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001265 uint8_t* requested_base,
1266 uint8_t* oat_file_begin,
1267 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001268 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001269 const char* abs_dex_location,
1270 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001271 ScopedTrace trace("Open oat file " + oat_location);
1272 CHECK(!oat_filename.empty()) << oat_location;
1273 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001274
Calin Juravle367b9d82017-05-15 18:18:39 -07001275 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001276
1277 // Check that the files even exist, fast-fail.
1278 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
1279 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1280 return nullptr;
1281 } else if (!OS::FileExists(oat_filename.c_str())) {
1282 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001283 return nullptr;
1284 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001285
1286 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1287 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001288 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1289 oat_filename,
1290 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001291 requested_base,
1292 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001293 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001294 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001295 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001296 abs_dex_location,
1297 error_msg);
1298 if (with_dlopen != nullptr) {
1299 return with_dlopen;
1300 }
1301 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001302 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001303 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001304 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1305 //
1306 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1307 //
1308 // We use our own ELF loader for Quick to deal with legacy apps that
1309 // open a generated dex file by name, remove the file, then open
1310 // another generated dex file with the same name. http://b/10614658
1311 //
1312 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1313 //
1314 //
1315 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1316 // 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 +01001317 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1318 oat_filename,
1319 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001320 requested_base,
1321 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001322 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001323 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001324 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001325 abs_dex_location,
1326 error_msg);
1327 return with_internal;
1328}
1329
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001330OatFile* OatFile::Open(int vdex_fd,
1331 int oat_fd,
1332 const std::string& oat_location,
1333 uint8_t* requested_base,
1334 uint8_t* oat_file_begin,
1335 bool executable,
1336 bool low_4gb,
1337 const char* abs_dex_location,
1338 std::string* error_msg) {
1339 CHECK(!oat_location.empty()) << oat_location;
1340
1341 std::string vdex_location = GetVdexFilename(oat_location);
1342
1343 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_fd,
1344 oat_fd,
1345 vdex_location,
1346 oat_location,
1347 requested_base,
1348 oat_file_begin,
1349 false /* writable */,
1350 executable,
1351 low_4gb,
1352 abs_dex_location,
1353 error_msg);
1354 return with_internal;
1355}
1356
Andreas Gampe049cff02015-12-01 23:27:12 -08001357OatFile* OatFile::OpenWritable(File* file,
1358 const std::string& location,
1359 const char* abs_dex_location,
1360 std::string* error_msg) {
1361 CheckLocation(location);
1362 return ElfOatFile::OpenElfFile(file,
1363 location,
1364 nullptr,
1365 nullptr,
1366 true,
1367 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001368 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001369 abs_dex_location,
1370 error_msg);
1371}
1372
1373OatFile* OatFile::OpenReadable(File* file,
1374 const std::string& location,
1375 const char* abs_dex_location,
1376 std::string* error_msg) {
1377 CheckLocation(location);
1378 return ElfOatFile::OpenElfFile(file,
1379 location,
1380 nullptr,
1381 nullptr,
1382 false,
1383 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001384 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001385 abs_dex_location,
1386 error_msg);
1387}
1388
1389OatFile::OatFile(const std::string& location, bool is_executable)
1390 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001391 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001392 begin_(nullptr),
1393 end_(nullptr),
1394 bss_begin_(nullptr),
1395 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001396 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001397 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001398 is_executable_(is_executable),
1399 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1400 CHECK(!location_.empty());
1401}
1402
1403OatFile::~OatFile() {
1404 STLDeleteElements(&oat_dex_files_storage_);
1405}
1406
Brian Carlstrome24fa612011-09-29 00:53:55 -07001407const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001408 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001409}
1410
Ian Rogers13735952014-10-08 12:43:28 -07001411const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001412 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001413 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001414}
1415
Ian Rogers13735952014-10-08 12:43:28 -07001416const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001417 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001418 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001419}
1420
Vladimir Marko5c42c292015-02-25 12:02:49 +00001421const uint8_t* OatFile::BssBegin() const {
1422 return bss_begin_;
1423}
1424
1425const uint8_t* OatFile::BssEnd() const {
1426 return bss_end_;
1427}
1428
David Brazdil7b49e6c2016-09-01 11:06:18 +01001429const uint8_t* OatFile::DexBegin() const {
1430 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1431}
1432
1433const uint8_t* OatFile::DexEnd() const {
1434 return kIsVdexEnabled ? vdex_->End() : End();
1435}
1436
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001437ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1438 if (bss_methods_ != nullptr) {
1439 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1440 ArtMethod** methods_end =
1441 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1442 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1443 } else {
1444 return ArrayRef<ArtMethod*>();
1445 }
1446}
1447
Vladimir Markoaad75c62016-10-03 08:46:48 +00001448ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1449 if (bss_roots_ != nullptr) {
1450 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1451 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1452 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1453 } else {
1454 return ArrayRef<GcRoot<mirror::Object>>();
1455 }
1456}
1457
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001458const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1459 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001460 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001461 // NOTE: We assume here that the canonical location for a given dex_location never
1462 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1463 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1464 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001465
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001466 // TODO: Additional analysis of usage patterns to see if this can be simplified
1467 // without any performance loss, for example by not doing the first lock-free lookup.
1468
1469 const OatFile::OatDexFile* oat_dex_file = nullptr;
1470 StringPiece key(dex_location);
1471 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1472 // directly mentioned in the oat file and doesn't require locking.
1473 auto primary_it = oat_dex_files_.find(key);
1474 if (primary_it != oat_dex_files_.end()) {
1475 oat_dex_file = primary_it->second;
1476 DCHECK(oat_dex_file != nullptr);
1477 } else {
1478 // This dex_location is not one of the dex locations directly mentioned in the
1479 // oat file. The correct lookup is via the canonical location but first see in
1480 // the secondary_oat_dex_files_ whether we've looked up this location before.
1481 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1482 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1483 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001484 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001485 } else {
1486 // We haven't seen this dex_location before, we must check the canonical location.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001487 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001488 if (dex_canonical_location != dex_location) {
1489 StringPiece canonical_key(dex_canonical_location);
1490 auto canonical_it = oat_dex_files_.find(canonical_key);
1491 if (canonical_it != oat_dex_files_.end()) {
1492 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001493 } // else keep null.
1494 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001495
1496 // Copy the key to the string_cache_ and store the result in secondary map.
1497 string_cache_.emplace_back(key.data(), key.length());
1498 StringPiece key_copy(string_cache_.back());
1499 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001500 }
1501 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001502
1503 if (oat_dex_file == nullptr) {
1504 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001505 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001506 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1507 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1508 }
1509 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001510 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001511
Richard Uhler9a37efc2016-08-05 16:32:55 -07001512 if (dex_location_checksum != nullptr &&
1513 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1514 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001515 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001516 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1517 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1518 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1519 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1520 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001521 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001522 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001523 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001524 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001525}
1526
Brian Carlstrome24fa612011-09-29 00:53:55 -07001527OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001528 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001529 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001530 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001531 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001532 const uint8_t* lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001533 const MethodBssMapping* method_bss_mapping_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001534 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001535 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001536 : oat_file_(oat_file),
1537 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001538 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001539 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001540 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001541 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001542 method_bss_mapping_(method_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001543 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001544 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001545 // Initialize TypeLookupTable.
1546 if (lookup_table_data_ != nullptr) {
1547 // Peek the number of classes from the DexFile.
1548 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1549 const uint32_t num_class_defs = dex_header->class_defs_size_;
1550 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1551 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1552 } else {
Mathieu Chartier1b868492016-11-16 16:22:37 -08001553 lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001554 }
1555 }
1556}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001557
Mathieu Chartier1b868492016-11-16 16:22:37 -08001558OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table)
1559 : lookup_table_(std::move(lookup_table)) {}
1560
Brian Carlstrome24fa612011-09-29 00:53:55 -07001561OatFile::OatDexFile::~OatDexFile() {}
1562
Ian Rogers05f28c62012-10-23 18:12:13 -07001563size_t OatFile::OatDexFile::FileSize() const {
1564 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1565}
1566
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001567std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001568 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001569 static constexpr bool kVerify = false;
1570 static constexpr bool kVerifyChecksum = false;
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001571 return DexFileLoader::Open(dex_file_pointer_,
1572 FileSize(),
1573 dex_file_location_,
1574 dex_file_location_checksum_,
1575 this,
1576 kVerify,
1577 kVerifyChecksum,
1578 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001579}
1580
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001581uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1582 return oat_class_offsets_pointer_[class_def_index];
1583}
1584
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001585OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001586 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001587
Ian Rogers13735952014-10-08 12:43:28 -07001588 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001589 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001590
Ian Rogers13735952014-10-08 12:43:28 -07001591 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001592 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1593 mirror::Class::Status status =
1594 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1595 CHECK_LT(status, mirror::Class::kStatusMax);
1596
Ian Rogers13735952014-10-08 12:43:28 -07001597 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001598 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1599 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1600 CHECK_LT(type, kOatClassMax);
1601
Ian Rogers13735952014-10-08 12:43:28 -07001602 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001603 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001604
Brian Carlstromcd937a92014-03-04 22:53:23 -08001605 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001606 const uint8_t* bitmap_pointer = nullptr;
1607 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001608 if (type != kOatClassNoneCompiled) {
1609 if (type == kOatClassSomeCompiled) {
1610 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1611 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1612 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1613 methods_pointer = bitmap_pointer + bitmap_size;
1614 } else {
1615 methods_pointer = after_type_pointer;
1616 }
1617 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001618 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001619
Richard Uhler07b3c232015-03-31 15:57:54 -07001620 return OatFile::OatClass(oat_file_,
1621 status,
1622 type,
1623 bitmap_size,
1624 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1625 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001626}
1627
David Sehr9aa352e2016-09-15 18:13:52 -07001628const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1629 const char* descriptor,
1630 size_t hash) {
1631 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1632 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1633 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1634 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
Andreas Gampee2abbc62017-09-15 11:59:26 -07001635 return (class_def_idx != dex::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
David Sehr9aa352e2016-09-15 18:13:52 -07001636 }
1637 // Fast path for rare no class defs case.
1638 const uint32_t num_class_defs = dex_file.NumClassDefs();
1639 if (num_class_defs == 0) {
1640 return nullptr;
1641 }
1642 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1643 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001644 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
David Sehr9aa352e2016-09-15 18:13:52 -07001645 return dex_file.FindClassDef(type_idx);
1646 }
1647 return nullptr;
1648}
1649
Mathieu Chartier120aa282017-08-05 16:03:03 -07001650// Madvise the dex file based on the state we are moving to.
1651void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001652 Runtime* const runtime = Runtime::Current();
1653 const bool low_ram = runtime->GetHeap()->IsLowMemoryMode();
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001654 // TODO: Also do madvise hints for non low ram devices.
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001655 if (!low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07001656 return;
1657 }
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001658 if (state == MadviseState::kMadviseStateAtLoad && runtime->MAdviseRandomAccess()) {
1659 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
1660 // Other devices have enough page cache to get performance benefits from loading more pages
1661 // into the page cache.
1662 MadviseLargestPageAlignedRegion(dex_file.Begin(),
1663 dex_file.Begin() + dex_file.Size(),
1664 MADV_RANDOM);
Mathieu Chartier120aa282017-08-05 16:03:03 -07001665 }
1666 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1667 if (oat_dex_file != nullptr) {
1668 // Should always be there.
1669 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
1670 CHECK(sections != nullptr);
1671 sections->Madvise(&dex_file, state);
1672 }
1673}
1674
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001675OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001676 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001677 OatClassType type,
1678 uint32_t bitmap_size,
1679 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001680 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001681 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001682 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001683 switch (type_) {
1684 case kOatClassAllCompiled: {
1685 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001686 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001687 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001688 break;
1689 }
1690 case kOatClassSomeCompiled: {
1691 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001692 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001693 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001694 break;
1695 }
1696 case kOatClassNoneCompiled: {
1697 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001698 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001699 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001700 break;
1701 }
1702 case kOatClassMax: {
1703 LOG(FATAL) << "Invalid OatClassType " << type_;
1704 break;
1705 }
1706 }
1707}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001708
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001709uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1710 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1711 if (oat_method_offsets == nullptr) {
1712 return 0u;
1713 }
1714 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1715}
1716
1717const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001718 // 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 -07001719 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001720 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001721 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001722 }
1723 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001724 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001725 CHECK_EQ(kOatClassAllCompiled, type_);
1726 methods_pointer_index = method_index;
1727 } else {
1728 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001729 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001730 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001731 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001732 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1733 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001734 }
1735 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001736 return &oat_method_offsets;
1737}
1738
1739const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1740 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1741 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001742 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001743 }
1744 if (oat_file_->IsExecutable() ||
1745 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001746 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001747 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001748 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001749 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1750 // version.
1751 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001752}
1753
Mathieu Chartiere401d142015-04-22 13:56:20 -07001754void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001755 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001756 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001757}
1758
Andreas Gampe7ba64962014-10-23 11:37:40 -07001759bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001760 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001761 // TODO: Check against oat_patches. b/18144996
1762}
1763
Sebastien Hertz0de11332015-05-13 12:14:05 +02001764bool OatFile::IsDebuggable() const {
1765 return GetOatHeader().IsDebuggable();
1766}
1767
Andreas Gampe29d38e72016-03-23 15:31:51 +00001768CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1769 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001770}
1771
Calin Juravle44e5efa2017-09-12 00:54:26 -07001772std::string OatFile::GetClassLoaderContext() const {
1773 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1774};
1775
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001776OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1777 uint16_t class_def_idx,
1778 bool* found) {
1779 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
1780 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001781 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001782 *found = false;
1783 return OatFile::OatClass::Invalid();
1784 }
1785 *found = true;
1786 return oat_dex_file->GetOatClass(class_def_idx);
1787}
1788
Mathieu Chartier1b868492016-11-16 16:22:37 -08001789void OatFile::OatDexFile::AssertAotCompiler() {
1790 CHECK(Runtime::Current()->IsAotCompiler());
1791}
1792
Brian Carlstrome24fa612011-09-29 00:53:55 -07001793} // namespace art