blob: bdf8b0e65d1f8983ef5d15f5fa91817822d6da8a [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"
Andreas Gampea5b09a62016-11-17 15:21:22 -080041#include "dex_file_types.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070043#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000044#include "gc_root.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000046#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070048#include "mirror/object-inl.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010049#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070050#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070051#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070052#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000053#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070054#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010056#include "utils/dex_cache_arrays_layout-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010057#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070058
59namespace art {
60
Andreas Gampe049cff02015-12-01 23:27:12 -080061// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000062static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070063
Andreas Gampe049cff02015-12-01 23:27:12 -080064// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070065// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
66// multiple times). However, if/when we switch the above, we likely want to switch this, too,
67// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000068static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070069
70// For debugging, Open will print DlOpen error message if set to true.
71static constexpr bool kPrintDlOpenErrorMessage = false;
72
Andreas Gampe049cff02015-12-01 23:27:12 -080073// Note for OatFileBase and descendents:
74//
75// These are used in OatFile::Open to try all our loaders.
76//
77// The process is simple:
78//
79// 1) Allocate an instance through the standard constructor (location, executable)
80// 2) Load() to try to open the file.
81// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
82// 4) PreSetup() for any steps that should be done before the final setup.
83// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070084
Andreas Gampe049cff02015-12-01 23:27:12 -080085class OatFileBase : public OatFile {
86 public:
87 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070088
Andreas Gampe049cff02015-12-01 23:27:12 -080089 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010090 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
91 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070092 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080093 uint8_t* requested_base,
94 uint8_t* oat_file_begin,
95 bool writable,
96 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080097 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070098 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080099 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800100
Andreas Gampe049cff02015-12-01 23:27:12 -0800101 protected:
102 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700103
Andreas Gampe049cff02015-12-01 23:27:12 -0800104 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
105 std::string* error_msg) const = 0;
106
Andreas Gampe4075f832016-05-18 13:09:54 -0700107 virtual void PreLoad() = 0;
108
David Brazdil7b49e6c2016-09-01 11:06:18 +0100109 bool LoadVdex(const std::string& vdex_filename,
110 bool writable,
111 bool low_4gb,
112 std::string* error_msg);
113
Andreas Gampe049cff02015-12-01 23:27:12 -0800114 virtual bool Load(const std::string& elf_filename,
115 uint8_t* oat_file_begin,
116 bool writable,
117 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800118 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800119 std::string* error_msg) = 0;
120
121 bool ComputeFields(uint8_t* requested_base,
122 const std::string& file_path,
123 std::string* error_msg);
124
125 virtual void PreSetup(const std::string& elf_filename) = 0;
126
127 bool Setup(const char* abs_dex_location, std::string* error_msg);
128
129 // Setters exposed for ElfOatFile.
130
131 void SetBegin(const uint8_t* begin) {
132 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700133 }
134
Andreas Gampe049cff02015-12-01 23:27:12 -0800135 void SetEnd(const uint8_t* end) {
136 end_ = end;
137 }
138
David Brazdilc93b3be2016-09-12 18:49:58 +0100139 void SetVdex(VdexFile* vdex) {
140 vdex_.reset(vdex);
141 }
142
Andreas Gampe049cff02015-12-01 23:27:12 -0800143 private:
144 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
145};
146
147template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100148OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
149 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800150 const std::string& location,
151 uint8_t* requested_base,
152 uint8_t* oat_file_begin,
153 bool writable,
154 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800155 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800156 const char* abs_dex_location,
157 std::string* error_msg) {
158 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700159
160 ret->PreLoad();
161
David Brazdil7b49e6c2016-09-01 11:06:18 +0100162 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
163 return nullptr;
164 }
165
Andreas Gampe049cff02015-12-01 23:27:12 -0800166 if (!ret->Load(elf_filename,
167 oat_file_begin,
168 writable,
169 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800170 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800171 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800172 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800173 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800174
Andreas Gampe049cff02015-12-01 23:27:12 -0800175 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
176 return nullptr;
177 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700178
Andreas Gampe049cff02015-12-01 23:27:12 -0800179 ret->PreSetup(elf_filename);
180
181 if (!ret->Setup(abs_dex_location, error_msg)) {
182 return nullptr;
183 }
184
Dave Allison69dfe512014-07-11 17:11:58 +0000185 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800186}
187
David Brazdil7b49e6c2016-09-01 11:06:18 +0100188bool OatFileBase::LoadVdex(const std::string& vdex_filename,
189 bool writable,
190 bool low_4gb,
191 std::string* error_msg) {
192 vdex_.reset(VdexFile::Open(vdex_filename, writable, low_4gb, error_msg));
193 if (vdex_.get() == nullptr) {
194 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
195 vdex_filename.c_str(),
196 error_msg->c_str());
197 return false;
198 }
199 return true;
200}
201
Andreas Gampe049cff02015-12-01 23:27:12 -0800202bool OatFileBase::ComputeFields(uint8_t* requested_base,
203 const std::string& file_path,
204 std::string* error_msg) {
205 std::string symbol_error_msg;
206 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700207 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800208 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
209 file_path.c_str(),
210 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700211 return false;
212 }
213 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000214 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700215 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000216 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
217 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700218 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800219 "oatdata=%p != expected=%p. See process maps in the log.",
220 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700221 return false;
222 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800223 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700224 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800225 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
226 file_path.c_str(),
227 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700228 return false;
229 }
230 // Readjust to be non-inclusive upper bound.
231 end_ += sizeof(uint32_t);
232
Andreas Gampe049cff02015-12-01 23:27:12 -0800233 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700234 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800235 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700236 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700237 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800238 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700239 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800240 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700241 return false;
242 }
243 // Readjust to be non-inclusive upper bound.
244 bss_end_ += sizeof(uint32_t);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000245 // Find bss roots if present.
246 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700247 }
248
Andreas Gampe049cff02015-12-01 23:27:12 -0800249 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800250}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800251
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100252// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
253// position by the number of bytes read, i.e. sizeof(T).
254// Return true on success, false if the read would go beyond the end of the OatFile.
255template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100256inline static bool ReadOatDexFileData(const OatFile& oat_file,
257 /*inout*/const uint8_t** oat,
258 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100259 DCHECK(oat != nullptr);
260 DCHECK(value != nullptr);
261 DCHECK_LE(*oat, oat_file.End());
262 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
263 return false;
264 }
265 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
266 typedef __attribute__((__aligned__(1))) T unaligned_type;
267 *value = *reinterpret_cast<const unaligned_type*>(*oat);
268 *oat += sizeof(T);
269 return true;
270}
271
Andreas Gampe049cff02015-12-01 23:27:12 -0800272bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700273 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800274 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100275 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
276 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800277 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700278 return false;
279 }
Ian Rogers13735952014-10-08 12:43:28 -0700280 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700281 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700282 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700283 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700284 return false;
285 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700286
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700287 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700288 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700289 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100290 "%p + %zu + %u <= %p",
291 GetLocation().c_str(),
292 Begin(),
293 sizeof(OatHeader),
294 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700295 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700296 return false;
297 }
298
Vladimir Markoaad75c62016-10-03 08:46:48 +0000299 if (!IsAligned<alignof(GcRoot<mirror::Object>)>(bss_begin_) ||
300 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_roots_) ||
301 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
302 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
303 "begin = %p, roots = %p, end = %p",
304 GetLocation().c_str(),
305 bss_begin_,
306 bss_roots_,
307 bss_end_);
308 return false;
309 }
310
311 if (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) {
312 *error_msg = StringPrintf("In oat file '%s' found bss roots outside .bss: "
313 "%p is outside range [%p, %p]",
314 GetLocation().c_str(),
315 bss_roots_,
316 bss_begin_,
317 bss_end_);
318 return false;
319 }
320
Andreas Gampe542451c2016-07-26 09:02:02 -0700321 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100322 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000323 uint8_t* dex_cache_arrays_end = (bss_roots_ != nullptr) ? bss_roots_ : bss_end_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100324 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
325 oat_dex_files_storage_.reserve(dex_file_count);
326 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100327 uint32_t dex_file_location_size;
328 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
329 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
330 "location size",
331 GetLocation().c_str(),
332 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700333 return false;
334 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100335 if (UNLIKELY(dex_file_location_size == 0U)) {
336 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
337 GetLocation().c_str(),
338 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700339 return false;
340 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000341 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100342 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
343 "location",
344 GetLocation().c_str(),
345 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700346 return false;
347 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000348 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
349 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700350
Richard Uhlere5fed032015-03-18 08:21:11 -0700351 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
352 abs_dex_location,
353 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100355 uint32_t dex_file_checksum;
356 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
357 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
358 "dex file checksum",
359 GetLocation().c_str(),
360 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700361 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700362 return false;
363 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700364
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100365 uint32_t dex_file_offset;
366 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
367 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
368 "after dex file offsets",
369 GetLocation().c_str(),
370 i,
371 dex_file_location.c_str());
372 return false;
373 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700374 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100375 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
376 "file offset",
377 GetLocation().c_str(),
378 i,
379 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700380 return false;
381 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100382 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100383 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
384 "offset %u > %zu",
385 GetLocation().c_str(),
386 i,
387 dex_file_location.c_str(),
388 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100389 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700390 return false;
391 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100392 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000393 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
394 "offset %u of %zu but the size of dex file header is %zu",
395 GetLocation().c_str(),
396 i,
397 dex_file_location.c_str(),
398 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100399 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000400 sizeof(DexFile::Header));
401 return false;
402 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800403
David Brazdil7b49e6c2016-09-01 11:06:18 +0100404 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700405 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100406 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
407 "dex file magic '%s'",
408 GetLocation().c_str(),
409 i,
410 dex_file_location.c_str(),
411 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700412 return false;
413 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700414 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100415 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
416 "dex file version '%s'",
417 GetLocation().c_str(),
418 i,
419 dex_file_location.c_str(),
420 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700421 return false;
422 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800423 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100424 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000425 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
426 "offset %u and size %u truncated at %zu",
427 GetLocation().c_str(),
428 i,
429 dex_file_location.c_str(),
430 dex_file_offset,
431 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100432 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000433 return false;
434 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300435
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000436 uint32_t class_offsets_offset;
437 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
438 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
439 "after class offsets offset",
440 GetLocation().c_str(),
441 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300442 dex_file_location.c_str());
443 return false;
444 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000445 if (UNLIKELY(class_offsets_offset > Size()) ||
446 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
447 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
448 "class offsets, offset %u of %zu, class defs %u",
449 GetLocation().c_str(),
450 i,
451 dex_file_location.c_str(),
452 class_offsets_offset,
453 Size(),
454 header->class_defs_size_);
455 return false;
456 }
457 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
458 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
459 "class offsets, offset %u",
460 GetLocation().c_str(),
461 i,
462 dex_file_location.c_str(),
463 class_offsets_offset);
464 return false;
465 }
466 const uint32_t* class_offsets_pointer =
467 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
468
469 uint32_t lookup_table_offset;
470 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
471 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
472 "after lookup table offset",
473 GetLocation().c_str(),
474 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300475 dex_file_location.c_str());
476 return false;
477 }
478 const uint8_t* lookup_table_data = lookup_table_offset != 0u
479 ? Begin() + lookup_table_offset
480 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000481 if (lookup_table_offset != 0u &&
482 (UNLIKELY(lookup_table_offset > Size()) ||
483 UNLIKELY(Size() - lookup_table_offset <
484 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100485 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000486 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100487 GetLocation().c_str(),
488 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000489 dex_file_location.c_str(),
490 lookup_table_offset,
491 Size(),
492 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700493 return false;
494 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700495
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100496 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100497 if (dex_cache_arrays != nullptr) {
498 DexCacheArraysLayout layout(pointer_size, *header);
499 if (layout.Size() != 0u) {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000500 if (static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100501 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
502 "truncated dex cache arrays, %zu < %zu.",
503 GetLocation().c_str(),
504 i,
505 dex_file_location.c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000506 static_cast<size_t>(dex_cache_arrays_end - dex_cache_arrays),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100507 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100508 return false;
509 }
510 current_dex_cache_arrays = dex_cache_arrays;
511 dex_cache_arrays += layout.Size();
512 }
513 }
514
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100515 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
516
517 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100518 OatDexFile* oat_dex_file = new OatDexFile(this,
519 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100520 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100521 dex_file_checksum,
522 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300523 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000524 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100525 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100526 oat_dex_files_storage_.push_back(oat_dex_file);
527
528 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100529 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100530 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100531 if (canonical_location != dex_file_location) {
532 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
533 oat_dex_files_.Put(canonical_key, oat_dex_file);
534 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700535 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100536
Vladimir Markoaad75c62016-10-03 08:46:48 +0000537 if (dex_cache_arrays != dex_cache_arrays_end) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100538 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
Vladimir Markoaad75c62016-10-03 08:46:48 +0000539 // both null) or contain just the dex cache arrays and optionally some GC roots.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100540 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100541 GetLocation().c_str(),
542 static_cast<size_t>(bss_end_ - dex_cache_arrays));
543 return false;
544 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700545 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700546}
547
Andreas Gampe049cff02015-12-01 23:27:12 -0800548////////////////////////
549// OatFile via dlopen //
550////////////////////////
551
Andreas Gampe049cff02015-12-01 23:27:12 -0800552class DlOpenOatFile FINAL : public OatFileBase {
553 public:
554 DlOpenOatFile(const std::string& filename, bool executable)
555 : OatFileBase(filename, executable),
556 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700557 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800558 }
559
560 ~DlOpenOatFile() {
561 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700562 if (!kIsTargetBuild) {
563 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
564 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700565 dlclose(dlopen_handle_);
566 } else {
567 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700568 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800569 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800570 }
571
572 protected:
573 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
574 std::string* error_msg) const OVERRIDE {
575 const uint8_t* ptr =
576 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
577 if (ptr == nullptr) {
578 *error_msg = dlerror();
579 }
580 return ptr;
581 }
582
Andreas Gampe4075f832016-05-18 13:09:54 -0700583 void PreLoad() OVERRIDE;
584
Andreas Gampe049cff02015-12-01 23:27:12 -0800585 bool Load(const std::string& elf_filename,
586 uint8_t* oat_file_begin,
587 bool writable,
588 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800589 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800590 std::string* error_msg) OVERRIDE;
591
592 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
593 void PreSetup(const std::string& elf_filename) OVERRIDE;
594
595 private:
596 bool Dlopen(const std::string& elf_filename,
597 uint8_t* oat_file_begin,
598 std::string* error_msg);
599
Richard Uhlera206c742016-05-24 15:04:22 -0700600 // On the host, if the same library is loaded again with dlopen the same
601 // file handle is returned. This differs from the behavior of dlopen on the
602 // target, where dlopen reloads the library at a different address every
603 // time you load it. The runtime relies on the target behavior to ensure
604 // each instance of the loaded library has a unique dex cache. To avoid
605 // problems, we fall back to our own linker in the case when the same
606 // library is opened multiple times on host. dlopen_handles_ is used to
607 // detect that case.
608 // Guarded by host_dlopen_handles_lock_;
609 static std::unordered_set<void*> host_dlopen_handles_;
610
Andreas Gampe049cff02015-12-01 23:27:12 -0800611 // dlopen handle during runtime.
612 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
613
614 // Dummy memory map objects corresponding to the regions mapped by dlopen.
615 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
616
Andreas Gampe4075f832016-05-18 13:09:54 -0700617 // The number of shared objects the linker told us about before loading. Used to
618 // (optimistically) optimize the PreSetup stage (see comment there).
619 size_t shared_objects_before_;
620
Andreas Gampe049cff02015-12-01 23:27:12 -0800621 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
622};
623
Richard Uhlera206c742016-05-24 15:04:22 -0700624std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
625
Andreas Gampe4075f832016-05-18 13:09:54 -0700626void DlOpenOatFile::PreLoad() {
627#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700628 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700629 LOG(FATAL) << "Should not reach here.";
630 UNREACHABLE();
631#else
632 // Count the entries in dl_iterate_phdr we get at this point in time.
633 struct dl_iterate_context {
634 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
635 size_t size ATTRIBUTE_UNUSED,
636 void *data) {
637 reinterpret_cast<dl_iterate_context*>(data)->count++;
638 return 0; // Continue iteration.
639 }
640 size_t count = 0;
641 } context;
642
643 dl_iterate_phdr(dl_iterate_context::callback, &context);
644 shared_objects_before_ = context.count;
645#endif
646}
647
Andreas Gampe049cff02015-12-01 23:27:12 -0800648bool DlOpenOatFile::Load(const std::string& elf_filename,
649 uint8_t* oat_file_begin,
650 bool writable,
651 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800652 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800653 std::string* error_msg) {
654 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
655 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
656 // !executable is a sign that we may want to patch), which may not be allowed for
657 // various reasons.
658 if (!kUseDlopen) {
659 *error_msg = "DlOpen is disabled.";
660 return false;
661 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800662 if (low_4gb) {
663 *error_msg = "DlOpen does not support low 4gb loading.";
664 return false;
665 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800666 if (writable) {
667 *error_msg = "DlOpen does not support writable loading.";
668 return false;
669 }
670 if (!executable) {
671 *error_msg = "DlOpen does not support non-executable loading.";
672 return false;
673 }
674
675 // dlopen always returns the same library if it is already opened on the host. For this reason
676 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
677 // the same library loaded multiple times at different addresses is required for class unloading
678 // and for having dex caches arrays in the .bss section.
679 if (!kIsTargetBuild) {
680 if (!kUseDlopenOnHost) {
681 *error_msg = "DlOpen disabled for host.";
682 return false;
683 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800684 }
685
686 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
687 DCHECK(dlopen_handle_ != nullptr || !success);
688
689 return success;
690}
691
692bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
693 uint8_t* oat_file_begin,
694 std::string* error_msg) {
695#ifdef __APPLE__
696 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
697 // but let's fallback to the custom loading code for the time being.
698 UNUSED(elf_filename, oat_file_begin);
699 *error_msg = "Dlopen unsupported on Mac.";
700 return false;
701#else
702 {
703 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
704 if (absolute_path == nullptr) {
705 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
706 return false;
707 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100708#ifdef ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800709 android_dlextinfo extinfo;
710 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
711 // (open oat files multiple
712 // times).
713 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
714 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800715 if (oat_file_begin != nullptr) { //
716 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
717 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
718 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800719 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
720#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800721 UNUSED(oat_file_begin);
Julien Duraj1af0c4f2016-11-16 14:05:48 +0000722 static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700723 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700724 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
725 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700726 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
727 dlclose(dlopen_handle_);
728 dlopen_handle_ = nullptr;
729 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
730 return false;
731 }
732 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100733#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800734 }
735 if (dlopen_handle_ == nullptr) {
736 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
737 return false;
738 }
739 return true;
740#endif
741}
742
743void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800744#ifdef __APPLE__
745 UNUSED(elf_filename);
746 LOG(FATAL) << "Should not reach here.";
747 UNREACHABLE();
748#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800749 struct dl_iterate_context {
750 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
751 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700752 context->shared_objects_seen++;
753 if (context->shared_objects_seen < context->shared_objects_before) {
754 // We haven't been called yet for anything we haven't seen before. Just continue.
755 // Note: this is aggressively optimistic. If another thread was unloading a library,
756 // we may miss out here. However, this does not happen often in practice.
757 return 0;
758 }
759
Andreas Gampe049cff02015-12-01 23:27:12 -0800760 // See whether this callback corresponds to the file which we have just loaded.
761 bool contains_begin = false;
762 for (int i = 0; i < info->dlpi_phnum; i++) {
763 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
764 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
765 info->dlpi_phdr[i].p_vaddr);
766 size_t memsz = info->dlpi_phdr[i].p_memsz;
767 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
768 contains_begin = true;
769 break;
770 }
771 }
772 }
773 // Add dummy mmaps for this file.
774 if (contains_begin) {
775 for (int i = 0; i < info->dlpi_phnum; i++) {
776 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
777 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
778 info->dlpi_phdr[i].p_vaddr);
779 size_t memsz = info->dlpi_phdr[i].p_memsz;
780 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
781 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
782 }
783 }
784 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
785 }
786 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
787 }
788 const uint8_t* const begin_;
789 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700790 const size_t shared_objects_before;
791 size_t shared_objects_seen;
792 };
793 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800794
795 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700796 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
797 // before giving up. This should be unusual.
798 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
799 << shared_objects_before_;
800 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
801 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
802 // OK, give up and print an error.
803 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
804 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
805 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800806 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800807#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800808}
809
810////////////////////////////////////////////////
811// OatFile via our own ElfFile implementation //
812////////////////////////////////////////////////
813
814class ElfOatFile FINAL : public OatFileBase {
815 public:
816 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
817
818 static ElfOatFile* OpenElfFile(File* file,
819 const std::string& location,
820 uint8_t* requested_base,
821 uint8_t* oat_file_begin, // Override base if not null
822 bool writable,
823 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800824 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800825 const char* abs_dex_location,
826 std::string* error_msg);
827
828 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100829 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800830 const char* abs_dex_location,
831 std::string* error_msg);
832
833 protected:
834 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
835 std::string* error_msg) const OVERRIDE {
836 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
837 if (ptr == nullptr) {
838 *error_msg = "(Internal implementation could not find symbol)";
839 }
840 return ptr;
841 }
842
Andreas Gampe4075f832016-05-18 13:09:54 -0700843 void PreLoad() OVERRIDE {
844 }
845
Andreas Gampe049cff02015-12-01 23:27:12 -0800846 bool Load(const std::string& elf_filename,
847 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
848 bool writable,
849 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800850 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800851 std::string* error_msg) OVERRIDE;
852
853 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
854 }
855
856 private:
857 bool ElfFileOpen(File* file,
858 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
859 bool writable,
860 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800861 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800862 std::string* error_msg);
863
864 private:
865 // Backing memory map for oat file during cross compilation.
866 std::unique_ptr<ElfFile> elf_file_;
867
868 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
869};
870
871ElfOatFile* ElfOatFile::OpenElfFile(File* file,
872 const std::string& location,
873 uint8_t* requested_base,
874 uint8_t* oat_file_begin, // Override base if not null
875 bool writable,
876 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800877 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800878 const char* abs_dex_location,
879 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800880 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800881 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800882 bool success = oat_file->ElfFileOpen(file,
883 oat_file_begin,
884 writable,
885 low_4gb,
886 executable,
887 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800888 if (!success) {
889 CHECK(!error_msg->empty());
890 return nullptr;
891 }
892
893 // Complete the setup.
894 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
895 return nullptr;
896 }
897
898 if (!oat_file->Setup(abs_dex_location, error_msg)) {
899 return nullptr;
900 }
901
902 return oat_file.release();
903}
904
905bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100906 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800907 const char* abs_dex_location,
908 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800909 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800910 if (IsExecutable()) {
911 *error_msg = "Cannot initialize from elf file in executable mode.";
912 return false;
913 }
914 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +0100915 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -0800916 uint64_t offset, size;
917 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
918 CHECK(has_section);
919 SetBegin(elf_file->Begin() + offset);
920 SetEnd(elf_file->Begin() + size + offset);
921 // Ignore the optional .bss section when opening non-executable.
922 return Setup(abs_dex_location, error_msg);
923}
924
925bool ElfOatFile::Load(const std::string& elf_filename,
926 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
927 bool writable,
928 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800929 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800930 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800931 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800932 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
933 if (file == nullptr) {
934 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
935 return false;
936 }
937 return ElfOatFile::ElfFileOpen(file.get(),
938 oat_file_begin,
939 writable,
940 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800941 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800942 error_msg);
943}
944
945bool ElfOatFile::ElfFileOpen(File* file,
946 uint8_t* oat_file_begin,
947 bool writable,
948 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800949 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800950 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800951 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800952 // TODO: rename requested_base to oat_data_begin
953 elf_file_.reset(ElfFile::Open(file,
954 writable,
955 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800956 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800957 error_msg,
958 oat_file_begin));
959 if (elf_file_ == nullptr) {
960 DCHECK(!error_msg->empty());
961 return false;
962 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700963 bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800964 DCHECK(loaded || !error_msg->empty());
965 return loaded;
966}
967
968//////////////////////////
969// General OatFile code //
970//////////////////////////
971
972std::string OatFile::ResolveRelativeEncodedDexLocation(
973 const char* abs_dex_location, const std::string& rel_dex_location) {
974 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
975 // Strip :classes<N>.dex used for secondary multidex files.
976 std::string base = DexFile::GetBaseLocation(rel_dex_location);
977 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
978
979 // Check if the base is a suffix of the provided abs_dex_location.
980 std::string target_suffix = "/" + base;
981 std::string abs_location(abs_dex_location);
982 if (abs_location.size() > target_suffix.size()) {
983 size_t pos = abs_location.size() - target_suffix.size();
984 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
985 return abs_location + multidex_suffix;
986 }
987 }
988 }
989 return rel_dex_location;
990}
991
992static void CheckLocation(const std::string& location) {
993 CHECK(!location.empty());
994}
995
996OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100997 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800998 const std::string& location,
999 const char* abs_dex_location,
1000 std::string* error_msg) {
1001 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001002 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001003 ? oat_file.release()
1004 : nullptr;
1005}
1006
David Brazdil7b49e6c2016-09-01 11:06:18 +01001007OatFile* OatFile::Open(const std::string& oat_filename,
1008 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001009 uint8_t* requested_base,
1010 uint8_t* oat_file_begin,
1011 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001012 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001013 const char* abs_dex_location,
1014 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001015 ScopedTrace trace("Open oat file " + oat_location);
1016 CHECK(!oat_filename.empty()) << oat_location;
1017 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001018
David Brazdil7b49e6c2016-09-01 11:06:18 +01001019 std::string vdex_filename = ReplaceFileExtension(oat_filename, "vdex");
1020
1021 // Check that the files even exist, fast-fail.
1022 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
1023 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1024 return nullptr;
1025 } else if (!OS::FileExists(oat_filename.c_str())) {
1026 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001027 return nullptr;
1028 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001029
1030 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1031 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001032 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1033 oat_filename,
1034 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001035 requested_base,
1036 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001037 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001038 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001039 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001040 abs_dex_location,
1041 error_msg);
1042 if (with_dlopen != nullptr) {
1043 return with_dlopen;
1044 }
1045 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001046 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001047 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001048 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1049 //
1050 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1051 //
1052 // We use our own ELF loader for Quick to deal with legacy apps that
1053 // open a generated dex file by name, remove the file, then open
1054 // another generated dex file with the same name. http://b/10614658
1055 //
1056 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1057 //
1058 //
1059 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1060 // 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 +01001061 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1062 oat_filename,
1063 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001064 requested_base,
1065 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001066 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001067 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001068 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001069 abs_dex_location,
1070 error_msg);
1071 return with_internal;
1072}
1073
1074OatFile* OatFile::OpenWritable(File* file,
1075 const std::string& location,
1076 const char* abs_dex_location,
1077 std::string* error_msg) {
1078 CheckLocation(location);
1079 return ElfOatFile::OpenElfFile(file,
1080 location,
1081 nullptr,
1082 nullptr,
1083 true,
1084 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001085 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001086 abs_dex_location,
1087 error_msg);
1088}
1089
1090OatFile* OatFile::OpenReadable(File* file,
1091 const std::string& location,
1092 const char* abs_dex_location,
1093 std::string* error_msg) {
1094 CheckLocation(location);
1095 return ElfOatFile::OpenElfFile(file,
1096 location,
1097 nullptr,
1098 nullptr,
1099 false,
1100 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001101 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001102 abs_dex_location,
1103 error_msg);
1104}
1105
1106OatFile::OatFile(const std::string& location, bool is_executable)
1107 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001108 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001109 begin_(nullptr),
1110 end_(nullptr),
1111 bss_begin_(nullptr),
1112 bss_end_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001113 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001114 is_executable_(is_executable),
1115 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1116 CHECK(!location_.empty());
1117}
1118
1119OatFile::~OatFile() {
1120 STLDeleteElements(&oat_dex_files_storage_);
1121}
1122
Brian Carlstrome24fa612011-09-29 00:53:55 -07001123const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001124 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001125}
1126
Ian Rogers13735952014-10-08 12:43:28 -07001127const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001128 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001129 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001130}
1131
Ian Rogers13735952014-10-08 12:43:28 -07001132const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001133 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001134 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001135}
1136
Vladimir Marko5c42c292015-02-25 12:02:49 +00001137const uint8_t* OatFile::BssBegin() const {
1138 return bss_begin_;
1139}
1140
1141const uint8_t* OatFile::BssEnd() const {
1142 return bss_end_;
1143}
1144
David Brazdil7b49e6c2016-09-01 11:06:18 +01001145const uint8_t* OatFile::DexBegin() const {
1146 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1147}
1148
1149const uint8_t* OatFile::DexEnd() const {
1150 return kIsVdexEnabled ? vdex_->End() : End();
1151}
1152
Vladimir Markoaad75c62016-10-03 08:46:48 +00001153ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1154 if (bss_roots_ != nullptr) {
1155 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1156 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1157 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1158 } else {
1159 return ArrayRef<GcRoot<mirror::Object>>();
1160 }
1161}
1162
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001163const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1164 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001165 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001166 // NOTE: We assume here that the canonical location for a given dex_location never
1167 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1168 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1169 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001170
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001171 // TODO: Additional analysis of usage patterns to see if this can be simplified
1172 // without any performance loss, for example by not doing the first lock-free lookup.
1173
1174 const OatFile::OatDexFile* oat_dex_file = nullptr;
1175 StringPiece key(dex_location);
1176 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1177 // directly mentioned in the oat file and doesn't require locking.
1178 auto primary_it = oat_dex_files_.find(key);
1179 if (primary_it != oat_dex_files_.end()) {
1180 oat_dex_file = primary_it->second;
1181 DCHECK(oat_dex_file != nullptr);
1182 } else {
1183 // This dex_location is not one of the dex locations directly mentioned in the
1184 // oat file. The correct lookup is via the canonical location but first see in
1185 // the secondary_oat_dex_files_ whether we've looked up this location before.
1186 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1187 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1188 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001189 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001190 } else {
1191 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001192 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001193 if (dex_canonical_location != dex_location) {
1194 StringPiece canonical_key(dex_canonical_location);
1195 auto canonical_it = oat_dex_files_.find(canonical_key);
1196 if (canonical_it != oat_dex_files_.end()) {
1197 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001198 } // else keep null.
1199 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001200
1201 // Copy the key to the string_cache_ and store the result in secondary map.
1202 string_cache_.emplace_back(key.data(), key.length());
1203 StringPiece key_copy(string_cache_.back());
1204 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001205 }
1206 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001207
1208 if (oat_dex_file == nullptr) {
1209 if (error_msg != nullptr) {
1210 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1211 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1212 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1213 }
1214 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001215 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001216
Richard Uhler9a37efc2016-08-05 16:32:55 -07001217 if (dex_location_checksum != nullptr &&
1218 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1219 if (error_msg != nullptr) {
1220 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1221 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1222 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1223 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1224 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1225 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001226 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001227 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001228 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001229 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001230}
1231
Brian Carlstrome24fa612011-09-29 00:53:55 -07001232OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001233 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001234 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001235 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001236 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001237 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001238 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001239 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001240 : oat_file_(oat_file),
1241 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001242 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001243 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001244 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001245 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001246 oat_class_offsets_pointer_(oat_class_offsets_pointer),
David Sehr9aa352e2016-09-15 18:13:52 -07001247 dex_cache_arrays_(dex_cache_arrays) {
1248 // Initialize TypeLookupTable.
1249 if (lookup_table_data_ != nullptr) {
1250 // Peek the number of classes from the DexFile.
1251 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1252 const uint32_t num_class_defs = dex_header->class_defs_size_;
1253 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1254 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1255 } else {
Mathieu Chartier1b868492016-11-16 16:22:37 -08001256 lookup_table_ = TypeLookupTable::Open(dex_file_pointer_, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001257 }
1258 }
1259}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001260
Mathieu Chartier1b868492016-11-16 16:22:37 -08001261OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table)
1262 : lookup_table_(std::move(lookup_table)) {}
1263
Brian Carlstrome24fa612011-09-29 00:53:55 -07001264OatFile::OatDexFile::~OatDexFile() {}
1265
Ian Rogers05f28c62012-10-23 18:12:13 -07001266size_t OatFile::OatDexFile::FileSize() const {
1267 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1268}
1269
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001270std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001271 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001272 static constexpr bool kVerify = false;
1273 static constexpr bool kVerifyChecksum = false;
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001274 return DexFile::Open(dex_file_pointer_,
1275 FileSize(),
1276 dex_file_location_,
1277 dex_file_location_checksum_,
1278 this,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001279 kVerify,
1280 kVerifyChecksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001281 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001282}
1283
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1285 return oat_class_offsets_pointer_[class_def_index];
1286}
1287
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001288OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001289 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001290
Ian Rogers13735952014-10-08 12:43:28 -07001291 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001292 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001293
Ian Rogers13735952014-10-08 12:43:28 -07001294 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001295 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1296 mirror::Class::Status status =
1297 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1298 CHECK_LT(status, mirror::Class::kStatusMax);
1299
Ian Rogers13735952014-10-08 12:43:28 -07001300 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001301 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1302 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1303 CHECK_LT(type, kOatClassMax);
1304
Ian Rogers13735952014-10-08 12:43:28 -07001305 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001306 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001307
Brian Carlstromcd937a92014-03-04 22:53:23 -08001308 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001309 const uint8_t* bitmap_pointer = nullptr;
1310 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001311 if (type != kOatClassNoneCompiled) {
1312 if (type == kOatClassSomeCompiled) {
1313 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1314 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1315 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1316 methods_pointer = bitmap_pointer + bitmap_size;
1317 } else {
1318 methods_pointer = after_type_pointer;
1319 }
1320 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001321 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001322
Richard Uhler07b3c232015-03-31 15:57:54 -07001323 return OatFile::OatClass(oat_file_,
1324 status,
1325 type,
1326 bitmap_size,
1327 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1328 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001329}
1330
David Sehr9aa352e2016-09-15 18:13:52 -07001331const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1332 const char* descriptor,
1333 size_t hash) {
1334 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1335 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1336 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1337 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
1338 return (class_def_idx != DexFile::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
1339 }
1340 // Fast path for rare no class defs case.
1341 const uint32_t num_class_defs = dex_file.NumClassDefs();
1342 if (num_class_defs == 0) {
1343 return nullptr;
1344 }
1345 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1346 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001347 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
David Sehr9aa352e2016-09-15 18:13:52 -07001348 return dex_file.FindClassDef(type_idx);
1349 }
1350 return nullptr;
1351}
1352
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001353OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001354 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001355 OatClassType type,
1356 uint32_t bitmap_size,
1357 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001358 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001359 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001360 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001361 switch (type_) {
1362 case kOatClassAllCompiled: {
1363 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001364 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001365 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001366 break;
1367 }
1368 case kOatClassSomeCompiled: {
1369 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001370 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001371 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001372 break;
1373 }
1374 case kOatClassNoneCompiled: {
1375 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001376 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001377 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001378 break;
1379 }
1380 case kOatClassMax: {
1381 LOG(FATAL) << "Invalid OatClassType " << type_;
1382 break;
1383 }
1384 }
1385}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001386
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001387uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1388 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1389 if (oat_method_offsets == nullptr) {
1390 return 0u;
1391 }
1392 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1393}
1394
1395const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001396 // 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 -07001397 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001398 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001399 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001400 }
1401 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001402 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001403 CHECK_EQ(kOatClassAllCompiled, type_);
1404 methods_pointer_index = method_index;
1405 } else {
1406 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001407 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001408 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001409 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001410 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1411 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001412 }
1413 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001414 return &oat_method_offsets;
1415}
1416
1417const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1418 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1419 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001420 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001421 }
1422 if (oat_file_->IsExecutable() ||
1423 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001424 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001425 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001426 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001427 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1428 // version.
1429 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001430}
1431
Mathieu Chartiere401d142015-04-22 13:56:20 -07001432void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001433 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001434 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001435}
1436
Richard Uhlerd1537b52016-03-29 13:27:41 -07001437bool OatFile::HasPatchInfo() const {
1438 return GetOatHeader().HasPatchInfo();
1439}
1440
Andreas Gampe7ba64962014-10-23 11:37:40 -07001441bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001442 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001443 // TODO: Check against oat_patches. b/18144996
1444}
1445
Sebastien Hertz0de11332015-05-13 12:14:05 +02001446bool OatFile::IsDebuggable() const {
1447 return GetOatHeader().IsDebuggable();
1448}
1449
Andreas Gampe29d38e72016-03-23 15:31:51 +00001450CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1451 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001452}
1453
Andreas Gampe7848da42015-04-09 11:15:04 -07001454static constexpr char kDexClassPathEncodingSeparator = '*';
1455
1456std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1457 std::ostringstream out;
1458
1459 for (const DexFile* dex_file : dex_files) {
1460 out << dex_file->GetLocation().c_str();
1461 out << kDexClassPathEncodingSeparator;
1462 out << dex_file->GetLocationChecksum();
1463 out << kDexClassPathEncodingSeparator;
1464 }
1465
1466 return out.str();
1467}
1468
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001469bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001470 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1471 // No dependencies.
1472 return true;
1473 }
1474
1475 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1476 // Split() instead of manual parsing of the combined char*.
1477 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001478 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001479 if (split.size() % 2 != 0) {
1480 // Expected pairs of location and checksum.
1481 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1482 return false;
1483 }
1484
1485 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1486 std::string& location = *it;
1487 std::string& checksum = *(it + 1);
1488 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1489 if (converted == 0) {
1490 // Conversion error.
1491 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1492 return false;
1493 }
1494
1495 uint32_t dex_checksum;
1496 std::string error_msg;
1497 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001498 &dex_checksum,
1499 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001500 if (converted != dex_checksum) {
1501 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1502 location.c_str(), converted, dex_checksum);
1503 return false;
1504 }
1505 } else {
1506 // Problem retrieving checksum.
1507 // TODO: odex files?
1508 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1509 error_msg.c_str());
1510 return false;
1511 }
1512 }
1513
1514 return true;
1515}
1516
1517bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001518 std::vector<std::string>* locations) {
1519 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001520 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1521 return true;
1522 }
1523
1524 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1525 // Split() instead of manual parsing of the combined char*.
1526 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001527 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001528 if (split.size() % 2 != 0) {
1529 // Expected pairs of location and checksum.
1530 return false;
1531 }
1532
1533 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1534 locations->push_back(*it);
1535 }
1536
1537 return true;
1538}
1539
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001540OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1541 uint16_t class_def_idx,
1542 bool* found) {
1543 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
1544 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001545 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001546 *found = false;
1547 return OatFile::OatClass::Invalid();
1548 }
1549 *found = true;
1550 return oat_dex_file->GetOatClass(class_def_idx);
1551}
1552
Mathieu Chartier1b868492016-11-16 16:22:37 -08001553void OatFile::OatDexFile::AssertAotCompiler() {
1554 CHECK(Runtime::Current()->IsAotCompiler());
1555}
1556
Brian Carlstrome24fa612011-09-29 00:53:55 -07001557} // namespace art