blob: ea692cdaaecd2ca26021982f4ab22a10e4174de2 [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"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000044#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070046#include "mirror/object-inl.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010047#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070048#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070049#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070050#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000051#include "type_lookup_table.h"
David Sehr9aa352e2016-09-15 18:13:52 -070052#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010054#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070055
56namespace art {
57
Andreas Gampe049cff02015-12-01 23:27:12 -080058// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000059static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070060
Andreas Gampe049cff02015-12-01 23:27:12 -080061// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070062// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
63// multiple times). However, if/when we switch the above, we likely want to switch this, too,
64// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000065static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070066
67// For debugging, Open will print DlOpen error message if set to true.
68static constexpr bool kPrintDlOpenErrorMessage = false;
69
Andreas Gampe049cff02015-12-01 23:27:12 -080070// Note for OatFileBase and descendents:
71//
72// These are used in OatFile::Open to try all our loaders.
73//
74// The process is simple:
75//
76// 1) Allocate an instance through the standard constructor (location, executable)
77// 2) Load() to try to open the file.
78// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
79// 4) PreSetup() for any steps that should be done before the final setup.
80// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070081
Andreas Gampe049cff02015-12-01 23:27:12 -080082class OatFileBase : public OatFile {
83 public:
84 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070085
Andreas Gampe049cff02015-12-01 23:27:12 -080086 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010087 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
88 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070089 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080090 uint8_t* requested_base,
91 uint8_t* oat_file_begin,
92 bool writable,
93 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080094 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070095 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080096 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080097
Andreas Gampe049cff02015-12-01 23:27:12 -080098 protected:
99 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700100
Andreas Gampe049cff02015-12-01 23:27:12 -0800101 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
102 std::string* error_msg) const = 0;
103
Andreas Gampe4075f832016-05-18 13:09:54 -0700104 virtual void PreLoad() = 0;
105
David Brazdil7b49e6c2016-09-01 11:06:18 +0100106 bool LoadVdex(const std::string& vdex_filename,
107 bool writable,
108 bool low_4gb,
109 std::string* error_msg);
110
Andreas Gampe049cff02015-12-01 23:27:12 -0800111 virtual bool Load(const std::string& elf_filename,
112 uint8_t* oat_file_begin,
113 bool writable,
114 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800115 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800116 std::string* error_msg) = 0;
117
118 bool ComputeFields(uint8_t* requested_base,
119 const std::string& file_path,
120 std::string* error_msg);
121
122 virtual void PreSetup(const std::string& elf_filename) = 0;
123
124 bool Setup(const char* abs_dex_location, std::string* error_msg);
125
126 // Setters exposed for ElfOatFile.
127
128 void SetBegin(const uint8_t* begin) {
129 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700130 }
131
Andreas Gampe049cff02015-12-01 23:27:12 -0800132 void SetEnd(const uint8_t* end) {
133 end_ = end;
134 }
135
David Brazdilc93b3be2016-09-12 18:49:58 +0100136 void SetVdex(VdexFile* vdex) {
137 vdex_.reset(vdex);
138 }
139
Andreas Gampe049cff02015-12-01 23:27:12 -0800140 private:
141 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
142};
143
144template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100145OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
146 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800147 const std::string& location,
148 uint8_t* requested_base,
149 uint8_t* oat_file_begin,
150 bool writable,
151 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800152 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800153 const char* abs_dex_location,
154 std::string* error_msg) {
155 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700156
157 ret->PreLoad();
158
David Brazdil7b49e6c2016-09-01 11:06:18 +0100159 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
160 return nullptr;
161 }
162
Andreas Gampe049cff02015-12-01 23:27:12 -0800163 if (!ret->Load(elf_filename,
164 oat_file_begin,
165 writable,
166 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800167 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800168 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800169 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800170 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800171
Andreas Gampe049cff02015-12-01 23:27:12 -0800172 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
173 return nullptr;
174 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700175
Andreas Gampe049cff02015-12-01 23:27:12 -0800176 ret->PreSetup(elf_filename);
177
178 if (!ret->Setup(abs_dex_location, error_msg)) {
179 return nullptr;
180 }
181
Dave Allison69dfe512014-07-11 17:11:58 +0000182 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800183}
184
David Brazdil7b49e6c2016-09-01 11:06:18 +0100185bool OatFileBase::LoadVdex(const std::string& vdex_filename,
186 bool writable,
187 bool low_4gb,
188 std::string* error_msg) {
189 vdex_.reset(VdexFile::Open(vdex_filename, writable, low_4gb, error_msg));
190 if (vdex_.get() == nullptr) {
191 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
192 vdex_filename.c_str(),
193 error_msg->c_str());
194 return false;
195 }
196 return true;
197}
198
Andreas Gampe049cff02015-12-01 23:27:12 -0800199bool OatFileBase::ComputeFields(uint8_t* requested_base,
200 const std::string& file_path,
201 std::string* error_msg) {
202 std::string symbol_error_msg;
203 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700204 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800205 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
206 file_path.c_str(),
207 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700208 return false;
209 }
210 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000211 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700212 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000213 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
214 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700215 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800216 "oatdata=%p != expected=%p. See process maps in the log.",
217 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700218 return false;
219 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800220 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700221 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800222 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
223 file_path.c_str(),
224 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700225 return false;
226 }
227 // Readjust to be non-inclusive upper bound.
228 end_ += sizeof(uint32_t);
229
Andreas Gampe049cff02015-12-01 23:27:12 -0800230 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700231 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800232 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700233 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700234 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800235 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700236 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800237 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700238 return false;
239 }
240 // Readjust to be non-inclusive upper bound.
241 bss_end_ += sizeof(uint32_t);
242 }
243
Andreas Gampe049cff02015-12-01 23:27:12 -0800244 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800245}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800246
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100247// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
248// position by the number of bytes read, i.e. sizeof(T).
249// Return true on success, false if the read would go beyond the end of the OatFile.
250template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100251inline static bool ReadOatDexFileData(const OatFile& oat_file,
252 /*inout*/const uint8_t** oat,
253 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100254 DCHECK(oat != nullptr);
255 DCHECK(value != nullptr);
256 DCHECK_LE(*oat, oat_file.End());
257 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
258 return false;
259 }
260 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
261 typedef __attribute__((__aligned__(1))) T unaligned_type;
262 *value = *reinterpret_cast<const unaligned_type*>(*oat);
263 *oat += sizeof(T);
264 return true;
265}
266
Andreas Gampe049cff02015-12-01 23:27:12 -0800267bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700268 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800269 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100270 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
271 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800272 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700273 return false;
274 }
Ian Rogers13735952014-10-08 12:43:28 -0700275 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700276 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700277 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700278 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700279 return false;
280 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700281
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700282 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700283 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700284 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100285 "%p + %zu + %u <= %p",
286 GetLocation().c_str(),
287 Begin(),
288 sizeof(OatHeader),
289 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700290 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700291 return false;
292 }
293
Andreas Gampe542451c2016-07-26 09:02:02 -0700294 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100295 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100296 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
297 oat_dex_files_storage_.reserve(dex_file_count);
298 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100299 uint32_t dex_file_location_size;
300 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
301 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
302 "location size",
303 GetLocation().c_str(),
304 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700305 return false;
306 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100307 if (UNLIKELY(dex_file_location_size == 0U)) {
308 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
309 GetLocation().c_str(),
310 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700311 return false;
312 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000313 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100314 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
315 "location",
316 GetLocation().c_str(),
317 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700318 return false;
319 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000320 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
321 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700322
Richard Uhlere5fed032015-03-18 08:21:11 -0700323 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
324 abs_dex_location,
325 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700326
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100327 uint32_t dex_file_checksum;
328 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
329 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
330 "dex file checksum",
331 GetLocation().c_str(),
332 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700333 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700334 return false;
335 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700336
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100337 uint32_t dex_file_offset;
338 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
339 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
340 "after dex file offsets",
341 GetLocation().c_str(),
342 i,
343 dex_file_location.c_str());
344 return false;
345 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700346 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100347 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
348 "file offset",
349 GetLocation().c_str(),
350 i,
351 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700352 return false;
353 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100354 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100355 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
356 "offset %u > %zu",
357 GetLocation().c_str(),
358 i,
359 dex_file_location.c_str(),
360 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100361 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700362 return false;
363 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100364 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000365 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
366 "offset %u of %zu but the size of dex file header is %zu",
367 GetLocation().c_str(),
368 i,
369 dex_file_location.c_str(),
370 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100371 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000372 sizeof(DexFile::Header));
373 return false;
374 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800375
David Brazdil7b49e6c2016-09-01 11:06:18 +0100376 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700377 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100378 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
379 "dex file magic '%s'",
380 GetLocation().c_str(),
381 i,
382 dex_file_location.c_str(),
383 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700384 return false;
385 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700386 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100387 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
388 "dex file version '%s'",
389 GetLocation().c_str(),
390 i,
391 dex_file_location.c_str(),
392 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700393 return false;
394 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800395 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100396 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000397 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
398 "offset %u and size %u truncated at %zu",
399 GetLocation().c_str(),
400 i,
401 dex_file_location.c_str(),
402 dex_file_offset,
403 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100404 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000405 return false;
406 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300407
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000408 uint32_t class_offsets_offset;
409 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
410 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
411 "after class offsets offset",
412 GetLocation().c_str(),
413 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300414 dex_file_location.c_str());
415 return false;
416 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000417 if (UNLIKELY(class_offsets_offset > Size()) ||
418 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
419 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
420 "class offsets, offset %u of %zu, class defs %u",
421 GetLocation().c_str(),
422 i,
423 dex_file_location.c_str(),
424 class_offsets_offset,
425 Size(),
426 header->class_defs_size_);
427 return false;
428 }
429 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
430 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
431 "class offsets, offset %u",
432 GetLocation().c_str(),
433 i,
434 dex_file_location.c_str(),
435 class_offsets_offset);
436 return false;
437 }
438 const uint32_t* class_offsets_pointer =
439 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
440
441 uint32_t lookup_table_offset;
442 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
443 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
444 "after lookup table offset",
445 GetLocation().c_str(),
446 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300447 dex_file_location.c_str());
448 return false;
449 }
450 const uint8_t* lookup_table_data = lookup_table_offset != 0u
451 ? Begin() + lookup_table_offset
452 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000453 if (lookup_table_offset != 0u &&
454 (UNLIKELY(lookup_table_offset > Size()) ||
455 UNLIKELY(Size() - lookup_table_offset <
456 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100457 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000458 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100459 GetLocation().c_str(),
460 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000461 dex_file_location.c_str(),
462 lookup_table_offset,
463 Size(),
464 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700465 return false;
466 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700467
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100468 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100469 if (dex_cache_arrays != nullptr) {
470 DexCacheArraysLayout layout(pointer_size, *header);
471 if (layout.Size() != 0u) {
472 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100473 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
474 "truncated dex cache arrays, %zu < %zu.",
475 GetLocation().c_str(),
476 i,
477 dex_file_location.c_str(),
478 static_cast<size_t>(bss_end_ - dex_cache_arrays),
479 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100480 return false;
481 }
482 current_dex_cache_arrays = dex_cache_arrays;
483 dex_cache_arrays += layout.Size();
484 }
485 }
486
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100487 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
488
489 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100490 OatDexFile* oat_dex_file = new OatDexFile(this,
491 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100492 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100493 dex_file_checksum,
494 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300495 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000496 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100497 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100498 oat_dex_files_storage_.push_back(oat_dex_file);
499
500 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100501 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100502 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100503 if (canonical_location != dex_file_location) {
504 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
505 oat_dex_files_.Put(canonical_key, oat_dex_file);
506 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700507 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100508
509 if (dex_cache_arrays != bss_end_) {
510 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
511 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100512 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100513 GetLocation().c_str(),
514 static_cast<size_t>(bss_end_ - dex_cache_arrays));
515 return false;
516 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700517 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700518}
519
Andreas Gampe049cff02015-12-01 23:27:12 -0800520////////////////////////
521// OatFile via dlopen //
522////////////////////////
523
Andreas Gampe049cff02015-12-01 23:27:12 -0800524class DlOpenOatFile FINAL : public OatFileBase {
525 public:
526 DlOpenOatFile(const std::string& filename, bool executable)
527 : OatFileBase(filename, executable),
528 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700529 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800530 }
531
532 ~DlOpenOatFile() {
533 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700534 if (!kIsTargetBuild) {
535 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
536 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700537 dlclose(dlopen_handle_);
538 } else {
539 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700540 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800541 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800542 }
543
544 protected:
545 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
546 std::string* error_msg) const OVERRIDE {
547 const uint8_t* ptr =
548 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
549 if (ptr == nullptr) {
550 *error_msg = dlerror();
551 }
552 return ptr;
553 }
554
Andreas Gampe4075f832016-05-18 13:09:54 -0700555 void PreLoad() OVERRIDE;
556
Andreas Gampe049cff02015-12-01 23:27:12 -0800557 bool Load(const std::string& elf_filename,
558 uint8_t* oat_file_begin,
559 bool writable,
560 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800561 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800562 std::string* error_msg) OVERRIDE;
563
564 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
565 void PreSetup(const std::string& elf_filename) OVERRIDE;
566
567 private:
568 bool Dlopen(const std::string& elf_filename,
569 uint8_t* oat_file_begin,
570 std::string* error_msg);
571
Richard Uhlera206c742016-05-24 15:04:22 -0700572 // On the host, if the same library is loaded again with dlopen the same
573 // file handle is returned. This differs from the behavior of dlopen on the
574 // target, where dlopen reloads the library at a different address every
575 // time you load it. The runtime relies on the target behavior to ensure
576 // each instance of the loaded library has a unique dex cache. To avoid
577 // problems, we fall back to our own linker in the case when the same
578 // library is opened multiple times on host. dlopen_handles_ is used to
579 // detect that case.
580 // Guarded by host_dlopen_handles_lock_;
581 static std::unordered_set<void*> host_dlopen_handles_;
582
Andreas Gampe049cff02015-12-01 23:27:12 -0800583 // dlopen handle during runtime.
584 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
585
586 // Dummy memory map objects corresponding to the regions mapped by dlopen.
587 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
588
Andreas Gampe4075f832016-05-18 13:09:54 -0700589 // The number of shared objects the linker told us about before loading. Used to
590 // (optimistically) optimize the PreSetup stage (see comment there).
591 size_t shared_objects_before_;
592
Andreas Gampe049cff02015-12-01 23:27:12 -0800593 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
594};
595
Richard Uhlera206c742016-05-24 15:04:22 -0700596std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
597
Andreas Gampe4075f832016-05-18 13:09:54 -0700598void DlOpenOatFile::PreLoad() {
599#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700600 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700601 LOG(FATAL) << "Should not reach here.";
602 UNREACHABLE();
603#else
604 // Count the entries in dl_iterate_phdr we get at this point in time.
605 struct dl_iterate_context {
606 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
607 size_t size ATTRIBUTE_UNUSED,
608 void *data) {
609 reinterpret_cast<dl_iterate_context*>(data)->count++;
610 return 0; // Continue iteration.
611 }
612 size_t count = 0;
613 } context;
614
615 dl_iterate_phdr(dl_iterate_context::callback, &context);
616 shared_objects_before_ = context.count;
617#endif
618}
619
Andreas Gampe049cff02015-12-01 23:27:12 -0800620bool DlOpenOatFile::Load(const std::string& elf_filename,
621 uint8_t* oat_file_begin,
622 bool writable,
623 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800624 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800625 std::string* error_msg) {
626 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
627 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
628 // !executable is a sign that we may want to patch), which may not be allowed for
629 // various reasons.
630 if (!kUseDlopen) {
631 *error_msg = "DlOpen is disabled.";
632 return false;
633 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800634 if (low_4gb) {
635 *error_msg = "DlOpen does not support low 4gb loading.";
636 return false;
637 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800638 if (writable) {
639 *error_msg = "DlOpen does not support writable loading.";
640 return false;
641 }
642 if (!executable) {
643 *error_msg = "DlOpen does not support non-executable loading.";
644 return false;
645 }
646
647 // dlopen always returns the same library if it is already opened on the host. For this reason
648 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
649 // the same library loaded multiple times at different addresses is required for class unloading
650 // and for having dex caches arrays in the .bss section.
651 if (!kIsTargetBuild) {
652 if (!kUseDlopenOnHost) {
653 *error_msg = "DlOpen disabled for host.";
654 return false;
655 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800656 }
657
658 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
659 DCHECK(dlopen_handle_ != nullptr || !success);
660
661 return success;
662}
663
664bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
665 uint8_t* oat_file_begin,
666 std::string* error_msg) {
667#ifdef __APPLE__
668 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
669 // but let's fallback to the custom loading code for the time being.
670 UNUSED(elf_filename, oat_file_begin);
671 *error_msg = "Dlopen unsupported on Mac.";
672 return false;
673#else
674 {
675 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
676 if (absolute_path == nullptr) {
677 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
678 return false;
679 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100680#ifdef ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800681 android_dlextinfo extinfo;
682 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
683 // (open oat files multiple
684 // times).
685 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
686 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800687 if (oat_file_begin != nullptr) { //
688 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
689 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
690 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800691 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
692#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800693 UNUSED(oat_file_begin);
Richard Uhlera206c742016-05-24 15:04:22 -0700694 static_assert(!kIsTargetBuild, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700695 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700696 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
697 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700698 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
699 dlclose(dlopen_handle_);
700 dlopen_handle_ = nullptr;
701 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
702 return false;
703 }
704 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100705#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800706 }
707 if (dlopen_handle_ == nullptr) {
708 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
709 return false;
710 }
711 return true;
712#endif
713}
714
715void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800716#ifdef __APPLE__
717 UNUSED(elf_filename);
718 LOG(FATAL) << "Should not reach here.";
719 UNREACHABLE();
720#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800721 struct dl_iterate_context {
722 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
723 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700724 context->shared_objects_seen++;
725 if (context->shared_objects_seen < context->shared_objects_before) {
726 // We haven't been called yet for anything we haven't seen before. Just continue.
727 // Note: this is aggressively optimistic. If another thread was unloading a library,
728 // we may miss out here. However, this does not happen often in practice.
729 return 0;
730 }
731
Andreas Gampe049cff02015-12-01 23:27:12 -0800732 // See whether this callback corresponds to the file which we have just loaded.
733 bool contains_begin = false;
734 for (int i = 0; i < info->dlpi_phnum; i++) {
735 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
736 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
737 info->dlpi_phdr[i].p_vaddr);
738 size_t memsz = info->dlpi_phdr[i].p_memsz;
739 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
740 contains_begin = true;
741 break;
742 }
743 }
744 }
745 // Add dummy mmaps for this file.
746 if (contains_begin) {
747 for (int i = 0; i < info->dlpi_phnum; i++) {
748 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
749 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
750 info->dlpi_phdr[i].p_vaddr);
751 size_t memsz = info->dlpi_phdr[i].p_memsz;
752 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
753 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
754 }
755 }
756 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
757 }
758 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
759 }
760 const uint8_t* const begin_;
761 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700762 const size_t shared_objects_before;
763 size_t shared_objects_seen;
764 };
765 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800766
767 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700768 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
769 // before giving up. This should be unusual.
770 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
771 << shared_objects_before_;
772 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
773 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
774 // OK, give up and print an error.
775 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
776 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
777 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800778 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800779#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800780}
781
782////////////////////////////////////////////////
783// OatFile via our own ElfFile implementation //
784////////////////////////////////////////////////
785
786class ElfOatFile FINAL : public OatFileBase {
787 public:
788 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
789
790 static ElfOatFile* OpenElfFile(File* file,
791 const std::string& location,
792 uint8_t* requested_base,
793 uint8_t* oat_file_begin, // Override base if not null
794 bool writable,
795 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800796 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800797 const char* abs_dex_location,
798 std::string* error_msg);
799
800 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100801 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800802 const char* abs_dex_location,
803 std::string* error_msg);
804
805 protected:
806 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
807 std::string* error_msg) const OVERRIDE {
808 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
809 if (ptr == nullptr) {
810 *error_msg = "(Internal implementation could not find symbol)";
811 }
812 return ptr;
813 }
814
Andreas Gampe4075f832016-05-18 13:09:54 -0700815 void PreLoad() OVERRIDE {
816 }
817
Andreas Gampe049cff02015-12-01 23:27:12 -0800818 bool Load(const std::string& elf_filename,
819 uint8_t* oat_file_begin, // Override where the file is loaded to 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 std::string* error_msg) OVERRIDE;
824
825 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
826 }
827
828 private:
829 bool ElfFileOpen(File* file,
830 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
831 bool writable,
832 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800833 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800834 std::string* error_msg);
835
836 private:
837 // Backing memory map for oat file during cross compilation.
838 std::unique_ptr<ElfFile> elf_file_;
839
840 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
841};
842
843ElfOatFile* ElfOatFile::OpenElfFile(File* file,
844 const std::string& location,
845 uint8_t* requested_base,
846 uint8_t* oat_file_begin, // Override base if not null
847 bool writable,
848 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800849 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800850 const char* abs_dex_location,
851 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800852 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800853 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800854 bool success = oat_file->ElfFileOpen(file,
855 oat_file_begin,
856 writable,
857 low_4gb,
858 executable,
859 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800860 if (!success) {
861 CHECK(!error_msg->empty());
862 return nullptr;
863 }
864
865 // Complete the setup.
866 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
867 return nullptr;
868 }
869
870 if (!oat_file->Setup(abs_dex_location, error_msg)) {
871 return nullptr;
872 }
873
874 return oat_file.release();
875}
876
877bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100878 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800879 const char* abs_dex_location,
880 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800881 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800882 if (IsExecutable()) {
883 *error_msg = "Cannot initialize from elf file in executable mode.";
884 return false;
885 }
886 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +0100887 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -0800888 uint64_t offset, size;
889 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
890 CHECK(has_section);
891 SetBegin(elf_file->Begin() + offset);
892 SetEnd(elf_file->Begin() + size + offset);
893 // Ignore the optional .bss section when opening non-executable.
894 return Setup(abs_dex_location, error_msg);
895}
896
897bool ElfOatFile::Load(const std::string& elf_filename,
898 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
899 bool writable,
900 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800901 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800902 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800903 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800904 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
905 if (file == nullptr) {
906 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
907 return false;
908 }
909 return ElfOatFile::ElfFileOpen(file.get(),
910 oat_file_begin,
911 writable,
912 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800913 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800914 error_msg);
915}
916
917bool ElfOatFile::ElfFileOpen(File* file,
918 uint8_t* oat_file_begin,
919 bool writable,
920 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800921 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800922 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800923 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800924 // TODO: rename requested_base to oat_data_begin
925 elf_file_.reset(ElfFile::Open(file,
926 writable,
927 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800928 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800929 error_msg,
930 oat_file_begin));
931 if (elf_file_ == nullptr) {
932 DCHECK(!error_msg->empty());
933 return false;
934 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800935 bool loaded = elf_file_->Load(executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800936 DCHECK(loaded || !error_msg->empty());
937 return loaded;
938}
939
940//////////////////////////
941// General OatFile code //
942//////////////////////////
943
944std::string OatFile::ResolveRelativeEncodedDexLocation(
945 const char* abs_dex_location, const std::string& rel_dex_location) {
946 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
947 // Strip :classes<N>.dex used for secondary multidex files.
948 std::string base = DexFile::GetBaseLocation(rel_dex_location);
949 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
950
951 // Check if the base is a suffix of the provided abs_dex_location.
952 std::string target_suffix = "/" + base;
953 std::string abs_location(abs_dex_location);
954 if (abs_location.size() > target_suffix.size()) {
955 size_t pos = abs_location.size() - target_suffix.size();
956 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
957 return abs_location + multidex_suffix;
958 }
959 }
960 }
961 return rel_dex_location;
962}
963
964static void CheckLocation(const std::string& location) {
965 CHECK(!location.empty());
966}
967
968OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100969 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800970 const std::string& location,
971 const char* abs_dex_location,
972 std::string* error_msg) {
973 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +0100974 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -0800975 ? oat_file.release()
976 : nullptr;
977}
978
David Brazdil7b49e6c2016-09-01 11:06:18 +0100979OatFile* OatFile::Open(const std::string& oat_filename,
980 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800981 uint8_t* requested_base,
982 uint8_t* oat_file_begin,
983 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800984 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800985 const char* abs_dex_location,
986 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100987 ScopedTrace trace("Open oat file " + oat_location);
988 CHECK(!oat_filename.empty()) << oat_location;
989 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -0700990
David Brazdil7b49e6c2016-09-01 11:06:18 +0100991 std::string vdex_filename = ReplaceFileExtension(oat_filename, "vdex");
992
993 // Check that the files even exist, fast-fail.
994 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
995 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
996 return nullptr;
997 } else if (!OS::FileExists(oat_filename.c_str())) {
998 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -0700999 return nullptr;
1000 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001001
1002 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1003 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001004 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1005 oat_filename,
1006 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001007 requested_base,
1008 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001009 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001010 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001011 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001012 abs_dex_location,
1013 error_msg);
1014 if (with_dlopen != nullptr) {
1015 return with_dlopen;
1016 }
1017 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001018 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001019 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001020 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1021 //
1022 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1023 //
1024 // We use our own ELF loader for Quick to deal with legacy apps that
1025 // open a generated dex file by name, remove the file, then open
1026 // another generated dex file with the same name. http://b/10614658
1027 //
1028 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1029 //
1030 //
1031 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1032 // 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 +01001033 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1034 oat_filename,
1035 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001036 requested_base,
1037 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001038 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001039 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001040 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001041 abs_dex_location,
1042 error_msg);
1043 return with_internal;
1044}
1045
1046OatFile* OatFile::OpenWritable(File* file,
1047 const std::string& location,
1048 const char* abs_dex_location,
1049 std::string* error_msg) {
1050 CheckLocation(location);
1051 return ElfOatFile::OpenElfFile(file,
1052 location,
1053 nullptr,
1054 nullptr,
1055 true,
1056 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001057 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001058 abs_dex_location,
1059 error_msg);
1060}
1061
1062OatFile* OatFile::OpenReadable(File* file,
1063 const std::string& location,
1064 const char* abs_dex_location,
1065 std::string* error_msg) {
1066 CheckLocation(location);
1067 return ElfOatFile::OpenElfFile(file,
1068 location,
1069 nullptr,
1070 nullptr,
1071 false,
1072 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001073 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001074 abs_dex_location,
1075 error_msg);
1076}
1077
1078OatFile::OatFile(const std::string& location, bool is_executable)
1079 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001080 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001081 begin_(nullptr),
1082 end_(nullptr),
1083 bss_begin_(nullptr),
1084 bss_end_(nullptr),
1085 is_executable_(is_executable),
1086 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1087 CHECK(!location_.empty());
1088}
1089
1090OatFile::~OatFile() {
1091 STLDeleteElements(&oat_dex_files_storage_);
1092}
1093
Brian Carlstrome24fa612011-09-29 00:53:55 -07001094const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001095 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001096}
1097
Ian Rogers13735952014-10-08 12:43:28 -07001098const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001099 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001100 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001101}
1102
Ian Rogers13735952014-10-08 12:43:28 -07001103const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001104 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001105 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001106}
1107
Vladimir Marko5c42c292015-02-25 12:02:49 +00001108const uint8_t* OatFile::BssBegin() const {
1109 return bss_begin_;
1110}
1111
1112const uint8_t* OatFile::BssEnd() const {
1113 return bss_end_;
1114}
1115
David Brazdil7b49e6c2016-09-01 11:06:18 +01001116const uint8_t* OatFile::DexBegin() const {
1117 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1118}
1119
1120const uint8_t* OatFile::DexEnd() const {
1121 return kIsVdexEnabled ? vdex_->End() : End();
1122}
1123
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001124const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1125 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001126 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001127 // NOTE: We assume here that the canonical location for a given dex_location never
1128 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1129 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1130 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001131
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001132 // TODO: Additional analysis of usage patterns to see if this can be simplified
1133 // without any performance loss, for example by not doing the first lock-free lookup.
1134
1135 const OatFile::OatDexFile* oat_dex_file = nullptr;
1136 StringPiece key(dex_location);
1137 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1138 // directly mentioned in the oat file and doesn't require locking.
1139 auto primary_it = oat_dex_files_.find(key);
1140 if (primary_it != oat_dex_files_.end()) {
1141 oat_dex_file = primary_it->second;
1142 DCHECK(oat_dex_file != nullptr);
1143 } else {
1144 // This dex_location is not one of the dex locations directly mentioned in the
1145 // oat file. The correct lookup is via the canonical location but first see in
1146 // the secondary_oat_dex_files_ whether we've looked up this location before.
1147 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1148 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1149 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001150 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001151 } else {
1152 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001153 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001154 if (dex_canonical_location != dex_location) {
1155 StringPiece canonical_key(dex_canonical_location);
1156 auto canonical_it = oat_dex_files_.find(canonical_key);
1157 if (canonical_it != oat_dex_files_.end()) {
1158 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001159 } // else keep null.
1160 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001161
1162 // Copy the key to the string_cache_ and store the result in secondary map.
1163 string_cache_.emplace_back(key.data(), key.length());
1164 StringPiece key_copy(string_cache_.back());
1165 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001166 }
1167 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001168
1169 if (oat_dex_file == nullptr) {
1170 if (error_msg != nullptr) {
1171 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1172 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1173 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1174 }
1175 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001176 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001177
Richard Uhler9a37efc2016-08-05 16:32:55 -07001178 if (dex_location_checksum != nullptr &&
1179 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1180 if (error_msg != nullptr) {
1181 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1182 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1183 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1184 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1185 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1186 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001187 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001188 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001189 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001190 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001191}
1192
Brian Carlstrome24fa612011-09-29 00:53:55 -07001193OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001194 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001195 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001196 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001197 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001198 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001199 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001200 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001201 : oat_file_(oat_file),
1202 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001203 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001204 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001205 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001206 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001207 oat_class_offsets_pointer_(oat_class_offsets_pointer),
David Sehr9aa352e2016-09-15 18:13:52 -07001208 dex_cache_arrays_(dex_cache_arrays) {
1209 // Initialize TypeLookupTable.
1210 if (lookup_table_data_ != nullptr) {
1211 // Peek the number of classes from the DexFile.
1212 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1213 const uint32_t num_class_defs = dex_header->class_defs_size_;
1214 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1215 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1216 } else {
1217 lookup_table_.reset(TypeLookupTable::Open(dex_file_pointer_,
1218 lookup_table_data_,
1219 num_class_defs));
1220 }
1221 }
1222}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001223
1224OatFile::OatDexFile::~OatDexFile() {}
1225
Ian Rogers05f28c62012-10-23 18:12:13 -07001226size_t OatFile::OatDexFile::FileSize() const {
1227 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1228}
1229
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001230std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001231 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001232 static constexpr bool kVerify = false;
1233 static constexpr bool kVerifyChecksum = false;
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001234 return DexFile::Open(dex_file_pointer_,
1235 FileSize(),
1236 dex_file_location_,
1237 dex_file_location_checksum_,
1238 this,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001239 kVerify,
1240 kVerifyChecksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001241 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001242}
1243
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001244uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1245 return oat_class_offsets_pointer_[class_def_index];
1246}
1247
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001248OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001250
Ian Rogers13735952014-10-08 12:43:28 -07001251 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001252 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001253
Ian Rogers13735952014-10-08 12:43:28 -07001254 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001255 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1256 mirror::Class::Status status =
1257 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1258 CHECK_LT(status, mirror::Class::kStatusMax);
1259
Ian Rogers13735952014-10-08 12:43:28 -07001260 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001261 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1262 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1263 CHECK_LT(type, kOatClassMax);
1264
Ian Rogers13735952014-10-08 12:43:28 -07001265 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001266 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001267
Brian Carlstromcd937a92014-03-04 22:53:23 -08001268 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001269 const uint8_t* bitmap_pointer = nullptr;
1270 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001271 if (type != kOatClassNoneCompiled) {
1272 if (type == kOatClassSomeCompiled) {
1273 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1274 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1275 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1276 methods_pointer = bitmap_pointer + bitmap_size;
1277 } else {
1278 methods_pointer = after_type_pointer;
1279 }
1280 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001281 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001282
Richard Uhler07b3c232015-03-31 15:57:54 -07001283 return OatFile::OatClass(oat_file_,
1284 status,
1285 type,
1286 bitmap_size,
1287 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1288 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001289}
1290
David Sehr9aa352e2016-09-15 18:13:52 -07001291const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1292 const char* descriptor,
1293 size_t hash) {
1294 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1295 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
1296 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
1297 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
1298 return (class_def_idx != DexFile::kDexNoIndex) ? &dex_file.GetClassDef(class_def_idx) : nullptr;
1299 }
1300 // Fast path for rare no class defs case.
1301 const uint32_t num_class_defs = dex_file.NumClassDefs();
1302 if (num_class_defs == 0) {
1303 return nullptr;
1304 }
1305 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1306 if (type_id != nullptr) {
1307 uint16_t type_idx = dex_file.GetIndexForTypeId(*type_id);
1308 return dex_file.FindClassDef(type_idx);
1309 }
1310 return nullptr;
1311}
1312
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001313OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001314 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001315 OatClassType type,
1316 uint32_t bitmap_size,
1317 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001318 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001319 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001320 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001321 switch (type_) {
1322 case kOatClassAllCompiled: {
1323 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001324 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001325 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001326 break;
1327 }
1328 case kOatClassSomeCompiled: {
1329 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001330 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001331 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001332 break;
1333 }
1334 case kOatClassNoneCompiled: {
1335 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001336 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001337 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001338 break;
1339 }
1340 case kOatClassMax: {
1341 LOG(FATAL) << "Invalid OatClassType " << type_;
1342 break;
1343 }
1344 }
1345}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001346
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001347uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1348 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1349 if (oat_method_offsets == nullptr) {
1350 return 0u;
1351 }
1352 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1353}
1354
1355const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001356 // 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 -07001357 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001358 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001359 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001360 }
1361 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001362 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001363 CHECK_EQ(kOatClassAllCompiled, type_);
1364 methods_pointer_index = method_index;
1365 } else {
1366 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001367 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001368 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001369 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001370 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1371 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001372 }
1373 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001374 return &oat_method_offsets;
1375}
1376
1377const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1378 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1379 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001380 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001381 }
1382 if (oat_file_->IsExecutable() ||
1383 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001384 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001385 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001386 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001387 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1388 // version.
1389 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001390}
1391
Mathieu Chartiere401d142015-04-22 13:56:20 -07001392void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001393 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001394 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001395}
1396
Richard Uhlerd1537b52016-03-29 13:27:41 -07001397bool OatFile::HasPatchInfo() const {
1398 return GetOatHeader().HasPatchInfo();
1399}
1400
Andreas Gampe7ba64962014-10-23 11:37:40 -07001401bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001402 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001403 // TODO: Check against oat_patches. b/18144996
1404}
1405
Sebastien Hertz0de11332015-05-13 12:14:05 +02001406bool OatFile::IsDebuggable() const {
1407 return GetOatHeader().IsDebuggable();
1408}
1409
Andreas Gampe29d38e72016-03-23 15:31:51 +00001410CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1411 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001412}
1413
Andreas Gampe7848da42015-04-09 11:15:04 -07001414static constexpr char kDexClassPathEncodingSeparator = '*';
1415
1416std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1417 std::ostringstream out;
1418
1419 for (const DexFile* dex_file : dex_files) {
1420 out << dex_file->GetLocation().c_str();
1421 out << kDexClassPathEncodingSeparator;
1422 out << dex_file->GetLocationChecksum();
1423 out << kDexClassPathEncodingSeparator;
1424 }
1425
1426 return out.str();
1427}
1428
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001429bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001430 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1431 // No dependencies.
1432 return true;
1433 }
1434
1435 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1436 // Split() instead of manual parsing of the combined char*.
1437 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001438 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001439 if (split.size() % 2 != 0) {
1440 // Expected pairs of location and checksum.
1441 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1442 return false;
1443 }
1444
1445 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1446 std::string& location = *it;
1447 std::string& checksum = *(it + 1);
1448 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1449 if (converted == 0) {
1450 // Conversion error.
1451 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1452 return false;
1453 }
1454
1455 uint32_t dex_checksum;
1456 std::string error_msg;
1457 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001458 &dex_checksum,
1459 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001460 if (converted != dex_checksum) {
1461 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1462 location.c_str(), converted, dex_checksum);
1463 return false;
1464 }
1465 } else {
1466 // Problem retrieving checksum.
1467 // TODO: odex files?
1468 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1469 error_msg.c_str());
1470 return false;
1471 }
1472 }
1473
1474 return true;
1475}
1476
1477bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001478 std::vector<std::string>* locations) {
1479 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001480 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1481 return true;
1482 }
1483
1484 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1485 // Split() instead of manual parsing of the combined char*.
1486 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001487 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001488 if (split.size() % 2 != 0) {
1489 // Expected pairs of location and checksum.
1490 return false;
1491 }
1492
1493 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1494 locations->push_back(*it);
1495 }
1496
1497 return true;
1498}
1499
Brian Carlstrome24fa612011-09-29 00:53:55 -07001500} // namespace art