blob: 76b71a3271d6bc259963edc8993d432f87acac65 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010053#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070054
55namespace art {
56
Andreas Gampe049cff02015-12-01 23:27:12 -080057// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000058static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070059
Andreas Gampe049cff02015-12-01 23:27:12 -080060// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070061// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
62// multiple times). However, if/when we switch the above, we likely want to switch this, too,
63// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000064static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070065
66// For debugging, Open will print DlOpen error message if set to true.
67static constexpr bool kPrintDlOpenErrorMessage = false;
68
Andreas Gampe049cff02015-12-01 23:27:12 -080069// Note for OatFileBase and descendents:
70//
71// These are used in OatFile::Open to try all our loaders.
72//
73// The process is simple:
74//
75// 1) Allocate an instance through the standard constructor (location, executable)
76// 2) Load() to try to open the file.
77// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
78// 4) PreSetup() for any steps that should be done before the final setup.
79// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070080
Andreas Gampe049cff02015-12-01 23:27:12 -080081class OatFileBase : public OatFile {
82 public:
83 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070084
Andreas Gampe049cff02015-12-01 23:27:12 -080085 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +010086 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
87 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070088 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080089 uint8_t* requested_base,
90 uint8_t* oat_file_begin,
91 bool writable,
92 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080093 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070094 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080095 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080096
Andreas Gampe049cff02015-12-01 23:27:12 -080097 protected:
98 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -070099
Andreas Gampe049cff02015-12-01 23:27:12 -0800100 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
101 std::string* error_msg) const = 0;
102
Andreas Gampe4075f832016-05-18 13:09:54 -0700103 virtual void PreLoad() = 0;
104
David Brazdil7b49e6c2016-09-01 11:06:18 +0100105 bool LoadVdex(const std::string& vdex_filename,
106 bool writable,
107 bool low_4gb,
108 std::string* error_msg);
109
Andreas Gampe049cff02015-12-01 23:27:12 -0800110 virtual bool Load(const std::string& elf_filename,
111 uint8_t* oat_file_begin,
112 bool writable,
113 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800114 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800115 std::string* error_msg) = 0;
116
117 bool ComputeFields(uint8_t* requested_base,
118 const std::string& file_path,
119 std::string* error_msg);
120
121 virtual void PreSetup(const std::string& elf_filename) = 0;
122
123 bool Setup(const char* abs_dex_location, std::string* error_msg);
124
125 // Setters exposed for ElfOatFile.
126
127 void SetBegin(const uint8_t* begin) {
128 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700129 }
130
Andreas Gampe049cff02015-12-01 23:27:12 -0800131 void SetEnd(const uint8_t* end) {
132 end_ = end;
133 }
134
David Brazdilc93b3be2016-09-12 18:49:58 +0100135 void SetVdex(VdexFile* vdex) {
136 vdex_.reset(vdex);
137 }
138
Andreas Gampe049cff02015-12-01 23:27:12 -0800139 private:
140 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
141};
142
143template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100144OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
145 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800146 const std::string& location,
147 uint8_t* requested_base,
148 uint8_t* oat_file_begin,
149 bool writable,
150 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800151 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800152 const char* abs_dex_location,
153 std::string* error_msg) {
154 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700155
156 ret->PreLoad();
157
David Brazdil7b49e6c2016-09-01 11:06:18 +0100158 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
159 return nullptr;
160 }
161
Andreas Gampe049cff02015-12-01 23:27:12 -0800162 if (!ret->Load(elf_filename,
163 oat_file_begin,
164 writable,
165 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800166 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800167 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800168 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800169 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800170
Andreas Gampe049cff02015-12-01 23:27:12 -0800171 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
172 return nullptr;
173 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700174
Andreas Gampe049cff02015-12-01 23:27:12 -0800175 ret->PreSetup(elf_filename);
176
177 if (!ret->Setup(abs_dex_location, error_msg)) {
178 return nullptr;
179 }
180
Dave Allison69dfe512014-07-11 17:11:58 +0000181 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800182}
183
David Brazdil7b49e6c2016-09-01 11:06:18 +0100184bool OatFileBase::LoadVdex(const std::string& vdex_filename,
185 bool writable,
186 bool low_4gb,
187 std::string* error_msg) {
188 vdex_.reset(VdexFile::Open(vdex_filename, writable, low_4gb, error_msg));
189 if (vdex_.get() == nullptr) {
190 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
191 vdex_filename.c_str(),
192 error_msg->c_str());
193 return false;
194 }
195 return true;
196}
197
Andreas Gampe049cff02015-12-01 23:27:12 -0800198bool OatFileBase::ComputeFields(uint8_t* requested_base,
199 const std::string& file_path,
200 std::string* error_msg) {
201 std::string symbol_error_msg;
202 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700203 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800204 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
205 file_path.c_str(),
206 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700207 return false;
208 }
209 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000210 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700211 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000212 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
213 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700214 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800215 "oatdata=%p != expected=%p. See process maps in the log.",
216 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700217 return false;
218 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800219 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700220 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800221 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
222 file_path.c_str(),
223 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700224 return false;
225 }
226 // Readjust to be non-inclusive upper bound.
227 end_ += sizeof(uint32_t);
228
Andreas Gampe049cff02015-12-01 23:27:12 -0800229 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700230 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800231 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700232 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700233 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800234 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700235 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800236 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700237 return false;
238 }
239 // Readjust to be non-inclusive upper bound.
240 bss_end_ += sizeof(uint32_t);
241 }
242
Andreas Gampe049cff02015-12-01 23:27:12 -0800243 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800244}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800245
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100246// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
247// position by the number of bytes read, i.e. sizeof(T).
248// Return true on success, false if the read would go beyond the end of the OatFile.
249template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100250inline static bool ReadOatDexFileData(const OatFile& oat_file,
251 /*inout*/const uint8_t** oat,
252 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100253 DCHECK(oat != nullptr);
254 DCHECK(value != nullptr);
255 DCHECK_LE(*oat, oat_file.End());
256 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
257 return false;
258 }
259 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
260 typedef __attribute__((__aligned__(1))) T unaligned_type;
261 *value = *reinterpret_cast<const unaligned_type*>(*oat);
262 *oat += sizeof(T);
263 return true;
264}
265
Andreas Gampe049cff02015-12-01 23:27:12 -0800266bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700267 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800268 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100269 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
270 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800271 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700272 return false;
273 }
Ian Rogers13735952014-10-08 12:43:28 -0700274 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700276 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700278 return false;
279 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700280
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700281 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700282 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700283 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100284 "%p + %zu + %u <= %p",
285 GetLocation().c_str(),
286 Begin(),
287 sizeof(OatHeader),
288 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700289 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700290 return false;
291 }
292
Andreas Gampe542451c2016-07-26 09:02:02 -0700293 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100294 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100295 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
296 oat_dex_files_storage_.reserve(dex_file_count);
297 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100298 uint32_t dex_file_location_size;
299 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
300 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
301 "location size",
302 GetLocation().c_str(),
303 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700304 return false;
305 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100306 if (UNLIKELY(dex_file_location_size == 0U)) {
307 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
308 GetLocation().c_str(),
309 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700310 return false;
311 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000312 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100313 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
314 "location",
315 GetLocation().c_str(),
316 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700317 return false;
318 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000319 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
320 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700321
Richard Uhlere5fed032015-03-18 08:21:11 -0700322 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
323 abs_dex_location,
324 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700325
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100326 uint32_t dex_file_checksum;
327 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
328 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
329 "dex file checksum",
330 GetLocation().c_str(),
331 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700333 return false;
334 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700335
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100336 uint32_t dex_file_offset;
337 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
338 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
339 "after dex file offsets",
340 GetLocation().c_str(),
341 i,
342 dex_file_location.c_str());
343 return false;
344 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700345 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100346 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
347 "file offset",
348 GetLocation().c_str(),
349 i,
350 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700351 return false;
352 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100353 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100354 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
355 "offset %u > %zu",
356 GetLocation().c_str(),
357 i,
358 dex_file_location.c_str(),
359 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100360 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700361 return false;
362 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100363 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000364 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
365 "offset %u of %zu but the size of dex file header is %zu",
366 GetLocation().c_str(),
367 i,
368 dex_file_location.c_str(),
369 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100370 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000371 sizeof(DexFile::Header));
372 return false;
373 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800374
David Brazdil7b49e6c2016-09-01 11:06:18 +0100375 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700376 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100377 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
378 "dex file magic '%s'",
379 GetLocation().c_str(),
380 i,
381 dex_file_location.c_str(),
382 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700383 return false;
384 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700385 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100386 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
387 "dex file version '%s'",
388 GetLocation().c_str(),
389 i,
390 dex_file_location.c_str(),
391 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700392 return false;
393 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800394 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100395 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000396 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
397 "offset %u and size %u truncated at %zu",
398 GetLocation().c_str(),
399 i,
400 dex_file_location.c_str(),
401 dex_file_offset,
402 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100403 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000404 return false;
405 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300406
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000407 uint32_t class_offsets_offset;
408 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
409 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
410 "after class offsets offset",
411 GetLocation().c_str(),
412 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300413 dex_file_location.c_str());
414 return false;
415 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000416 if (UNLIKELY(class_offsets_offset > Size()) ||
417 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
418 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
419 "class offsets, offset %u of %zu, class defs %u",
420 GetLocation().c_str(),
421 i,
422 dex_file_location.c_str(),
423 class_offsets_offset,
424 Size(),
425 header->class_defs_size_);
426 return false;
427 }
428 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
429 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
430 "class offsets, offset %u",
431 GetLocation().c_str(),
432 i,
433 dex_file_location.c_str(),
434 class_offsets_offset);
435 return false;
436 }
437 const uint32_t* class_offsets_pointer =
438 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
439
440 uint32_t lookup_table_offset;
441 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
442 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
443 "after lookup table offset",
444 GetLocation().c_str(),
445 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300446 dex_file_location.c_str());
447 return false;
448 }
449 const uint8_t* lookup_table_data = lookup_table_offset != 0u
450 ? Begin() + lookup_table_offset
451 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000452 if (lookup_table_offset != 0u &&
453 (UNLIKELY(lookup_table_offset > Size()) ||
454 UNLIKELY(Size() - lookup_table_offset <
455 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100456 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000457 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100458 GetLocation().c_str(),
459 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000460 dex_file_location.c_str(),
461 lookup_table_offset,
462 Size(),
463 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700464 return false;
465 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700466
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100467 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100468 if (dex_cache_arrays != nullptr) {
469 DexCacheArraysLayout layout(pointer_size, *header);
470 if (layout.Size() != 0u) {
471 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100472 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
473 "truncated dex cache arrays, %zu < %zu.",
474 GetLocation().c_str(),
475 i,
476 dex_file_location.c_str(),
477 static_cast<size_t>(bss_end_ - dex_cache_arrays),
478 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100479 return false;
480 }
481 current_dex_cache_arrays = dex_cache_arrays;
482 dex_cache_arrays += layout.Size();
483 }
484 }
485
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100486 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
487
488 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100489 OatDexFile* oat_dex_file = new OatDexFile(this,
490 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100491 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100492 dex_file_checksum,
493 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300494 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000495 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100496 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100497 oat_dex_files_storage_.push_back(oat_dex_file);
498
499 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100500 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100501 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100502 if (canonical_location != dex_file_location) {
503 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
504 oat_dex_files_.Put(canonical_key, oat_dex_file);
505 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700506 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100507
508 if (dex_cache_arrays != bss_end_) {
509 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
510 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100511 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100512 GetLocation().c_str(),
513 static_cast<size_t>(bss_end_ - dex_cache_arrays));
514 return false;
515 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700516 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700517}
518
Andreas Gampe049cff02015-12-01 23:27:12 -0800519////////////////////////
520// OatFile via dlopen //
521////////////////////////
522
Andreas Gampe049cff02015-12-01 23:27:12 -0800523class DlOpenOatFile FINAL : public OatFileBase {
524 public:
525 DlOpenOatFile(const std::string& filename, bool executable)
526 : OatFileBase(filename, executable),
527 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700528 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800529 }
530
531 ~DlOpenOatFile() {
532 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700533 if (!kIsTargetBuild) {
534 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
535 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700536 dlclose(dlopen_handle_);
537 } else {
538 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700539 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800540 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800541 }
542
543 protected:
544 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
545 std::string* error_msg) const OVERRIDE {
546 const uint8_t* ptr =
547 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
548 if (ptr == nullptr) {
549 *error_msg = dlerror();
550 }
551 return ptr;
552 }
553
Andreas Gampe4075f832016-05-18 13:09:54 -0700554 void PreLoad() OVERRIDE;
555
Andreas Gampe049cff02015-12-01 23:27:12 -0800556 bool Load(const std::string& elf_filename,
557 uint8_t* oat_file_begin,
558 bool writable,
559 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800560 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800561 std::string* error_msg) OVERRIDE;
562
563 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
564 void PreSetup(const std::string& elf_filename) OVERRIDE;
565
566 private:
567 bool Dlopen(const std::string& elf_filename,
568 uint8_t* oat_file_begin,
569 std::string* error_msg);
570
Richard Uhlera206c742016-05-24 15:04:22 -0700571 // On the host, if the same library is loaded again with dlopen the same
572 // file handle is returned. This differs from the behavior of dlopen on the
573 // target, where dlopen reloads the library at a different address every
574 // time you load it. The runtime relies on the target behavior to ensure
575 // each instance of the loaded library has a unique dex cache. To avoid
576 // problems, we fall back to our own linker in the case when the same
577 // library is opened multiple times on host. dlopen_handles_ is used to
578 // detect that case.
579 // Guarded by host_dlopen_handles_lock_;
580 static std::unordered_set<void*> host_dlopen_handles_;
581
Andreas Gampe049cff02015-12-01 23:27:12 -0800582 // dlopen handle during runtime.
583 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
584
585 // Dummy memory map objects corresponding to the regions mapped by dlopen.
586 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
587
Andreas Gampe4075f832016-05-18 13:09:54 -0700588 // The number of shared objects the linker told us about before loading. Used to
589 // (optimistically) optimize the PreSetup stage (see comment there).
590 size_t shared_objects_before_;
591
Andreas Gampe049cff02015-12-01 23:27:12 -0800592 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
593};
594
Richard Uhlera206c742016-05-24 15:04:22 -0700595std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
596
Andreas Gampe4075f832016-05-18 13:09:54 -0700597void DlOpenOatFile::PreLoad() {
598#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700599 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700600 LOG(FATAL) << "Should not reach here.";
601 UNREACHABLE();
602#else
603 // Count the entries in dl_iterate_phdr we get at this point in time.
604 struct dl_iterate_context {
605 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
606 size_t size ATTRIBUTE_UNUSED,
607 void *data) {
608 reinterpret_cast<dl_iterate_context*>(data)->count++;
609 return 0; // Continue iteration.
610 }
611 size_t count = 0;
612 } context;
613
614 dl_iterate_phdr(dl_iterate_context::callback, &context);
615 shared_objects_before_ = context.count;
616#endif
617}
618
Andreas Gampe049cff02015-12-01 23:27:12 -0800619bool DlOpenOatFile::Load(const std::string& elf_filename,
620 uint8_t* oat_file_begin,
621 bool writable,
622 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800623 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800624 std::string* error_msg) {
625 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
626 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
627 // !executable is a sign that we may want to patch), which may not be allowed for
628 // various reasons.
629 if (!kUseDlopen) {
630 *error_msg = "DlOpen is disabled.";
631 return false;
632 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800633 if (low_4gb) {
634 *error_msg = "DlOpen does not support low 4gb loading.";
635 return false;
636 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800637 if (writable) {
638 *error_msg = "DlOpen does not support writable loading.";
639 return false;
640 }
641 if (!executable) {
642 *error_msg = "DlOpen does not support non-executable loading.";
643 return false;
644 }
645
646 // dlopen always returns the same library if it is already opened on the host. For this reason
647 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
648 // the same library loaded multiple times at different addresses is required for class unloading
649 // and for having dex caches arrays in the .bss section.
650 if (!kIsTargetBuild) {
651 if (!kUseDlopenOnHost) {
652 *error_msg = "DlOpen disabled for host.";
653 return false;
654 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800655 }
656
657 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
658 DCHECK(dlopen_handle_ != nullptr || !success);
659
660 return success;
661}
662
663bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
664 uint8_t* oat_file_begin,
665 std::string* error_msg) {
666#ifdef __APPLE__
667 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
668 // but let's fallback to the custom loading code for the time being.
669 UNUSED(elf_filename, oat_file_begin);
670 *error_msg = "Dlopen unsupported on Mac.";
671 return false;
672#else
673 {
674 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
675 if (absolute_path == nullptr) {
676 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
677 return false;
678 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100679#ifdef ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800680 android_dlextinfo extinfo;
681 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
682 // (open oat files multiple
683 // times).
684 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
685 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800686 if (oat_file_begin != nullptr) { //
687 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
688 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
689 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800690 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
691#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800692 UNUSED(oat_file_begin);
Richard Uhlera206c742016-05-24 15:04:22 -0700693 static_assert(!kIsTargetBuild, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700694 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700695 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
696 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700697 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
698 dlclose(dlopen_handle_);
699 dlopen_handle_ = nullptr;
700 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
701 return false;
702 }
703 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100704#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800705 }
706 if (dlopen_handle_ == nullptr) {
707 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
708 return false;
709 }
710 return true;
711#endif
712}
713
714void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800715#ifdef __APPLE__
716 UNUSED(elf_filename);
717 LOG(FATAL) << "Should not reach here.";
718 UNREACHABLE();
719#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800720 struct dl_iterate_context {
721 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
722 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700723 context->shared_objects_seen++;
724 if (context->shared_objects_seen < context->shared_objects_before) {
725 // We haven't been called yet for anything we haven't seen before. Just continue.
726 // Note: this is aggressively optimistic. If another thread was unloading a library,
727 // we may miss out here. However, this does not happen often in practice.
728 return 0;
729 }
730
Andreas Gampe049cff02015-12-01 23:27:12 -0800731 // See whether this callback corresponds to the file which we have just loaded.
732 bool contains_begin = false;
733 for (int i = 0; i < info->dlpi_phnum; i++) {
734 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
735 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
736 info->dlpi_phdr[i].p_vaddr);
737 size_t memsz = info->dlpi_phdr[i].p_memsz;
738 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
739 contains_begin = true;
740 break;
741 }
742 }
743 }
744 // Add dummy mmaps for this file.
745 if (contains_begin) {
746 for (int i = 0; i < info->dlpi_phnum; i++) {
747 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
748 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
749 info->dlpi_phdr[i].p_vaddr);
750 size_t memsz = info->dlpi_phdr[i].p_memsz;
751 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
752 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
753 }
754 }
755 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
756 }
757 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
758 }
759 const uint8_t* const begin_;
760 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700761 const size_t shared_objects_before;
762 size_t shared_objects_seen;
763 };
764 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800765
766 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700767 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
768 // before giving up. This should be unusual.
769 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
770 << shared_objects_before_;
771 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
772 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
773 // OK, give up and print an error.
774 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
775 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
776 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800777 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800778#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800779}
780
781////////////////////////////////////////////////
782// OatFile via our own ElfFile implementation //
783////////////////////////////////////////////////
784
785class ElfOatFile FINAL : public OatFileBase {
786 public:
787 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
788
789 static ElfOatFile* OpenElfFile(File* file,
790 const std::string& location,
791 uint8_t* requested_base,
792 uint8_t* oat_file_begin, // Override base if not null
793 bool writable,
794 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800795 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800796 const char* abs_dex_location,
797 std::string* error_msg);
798
799 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100800 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800801 const char* abs_dex_location,
802 std::string* error_msg);
803
804 protected:
805 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
806 std::string* error_msg) const OVERRIDE {
807 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
808 if (ptr == nullptr) {
809 *error_msg = "(Internal implementation could not find symbol)";
810 }
811 return ptr;
812 }
813
Andreas Gampe4075f832016-05-18 13:09:54 -0700814 void PreLoad() OVERRIDE {
815 }
816
Andreas Gampe049cff02015-12-01 23:27:12 -0800817 bool Load(const std::string& elf_filename,
818 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
819 bool writable,
820 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800821 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800822 std::string* error_msg) OVERRIDE;
823
824 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
825 }
826
827 private:
828 bool ElfFileOpen(File* file,
829 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
830 bool writable,
831 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800832 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800833 std::string* error_msg);
834
835 private:
836 // Backing memory map for oat file during cross compilation.
837 std::unique_ptr<ElfFile> elf_file_;
838
839 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
840};
841
842ElfOatFile* ElfOatFile::OpenElfFile(File* file,
843 const std::string& location,
844 uint8_t* requested_base,
845 uint8_t* oat_file_begin, // Override base if not null
846 bool writable,
847 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800848 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800849 const char* abs_dex_location,
850 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800851 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800852 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800853 bool success = oat_file->ElfFileOpen(file,
854 oat_file_begin,
855 writable,
856 low_4gb,
857 executable,
858 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800859 if (!success) {
860 CHECK(!error_msg->empty());
861 return nullptr;
862 }
863
864 // Complete the setup.
865 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
866 return nullptr;
867 }
868
869 if (!oat_file->Setup(abs_dex_location, error_msg)) {
870 return nullptr;
871 }
872
873 return oat_file.release();
874}
875
876bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100877 VdexFile* vdex_file,
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(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800881 if (IsExecutable()) {
882 *error_msg = "Cannot initialize from elf file in executable mode.";
883 return false;
884 }
885 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +0100886 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -0800887 uint64_t offset, size;
888 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
889 CHECK(has_section);
890 SetBegin(elf_file->Begin() + offset);
891 SetEnd(elf_file->Begin() + size + offset);
892 // Ignore the optional .bss section when opening non-executable.
893 return Setup(abs_dex_location, error_msg);
894}
895
896bool ElfOatFile::Load(const std::string& elf_filename,
897 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
898 bool writable,
899 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800900 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800901 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800902 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800903 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
904 if (file == nullptr) {
905 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
906 return false;
907 }
908 return ElfOatFile::ElfFileOpen(file.get(),
909 oat_file_begin,
910 writable,
911 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800912 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800913 error_msg);
914}
915
916bool ElfOatFile::ElfFileOpen(File* file,
917 uint8_t* oat_file_begin,
918 bool writable,
919 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800920 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800921 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800922 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800923 // TODO: rename requested_base to oat_data_begin
924 elf_file_.reset(ElfFile::Open(file,
925 writable,
926 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800927 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800928 error_msg,
929 oat_file_begin));
930 if (elf_file_ == nullptr) {
931 DCHECK(!error_msg->empty());
932 return false;
933 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800934 bool loaded = elf_file_->Load(executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800935 DCHECK(loaded || !error_msg->empty());
936 return loaded;
937}
938
939//////////////////////////
940// General OatFile code //
941//////////////////////////
942
943std::string OatFile::ResolveRelativeEncodedDexLocation(
944 const char* abs_dex_location, const std::string& rel_dex_location) {
945 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
946 // Strip :classes<N>.dex used for secondary multidex files.
947 std::string base = DexFile::GetBaseLocation(rel_dex_location);
948 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
949
950 // Check if the base is a suffix of the provided abs_dex_location.
951 std::string target_suffix = "/" + base;
952 std::string abs_location(abs_dex_location);
953 if (abs_location.size() > target_suffix.size()) {
954 size_t pos = abs_location.size() - target_suffix.size();
955 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
956 return abs_location + multidex_suffix;
957 }
958 }
959 }
960 return rel_dex_location;
961}
962
963static void CheckLocation(const std::string& location) {
964 CHECK(!location.empty());
965}
966
967OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +0100968 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -0800969 const std::string& location,
970 const char* abs_dex_location,
971 std::string* error_msg) {
972 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +0100973 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -0800974 ? oat_file.release()
975 : nullptr;
976}
977
David Brazdil7b49e6c2016-09-01 11:06:18 +0100978OatFile* OatFile::Open(const std::string& oat_filename,
979 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800980 uint8_t* requested_base,
981 uint8_t* oat_file_begin,
982 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800983 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800984 const char* abs_dex_location,
985 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100986 ScopedTrace trace("Open oat file " + oat_location);
987 CHECK(!oat_filename.empty()) << oat_location;
988 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -0700989
David Brazdil7b49e6c2016-09-01 11:06:18 +0100990 std::string vdex_filename = ReplaceFileExtension(oat_filename, "vdex");
991
992 // Check that the files even exist, fast-fail.
993 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
994 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
995 return nullptr;
996 } else if (!OS::FileExists(oat_filename.c_str())) {
997 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -0700998 return nullptr;
999 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001000
1001 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1002 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001003 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1004 oat_filename,
1005 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001006 requested_base,
1007 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001008 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001009 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001010 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001011 abs_dex_location,
1012 error_msg);
1013 if (with_dlopen != nullptr) {
1014 return with_dlopen;
1015 }
1016 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001017 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001018 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001019 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1020 //
1021 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1022 //
1023 // We use our own ELF loader for Quick to deal with legacy apps that
1024 // open a generated dex file by name, remove the file, then open
1025 // another generated dex file with the same name. http://b/10614658
1026 //
1027 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1028 //
1029 //
1030 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1031 // 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 +01001032 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(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 return with_internal;
1043}
1044
1045OatFile* OatFile::OpenWritable(File* file,
1046 const std::string& location,
1047 const char* abs_dex_location,
1048 std::string* error_msg) {
1049 CheckLocation(location);
1050 return ElfOatFile::OpenElfFile(file,
1051 location,
1052 nullptr,
1053 nullptr,
1054 true,
1055 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001056 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001057 abs_dex_location,
1058 error_msg);
1059}
1060
1061OatFile* OatFile::OpenReadable(File* file,
1062 const std::string& location,
1063 const char* abs_dex_location,
1064 std::string* error_msg) {
1065 CheckLocation(location);
1066 return ElfOatFile::OpenElfFile(file,
1067 location,
1068 nullptr,
1069 nullptr,
1070 false,
1071 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001072 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001073 abs_dex_location,
1074 error_msg);
1075}
1076
1077OatFile::OatFile(const std::string& location, bool is_executable)
1078 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001079 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001080 begin_(nullptr),
1081 end_(nullptr),
1082 bss_begin_(nullptr),
1083 bss_end_(nullptr),
1084 is_executable_(is_executable),
1085 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1086 CHECK(!location_.empty());
1087}
1088
1089OatFile::~OatFile() {
1090 STLDeleteElements(&oat_dex_files_storage_);
1091}
1092
Brian Carlstrome24fa612011-09-29 00:53:55 -07001093const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001094 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001095}
1096
Ian Rogers13735952014-10-08 12:43:28 -07001097const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001098 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001099 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001100}
1101
Ian Rogers13735952014-10-08 12:43:28 -07001102const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001103 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001104 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001105}
1106
Vladimir Marko5c42c292015-02-25 12:02:49 +00001107const uint8_t* OatFile::BssBegin() const {
1108 return bss_begin_;
1109}
1110
1111const uint8_t* OatFile::BssEnd() const {
1112 return bss_end_;
1113}
1114
David Brazdil7b49e6c2016-09-01 11:06:18 +01001115const uint8_t* OatFile::DexBegin() const {
1116 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1117}
1118
1119const uint8_t* OatFile::DexEnd() const {
1120 return kIsVdexEnabled ? vdex_->End() : End();
1121}
1122
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001123const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1124 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001125 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001126 // NOTE: We assume here that the canonical location for a given dex_location never
1127 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1128 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1129 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001130
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001131 // TODO: Additional analysis of usage patterns to see if this can be simplified
1132 // without any performance loss, for example by not doing the first lock-free lookup.
1133
1134 const OatFile::OatDexFile* oat_dex_file = nullptr;
1135 StringPiece key(dex_location);
1136 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1137 // directly mentioned in the oat file and doesn't require locking.
1138 auto primary_it = oat_dex_files_.find(key);
1139 if (primary_it != oat_dex_files_.end()) {
1140 oat_dex_file = primary_it->second;
1141 DCHECK(oat_dex_file != nullptr);
1142 } else {
1143 // This dex_location is not one of the dex locations directly mentioned in the
1144 // oat file. The correct lookup is via the canonical location but first see in
1145 // the secondary_oat_dex_files_ whether we've looked up this location before.
1146 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1147 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1148 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001149 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001150 } else {
1151 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001152 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001153 if (dex_canonical_location != dex_location) {
1154 StringPiece canonical_key(dex_canonical_location);
1155 auto canonical_it = oat_dex_files_.find(canonical_key);
1156 if (canonical_it != oat_dex_files_.end()) {
1157 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001158 } // else keep null.
1159 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001160
1161 // Copy the key to the string_cache_ and store the result in secondary map.
1162 string_cache_.emplace_back(key.data(), key.length());
1163 StringPiece key_copy(string_cache_.back());
1164 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001165 }
1166 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001167
1168 if (oat_dex_file == nullptr) {
1169 if (error_msg != nullptr) {
1170 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1171 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1172 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1173 }
1174 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001175 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001176
Richard Uhler9a37efc2016-08-05 16:32:55 -07001177 if (dex_location_checksum != nullptr &&
1178 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1179 if (error_msg != nullptr) {
1180 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1181 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1182 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1183 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1184 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1185 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001186 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001187 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001188 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001189 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001190}
1191
Brian Carlstrome24fa612011-09-29 00:53:55 -07001192OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001193 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001194 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001195 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001196 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001197 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001198 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001199 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001200 : oat_file_(oat_file),
1201 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001202 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001203 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001204 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001205 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001206 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1207 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001208
1209OatFile::OatDexFile::~OatDexFile() {}
1210
Ian Rogers05f28c62012-10-23 18:12:13 -07001211size_t OatFile::OatDexFile::FileSize() const {
1212 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1213}
1214
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001215std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001216 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001217 static constexpr bool kVerify = false;
1218 static constexpr bool kVerifyChecksum = false;
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001219 return DexFile::Open(dex_file_pointer_,
1220 FileSize(),
1221 dex_file_location_,
1222 dex_file_location_checksum_,
1223 this,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001224 kVerify,
1225 kVerifyChecksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001226 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001227}
1228
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001229uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1230 return oat_class_offsets_pointer_[class_def_index];
1231}
1232
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001233OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001234 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001235
Ian Rogers13735952014-10-08 12:43:28 -07001236 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001237 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001238
Ian Rogers13735952014-10-08 12:43:28 -07001239 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001240 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1241 mirror::Class::Status status =
1242 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1243 CHECK_LT(status, mirror::Class::kStatusMax);
1244
Ian Rogers13735952014-10-08 12:43:28 -07001245 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001246 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1247 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1248 CHECK_LT(type, kOatClassMax);
1249
Ian Rogers13735952014-10-08 12:43:28 -07001250 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001251 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001252
Brian Carlstromcd937a92014-03-04 22:53:23 -08001253 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001254 const uint8_t* bitmap_pointer = nullptr;
1255 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001256 if (type != kOatClassNoneCompiled) {
1257 if (type == kOatClassSomeCompiled) {
1258 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1259 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1260 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1261 methods_pointer = bitmap_pointer + bitmap_size;
1262 } else {
1263 methods_pointer = after_type_pointer;
1264 }
1265 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001266 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001267
Richard Uhler07b3c232015-03-31 15:57:54 -07001268 return OatFile::OatClass(oat_file_,
1269 status,
1270 type,
1271 bitmap_size,
1272 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1273 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001274}
1275
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001276OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001277 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001278 OatClassType type,
1279 uint32_t bitmap_size,
1280 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001281 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001282 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001283 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001284 switch (type_) {
1285 case kOatClassAllCompiled: {
1286 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001287 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001288 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001289 break;
1290 }
1291 case kOatClassSomeCompiled: {
1292 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001293 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001294 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001295 break;
1296 }
1297 case kOatClassNoneCompiled: {
1298 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001299 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001300 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001301 break;
1302 }
1303 case kOatClassMax: {
1304 LOG(FATAL) << "Invalid OatClassType " << type_;
1305 break;
1306 }
1307 }
1308}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001309
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001310uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1311 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1312 if (oat_method_offsets == nullptr) {
1313 return 0u;
1314 }
1315 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1316}
1317
1318const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001319 // 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 -07001320 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001321 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001322 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001323 }
1324 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001325 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001326 CHECK_EQ(kOatClassAllCompiled, type_);
1327 methods_pointer_index = method_index;
1328 } else {
1329 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001330 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001331 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001332 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001333 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1334 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001335 }
1336 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001337 return &oat_method_offsets;
1338}
1339
1340const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1341 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1342 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001343 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001344 }
1345 if (oat_file_->IsExecutable() ||
1346 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001347 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001348 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001349 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001350 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1351 // version.
1352 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001353}
1354
Mathieu Chartiere401d142015-04-22 13:56:20 -07001355void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001356 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001357 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001358}
1359
Richard Uhlerd1537b52016-03-29 13:27:41 -07001360bool OatFile::HasPatchInfo() const {
1361 return GetOatHeader().HasPatchInfo();
1362}
1363
Andreas Gampe7ba64962014-10-23 11:37:40 -07001364bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001365 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001366 // TODO: Check against oat_patches. b/18144996
1367}
1368
Sebastien Hertz0de11332015-05-13 12:14:05 +02001369bool OatFile::IsDebuggable() const {
1370 return GetOatHeader().IsDebuggable();
1371}
1372
Andreas Gampe29d38e72016-03-23 15:31:51 +00001373CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1374 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001375}
1376
Andreas Gampe7848da42015-04-09 11:15:04 -07001377static constexpr char kDexClassPathEncodingSeparator = '*';
1378
1379std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1380 std::ostringstream out;
1381
1382 for (const DexFile* dex_file : dex_files) {
1383 out << dex_file->GetLocation().c_str();
1384 out << kDexClassPathEncodingSeparator;
1385 out << dex_file->GetLocationChecksum();
1386 out << kDexClassPathEncodingSeparator;
1387 }
1388
1389 return out.str();
1390}
1391
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001392bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001393 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1394 // No dependencies.
1395 return true;
1396 }
1397
1398 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1399 // Split() instead of manual parsing of the combined char*.
1400 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001401 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001402 if (split.size() % 2 != 0) {
1403 // Expected pairs of location and checksum.
1404 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1405 return false;
1406 }
1407
1408 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1409 std::string& location = *it;
1410 std::string& checksum = *(it + 1);
1411 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1412 if (converted == 0) {
1413 // Conversion error.
1414 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1415 return false;
1416 }
1417
1418 uint32_t dex_checksum;
1419 std::string error_msg;
1420 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001421 &dex_checksum,
1422 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001423 if (converted != dex_checksum) {
1424 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1425 location.c_str(), converted, dex_checksum);
1426 return false;
1427 }
1428 } else {
1429 // Problem retrieving checksum.
1430 // TODO: odex files?
1431 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1432 error_msg.c_str());
1433 return false;
1434 }
1435 }
1436
1437 return true;
1438}
1439
1440bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001441 std::vector<std::string>* locations) {
1442 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001443 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1444 return true;
1445 }
1446
1447 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1448 // Split() instead of manual parsing of the combined char*.
1449 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001450 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001451 if (split.size() % 2 != 0) {
1452 // Expected pairs of location and checksum.
1453 return false;
1454 }
1455
1456 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1457 locations->push_back(*it);
1458 }
1459
1460 return true;
1461}
1462
Brian Carlstrome24fa612011-09-29 00:53:55 -07001463} // namespace art