blob: ccb8b2970b00ce6921f520e01e9250461492c837 [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.
Andreas Gampec60e1b72015-07-30 08:57:50 -070031#ifdef __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"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080037#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080038#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080039#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070041#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000043#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070045#include "mirror/object-inl.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010046#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070047#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070048#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070049#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000050#include "type_lookup_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010052#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070053
54namespace art {
55
Andreas Gampe049cff02015-12-01 23:27:12 -080056// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000057static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070058
Andreas Gampe049cff02015-12-01 23:27:12 -080059// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070060// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
61// multiple times). However, if/when we switch the above, we likely want to switch this, too,
62// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000063static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070064
65// For debugging, Open will print DlOpen error message if set to true.
66static constexpr bool kPrintDlOpenErrorMessage = false;
67
Andreas Gampe049cff02015-12-01 23:27:12 -080068// Note for OatFileBase and descendents:
69//
70// These are used in OatFile::Open to try all our loaders.
71//
72// The process is simple:
73//
74// 1) Allocate an instance through the standard constructor (location, executable)
75// 2) Load() to try to open the file.
76// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
77// 4) PreSetup() for any steps that should be done before the final setup.
78// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070079
Andreas Gampe049cff02015-12-01 23:27:12 -080080class OatFileBase : public OatFile {
81 public:
82 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070083
Andreas Gampe049cff02015-12-01 23:27:12 -080084 template <typename kOatFileBaseSubType>
85 static OatFileBase* OpenOatFile(const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070086 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080087 uint8_t* requested_base,
88 uint8_t* oat_file_begin,
89 bool writable,
90 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080091 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070092 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080093 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080094
Andreas Gampe049cff02015-12-01 23:27:12 -080095 protected:
96 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -070097
Andreas Gampe049cff02015-12-01 23:27:12 -080098 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
99 std::string* error_msg) const = 0;
100
101 virtual bool Load(const std::string& elf_filename,
102 uint8_t* oat_file_begin,
103 bool writable,
104 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800105 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800106 std::string* error_msg) = 0;
107
108 bool ComputeFields(uint8_t* requested_base,
109 const std::string& file_path,
110 std::string* error_msg);
111
112 virtual void PreSetup(const std::string& elf_filename) = 0;
113
114 bool Setup(const char* abs_dex_location, std::string* error_msg);
115
116 // Setters exposed for ElfOatFile.
117
118 void SetBegin(const uint8_t* begin) {
119 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700120 }
121
Andreas Gampe049cff02015-12-01 23:27:12 -0800122 void SetEnd(const uint8_t* end) {
123 end_ = end;
124 }
125
126 private:
127 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
128};
129
130template <typename kOatFileBaseSubType>
131OatFileBase* OatFileBase::OpenOatFile(const std::string& elf_filename,
132 const std::string& location,
133 uint8_t* requested_base,
134 uint8_t* oat_file_begin,
135 bool writable,
136 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800137 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800138 const char* abs_dex_location,
139 std::string* error_msg) {
140 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
141 if (!ret->Load(elf_filename,
142 oat_file_begin,
143 writable,
144 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800145 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800146 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800147 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800148 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800149
Andreas Gampe049cff02015-12-01 23:27:12 -0800150 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
151 return nullptr;
152 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800153 ret->PreSetup(elf_filename);
154
155 if (!ret->Setup(abs_dex_location, error_msg)) {
156 return nullptr;
157 }
158
Dave Allison69dfe512014-07-11 17:11:58 +0000159 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800160}
161
Andreas Gampe049cff02015-12-01 23:27:12 -0800162bool OatFileBase::ComputeFields(uint8_t* requested_base,
163 const std::string& file_path,
164 std::string* error_msg) {
165 std::string symbol_error_msg;
166 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700167 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800168 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
169 file_path.c_str(),
170 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700171 return false;
172 }
173 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000174 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700175 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000176 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
177 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700178 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800179 "oatdata=%p != expected=%p. See process maps in the log.",
180 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700181 return false;
182 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800183 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700184 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800185 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
186 file_path.c_str(),
187 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700188 return false;
189 }
190 // Readjust to be non-inclusive upper bound.
191 end_ += sizeof(uint32_t);
192
Andreas Gampe049cff02015-12-01 23:27:12 -0800193 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700194 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800195 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700196 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700197 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800198 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700199 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800200 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700201 return false;
202 }
203 // Readjust to be non-inclusive upper bound.
204 bss_end_ += sizeof(uint32_t);
205 }
206
Andreas Gampe049cff02015-12-01 23:27:12 -0800207 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800208}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800209
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100210// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
211// position by the number of bytes read, i.e. sizeof(T).
212// Return true on success, false if the read would go beyond the end of the OatFile.
213template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100214inline static bool ReadOatDexFileData(const OatFile& oat_file,
215 /*inout*/const uint8_t** oat,
216 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100217 DCHECK(oat != nullptr);
218 DCHECK(value != nullptr);
219 DCHECK_LE(*oat, oat_file.End());
220 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
221 return false;
222 }
223 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
224 typedef __attribute__((__aligned__(1))) T unaligned_type;
225 *value = *reinterpret_cast<const unaligned_type*>(*oat);
226 *oat += sizeof(T);
227 return true;
228}
229
Andreas Gampe049cff02015-12-01 23:27:12 -0800230bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700231 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800232 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100233 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
234 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800235 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700236 return false;
237 }
Ian Rogers13735952014-10-08 12:43:28 -0700238 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700240 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700241 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700242 return false;
243 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700244
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700245 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700246 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700247 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100248 "%p + %zu + %u <= %p",
249 GetLocation().c_str(),
250 Begin(),
251 sizeof(OatHeader),
252 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700253 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700254 return false;
255 }
256
Vladimir Marko09d09432015-09-08 13:47:48 +0100257 size_t pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100258 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100259 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
260 oat_dex_files_storage_.reserve(dex_file_count);
261 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100262 uint32_t dex_file_location_size;
263 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
264 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
265 "location size",
266 GetLocation().c_str(),
267 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700268 return false;
269 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100270 if (UNLIKELY(dex_file_location_size == 0U)) {
271 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
272 GetLocation().c_str(),
273 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700274 return false;
275 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000276 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100277 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
278 "location",
279 GetLocation().c_str(),
280 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700281 return false;
282 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000283 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
284 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700285
Richard Uhlere5fed032015-03-18 08:21:11 -0700286 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
287 abs_dex_location,
288 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700289
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100290 uint32_t dex_file_checksum;
291 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
292 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
293 "dex file checksum",
294 GetLocation().c_str(),
295 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700296 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700297 return false;
298 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700299
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100300 uint32_t dex_file_offset;
301 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
302 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
303 "after dex file offsets",
304 GetLocation().c_str(),
305 i,
306 dex_file_location.c_str());
307 return false;
308 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700309 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100310 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
311 "file offset",
312 GetLocation().c_str(),
313 i,
314 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700315 return false;
316 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700317 if (UNLIKELY(dex_file_offset > Size())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100318 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
319 "offset %u > %zu",
320 GetLocation().c_str(),
321 i,
322 dex_file_location.c_str(),
323 dex_file_offset,
324 Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700325 return false;
326 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000327 if (UNLIKELY(Size() - dex_file_offset < sizeof(DexFile::Header))) {
328 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
329 "offset %u of %zu but the size of dex file header is %zu",
330 GetLocation().c_str(),
331 i,
332 dex_file_location.c_str(),
333 dex_file_offset,
334 Size(),
335 sizeof(DexFile::Header));
336 return false;
337 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800338
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800339 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700340 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100341 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
342 "dex file magic '%s'",
343 GetLocation().c_str(),
344 i,
345 dex_file_location.c_str(),
346 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700347 return false;
348 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700349 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100350 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
351 "dex file version '%s'",
352 GetLocation().c_str(),
353 i,
354 dex_file_location.c_str(),
355 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700356 return false;
357 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800358 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000359 if (Size() - dex_file_offset < header->file_size_) {
360 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
361 "offset %u and size %u truncated at %zu",
362 GetLocation().c_str(),
363 i,
364 dex_file_location.c_str(),
365 dex_file_offset,
366 header->file_size_,
367 Size());
368 return false;
369 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300370
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000371 uint32_t class_offsets_offset;
372 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
373 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
374 "after class offsets offset",
375 GetLocation().c_str(),
376 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300377 dex_file_location.c_str());
378 return false;
379 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000380 if (UNLIKELY(class_offsets_offset > Size()) ||
381 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
382 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
383 "class offsets, offset %u of %zu, class defs %u",
384 GetLocation().c_str(),
385 i,
386 dex_file_location.c_str(),
387 class_offsets_offset,
388 Size(),
389 header->class_defs_size_);
390 return false;
391 }
392 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
393 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
394 "class offsets, offset %u",
395 GetLocation().c_str(),
396 i,
397 dex_file_location.c_str(),
398 class_offsets_offset);
399 return false;
400 }
401 const uint32_t* class_offsets_pointer =
402 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
403
404 uint32_t lookup_table_offset;
405 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
406 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
407 "after lookup table offset",
408 GetLocation().c_str(),
409 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300410 dex_file_location.c_str());
411 return false;
412 }
413 const uint8_t* lookup_table_data = lookup_table_offset != 0u
414 ? Begin() + lookup_table_offset
415 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000416 if (lookup_table_offset != 0u &&
417 (UNLIKELY(lookup_table_offset > Size()) ||
418 UNLIKELY(Size() - lookup_table_offset <
419 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100420 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000421 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100422 GetLocation().c_str(),
423 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000424 dex_file_location.c_str(),
425 lookup_table_offset,
426 Size(),
427 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700428 return false;
429 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100431 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100432 if (dex_cache_arrays != nullptr) {
433 DexCacheArraysLayout layout(pointer_size, *header);
434 if (layout.Size() != 0u) {
435 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100436 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
437 "truncated dex cache arrays, %zu < %zu.",
438 GetLocation().c_str(),
439 i,
440 dex_file_location.c_str(),
441 static_cast<size_t>(bss_end_ - dex_cache_arrays),
442 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100443 return false;
444 }
445 current_dex_cache_arrays = dex_cache_arrays;
446 dex_cache_arrays += layout.Size();
447 }
448 }
449
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100450 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
451
452 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100453 OatDexFile* oat_dex_file = new OatDexFile(this,
454 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100455 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100456 dex_file_checksum,
457 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300458 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000459 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100460 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100461 oat_dex_files_storage_.push_back(oat_dex_file);
462
463 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100464 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100465 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100466 if (canonical_location != dex_file_location) {
467 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
468 oat_dex_files_.Put(canonical_key, oat_dex_file);
469 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700470 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100471
472 if (dex_cache_arrays != bss_end_) {
473 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
474 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100475 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100476 GetLocation().c_str(),
477 static_cast<size_t>(bss_end_ - dex_cache_arrays));
478 return false;
479 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700480 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700481}
482
Andreas Gampe049cff02015-12-01 23:27:12 -0800483////////////////////////
484// OatFile via dlopen //
485////////////////////////
486
487static bool RegisterOatFileLocation(const std::string& location) {
488 if (!kIsTargetBuild) {
489 Runtime* const runtime = Runtime::Current();
490 if (runtime != nullptr && !runtime->IsAotCompiler()) {
491 return runtime->GetOatFileManager().RegisterOatFileLocation(location);
492 }
493 return false;
494 }
495 return true;
496}
497
498static void UnregisterOatFileLocation(const std::string& location) {
499 if (!kIsTargetBuild) {
500 Runtime* const runtime = Runtime::Current();
501 if (runtime != nullptr && !runtime->IsAotCompiler()) {
502 runtime->GetOatFileManager().UnRegisterOatFileLocation(location);
503 }
504 }
505}
506
507class DlOpenOatFile FINAL : public OatFileBase {
508 public:
509 DlOpenOatFile(const std::string& filename, bool executable)
510 : OatFileBase(filename, executable),
511 dlopen_handle_(nullptr),
512 first_oat_(RegisterOatFileLocation(filename)) {
513 }
514
515 ~DlOpenOatFile() {
516 if (dlopen_handle_ != nullptr) {
517 dlclose(dlopen_handle_);
518 }
519 UnregisterOatFileLocation(GetLocation());
520 }
521
522 protected:
523 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
524 std::string* error_msg) const OVERRIDE {
525 const uint8_t* ptr =
526 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
527 if (ptr == nullptr) {
528 *error_msg = dlerror();
529 }
530 return ptr;
531 }
532
533 bool Load(const std::string& elf_filename,
534 uint8_t* oat_file_begin,
535 bool writable,
536 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800537 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800538 std::string* error_msg) OVERRIDE;
539
540 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
541 void PreSetup(const std::string& elf_filename) OVERRIDE;
542
543 private:
544 bool Dlopen(const std::string& elf_filename,
545 uint8_t* oat_file_begin,
546 std::string* error_msg);
547
548 // dlopen handle during runtime.
549 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
550
551 // Dummy memory map objects corresponding to the regions mapped by dlopen.
552 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
553
554 // Track the registration status (= was this the first oat file) for the location.
555 const bool first_oat_;
556
557 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
558};
559
560bool DlOpenOatFile::Load(const std::string& elf_filename,
561 uint8_t* oat_file_begin,
562 bool writable,
563 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800564 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800565 std::string* error_msg) {
566 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
567 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
568 // !executable is a sign that we may want to patch), which may not be allowed for
569 // various reasons.
570 if (!kUseDlopen) {
571 *error_msg = "DlOpen is disabled.";
572 return false;
573 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800574 if (low_4gb) {
575 *error_msg = "DlOpen does not support low 4gb loading.";
576 return false;
577 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800578 if (writable) {
579 *error_msg = "DlOpen does not support writable loading.";
580 return false;
581 }
582 if (!executable) {
583 *error_msg = "DlOpen does not support non-executable loading.";
584 return false;
585 }
586
587 // dlopen always returns the same library if it is already opened on the host. For this reason
588 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
589 // the same library loaded multiple times at different addresses is required for class unloading
590 // and for having dex caches arrays in the .bss section.
591 if (!kIsTargetBuild) {
592 if (!kUseDlopenOnHost) {
593 *error_msg = "DlOpen disabled for host.";
594 return false;
595 }
596 // For RAII, tracking multiple loads is done in the constructor and destructor. The result is
597 // stored in the first_oat_ flag.
598 if (!first_oat_) {
599 *error_msg = "Loading oat files multiple times with dlopen not supported on host.";
600 return false;
601 }
602 }
603
604 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
605 DCHECK(dlopen_handle_ != nullptr || !success);
606
607 return success;
608}
609
610bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
611 uint8_t* oat_file_begin,
612 std::string* error_msg) {
613#ifdef __APPLE__
614 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
615 // but let's fallback to the custom loading code for the time being.
616 UNUSED(elf_filename, oat_file_begin);
617 *error_msg = "Dlopen unsupported on Mac.";
618 return false;
619#else
620 {
621 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
622 if (absolute_path == nullptr) {
623 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
624 return false;
625 }
626#ifdef __ANDROID__
627 android_dlextinfo extinfo;
628 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
629 // (open oat files multiple
630 // times).
631 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
632 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800633 if (oat_file_begin != nullptr) { //
634 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
635 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
636 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800637 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
638#else
639 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
640 UNUSED(oat_file_begin);
641#endif
642 }
643 if (dlopen_handle_ == nullptr) {
644 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
645 return false;
646 }
647 return true;
648#endif
649}
650
651void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800652#ifdef __APPLE__
653 UNUSED(elf_filename);
654 LOG(FATAL) << "Should not reach here.";
655 UNREACHABLE();
656#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800657 struct dl_iterate_context {
658 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
659 auto* context = reinterpret_cast<dl_iterate_context*>(data);
660 // See whether this callback corresponds to the file which we have just loaded.
661 bool contains_begin = false;
662 for (int i = 0; i < info->dlpi_phnum; i++) {
663 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
664 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
665 info->dlpi_phdr[i].p_vaddr);
666 size_t memsz = info->dlpi_phdr[i].p_memsz;
667 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
668 contains_begin = true;
669 break;
670 }
671 }
672 }
673 // Add dummy mmaps for this file.
674 if (contains_begin) {
675 for (int i = 0; i < info->dlpi_phnum; i++) {
676 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
677 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
678 info->dlpi_phdr[i].p_vaddr);
679 size_t memsz = info->dlpi_phdr[i].p_memsz;
680 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
681 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
682 }
683 }
684 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
685 }
686 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
687 }
688 const uint8_t* const begin_;
689 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
690 } context = { Begin(), &dlopen_mmaps_ };
691
692 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
693 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Roland Levillain91d65e02016-01-19 15:59:16 +0000694 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
Andreas Gampe049cff02015-12-01 23:27:12 -0800695 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800696#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800697}
698
699////////////////////////////////////////////////
700// OatFile via our own ElfFile implementation //
701////////////////////////////////////////////////
702
703class ElfOatFile FINAL : public OatFileBase {
704 public:
705 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
706
707 static ElfOatFile* OpenElfFile(File* file,
708 const std::string& location,
709 uint8_t* requested_base,
710 uint8_t* oat_file_begin, // Override base if not null
711 bool writable,
712 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800713 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800714 const char* abs_dex_location,
715 std::string* error_msg);
716
717 bool InitializeFromElfFile(ElfFile* elf_file,
718 const char* abs_dex_location,
719 std::string* error_msg);
720
721 protected:
722 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
723 std::string* error_msg) const OVERRIDE {
724 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
725 if (ptr == nullptr) {
726 *error_msg = "(Internal implementation could not find symbol)";
727 }
728 return ptr;
729 }
730
731 bool Load(const std::string& elf_filename,
732 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
733 bool writable,
734 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800735 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800736 std::string* error_msg) OVERRIDE;
737
738 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
739 }
740
741 private:
742 bool ElfFileOpen(File* file,
743 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
744 bool writable,
745 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800746 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800747 std::string* error_msg);
748
749 private:
750 // Backing memory map for oat file during cross compilation.
751 std::unique_ptr<ElfFile> elf_file_;
752
753 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
754};
755
756ElfOatFile* ElfOatFile::OpenElfFile(File* file,
757 const std::string& location,
758 uint8_t* requested_base,
759 uint8_t* oat_file_begin, // Override base if not null
760 bool writable,
761 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800762 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800763 const char* abs_dex_location,
764 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800765 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800766 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800767 bool success = oat_file->ElfFileOpen(file,
768 oat_file_begin,
769 writable,
770 low_4gb,
771 executable,
772 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800773 if (!success) {
774 CHECK(!error_msg->empty());
775 return nullptr;
776 }
777
778 // Complete the setup.
779 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
780 return nullptr;
781 }
782
783 if (!oat_file->Setup(abs_dex_location, error_msg)) {
784 return nullptr;
785 }
786
787 return oat_file.release();
788}
789
790bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
791 const char* abs_dex_location,
792 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800793 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800794 if (IsExecutable()) {
795 *error_msg = "Cannot initialize from elf file in executable mode.";
796 return false;
797 }
798 elf_file_.reset(elf_file);
799 uint64_t offset, size;
800 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
801 CHECK(has_section);
802 SetBegin(elf_file->Begin() + offset);
803 SetEnd(elf_file->Begin() + size + offset);
804 // Ignore the optional .bss section when opening non-executable.
805 return Setup(abs_dex_location, error_msg);
806}
807
808bool ElfOatFile::Load(const std::string& elf_filename,
809 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
810 bool writable,
811 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800812 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800813 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800814 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800815 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
816 if (file == nullptr) {
817 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
818 return false;
819 }
820 return ElfOatFile::ElfFileOpen(file.get(),
821 oat_file_begin,
822 writable,
823 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800824 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800825 error_msg);
826}
827
828bool ElfOatFile::ElfFileOpen(File* file,
829 uint8_t* oat_file_begin,
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) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800834 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800835 // TODO: rename requested_base to oat_data_begin
836 elf_file_.reset(ElfFile::Open(file,
837 writable,
838 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800839 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800840 error_msg,
841 oat_file_begin));
842 if (elf_file_ == nullptr) {
843 DCHECK(!error_msg->empty());
844 return false;
845 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800846 bool loaded = elf_file_->Load(executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800847 DCHECK(loaded || !error_msg->empty());
848 return loaded;
849}
850
851//////////////////////////
852// General OatFile code //
853//////////////////////////
854
855std::string OatFile::ResolveRelativeEncodedDexLocation(
856 const char* abs_dex_location, const std::string& rel_dex_location) {
857 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
858 // Strip :classes<N>.dex used for secondary multidex files.
859 std::string base = DexFile::GetBaseLocation(rel_dex_location);
860 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
861
862 // Check if the base is a suffix of the provided abs_dex_location.
863 std::string target_suffix = "/" + base;
864 std::string abs_location(abs_dex_location);
865 if (abs_location.size() > target_suffix.size()) {
866 size_t pos = abs_location.size() - target_suffix.size();
867 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
868 return abs_location + multidex_suffix;
869 }
870 }
871 }
872 return rel_dex_location;
873}
874
875static void CheckLocation(const std::string& location) {
876 CHECK(!location.empty());
877}
878
879OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
880 const std::string& location,
881 const char* abs_dex_location,
882 std::string* error_msg) {
883 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
884 return oat_file->InitializeFromElfFile(elf_file, abs_dex_location, error_msg)
885 ? oat_file.release()
886 : nullptr;
887}
888
889OatFile* OatFile::Open(const std::string& filename,
890 const std::string& location,
891 uint8_t* requested_base,
892 uint8_t* oat_file_begin,
893 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800894 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800895 const char* abs_dex_location,
896 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800897 ScopedTrace trace("Open oat file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800898 CHECK(!filename.empty()) << location;
899 CheckLocation(location);
900 std::unique_ptr<OatFile> ret;
901
902 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
903 // disabled.
904 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(filename,
905 location,
906 requested_base,
907 oat_file_begin,
908 false,
909 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800910 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800911 abs_dex_location,
912 error_msg);
913 if (with_dlopen != nullptr) {
914 return with_dlopen;
915 }
916 if (kPrintDlOpenErrorMessage) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800917 LOG(ERROR) << "Failed to dlopen: " << filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -0800918 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800919 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
920 //
921 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
922 //
923 // We use our own ELF loader for Quick to deal with legacy apps that
924 // open a generated dex file by name, remove the file, then open
925 // another generated dex file with the same name. http://b/10614658
926 //
927 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
928 //
929 //
930 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
931 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
932 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(filename,
933 location,
934 requested_base,
935 oat_file_begin,
936 false,
937 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800938 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800939 abs_dex_location,
940 error_msg);
941 return with_internal;
942}
943
944OatFile* OatFile::OpenWritable(File* file,
945 const std::string& location,
946 const char* abs_dex_location,
947 std::string* error_msg) {
948 CheckLocation(location);
949 return ElfOatFile::OpenElfFile(file,
950 location,
951 nullptr,
952 nullptr,
953 true,
954 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800955 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -0800956 abs_dex_location,
957 error_msg);
958}
959
960OatFile* OatFile::OpenReadable(File* file,
961 const std::string& location,
962 const char* abs_dex_location,
963 std::string* error_msg) {
964 CheckLocation(location);
965 return ElfOatFile::OpenElfFile(file,
966 location,
967 nullptr,
968 nullptr,
969 false,
970 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800971 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -0800972 abs_dex_location,
973 error_msg);
974}
975
976OatFile::OatFile(const std::string& location, bool is_executable)
977 : location_(location),
978 begin_(nullptr),
979 end_(nullptr),
980 bss_begin_(nullptr),
981 bss_end_(nullptr),
982 is_executable_(is_executable),
983 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
984 CHECK(!location_.empty());
985}
986
987OatFile::~OatFile() {
988 STLDeleteElements(&oat_dex_files_storage_);
989}
990
Brian Carlstrome24fa612011-09-29 00:53:55 -0700991const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800992 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700993}
994
Ian Rogers13735952014-10-08 12:43:28 -0700995const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700996 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800997 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700998}
999
Ian Rogers13735952014-10-08 12:43:28 -07001000const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001001 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001002 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001003}
1004
Vladimir Marko5c42c292015-02-25 12:02:49 +00001005const uint8_t* OatFile::BssBegin() const {
1006 return bss_begin_;
1007}
1008
1009const uint8_t* OatFile::BssEnd() const {
1010 return bss_end_;
1011}
1012
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001013const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1014 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -08001015 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001016 // NOTE: We assume here that the canonical location for a given dex_location never
1017 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1018 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1019 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001020
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001021 // TODO: Additional analysis of usage patterns to see if this can be simplified
1022 // without any performance loss, for example by not doing the first lock-free lookup.
1023
1024 const OatFile::OatDexFile* oat_dex_file = nullptr;
1025 StringPiece key(dex_location);
1026 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1027 // directly mentioned in the oat file and doesn't require locking.
1028 auto primary_it = oat_dex_files_.find(key);
1029 if (primary_it != oat_dex_files_.end()) {
1030 oat_dex_file = primary_it->second;
1031 DCHECK(oat_dex_file != nullptr);
1032 } else {
1033 // This dex_location is not one of the dex locations directly mentioned in the
1034 // oat file. The correct lookup is via the canonical location but first see in
1035 // the secondary_oat_dex_files_ whether we've looked up this location before.
1036 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1037 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1038 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001039 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001040 } else {
1041 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001042 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001043 if (dex_canonical_location != dex_location) {
1044 StringPiece canonical_key(dex_canonical_location);
1045 auto canonical_it = oat_dex_files_.find(canonical_key);
1046 if (canonical_it != oat_dex_files_.end()) {
1047 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001048 } // else keep null.
1049 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001050
1051 // Copy the key to the string_cache_ and store the result in secondary map.
1052 string_cache_.emplace_back(key.data(), key.length());
1053 StringPiece key_copy(string_cache_.back());
1054 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001055 }
1056 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001057 if (oat_dex_file != nullptr &&
1058 (dex_location_checksum == nullptr ||
1059 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
1060 return oat_dex_file;
1061 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001062
1063 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001064 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001065 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -07001066 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001067 checksum = StringPrintf("0x%08x", *dex_location_checksum);
1068 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001069 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +01001070 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001071 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001072 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001073 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001074 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001075 << " contains OatDexFile " << odf->GetDexFileLocation()
1076 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
1077 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001078 }
Ian Rogers7fe2c692011-12-06 16:35:59 -08001079 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001080 }
Calin Juravle4e1d5792014-07-15 23:56:47 +01001081
Andreas Gampefa8429b2015-04-07 18:34:42 -07001082 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001083}
1084
Brian Carlstrome24fa612011-09-29 00:53:55 -07001085OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001086 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001087 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001088 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001089 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001090 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001091 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001092 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001093 : oat_file_(oat_file),
1094 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001095 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001096 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001097 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001098 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001099 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1100 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001101
1102OatFile::OatDexFile::~OatDexFile() {}
1103
Ian Rogers05f28c62012-10-23 18:12:13 -07001104size_t OatFile::OatDexFile::FileSize() const {
1105 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1106}
1107
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001108std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001109 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001110 return DexFile::Open(dex_file_pointer_,
1111 FileSize(),
1112 dex_file_location_,
1113 dex_file_location_checksum_,
1114 this,
1115 false /* verify */,
1116 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001117}
1118
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001119uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1120 return oat_class_offsets_pointer_[class_def_index];
1121}
1122
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001123OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001124 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001125
Ian Rogers13735952014-10-08 12:43:28 -07001126 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001127 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001128
Ian Rogers13735952014-10-08 12:43:28 -07001129 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001130 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1131 mirror::Class::Status status =
1132 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1133 CHECK_LT(status, mirror::Class::kStatusMax);
1134
Ian Rogers13735952014-10-08 12:43:28 -07001135 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001136 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1137 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1138 CHECK_LT(type, kOatClassMax);
1139
Ian Rogers13735952014-10-08 12:43:28 -07001140 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001141 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001142
Brian Carlstromcd937a92014-03-04 22:53:23 -08001143 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001144 const uint8_t* bitmap_pointer = nullptr;
1145 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001146 if (type != kOatClassNoneCompiled) {
1147 if (type == kOatClassSomeCompiled) {
1148 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1149 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1150 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1151 methods_pointer = bitmap_pointer + bitmap_size;
1152 } else {
1153 methods_pointer = after_type_pointer;
1154 }
1155 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001156 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001157
Richard Uhler07b3c232015-03-31 15:57:54 -07001158 return OatFile::OatClass(oat_file_,
1159 status,
1160 type,
1161 bitmap_size,
1162 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1163 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001164}
1165
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001166OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001167 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001168 OatClassType type,
1169 uint32_t bitmap_size,
1170 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001171 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001172 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001173 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001174 switch (type_) {
1175 case kOatClassAllCompiled: {
1176 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001177 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001178 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001179 break;
1180 }
1181 case kOatClassSomeCompiled: {
1182 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001183 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001184 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001185 break;
1186 }
1187 case kOatClassNoneCompiled: {
1188 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001189 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001190 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001191 break;
1192 }
1193 case kOatClassMax: {
1194 LOG(FATAL) << "Invalid OatClassType " << type_;
1195 break;
1196 }
1197 }
1198}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001199
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001200uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1201 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1202 if (oat_method_offsets == nullptr) {
1203 return 0u;
1204 }
1205 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1206}
1207
1208const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001209 // 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 -07001210 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001211 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001212 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001213 }
1214 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001215 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001216 CHECK_EQ(kOatClassAllCompiled, type_);
1217 methods_pointer_index = method_index;
1218 } else {
1219 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001220 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001221 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001222 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001223 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1224 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001225 }
1226 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001227 return &oat_method_offsets;
1228}
1229
1230const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1231 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1232 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001233 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001234 }
1235 if (oat_file_->IsExecutable() ||
1236 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001237 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001238 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001239 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001240 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1241 // version.
1242 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001243}
1244
Mathieu Chartiere401d142015-04-22 13:56:20 -07001245void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001246 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001247 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001248}
1249
Richard Uhlerd1537b52016-03-29 13:27:41 -07001250bool OatFile::HasPatchInfo() const {
1251 return GetOatHeader().HasPatchInfo();
1252}
1253
Andreas Gampe7ba64962014-10-23 11:37:40 -07001254bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001255 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001256 // TODO: Check against oat_patches. b/18144996
1257}
1258
Sebastien Hertz0de11332015-05-13 12:14:05 +02001259bool OatFile::IsDebuggable() const {
1260 return GetOatHeader().IsDebuggable();
1261}
1262
Andreas Gampe29d38e72016-03-23 15:31:51 +00001263CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1264 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001265}
1266
Andreas Gampe7848da42015-04-09 11:15:04 -07001267static constexpr char kDexClassPathEncodingSeparator = '*';
1268
1269std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1270 std::ostringstream out;
1271
1272 for (const DexFile* dex_file : dex_files) {
1273 out << dex_file->GetLocation().c_str();
1274 out << kDexClassPathEncodingSeparator;
1275 out << dex_file->GetLocationChecksum();
1276 out << kDexClassPathEncodingSeparator;
1277 }
1278
1279 return out.str();
1280}
1281
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001282bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001283 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1284 // No dependencies.
1285 return true;
1286 }
1287
1288 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1289 // Split() instead of manual parsing of the combined char*.
1290 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001291 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001292 if (split.size() % 2 != 0) {
1293 // Expected pairs of location and checksum.
1294 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1295 return false;
1296 }
1297
1298 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1299 std::string& location = *it;
1300 std::string& checksum = *(it + 1);
1301 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1302 if (converted == 0) {
1303 // Conversion error.
1304 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1305 return false;
1306 }
1307
1308 uint32_t dex_checksum;
1309 std::string error_msg;
1310 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001311 &dex_checksum,
1312 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001313 if (converted != dex_checksum) {
1314 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1315 location.c_str(), converted, dex_checksum);
1316 return false;
1317 }
1318 } else {
1319 // Problem retrieving checksum.
1320 // TODO: odex files?
1321 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1322 error_msg.c_str());
1323 return false;
1324 }
1325 }
1326
1327 return true;
1328}
1329
1330bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001331 std::vector<std::string>* locations) {
1332 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001333 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1334 return true;
1335 }
1336
1337 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1338 // Split() instead of manual parsing of the combined char*.
1339 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001340 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001341 if (split.size() % 2 != 0) {
1342 // Expected pairs of location and checksum.
1343 return false;
1344 }
1345
1346 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1347 locations->push_back(*it);
1348 }
1349
1350 return true;
1351}
1352
Brian Carlstrome24fa612011-09-29 00:53:55 -07001353} // namespace art