blob: 5f37b82a98aea3bad8cb5e4bdeb8ac592dee9692 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_file.h"
18
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019#include <dlfcn.h>
Calin Juravle4e1d5792014-07-15 23:56:47 +010020#include <string.h>
Vladimir Marko06d7aaa2015-10-16 11:23:41 +010021#include <type_traits>
Andreas Gampedaab38c2014-09-12 18:38:24 -070022#include <unistd.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080023
Andreas Gampe7848da42015-04-09 11:15:04 -070024#include <cstdlib>
David Srbecky1baabf02015-06-16 17:12:34 +000025#ifndef __APPLE__
26#include <link.h> // for dl_iterate_phdr.
27#endif
Richard Uhlere5fed032015-03-18 08:21:11 -070028#include <sstream>
29
Andreas Gampefa8429b2015-04-07 18:34:42 -070030// dlopen_ext support from bionic.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010031#ifdef ART_TARGET_ANDROID
Andreas Gampefa8429b2015-04-07 18:34:42 -070032#include "android/dlext.h"
33#endif
34
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "art_method-inl.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070036#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070037#include "base/enums.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080038#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080039#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080040#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070042#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000043#include "gc_root.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000045#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070047#include "mirror/object-inl.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010048#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070049#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070050#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070051#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000052#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070053#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010055#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070056
57namespace art {
58
Andreas Gampe049cff02015-12-01 23:27:12 -080059// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000060static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070061
Andreas Gampe049cff02015-12-01 23:27:12 -080062// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070063// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
64// multiple times). However, if/when we switch the above, we likely want to switch this, too,
65// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000066static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070067
68// For debugging, Open will print DlOpen error message if set to true.
69static constexpr bool kPrintDlOpenErrorMessage = false;
70
Andreas Gampe049cff02015-12-01 23:27:12 -080071// Note for OatFileBase and descendents:
72//
73// These are used in OatFile::Open to try all our loaders.
74//
75// The process is simple:
76//
77// 1) Allocate an instance through the standard constructor (location, executable)
78// 2) Load() to try to open the file.
79// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
80// 4) PreSetup() for any steps that should be done before the final setup.
81// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070082
Andreas Gampe049cff02015-12-01 23:27:12 -080083class OatFileBase : public OatFile {
84 public:
85 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070086
Andreas Gampe049cff02015-12-01 23:27:12 -080087 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010088 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
89 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070090 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080091 uint8_t* requested_base,
92 uint8_t* oat_file_begin,
93 bool writable,
94 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080095 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070096 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080097 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080098
Andreas Gampe049cff02015-12-01 23:27:12 -080099 protected:
100 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700101
Andreas Gampe049cff02015-12-01 23:27:12 -0800102 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
103 std::string* error_msg) const = 0;
104
Andreas Gampe4075f832016-05-18 13:09:54 -0700105 virtual void PreLoad() = 0;
106
David Brazdil7b49e6c2016-09-01 11:06:18 +0100107 bool LoadVdex(const std::string& vdex_filename,
108 bool writable,
109 bool low_4gb,
110 std::string* error_msg);
111
Andreas Gampe049cff02015-12-01 23:27:12 -0800112 virtual bool Load(const std::string& elf_filename,
113 uint8_t* oat_file_begin,
114 bool writable,
115 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800116 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800117 std::string* error_msg) = 0;
118
119 bool ComputeFields(uint8_t* requested_base,
120 const std::string& file_path,
121 std::string* error_msg);
122
123 virtual void PreSetup(const std::string& elf_filename) = 0;
124
125 bool Setup(const char* abs_dex_location, std::string* error_msg);
126
127 // Setters exposed for ElfOatFile.
128
129 void SetBegin(const uint8_t* begin) {
130 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700131 }
132
Andreas Gampe049cff02015-12-01 23:27:12 -0800133 void SetEnd(const uint8_t* end) {
134 end_ = end;
135 }
136
David Brazdilc93b3be2016-09-12 18:49:58 +0100137 void SetVdex(VdexFile* vdex) {
138 vdex_.reset(vdex);
139 }
140
Andreas Gampe049cff02015-12-01 23:27:12 -0800141 private:
142 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
143};
144
145template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100146OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
147 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800148 const std::string& location,
149 uint8_t* requested_base,
150 uint8_t* oat_file_begin,
151 bool writable,
152 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800153 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800154 const char* abs_dex_location,
155 std::string* error_msg) {
156 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700157
158 ret->PreLoad();
159
David Brazdil7b49e6c2016-09-01 11:06:18 +0100160 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
161 return nullptr;
162 }
163
Andreas Gampe049cff02015-12-01 23:27:12 -0800164 if (!ret->Load(elf_filename,
165 oat_file_begin,
166 writable,
167 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800168 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800169 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800170 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800171 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800172
Andreas Gampe049cff02015-12-01 23:27:12 -0800173 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
174 return nullptr;
175 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700176
Andreas Gampe049cff02015-12-01 23:27:12 -0800177 ret->PreSetup(elf_filename);
178
179 if (!ret->Setup(abs_dex_location, error_msg)) {
180 return nullptr;
181 }
182
Dave Allison69dfe512014-07-11 17:11:58 +0000183 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800184}
185
David Brazdil7b49e6c2016-09-01 11:06:18 +0100186bool OatFileBase::LoadVdex(const std::string& vdex_filename,
187 bool writable,
188 bool low_4gb,
189 std::string* error_msg) {
190 vdex_.reset(VdexFile::Open(vdex_filename, writable, low_4gb, error_msg));
191 if (vdex_.get() == nullptr) {
192 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
193 vdex_filename.c_str(),
194 error_msg->c_str());
195 return false;
196 }
197 return true;
198}
199
Andreas Gampe049cff02015-12-01 23:27:12 -0800200bool OatFileBase::ComputeFields(uint8_t* requested_base,
201 const std::string& file_path,
202 std::string* error_msg) {
203 std::string symbol_error_msg;
204 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700205 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800206 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
207 file_path.c_str(),
208 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700209 return false;
210 }
211 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000212 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700213 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000214 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
215 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700216 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800217 "oatdata=%p != expected=%p. See process maps in the log.",
218 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700219 return false;
220 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800221 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700222 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800223 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
224 file_path.c_str(),
225 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700226 return false;
227 }
228 // Readjust to be non-inclusive upper bound.
229 end_ += sizeof(uint32_t);
230
Andreas Gampe049cff02015-12-01 23:27:12 -0800231 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700232 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800233 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700234 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700235 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800236 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700237 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800238 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700239 return false;
240 }
241 // Readjust to be non-inclusive upper bound.
242 bss_end_ += sizeof(uint32_t);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000243 // Find bss roots if present.
244 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700245 }
246
Andreas Gampe049cff02015-12-01 23:27:12 -0800247 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800248}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800249
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100250// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
251// position by the number of bytes read, i.e. sizeof(T).
252// Return true on success, false if the read would go beyond the end of the OatFile.
253template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100254inline static bool ReadOatDexFileData(const OatFile& oat_file,
255 /*inout*/const uint8_t** oat,
256 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100257 DCHECK(oat != nullptr);
258 DCHECK(value != nullptr);
259 DCHECK_LE(*oat, oat_file.End());
260 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
261 return false;
262 }
263 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
264 typedef __attribute__((__aligned__(1))) T unaligned_type;
265 *value = *reinterpret_cast<const unaligned_type*>(*oat);
266 *oat += sizeof(T);
267 return true;
268}
269
Andreas Gampe049cff02015-12-01 23:27:12 -0800270bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700271 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800272 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100273 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
274 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800275 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700276 return false;
277 }
Ian Rogers13735952014-10-08 12:43:28 -0700278 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700280 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700282 return false;
283 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700284
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700285 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700286 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700287 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100288 "%p + %zu + %u <= %p",
289 GetLocation().c_str(),
290 Begin(),
291 sizeof(OatHeader),
292 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700293 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700294 return false;
295 }
296
Vladimir Markoaad75c62016-10-03 08:46:48 +0000297 if (!IsAligned<alignof(GcRoot<mirror::Object>)>(bss_begin_) ||
298 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_roots_) ||
299 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
300 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
301 "begin = %p, roots = %p, end = %p",
302 GetLocation().c_str(),
303 bss_begin_,
304 bss_roots_,
305 bss_end_);
306 return false;
307 }
308
309 if (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) {
310 *error_msg = StringPrintf("In oat file '%s' found bss roots outside .bss: "
311 "%p is outside range [%p, %p]",
312 GetLocation().c_str(),
313 bss_roots_,
314 bss_begin_,
315 bss_end_);
316 return false;
317 }
318
Andreas Gampe542451c2016-07-26 09:02:02 -0700319 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100320 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000321 uint8_t* dex_cache_arrays_end = (bss_roots_ != nullptr) ? bss_roots_ : bss_end_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100322 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
323 oat_dex_files_storage_.reserve(dex_file_count);
324 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100325 uint32_t dex_file_location_size;
326 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
327 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
328 "location size",
329 GetLocation().c_str(),
330 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700331 return false;
332 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100333 if (UNLIKELY(dex_file_location_size == 0U)) {
334 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
335 GetLocation().c_str(),
336 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700337 return false;
338 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000339 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100340 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
341 "location",
342 GetLocation().c_str(),
343 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700344 return false;
345 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000346 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
347 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700348
Richard Uhlere5fed032015-03-18 08:21:11 -0700349 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
350 abs_dex_location,
351 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100353 uint32_t dex_file_checksum;
354 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
355 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
356 "dex file checksum",
357 GetLocation().c_str(),
358 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700359 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700360 return false;
361 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700362
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100363 uint32_t dex_file_offset;
364 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
365 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
366 "after dex file offsets",
367 GetLocation().c_str(),
368 i,
369 dex_file_location.c_str());
370 return false;
371 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700372 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100373 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
374 "file offset",
375 GetLocation().c_str(),
376 i,
377 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700378 return false;
379 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100380 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100381 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
382 "offset %u > %zu",
383 GetLocation().c_str(),
384 i,
385 dex_file_location.c_str(),
386 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100387 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700388 return false;
389 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100390 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000391 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
392 "offset %u of %zu but the size of dex file header is %zu",
393 GetLocation().c_str(),
394 i,
395 dex_file_location.c_str(),
396 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100397 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000398 sizeof(DexFile::Header));
399 return false;
400 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800401
David Brazdil7b49e6c2016-09-01 11:06:18 +0100402 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700403 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100404 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
405 "dex file magic '%s'",
406 GetLocation().c_str(),
407 i,
408 dex_file_location.c_str(),
409 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700410 return false;
411 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700412 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100413 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
414 "dex file version '%s'",
415 GetLocation().c_str(),
416 i,
417 dex_file_location.c_str(),
418 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700419 return false;
420 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800421 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100422 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000423 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
424 "offset %u and size %u truncated at %zu",
425 GetLocation().c_str(),
426 i,
427 dex_file_location.c_str(),
428 dex_file_offset,
429 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100430 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000431 return false;
432 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300433
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000434 uint32_t class_offsets_offset;
435 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
436 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
437 "after class offsets offset",
438 GetLocation().c_str(),
439 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300440 dex_file_location.c_str());
441 return false;
442 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000443 if (UNLIKELY(class_offsets_offset > Size()) ||
444 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
445 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
446 "class offsets, offset %u of %zu, class defs %u",
447 GetLocation().c_str(),
448 i,
449 dex_file_location.c_str(),
450 class_offsets_offset,
451 Size(),
452 header->class_defs_size_);
453 return false;
454 }
455 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
456 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
457 "class offsets, offset %u",
458 GetLocation().c_str(),
459 i,
460 dex_file_location.c_str(),
461 class_offsets_offset);
462 return false;
463 }
464 const uint32_t* class_offsets_pointer =
465 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
466
467 uint32_t lookup_table_offset;
468 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
469 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
470 "after lookup table offset",
471 GetLocation().c_str(),
472 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300473 dex_file_location.c_str());
474 return false;
475 }
476 const uint8_t* lookup_table_data = lookup_table_offset != 0u
477 ? Begin() + lookup_table_offset
478 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000479 if (lookup_table_offset != 0u &&
480 (UNLIKELY(lookup_table_offset > Size()) ||
481 UNLIKELY(Size() - lookup_table_offset <
482 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100483 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000484 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100485 GetLocation().c_str(),
486 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000487 dex_file_location.c_str(),
488 lookup_table_offset,
489 Size(),
490 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700491 return false;
492 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700493
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100494 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100495 if (dex_cache_arrays != nullptr) {
496 DexCacheArraysLayout layout(pointer_size, *header);
497 if (layout.Size() != 0u) {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000498 if (static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100499 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
500 "truncated dex cache arrays, %zu < %zu.",
501 GetLocation().c_str(),
502 i,
503 dex_file_location.c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000504 static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100505 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100506 return false;
507 }
508 current_dex_cache_arrays = dex_cache_arrays;
509 dex_cache_arrays += layout.Size();
510 }
511 }
512
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100513 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
514
515 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100516 OatDexFile* oat_dex_file = new OatDexFile(this,
517 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100518 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100519 dex_file_checksum,
520 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300521 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000522 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100523 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100524 oat_dex_files_storage_.push_back(oat_dex_file);
525
526 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100527 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100528 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100529 if (canonical_location != dex_file_location) {
530 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
531 oat_dex_files_.Put(canonical_key, oat_dex_file);
532 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700533 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100534
Vladimir Markoaad75c62016-10-03 08:46:48 +0000535 if (dex_cache_arrays != dex_cache_arrays_end) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100536 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
Vladimir Markoaad75c62016-10-03 08:46:48 +0000537 // both null) or contain just the dex cache arrays and optionally some GC roots.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100538 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100539 GetLocation().c_str(),
540 static_cast<size_t>(bss_end_ - dex_cache_arrays));
541 return false;
542 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700543 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700544}
545
Andreas Gampe049cff02015-12-01 23:27:12 -0800546////////////////////////
547// OatFile via dlopen //
548////////////////////////
549
Andreas Gampe049cff02015-12-01 23:27:12 -0800550class DlOpenOatFile FINAL : public OatFileBase {
551 public:
552 DlOpenOatFile(const std::string& filename, bool executable)
553 : OatFileBase(filename, executable),
554 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700555 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800556 }
557
558 ~DlOpenOatFile() {
559 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700560 if (!kIsTargetBuild) {
561 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
562 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700563 dlclose(dlopen_handle_);
564 } else {
565 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700566 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800567 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800568 }
569
570 protected:
571 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
572 std::string* error_msg) const OVERRIDE {
573 const uint8_t* ptr =
574 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
575 if (ptr == nullptr) {
576 *error_msg = dlerror();
577 }
578 return ptr;
579 }
580
Andreas Gampe4075f832016-05-18 13:09:54 -0700581 void PreLoad() OVERRIDE;
582
Andreas Gampe049cff02015-12-01 23:27:12 -0800583 bool Load(const std::string& elf_filename,
584 uint8_t* oat_file_begin,
585 bool writable,
586 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800587 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800588 std::string* error_msg) OVERRIDE;
589
590 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
591 void PreSetup(const std::string& elf_filename) OVERRIDE;
592
593 private:
594 bool Dlopen(const std::string& elf_filename,
595 uint8_t* oat_file_begin,
596 std::string* error_msg);
597
Richard Uhlera206c742016-05-24 15:04:22 -0700598 // On the host, if the same library is loaded again with dlopen the same
599 // file handle is returned. This differs from the behavior of dlopen on the
600 // target, where dlopen reloads the library at a different address every
601 // time you load it. The runtime relies on the target behavior to ensure
602 // each instance of the loaded library has a unique dex cache. To avoid
603 // problems, we fall back to our own linker in the case when the same
604 // library is opened multiple times on host. dlopen_handles_ is used to
605 // detect that case.
606 // Guarded by host_dlopen_handles_lock_;
607 static std::unordered_set<void*> host_dlopen_handles_;
608
Andreas Gampe049cff02015-12-01 23:27:12 -0800609 // dlopen handle during runtime.
610 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
611
612 // Dummy memory map objects corresponding to the regions mapped by dlopen.
613 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
614
Andreas Gampe4075f832016-05-18 13:09:54 -0700615 // The number of shared objects the linker told us about before loading. Used to
616 // (optimistically) optimize the PreSetup stage (see comment there).
617 size_t shared_objects_before_;
618
Andreas Gampe049cff02015-12-01 23:27:12 -0800619 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
620};
621
Richard Uhlera206c742016-05-24 15:04:22 -0700622std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
623
Andreas Gampe4075f832016-05-18 13:09:54 -0700624void DlOpenOatFile::PreLoad() {
625#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700626 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700627 LOG(FATAL) << "Should not reach here.";
628 UNREACHABLE();
629#else
630 // Count the entries in dl_iterate_phdr we get at this point in time.
631 struct dl_iterate_context {
632 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
633 size_t size ATTRIBUTE_UNUSED,
634 void *data) {
635 reinterpret_cast<dl_iterate_context*>(data)->count++;
636 return 0; // Continue iteration.
637 }
638 size_t count = 0;
639 } context;
640
641 dl_iterate_phdr(dl_iterate_context::callback, &context);
642 shared_objects_before_ = context.count;
643#endif
644}
645
Andreas Gampe049cff02015-12-01 23:27:12 -0800646bool DlOpenOatFile::Load(const std::string& elf_filename,
647 uint8_t* oat_file_begin,
648 bool writable,
649 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800650 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800651 std::string* error_msg) {
652 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
653 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
654 // !executable is a sign that we may want to patch), which may not be allowed for
655 // various reasons.
656 if (!kUseDlopen) {
657 *error_msg = "DlOpen is disabled.";
658 return false;
659 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800660 if (low_4gb) {
661 *error_msg = "DlOpen does not support low 4gb loading.";
662 return false;
663 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800664 if (writable) {
665 *error_msg = "DlOpen does not support writable loading.";
666 return false;
667 }
668 if (!executable) {
669 *error_msg = "DlOpen does not support non-executable loading.";
670 return false;
671 }
672
673 // dlopen always returns the same library if it is already opened on the host. For this reason
674 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
675 // the same library loaded multiple times at different addresses is required for class unloading
676 // and for having dex caches arrays in the .bss section.
677 if (!kIsTargetBuild) {
678 if (!kUseDlopenOnHost) {
679 *error_msg = "DlOpen disabled for host.";
680 return false;
681 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800682 }
683
684 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
685 DCHECK(dlopen_handle_ != nullptr || !success);
686
687 return success;
688}
689
690bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
691 uint8_t* oat_file_begin,
692 std::string* error_msg) {
693#ifdef __APPLE__
694 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
695 // but let's fallback to the custom loading code for the time being.
696 UNUSED(elf_filename, oat_file_begin);
697 *error_msg = "Dlopen unsupported on Mac.";
698 return false;
699#else
700 {
701 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
702 if (absolute_path == nullptr) {
703 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
704 return false;
705 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100706#ifdef ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800707 android_dlextinfo extinfo;
708 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
709 // (open oat files multiple
710 // times).
711 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
712 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800713 if (oat_file_begin != nullptr) { //
714 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
715 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
716 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800717 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
718#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800719 UNUSED(oat_file_begin);
Richard Uhlera206c742016-05-24 15:04:22 -0700720 static_assert(!kIsTargetBuild, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700721 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700722 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
723 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700724 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
725 dlclose(dlopen_handle_);
726 dlopen_handle_ = nullptr;
727 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
728 return false;
729 }
730 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100731#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800732 }
733 if (dlopen_handle_ == nullptr) {
734 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
735 return false;
736 }
737 return true;
738#endif
739}
740
741void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800742#ifdef __APPLE__
743 UNUSED(elf_filename);
744 LOG(FATAL) << "Should not reach here.";
745 UNREACHABLE();
746#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800747 struct dl_iterate_context {
748 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
749 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700750 context->shared_objects_seen++;
751 if (context->shared_objects_seen < context->shared_objects_before) {
752 // We haven't been called yet for anything we haven't seen before. Just continue.
753 // Note: this is aggressively optimistic. If another thread was unloading a library,
754 // we may miss out here. However, this does not happen often in practice.
755 return 0;
756 }
757
Andreas Gampe049cff02015-12-01 23:27:12 -0800758 // See whether this callback corresponds to the file which we have just loaded.
759 bool contains_begin = false;
760 for (int i = 0; i < info->dlpi_phnum; i++) {
761 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
762 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
763 info->dlpi_phdr[i].p_vaddr);
764 size_t memsz = info->dlpi_phdr[i].p_memsz;
765 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
766 contains_begin = true;
767 break;
768 }
769 }
770 }
771 // Add dummy mmaps for this file.
772 if (contains_begin) {
773 for (int i = 0; i < info->dlpi_phnum; i++) {
774 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
775 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
776 info->dlpi_phdr[i].p_vaddr);
777 size_t memsz = info->dlpi_phdr[i].p_memsz;
778 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
779 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
780 }
781 }
782 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
783 }
784 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
785 }
786 const uint8_t* const begin_;
787 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700788 const size_t shared_objects_before;
789 size_t shared_objects_seen;
790 };
791 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800792
793 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700794 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
795 // before giving up. This should be unusual.
796 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
797 << shared_objects_before_;
798 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
799 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
800 // OK, give up and print an error.
801 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
802 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
803 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800804 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800805#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800806}
807
808////////////////////////////////////////////////
809// OatFile via our own ElfFile implementation //
810////////////////////////////////////////////////
811
812class ElfOatFile FINAL : public OatFileBase {
813 public:
814 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
815
816 static ElfOatFile* OpenElfFile(File* file,
817 const std::string& location,
818 uint8_t* requested_base,
819 uint8_t* oat_file_begin, // Override base if not null
820 bool writable,
821 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800822 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800823 const char* abs_dex_location,
824 std::string* error_msg);
825
826 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100827 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800828 const char* abs_dex_location,
829 std::string* error_msg);
830
831 protected:
832 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
833 std::string* error_msg) const OVERRIDE {
834 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
835 if (ptr == nullptr) {
836 *error_msg = "(Internal implementation could not find symbol)";
837 }
838 return ptr;
839 }
840
Andreas Gampe4075f832016-05-18 13:09:54 -0700841 void PreLoad() OVERRIDE {
842 }
843
Andreas Gampe049cff02015-12-01 23:27:12 -0800844 bool Load(const std::string& elf_filename,
845 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
846 bool writable,
847 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800848 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800849 std::string* error_msg) OVERRIDE;
850
851 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
852 }
853
854 private:
855 bool ElfFileOpen(File* file,
856 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
857 bool writable,
858 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800859 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800860 std::string* error_msg);
861
862 private:
863 // Backing memory map for oat file during cross compilation.
864 std::unique_ptr<ElfFile> elf_file_;
865
866 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
867};
868
869ElfOatFile* ElfOatFile::OpenElfFile(File* file,
870 const std::string& location,
871 uint8_t* requested_base,
872 uint8_t* oat_file_begin, // Override base if not null
873 bool writable,
874 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800875 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800876 const char* abs_dex_location,
877 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800878 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800879 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800880 bool success = oat_file->ElfFileOpen(file,
881 oat_file_begin,
882 writable,
883 low_4gb,
884 executable,
885 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800886 if (!success) {
887 CHECK(!error_msg->empty());
888 return nullptr;
889 }
890
891 // Complete the setup.
892 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
893 return nullptr;
894 }
895
896 if (!oat_file->Setup(abs_dex_location, error_msg)) {
897 return nullptr;
898 }
899
900 return oat_file.release();
901}
902
903bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100904 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800905 const char* abs_dex_location,
906 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800907 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800908 if (IsExecutable()) {
909 *error_msg = "Cannot initialize from elf file in executable mode.";
910 return false;
911 }
912 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +0100913 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -0800914 uint64_t offset, size;
915 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
916 CHECK(has_section);
917 SetBegin(elf_file->Begin() + offset);
918 SetEnd(elf_file->Begin() + size + offset);
919 // Ignore the optional .bss section when opening non-executable.
920 return Setup(abs_dex_location, error_msg);
921}
922
923bool ElfOatFile::Load(const std::string& elf_filename,
924 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
925 bool writable,
926 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800927 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800928 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800929 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800930 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
931 if (file == nullptr) {
932 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
933 return false;
934 }
935 return ElfOatFile::ElfFileOpen(file.get(),
936 oat_file_begin,
937 writable,
938 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800939 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800940 error_msg);
941}
942
943bool ElfOatFile::ElfFileOpen(File* file,
944 uint8_t* oat_file_begin,
945 bool writable,
946 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800947 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800948 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800949 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800950 // TODO: rename requested_base to oat_data_begin
951 elf_file_.reset(ElfFile::Open(file,
952 writable,
953 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800954 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800955 error_msg,
956 oat_file_begin));
957 if (elf_file_ == nullptr) {
958 DCHECK(!error_msg->empty());
959 return false;
960 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800961 bool loaded = elf_file_->Load(executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800962 DCHECK(loaded || !error_msg->empty());
963 return loaded;
964}
965
966//////////////////////////
967// General OatFile code //
968//////////////////////////
969
970std::string OatFile::ResolveRelativeEncodedDexLocation(
971 const char* abs_dex_location, const std::string& rel_dex_location) {
972 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
973 // Strip :classes<N>.dex used for secondary multidex files.
974 std::string base = DexFile::GetBaseLocation(rel_dex_location);
975 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
976
977 // Check if the base is a suffix of the provided abs_dex_location.
978 std::string target_suffix = "/" + base;
979 std::string abs_location(abs_dex_location);
980 if (abs_location.size() > target_suffix.size()) {
981 size_t pos = abs_location.size() - target_suffix.size();
982 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
983 return abs_location + multidex_suffix;
984 }
985 }
986 }
987 return rel_dex_location;
988}
989
990static void CheckLocation(const std::string& location) {
991 CHECK(!location.empty());
992}
993
994OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100995 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800996 const std::string& location,
997 const char* abs_dex_location,
998 std::string* error_msg) {
999 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001000 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001001 ? oat_file.release()
1002 : nullptr;
1003}
1004
David Brazdil7b49e6c2016-09-01 11:06:18 +01001005OatFile* OatFile::Open(const std::string& oat_filename,
1006 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001007 uint8_t* requested_base,
1008 uint8_t* oat_file_begin,
1009 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001010 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001011 const char* abs_dex_location,
1012 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001013 ScopedTrace trace("Open oat file " + oat_location);
1014 CHECK(!oat_filename.empty()) << oat_location;
1015 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001016
David Brazdil7b49e6c2016-09-01 11:06:18 +01001017 std::string vdex_filename = ReplaceFileExtension(oat_filename, "vdex");
1018
1019 // Check that the files even exist, fast-fail.
1020 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
1021 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1022 return nullptr;
1023 } else if (!OS::FileExists(oat_filename.c_str())) {
1024 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001025 return nullptr;
1026 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001027
1028 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1029 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001030 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1031 oat_filename,
1032 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001033 requested_base,
1034 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001035 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001036 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001037 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001038 abs_dex_location,
1039 error_msg);
1040 if (with_dlopen != nullptr) {
1041 return with_dlopen;
1042 }
1043 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001044 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001045 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001046 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1047 //
1048 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1049 //
1050 // We use our own ELF loader for Quick to deal with legacy apps that
1051 // open a generated dex file by name, remove the file, then open
1052 // another generated dex file with the same name. http://b/10614658
1053 //
1054 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1055 //
1056 //
1057 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1058 // 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 +01001059 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1060 oat_filename,
1061 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001062 requested_base,
1063 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001064 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001065 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001066 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001067 abs_dex_location,
1068 error_msg);
1069 return with_internal;
1070}
1071
1072OatFile* OatFile::OpenWritable(File* file,
1073 const std::string& location,
1074 const char* abs_dex_location,
1075 std::string* error_msg) {
1076 CheckLocation(location);
1077 return ElfOatFile::OpenElfFile(file,
1078 location,
1079 nullptr,
1080 nullptr,
1081 true,
1082 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001083 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001084 abs_dex_location,
1085 error_msg);
1086}
1087
1088OatFile* OatFile::OpenReadable(File* file,
1089 const std::string& location,
1090 const char* abs_dex_location,
1091 std::string* error_msg) {
1092 CheckLocation(location);
1093 return ElfOatFile::OpenElfFile(file,
1094 location,
1095 nullptr,
1096 nullptr,
1097 false,
1098 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001099 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001100 abs_dex_location,
1101 error_msg);
1102}
1103
1104OatFile::OatFile(const std::string& location, bool is_executable)
1105 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001106 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001107 begin_(nullptr),
1108 end_(nullptr),
1109 bss_begin_(nullptr),
1110 bss_end_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001111 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001112 is_executable_(is_executable),
1113 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1114 CHECK(!location_.empty());
1115}
1116
1117OatFile::~OatFile() {
1118 STLDeleteElements(&oat_dex_files_storage_);
1119}
1120
Brian Carlstrome24fa612011-09-29 00:53:55 -07001121const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001122 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001123}
1124
Ian Rogers13735952014-10-08 12:43:28 -07001125const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001126 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001127 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001128}
1129
Ian Rogers13735952014-10-08 12:43:28 -07001130const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001131 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001132 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001133}
1134
Vladimir Marko5c42c292015-02-25 12:02:49 +00001135const uint8_t* OatFile::BssBegin() const {
1136 return bss_begin_;
1137}
1138
1139const uint8_t* OatFile::BssEnd() const {
1140 return bss_end_;
1141}
1142
David Brazdil7b49e6c2016-09-01 11:06:18 +01001143const uint8_t* OatFile::DexBegin() const {
1144 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1145}
1146
1147const uint8_t* OatFile::DexEnd() const {
1148 return kIsVdexEnabled ? vdex_->End() : End();
1149}
1150
Vladimir Markoaad75c62016-10-03 08:46:48 +00001151ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1152 if (bss_roots_ != nullptr) {
1153 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1154 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1155 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1156 } else {
1157 return ArrayRef<GcRoot<mirror::Object>>();
1158 }
1159}
1160
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001161const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1162 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001163 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001164 // NOTE: We assume here that the canonical location for a given dex_location never
1165 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1166 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1167 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001168
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001169 // TODO: Additional analysis of usage patterns to see if this can be simplified
1170 // without any performance loss, for example by not doing the first lock-free lookup.
1171
1172 const OatFile::OatDexFile* oat_dex_file = nullptr;
1173 StringPiece key(dex_location);
1174 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1175 // directly mentioned in the oat file and doesn't require locking.
1176 auto primary_it = oat_dex_files_.find(key);
1177 if (primary_it != oat_dex_files_.end()) {
1178 oat_dex_file = primary_it->second;
1179 DCHECK(oat_dex_file != nullptr);
1180 } else {
1181 // This dex_location is not one of the dex locations directly mentioned in the
1182 // oat file. The correct lookup is via the canonical location but first see in
1183 // the secondary_oat_dex_files_ whether we've looked up this location before.
1184 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1185 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1186 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001187 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001188 } else {
1189 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001190 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001191 if (dex_canonical_location != dex_location) {
1192 StringPiece canonical_key(dex_canonical_location);
1193 auto canonical_it = oat_dex_files_.find(canonical_key);
1194 if (canonical_it != oat_dex_files_.end()) {
1195 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001196 } // else keep null.
1197 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001198
1199 // Copy the key to the string_cache_ and store the result in secondary map.
1200 string_cache_.emplace_back(key.data(), key.length());
1201 StringPiece key_copy(string_cache_.back());
1202 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001203 }
1204 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001205
1206 if (oat_dex_file == nullptr) {
1207 if (error_msg != nullptr) {
1208 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1209 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1210 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1211 }
1212 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001213 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001214
Richard Uhler9a37efc2016-08-05 16:32:55 -07001215 if (dex_location_checksum != nullptr &&
1216 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1217 if (error_msg != nullptr) {
1218 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1219 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1220 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1221 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1222 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1223 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001224 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001225 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001226 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001227 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001228}
1229
Brian Carlstrome24fa612011-09-29 00:53:55 -07001230OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001231 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001232 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001233 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001234 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001235 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001236 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001237 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001238 : oat_file_(oat_file),
1239 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001240 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001241 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001242 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001243 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001244 oat_class_offsets_pointer_(oat_class_offsets_pointer),
David Sehr9aa352e2016-09-15 18:13:52 -07001245 dex_cache_arrays_(dex_cache_arrays) {
1246 // Initialize TypeLookupTable.
1247 if (lookup_table_data_ != nullptr) {
1248 // Peek the number of classes from the DexFile.
1249 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1250 const uint32_t num_class_defs = dex_header->class_defs_size_;
1251 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1252 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1253 } else {
1254 lookup_table_.reset(TypeLookupTable::Open(dex_file_pointer_,
1255 lookup_table_data_,
1256 num_class_defs));
1257 }
1258 }
1259}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001260
1261OatFile::OatDexFile::~OatDexFile() {}
1262
Ian Rogers05f28c62012-10-23 18:12:13 -07001263size_t OatFile::OatDexFile::FileSize() const {
1264 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1265}
1266
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001267std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001268 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001269 static constexpr bool kVerify = false;
1270 static constexpr bool kVerifyChecksum = false;
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001271 return DexFile::Open(dex_file_pointer_,
1272 FileSize(),
1273 dex_file_location_,
1274 dex_file_location_checksum_,
1275 this,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001276 kVerify,
1277 kVerifyChecksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001278 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001279}
1280
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001281uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1282 return oat_class_offsets_pointer_[class_def_index];
1283}
1284
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001285OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001286 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001287
Ian Rogers13735952014-10-08 12:43:28 -07001288 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001289 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001290
Ian Rogers13735952014-10-08 12:43:28 -07001291 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001292 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1293 mirror::Class::Status status =
1294 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1295 CHECK_LT(status, mirror::Class::kStatusMax);
1296
Ian Rogers13735952014-10-08 12:43:28 -07001297 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001298 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1299 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1300 CHECK_LT(type, kOatClassMax);
1301
Ian Rogers13735952014-10-08 12:43:28 -07001302 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001303 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001304
Brian Carlstromcd937a92014-03-04 22:53:23 -08001305 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001306 const uint8_t* bitmap_pointer = nullptr;
1307 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001308 if (type != kOatClassNoneCompiled) {
1309 if (type == kOatClassSomeCompiled) {
1310 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1311 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1312 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1313 methods_pointer = bitmap_pointer + bitmap_size;
1314 } else {
1315 methods_pointer = after_type_pointer;
1316 }
1317 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001318 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001319
Richard Uhler07b3c232015-03-31 15:57:54 -07001320 return OatFile::OatClass(oat_file_,
1321 status,
1322 type,
1323 bitmap_size,
1324 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1325 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001326}
1327
David Sehr9aa352e2016-09-15 18:13:52 -07001328const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1329 const char* descriptor,
1330 size_t hash) {
1331 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1332 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1333 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1334 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
1335 return (class_def_idx != DexFile::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
1336 }
1337 // Fast path for rare no class defs case.
1338 const uint32_t num_class_defs = dex_file.NumClassDefs();
1339 if (num_class_defs == 0) {
1340 return nullptr;
1341 }
1342 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1343 if (type_id != nullptr) {
1344 uint16_t type_idx = dex_file.GetIndexForTypeId(*type_id);
1345 return dex_file.FindClassDef(type_idx);
1346 }
1347 return nullptr;
1348}
1349
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001350OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001351 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001352 OatClassType type,
1353 uint32_t bitmap_size,
1354 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001355 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001356 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001357 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001358 switch (type_) {
1359 case kOatClassAllCompiled: {
1360 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001361 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001362 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001363 break;
1364 }
1365 case kOatClassSomeCompiled: {
1366 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001367 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001368 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001369 break;
1370 }
1371 case kOatClassNoneCompiled: {
1372 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001373 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001374 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001375 break;
1376 }
1377 case kOatClassMax: {
1378 LOG(FATAL) << "Invalid OatClassType " << type_;
1379 break;
1380 }
1381 }
1382}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001383
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001384uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1385 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1386 if (oat_method_offsets == nullptr) {
1387 return 0u;
1388 }
1389 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1390}
1391
1392const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001393 // 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 -07001394 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001395 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001396 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001397 }
1398 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001399 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001400 CHECK_EQ(kOatClassAllCompiled, type_);
1401 methods_pointer_index = method_index;
1402 } else {
1403 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001404 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001405 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001406 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001407 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1408 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001409 }
1410 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001411 return &oat_method_offsets;
1412}
1413
1414const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1415 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1416 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001417 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001418 }
1419 if (oat_file_->IsExecutable() ||
1420 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001421 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001422 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001423 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001424 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1425 // version.
1426 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001427}
1428
Mathieu Chartiere401d142015-04-22 13:56:20 -07001429void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001430 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001431 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001432}
1433
Richard Uhlerd1537b52016-03-29 13:27:41 -07001434bool OatFile::HasPatchInfo() const {
1435 return GetOatHeader().HasPatchInfo();
1436}
1437
Andreas Gampe7ba64962014-10-23 11:37:40 -07001438bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001439 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001440 // TODO: Check against oat_patches. b/18144996
1441}
1442
Sebastien Hertz0de11332015-05-13 12:14:05 +02001443bool OatFile::IsDebuggable() const {
1444 return GetOatHeader().IsDebuggable();
1445}
1446
Andreas Gampe29d38e72016-03-23 15:31:51 +00001447CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1448 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001449}
1450
Andreas Gampe7848da42015-04-09 11:15:04 -07001451static constexpr char kDexClassPathEncodingSeparator = '*';
1452
1453std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1454 std::ostringstream out;
1455
1456 for (const DexFile* dex_file : dex_files) {
1457 out << dex_file->GetLocation().c_str();
1458 out << kDexClassPathEncodingSeparator;
1459 out << dex_file->GetLocationChecksum();
1460 out << kDexClassPathEncodingSeparator;
1461 }
1462
1463 return out.str();
1464}
1465
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001466bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001467 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1468 // No dependencies.
1469 return true;
1470 }
1471
1472 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1473 // Split() instead of manual parsing of the combined char*.
1474 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001475 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001476 if (split.size() % 2 != 0) {
1477 // Expected pairs of location and checksum.
1478 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1479 return false;
1480 }
1481
1482 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1483 std::string& location = *it;
1484 std::string& checksum = *(it + 1);
1485 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1486 if (converted == 0) {
1487 // Conversion error.
1488 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1489 return false;
1490 }
1491
1492 uint32_t dex_checksum;
1493 std::string error_msg;
1494 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001495 &dex_checksum,
1496 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001497 if (converted != dex_checksum) {
1498 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1499 location.c_str(), converted, dex_checksum);
1500 return false;
1501 }
1502 } else {
1503 // Problem retrieving checksum.
1504 // TODO: odex files?
1505 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1506 error_msg.c_str());
1507 return false;
1508 }
1509 }
1510
1511 return true;
1512}
1513
1514bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001515 std::vector<std::string>* locations) {
1516 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001517 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1518 return true;
1519 }
1520
1521 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1522 // Split() instead of manual parsing of the combined char*.
1523 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001524 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001525 if (split.size() % 2 != 0) {
1526 // Expected pairs of location and checksum.
1527 return false;
1528 }
1529
1530 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1531 locations->push_back(*it);
1532 }
1533
1534 return true;
1535}
1536
Brian Carlstrome24fa612011-09-29 00:53:55 -07001537} // namespace art