blob: cbc5d3c209a6de3824e7595189ff5671bacb9629 [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
135 private:
136 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
137};
138
139template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100140OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
141 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800142 const std::string& location,
143 uint8_t* requested_base,
144 uint8_t* oat_file_begin,
145 bool writable,
146 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800147 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800148 const char* abs_dex_location,
149 std::string* error_msg) {
150 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700151
152 ret->PreLoad();
153
David Brazdil7b49e6c2016-09-01 11:06:18 +0100154 if (kIsVdexEnabled && !ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
155 return nullptr;
156 }
157
Andreas Gampe049cff02015-12-01 23:27:12 -0800158 if (!ret->Load(elf_filename,
159 oat_file_begin,
160 writable,
161 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800162 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800163 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800164 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800165 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800166
Andreas Gampe049cff02015-12-01 23:27:12 -0800167 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
168 return nullptr;
169 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700170
Andreas Gampe049cff02015-12-01 23:27:12 -0800171 ret->PreSetup(elf_filename);
172
173 if (!ret->Setup(abs_dex_location, error_msg)) {
174 return nullptr;
175 }
176
Dave Allison69dfe512014-07-11 17:11:58 +0000177 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178}
179
David Brazdil7b49e6c2016-09-01 11:06:18 +0100180bool OatFileBase::LoadVdex(const std::string& vdex_filename,
181 bool writable,
182 bool low_4gb,
183 std::string* error_msg) {
184 vdex_.reset(VdexFile::Open(vdex_filename, writable, low_4gb, error_msg));
185 if (vdex_.get() == nullptr) {
186 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
187 vdex_filename.c_str(),
188 error_msg->c_str());
189 return false;
190 }
191 return true;
192}
193
Andreas Gampe049cff02015-12-01 23:27:12 -0800194bool OatFileBase::ComputeFields(uint8_t* requested_base,
195 const std::string& file_path,
196 std::string* error_msg) {
197 std::string symbol_error_msg;
198 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700199 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800200 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
201 file_path.c_str(),
202 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700203 return false;
204 }
205 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000206 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700207 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000208 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
209 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700210 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800211 "oatdata=%p != expected=%p. See process maps in the log.",
212 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700213 return false;
214 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800215 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700216 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800217 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
218 file_path.c_str(),
219 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700220 return false;
221 }
222 // Readjust to be non-inclusive upper bound.
223 end_ += sizeof(uint32_t);
224
Andreas Gampe049cff02015-12-01 23:27:12 -0800225 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700226 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800227 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700228 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700229 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800230 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700231 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800232 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700233 return false;
234 }
235 // Readjust to be non-inclusive upper bound.
236 bss_end_ += sizeof(uint32_t);
237 }
238
Andreas Gampe049cff02015-12-01 23:27:12 -0800239 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800240}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800241
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100242// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
243// position by the number of bytes read, i.e. sizeof(T).
244// Return true on success, false if the read would go beyond the end of the OatFile.
245template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100246inline static bool ReadOatDexFileData(const OatFile& oat_file,
247 /*inout*/const uint8_t** oat,
248 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100249 DCHECK(oat != nullptr);
250 DCHECK(value != nullptr);
251 DCHECK_LE(*oat, oat_file.End());
252 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
253 return false;
254 }
255 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
256 typedef __attribute__((__aligned__(1))) T unaligned_type;
257 *value = *reinterpret_cast<const unaligned_type*>(*oat);
258 *oat += sizeof(T);
259 return true;
260}
261
Andreas Gampe049cff02015-12-01 23:27:12 -0800262bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700263 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800264 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100265 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
266 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800267 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700268 return false;
269 }
Ian Rogers13735952014-10-08 12:43:28 -0700270 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700272 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700273 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700274 return false;
275 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700276
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700277 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700278 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700279 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100280 "%p + %zu + %u <= %p",
281 GetLocation().c_str(),
282 Begin(),
283 sizeof(OatHeader),
284 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700285 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700286 return false;
287 }
288
Andreas Gampe542451c2016-07-26 09:02:02 -0700289 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100290 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100291 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
292 oat_dex_files_storage_.reserve(dex_file_count);
293 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100294 uint32_t dex_file_location_size;
295 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
296 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
297 "location size",
298 GetLocation().c_str(),
299 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700300 return false;
301 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100302 if (UNLIKELY(dex_file_location_size == 0U)) {
303 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
304 GetLocation().c_str(),
305 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700306 return false;
307 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000308 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100309 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
310 "location",
311 GetLocation().c_str(),
312 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700313 return false;
314 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000315 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
316 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700317
Richard Uhlere5fed032015-03-18 08:21:11 -0700318 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
319 abs_dex_location,
320 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700321
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100322 uint32_t dex_file_checksum;
323 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
324 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
325 "dex file checksum",
326 GetLocation().c_str(),
327 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700328 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700329 return false;
330 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700331
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100332 uint32_t dex_file_offset;
333 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
334 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
335 "after dex file offsets",
336 GetLocation().c_str(),
337 i,
338 dex_file_location.c_str());
339 return false;
340 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700341 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100342 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
343 "file offset",
344 GetLocation().c_str(),
345 i,
346 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700347 return false;
348 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100349 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100350 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
351 "offset %u > %zu",
352 GetLocation().c_str(),
353 i,
354 dex_file_location.c_str(),
355 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100356 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700357 return false;
358 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100359 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000360 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
361 "offset %u of %zu but the size of dex file header is %zu",
362 GetLocation().c_str(),
363 i,
364 dex_file_location.c_str(),
365 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100366 DexSize(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000367 sizeof(DexFile::Header));
368 return false;
369 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800370
David Brazdil7b49e6c2016-09-01 11:06:18 +0100371 const uint8_t* dex_file_pointer = DexBegin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700372 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100373 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
374 "dex file magic '%s'",
375 GetLocation().c_str(),
376 i,
377 dex_file_location.c_str(),
378 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700379 return false;
380 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700381 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100382 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
383 "dex file version '%s'",
384 GetLocation().c_str(),
385 i,
386 dex_file_location.c_str(),
387 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700388 return false;
389 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800390 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100391 if (DexSize() - dex_file_offset < header->file_size_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000392 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
393 "offset %u and size %u truncated at %zu",
394 GetLocation().c_str(),
395 i,
396 dex_file_location.c_str(),
397 dex_file_offset,
398 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100399 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000400 return false;
401 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300402
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000403 uint32_t class_offsets_offset;
404 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
405 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
406 "after class offsets offset",
407 GetLocation().c_str(),
408 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300409 dex_file_location.c_str());
410 return false;
411 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000412 if (UNLIKELY(class_offsets_offset > Size()) ||
413 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
414 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
415 "class offsets, offset %u of %zu, class defs %u",
416 GetLocation().c_str(),
417 i,
418 dex_file_location.c_str(),
419 class_offsets_offset,
420 Size(),
421 header->class_defs_size_);
422 return false;
423 }
424 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
425 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
426 "class offsets, offset %u",
427 GetLocation().c_str(),
428 i,
429 dex_file_location.c_str(),
430 class_offsets_offset);
431 return false;
432 }
433 const uint32_t* class_offsets_pointer =
434 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
435
436 uint32_t lookup_table_offset;
437 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
438 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
439 "after lookup table offset",
440 GetLocation().c_str(),
441 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300442 dex_file_location.c_str());
443 return false;
444 }
445 const uint8_t* lookup_table_data = lookup_table_offset != 0u
446 ? Begin() + lookup_table_offset
447 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000448 if (lookup_table_offset != 0u &&
449 (UNLIKELY(lookup_table_offset > Size()) ||
450 UNLIKELY(Size() - lookup_table_offset <
451 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100452 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000453 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100454 GetLocation().c_str(),
455 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000456 dex_file_location.c_str(),
457 lookup_table_offset,
458 Size(),
459 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700460 return false;
461 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700462
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100463 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100464 if (dex_cache_arrays != nullptr) {
465 DexCacheArraysLayout layout(pointer_size, *header);
466 if (layout.Size() != 0u) {
467 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100468 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
469 "truncated dex cache arrays, %zu < %zu.",
470 GetLocation().c_str(),
471 i,
472 dex_file_location.c_str(),
473 static_cast<size_t>(bss_end_ - dex_cache_arrays),
474 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100475 return false;
476 }
477 current_dex_cache_arrays = dex_cache_arrays;
478 dex_cache_arrays += layout.Size();
479 }
480 }
481
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100482 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
483
484 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100485 OatDexFile* oat_dex_file = new OatDexFile(this,
486 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100487 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100488 dex_file_checksum,
489 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300490 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000491 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100492 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100493 oat_dex_files_storage_.push_back(oat_dex_file);
494
495 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100496 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100497 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100498 if (canonical_location != dex_file_location) {
499 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
500 oat_dex_files_.Put(canonical_key, oat_dex_file);
501 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700502 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100503
504 if (dex_cache_arrays != bss_end_) {
505 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
506 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100507 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100508 GetLocation().c_str(),
509 static_cast<size_t>(bss_end_ - dex_cache_arrays));
510 return false;
511 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700512 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700513}
514
Andreas Gampe049cff02015-12-01 23:27:12 -0800515////////////////////////
516// OatFile via dlopen //
517////////////////////////
518
Andreas Gampe049cff02015-12-01 23:27:12 -0800519class DlOpenOatFile FINAL : public OatFileBase {
520 public:
521 DlOpenOatFile(const std::string& filename, bool executable)
522 : OatFileBase(filename, executable),
523 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700524 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800525 }
526
527 ~DlOpenOatFile() {
528 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700529 if (!kIsTargetBuild) {
530 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
531 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700532 dlclose(dlopen_handle_);
533 } else {
534 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700535 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800536 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800537 }
538
539 protected:
540 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
541 std::string* error_msg) const OVERRIDE {
542 const uint8_t* ptr =
543 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
544 if (ptr == nullptr) {
545 *error_msg = dlerror();
546 }
547 return ptr;
548 }
549
Andreas Gampe4075f832016-05-18 13:09:54 -0700550 void PreLoad() OVERRIDE;
551
Andreas Gampe049cff02015-12-01 23:27:12 -0800552 bool Load(const std::string& elf_filename,
553 uint8_t* oat_file_begin,
554 bool writable,
555 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800556 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800557 std::string* error_msg) OVERRIDE;
558
559 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
560 void PreSetup(const std::string& elf_filename) OVERRIDE;
561
562 private:
563 bool Dlopen(const std::string& elf_filename,
564 uint8_t* oat_file_begin,
565 std::string* error_msg);
566
Richard Uhlera206c742016-05-24 15:04:22 -0700567 // On the host, if the same library is loaded again with dlopen the same
568 // file handle is returned. This differs from the behavior of dlopen on the
569 // target, where dlopen reloads the library at a different address every
570 // time you load it. The runtime relies on the target behavior to ensure
571 // each instance of the loaded library has a unique dex cache. To avoid
572 // problems, we fall back to our own linker in the case when the same
573 // library is opened multiple times on host. dlopen_handles_ is used to
574 // detect that case.
575 // Guarded by host_dlopen_handles_lock_;
576 static std::unordered_set<void*> host_dlopen_handles_;
577
Andreas Gampe049cff02015-12-01 23:27:12 -0800578 // dlopen handle during runtime.
579 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
580
581 // Dummy memory map objects corresponding to the regions mapped by dlopen.
582 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
583
Andreas Gampe4075f832016-05-18 13:09:54 -0700584 // The number of shared objects the linker told us about before loading. Used to
585 // (optimistically) optimize the PreSetup stage (see comment there).
586 size_t shared_objects_before_;
587
Andreas Gampe049cff02015-12-01 23:27:12 -0800588 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
589};
590
Richard Uhlera206c742016-05-24 15:04:22 -0700591std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
592
Andreas Gampe4075f832016-05-18 13:09:54 -0700593void DlOpenOatFile::PreLoad() {
594#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700595 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700596 LOG(FATAL) << "Should not reach here.";
597 UNREACHABLE();
598#else
599 // Count the entries in dl_iterate_phdr we get at this point in time.
600 struct dl_iterate_context {
601 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
602 size_t size ATTRIBUTE_UNUSED,
603 void *data) {
604 reinterpret_cast<dl_iterate_context*>(data)->count++;
605 return 0; // Continue iteration.
606 }
607 size_t count = 0;
608 } context;
609
610 dl_iterate_phdr(dl_iterate_context::callback, &context);
611 shared_objects_before_ = context.count;
612#endif
613}
614
Andreas Gampe049cff02015-12-01 23:27:12 -0800615bool DlOpenOatFile::Load(const std::string& elf_filename,
616 uint8_t* oat_file_begin,
617 bool writable,
618 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800619 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800620 std::string* error_msg) {
621 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
622 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
623 // !executable is a sign that we may want to patch), which may not be allowed for
624 // various reasons.
625 if (!kUseDlopen) {
626 *error_msg = "DlOpen is disabled.";
627 return false;
628 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800629 if (low_4gb) {
630 *error_msg = "DlOpen does not support low 4gb loading.";
631 return false;
632 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800633 if (writable) {
634 *error_msg = "DlOpen does not support writable loading.";
635 return false;
636 }
637 if (!executable) {
638 *error_msg = "DlOpen does not support non-executable loading.";
639 return false;
640 }
641
642 // dlopen always returns the same library if it is already opened on the host. For this reason
643 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
644 // the same library loaded multiple times at different addresses is required for class unloading
645 // and for having dex caches arrays in the .bss section.
646 if (!kIsTargetBuild) {
647 if (!kUseDlopenOnHost) {
648 *error_msg = "DlOpen disabled for host.";
649 return false;
650 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800651 }
652
653 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
654 DCHECK(dlopen_handle_ != nullptr || !success);
655
656 return success;
657}
658
659bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
660 uint8_t* oat_file_begin,
661 std::string* error_msg) {
662#ifdef __APPLE__
663 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
664 // but let's fallback to the custom loading code for the time being.
665 UNUSED(elf_filename, oat_file_begin);
666 *error_msg = "Dlopen unsupported on Mac.";
667 return false;
668#else
669 {
670 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
671 if (absolute_path == nullptr) {
672 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
673 return false;
674 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100675#ifdef ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800676 android_dlextinfo extinfo;
677 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
678 // (open oat files multiple
679 // times).
680 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
681 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800682 if (oat_file_begin != nullptr) { //
683 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
684 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
685 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800686 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
687#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800688 UNUSED(oat_file_begin);
Richard Uhlera206c742016-05-24 15:04:22 -0700689 static_assert(!kIsTargetBuild, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700690 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -0700691 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
692 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700693 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
694 dlclose(dlopen_handle_);
695 dlopen_handle_ = nullptr;
696 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
697 return false;
698 }
699 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100700#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800701 }
702 if (dlopen_handle_ == nullptr) {
703 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
704 return false;
705 }
706 return true;
707#endif
708}
709
710void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800711#ifdef __APPLE__
712 UNUSED(elf_filename);
713 LOG(FATAL) << "Should not reach here.";
714 UNREACHABLE();
715#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800716 struct dl_iterate_context {
717 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
718 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700719 context->shared_objects_seen++;
720 if (context->shared_objects_seen < context->shared_objects_before) {
721 // We haven't been called yet for anything we haven't seen before. Just continue.
722 // Note: this is aggressively optimistic. If another thread was unloading a library,
723 // we may miss out here. However, this does not happen often in practice.
724 return 0;
725 }
726
Andreas Gampe049cff02015-12-01 23:27:12 -0800727 // See whether this callback corresponds to the file which we have just loaded.
728 bool contains_begin = false;
729 for (int i = 0; i < info->dlpi_phnum; i++) {
730 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
731 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
732 info->dlpi_phdr[i].p_vaddr);
733 size_t memsz = info->dlpi_phdr[i].p_memsz;
734 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
735 contains_begin = true;
736 break;
737 }
738 }
739 }
740 // Add dummy mmaps for this file.
741 if (contains_begin) {
742 for (int i = 0; i < info->dlpi_phnum; i++) {
743 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
744 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
745 info->dlpi_phdr[i].p_vaddr);
746 size_t memsz = info->dlpi_phdr[i].p_memsz;
747 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
748 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
749 }
750 }
751 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
752 }
753 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
754 }
755 const uint8_t* const begin_;
756 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700757 const size_t shared_objects_before;
758 size_t shared_objects_seen;
759 };
760 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800761
762 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700763 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
764 // before giving up. This should be unusual.
765 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
766 << shared_objects_before_;
767 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
768 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
769 // OK, give up and print an error.
770 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
771 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
772 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800773 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800774#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800775}
776
777////////////////////////////////////////////////
778// OatFile via our own ElfFile implementation //
779////////////////////////////////////////////////
780
781class ElfOatFile FINAL : public OatFileBase {
782 public:
783 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
784
785 static ElfOatFile* OpenElfFile(File* file,
786 const std::string& location,
787 uint8_t* requested_base,
788 uint8_t* oat_file_begin, // Override base if not null
789 bool writable,
790 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800791 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800792 const char* abs_dex_location,
793 std::string* error_msg);
794
795 bool InitializeFromElfFile(ElfFile* elf_file,
796 const char* abs_dex_location,
797 std::string* error_msg);
798
799 protected:
800 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
801 std::string* error_msg) const OVERRIDE {
802 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
803 if (ptr == nullptr) {
804 *error_msg = "(Internal implementation could not find symbol)";
805 }
806 return ptr;
807 }
808
Andreas Gampe4075f832016-05-18 13:09:54 -0700809 void PreLoad() OVERRIDE {
810 }
811
Andreas Gampe049cff02015-12-01 23:27:12 -0800812 bool Load(const std::string& elf_filename,
813 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
814 bool writable,
815 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800816 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800817 std::string* error_msg) OVERRIDE;
818
819 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
820 }
821
822 private:
823 bool ElfFileOpen(File* file,
824 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
825 bool writable,
826 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800827 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800828 std::string* error_msg);
829
830 private:
831 // Backing memory map for oat file during cross compilation.
832 std::unique_ptr<ElfFile> elf_file_;
833
834 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
835};
836
837ElfOatFile* ElfOatFile::OpenElfFile(File* file,
838 const std::string& location,
839 uint8_t* requested_base,
840 uint8_t* oat_file_begin, // Override base if not null
841 bool writable,
842 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800843 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800844 const char* abs_dex_location,
845 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800846 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800847 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800848 bool success = oat_file->ElfFileOpen(file,
849 oat_file_begin,
850 writable,
851 low_4gb,
852 executable,
853 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800854 if (!success) {
855 CHECK(!error_msg->empty());
856 return nullptr;
857 }
858
859 // Complete the setup.
860 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
861 return nullptr;
862 }
863
864 if (!oat_file->Setup(abs_dex_location, error_msg)) {
865 return nullptr;
866 }
867
868 return oat_file.release();
869}
870
871bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
872 const char* abs_dex_location,
873 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800874 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800875 if (IsExecutable()) {
876 *error_msg = "Cannot initialize from elf file in executable mode.";
877 return false;
878 }
879 elf_file_.reset(elf_file);
880 uint64_t offset, size;
881 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
882 CHECK(has_section);
883 SetBegin(elf_file->Begin() + offset);
884 SetEnd(elf_file->Begin() + size + offset);
885 // Ignore the optional .bss section when opening non-executable.
886 return Setup(abs_dex_location, error_msg);
887}
888
889bool ElfOatFile::Load(const std::string& elf_filename,
890 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
891 bool writable,
892 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800893 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800894 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800895 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800896 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
897 if (file == nullptr) {
898 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
899 return false;
900 }
901 return ElfOatFile::ElfFileOpen(file.get(),
902 oat_file_begin,
903 writable,
904 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800905 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800906 error_msg);
907}
908
909bool ElfOatFile::ElfFileOpen(File* file,
910 uint8_t* oat_file_begin,
911 bool writable,
912 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800913 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800914 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800915 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800916 // TODO: rename requested_base to oat_data_begin
917 elf_file_.reset(ElfFile::Open(file,
918 writable,
919 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800920 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800921 error_msg,
922 oat_file_begin));
923 if (elf_file_ == nullptr) {
924 DCHECK(!error_msg->empty());
925 return false;
926 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800927 bool loaded = elf_file_->Load(executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800928 DCHECK(loaded || !error_msg->empty());
929 return loaded;
930}
931
932//////////////////////////
933// General OatFile code //
934//////////////////////////
935
936std::string OatFile::ResolveRelativeEncodedDexLocation(
937 const char* abs_dex_location, const std::string& rel_dex_location) {
938 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
939 // Strip :classes<N>.dex used for secondary multidex files.
940 std::string base = DexFile::GetBaseLocation(rel_dex_location);
941 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
942
943 // Check if the base is a suffix of the provided abs_dex_location.
944 std::string target_suffix = "/" + base;
945 std::string abs_location(abs_dex_location);
946 if (abs_location.size() > target_suffix.size()) {
947 size_t pos = abs_location.size() - target_suffix.size();
948 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
949 return abs_location + multidex_suffix;
950 }
951 }
952 }
953 return rel_dex_location;
954}
955
956static void CheckLocation(const std::string& location) {
957 CHECK(!location.empty());
958}
959
960OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
961 const std::string& location,
962 const char* abs_dex_location,
963 std::string* error_msg) {
964 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
965 return oat_file->InitializeFromElfFile(elf_file, abs_dex_location, error_msg)
966 ? oat_file.release()
967 : nullptr;
968}
969
David Brazdil7b49e6c2016-09-01 11:06:18 +0100970OatFile* OatFile::Open(const std::string& oat_filename,
971 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800972 uint8_t* requested_base,
973 uint8_t* oat_file_begin,
974 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800975 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800976 const char* abs_dex_location,
977 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100978 ScopedTrace trace("Open oat file " + oat_location);
979 CHECK(!oat_filename.empty()) << oat_location;
980 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -0700981
David Brazdil7b49e6c2016-09-01 11:06:18 +0100982 std::string vdex_filename = ReplaceFileExtension(oat_filename, "vdex");
983
984 // Check that the files even exist, fast-fail.
985 if (kIsVdexEnabled && !OS::FileExists(vdex_filename.c_str())) {
986 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
987 return nullptr;
988 } else if (!OS::FileExists(oat_filename.c_str())) {
989 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -0700990 return nullptr;
991 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800992
993 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
994 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100995 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
996 oat_filename,
997 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800998 requested_base,
999 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001000 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001001 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001002 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001003 abs_dex_location,
1004 error_msg);
1005 if (with_dlopen != nullptr) {
1006 return with_dlopen;
1007 }
1008 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001009 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001010 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001011 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1012 //
1013 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1014 //
1015 // We use our own ELF loader for Quick to deal with legacy apps that
1016 // open a generated dex file by name, remove the file, then open
1017 // another generated dex file with the same name. http://b/10614658
1018 //
1019 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1020 //
1021 //
1022 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1023 // 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 +01001024 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1025 oat_filename,
1026 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001027 requested_base,
1028 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001029 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001030 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001031 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001032 abs_dex_location,
1033 error_msg);
1034 return with_internal;
1035}
1036
1037OatFile* OatFile::OpenWritable(File* file,
1038 const std::string& location,
1039 const char* abs_dex_location,
1040 std::string* error_msg) {
1041 CheckLocation(location);
1042 return ElfOatFile::OpenElfFile(file,
1043 location,
1044 nullptr,
1045 nullptr,
1046 true,
1047 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001048 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001049 abs_dex_location,
1050 error_msg);
1051}
1052
1053OatFile* OatFile::OpenReadable(File* file,
1054 const std::string& location,
1055 const char* abs_dex_location,
1056 std::string* error_msg) {
1057 CheckLocation(location);
1058 return ElfOatFile::OpenElfFile(file,
1059 location,
1060 nullptr,
1061 nullptr,
1062 false,
1063 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001064 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001065 abs_dex_location,
1066 error_msg);
1067}
1068
1069OatFile::OatFile(const std::string& location, bool is_executable)
1070 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001071 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001072 begin_(nullptr),
1073 end_(nullptr),
1074 bss_begin_(nullptr),
1075 bss_end_(nullptr),
1076 is_executable_(is_executable),
1077 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1078 CHECK(!location_.empty());
1079}
1080
1081OatFile::~OatFile() {
1082 STLDeleteElements(&oat_dex_files_storage_);
1083}
1084
Brian Carlstrome24fa612011-09-29 00:53:55 -07001085const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001086 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001087}
1088
Ian Rogers13735952014-10-08 12:43:28 -07001089const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001090 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001091 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001092}
1093
Ian Rogers13735952014-10-08 12:43:28 -07001094const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001095 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001096 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001097}
1098
Vladimir Marko5c42c292015-02-25 12:02:49 +00001099const uint8_t* OatFile::BssBegin() const {
1100 return bss_begin_;
1101}
1102
1103const uint8_t* OatFile::BssEnd() const {
1104 return bss_end_;
1105}
1106
David Brazdil7b49e6c2016-09-01 11:06:18 +01001107const uint8_t* OatFile::DexBegin() const {
1108 return kIsVdexEnabled ? vdex_->Begin() : Begin();
1109}
1110
1111const uint8_t* OatFile::DexEnd() const {
1112 return kIsVdexEnabled ? vdex_->End() : End();
1113}
1114
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001115const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1116 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001117 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001118 // NOTE: We assume here that the canonical location for a given dex_location never
1119 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1120 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1121 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001122
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001123 // TODO: Additional analysis of usage patterns to see if this can be simplified
1124 // without any performance loss, for example by not doing the first lock-free lookup.
1125
1126 const OatFile::OatDexFile* oat_dex_file = nullptr;
1127 StringPiece key(dex_location);
1128 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1129 // directly mentioned in the oat file and doesn't require locking.
1130 auto primary_it = oat_dex_files_.find(key);
1131 if (primary_it != oat_dex_files_.end()) {
1132 oat_dex_file = primary_it->second;
1133 DCHECK(oat_dex_file != nullptr);
1134 } else {
1135 // This dex_location is not one of the dex locations directly mentioned in the
1136 // oat file. The correct lookup is via the canonical location but first see in
1137 // the secondary_oat_dex_files_ whether we've looked up this location before.
1138 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1139 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1140 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001141 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001142 } else {
1143 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001144 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001145 if (dex_canonical_location != dex_location) {
1146 StringPiece canonical_key(dex_canonical_location);
1147 auto canonical_it = oat_dex_files_.find(canonical_key);
1148 if (canonical_it != oat_dex_files_.end()) {
1149 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001150 } // else keep null.
1151 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001152
1153 // Copy the key to the string_cache_ and store the result in secondary map.
1154 string_cache_.emplace_back(key.data(), key.length());
1155 StringPiece key_copy(string_cache_.back());
1156 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001157 }
1158 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001159
1160 if (oat_dex_file == nullptr) {
1161 if (error_msg != nullptr) {
1162 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1163 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1164 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1165 }
1166 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001167 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001168
Richard Uhler9a37efc2016-08-05 16:32:55 -07001169 if (dex_location_checksum != nullptr &&
1170 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1171 if (error_msg != nullptr) {
1172 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
1173 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1174 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1175 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1176 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1177 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001178 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001179 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001180 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001181 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001182}
1183
Brian Carlstrome24fa612011-09-29 00:53:55 -07001184OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001185 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001186 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001187 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001188 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001189 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001190 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001191 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001192 : oat_file_(oat_file),
1193 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001194 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001195 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001196 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001197 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001198 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1199 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001200
1201OatFile::OatDexFile::~OatDexFile() {}
1202
Ian Rogers05f28c62012-10-23 18:12:13 -07001203size_t OatFile::OatDexFile::FileSize() const {
1204 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1205}
1206
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001207std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001208 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001209 static constexpr bool kVerify = false;
1210 static constexpr bool kVerifyChecksum = false;
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001211 return DexFile::Open(dex_file_pointer_,
1212 FileSize(),
1213 dex_file_location_,
1214 dex_file_location_checksum_,
1215 this,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001216 kVerify,
1217 kVerifyChecksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001218 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001219}
1220
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001221uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1222 return oat_class_offsets_pointer_[class_def_index];
1223}
1224
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001225OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001226 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001227
Ian Rogers13735952014-10-08 12:43:28 -07001228 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001229 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001230
Ian Rogers13735952014-10-08 12:43:28 -07001231 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001232 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1233 mirror::Class::Status status =
1234 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1235 CHECK_LT(status, mirror::Class::kStatusMax);
1236
Ian Rogers13735952014-10-08 12:43:28 -07001237 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001238 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1239 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1240 CHECK_LT(type, kOatClassMax);
1241
Ian Rogers13735952014-10-08 12:43:28 -07001242 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001243 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001244
Brian Carlstromcd937a92014-03-04 22:53:23 -08001245 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001246 const uint8_t* bitmap_pointer = nullptr;
1247 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001248 if (type != kOatClassNoneCompiled) {
1249 if (type == kOatClassSomeCompiled) {
1250 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1251 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1252 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1253 methods_pointer = bitmap_pointer + bitmap_size;
1254 } else {
1255 methods_pointer = after_type_pointer;
1256 }
1257 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001258 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001259
Richard Uhler07b3c232015-03-31 15:57:54 -07001260 return OatFile::OatClass(oat_file_,
1261 status,
1262 type,
1263 bitmap_size,
1264 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1265 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001266}
1267
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001268OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001269 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001270 OatClassType type,
1271 uint32_t bitmap_size,
1272 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001273 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001274 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001275 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001276 switch (type_) {
1277 case kOatClassAllCompiled: {
1278 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001279 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001280 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001281 break;
1282 }
1283 case kOatClassSomeCompiled: {
1284 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001285 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001286 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001287 break;
1288 }
1289 case kOatClassNoneCompiled: {
1290 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001291 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001292 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001293 break;
1294 }
1295 case kOatClassMax: {
1296 LOG(FATAL) << "Invalid OatClassType " << type_;
1297 break;
1298 }
1299 }
1300}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001301
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001302uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1303 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1304 if (oat_method_offsets == nullptr) {
1305 return 0u;
1306 }
1307 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1308}
1309
1310const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001311 // 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 -07001312 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001313 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001314 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001315 }
1316 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001317 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001318 CHECK_EQ(kOatClassAllCompiled, type_);
1319 methods_pointer_index = method_index;
1320 } else {
1321 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001322 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001323 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001324 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001325 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1326 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001327 }
1328 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001329 return &oat_method_offsets;
1330}
1331
1332const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1333 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1334 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001335 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001336 }
1337 if (oat_file_->IsExecutable() ||
1338 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001339 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001340 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001341 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001342 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1343 // version.
1344 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001345}
1346
Mathieu Chartiere401d142015-04-22 13:56:20 -07001347void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001348 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001349 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001350}
1351
Richard Uhlerd1537b52016-03-29 13:27:41 -07001352bool OatFile::HasPatchInfo() const {
1353 return GetOatHeader().HasPatchInfo();
1354}
1355
Andreas Gampe7ba64962014-10-23 11:37:40 -07001356bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001357 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001358 // TODO: Check against oat_patches. b/18144996
1359}
1360
Sebastien Hertz0de11332015-05-13 12:14:05 +02001361bool OatFile::IsDebuggable() const {
1362 return GetOatHeader().IsDebuggable();
1363}
1364
Andreas Gampe29d38e72016-03-23 15:31:51 +00001365CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1366 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001367}
1368
Andreas Gampe7848da42015-04-09 11:15:04 -07001369static constexpr char kDexClassPathEncodingSeparator = '*';
1370
1371std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1372 std::ostringstream out;
1373
1374 for (const DexFile* dex_file : dex_files) {
1375 out << dex_file->GetLocation().c_str();
1376 out << kDexClassPathEncodingSeparator;
1377 out << dex_file->GetLocationChecksum();
1378 out << kDexClassPathEncodingSeparator;
1379 }
1380
1381 return out.str();
1382}
1383
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001384bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001385 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1386 // No dependencies.
1387 return true;
1388 }
1389
1390 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1391 // Split() instead of manual parsing of the combined char*.
1392 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001393 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001394 if (split.size() % 2 != 0) {
1395 // Expected pairs of location and checksum.
1396 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1397 return false;
1398 }
1399
1400 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1401 std::string& location = *it;
1402 std::string& checksum = *(it + 1);
1403 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1404 if (converted == 0) {
1405 // Conversion error.
1406 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1407 return false;
1408 }
1409
1410 uint32_t dex_checksum;
1411 std::string error_msg;
1412 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001413 &dex_checksum,
1414 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001415 if (converted != dex_checksum) {
1416 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1417 location.c_str(), converted, dex_checksum);
1418 return false;
1419 }
1420 } else {
1421 // Problem retrieving checksum.
1422 // TODO: odex files?
1423 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1424 error_msg.c_str());
1425 return false;
1426 }
1427 }
1428
1429 return true;
1430}
1431
1432bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001433 std::vector<std::string>* locations) {
1434 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001435 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1436 return true;
1437 }
1438
1439 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1440 // Split() instead of manual parsing of the combined char*.
1441 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001442 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001443 if (split.size() % 2 != 0) {
1444 // Expected pairs of location and checksum.
1445 return false;
1446 }
1447
1448 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1449 locations->push_back(*it);
1450 }
1451
1452 return true;
1453}
1454
Brian Carlstrome24fa612011-09-29 00:53:55 -07001455} // namespace art