blob: 033ea563b4a8cf96089c88fac514d713d43b8762 [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"
Ian Rogers1809a722013-08-09 22:05:32 -070053#include "vmap_table.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>
86 static OatFileBase* OpenOatFile(const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070087 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080088 uint8_t* requested_base,
89 uint8_t* oat_file_begin,
90 bool writable,
91 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080092 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070093 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080094 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080095
Andreas Gampe049cff02015-12-01 23:27:12 -080096 protected:
97 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -070098
Andreas Gampe049cff02015-12-01 23:27:12 -080099 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
100 std::string* error_msg) const = 0;
101
102 virtual bool Load(const std::string& elf_filename,
103 uint8_t* oat_file_begin,
104 bool writable,
105 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800106 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800107 std::string* error_msg) = 0;
108
109 bool ComputeFields(uint8_t* requested_base,
110 const std::string& file_path,
111 std::string* error_msg);
112
113 virtual void PreSetup(const std::string& elf_filename) = 0;
114
115 bool Setup(const char* abs_dex_location, std::string* error_msg);
116
117 // Setters exposed for ElfOatFile.
118
119 void SetBegin(const uint8_t* begin) {
120 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700121 }
122
Andreas Gampe049cff02015-12-01 23:27:12 -0800123 void SetEnd(const uint8_t* end) {
124 end_ = end;
125 }
126
127 private:
128 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
129};
130
131template <typename kOatFileBaseSubType>
132OatFileBase* OatFileBase::OpenOatFile(const std::string& elf_filename,
133 const std::string& location,
134 uint8_t* requested_base,
135 uint8_t* oat_file_begin,
136 bool writable,
137 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800138 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800139 const char* abs_dex_location,
140 std::string* error_msg) {
141 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
142 if (!ret->Load(elf_filename,
143 oat_file_begin,
144 writable,
145 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800146 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800147 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800148 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800149 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800150
Andreas Gampe049cff02015-12-01 23:27:12 -0800151 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
152 return nullptr;
153 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800154 ret->PreSetup(elf_filename);
155
156 if (!ret->Setup(abs_dex_location, error_msg)) {
157 return nullptr;
158 }
159
Dave Allison69dfe512014-07-11 17:11:58 +0000160 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800161}
162
Andreas Gampe049cff02015-12-01 23:27:12 -0800163bool OatFileBase::ComputeFields(uint8_t* requested_base,
164 const std::string& file_path,
165 std::string* error_msg) {
166 std::string symbol_error_msg;
167 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700168 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800169 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
170 file_path.c_str(),
171 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700172 return false;
173 }
174 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000175 // Host can fail this check. Do not dump there to avoid polluting the output.
176 if (kIsTargetBuild) {
177 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
178 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700179 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800180 "oatdata=%p != expected=%p. See process maps in the log.",
181 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700182 return false;
183 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800184 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700185 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800186 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
187 file_path.c_str(),
188 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700189 return false;
190 }
191 // Readjust to be non-inclusive upper bound.
192 end_ += sizeof(uint32_t);
193
Andreas Gampe049cff02015-12-01 23:27:12 -0800194 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700195 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800196 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700197 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700198 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800199 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700200 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800201 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700202 return false;
203 }
204 // Readjust to be non-inclusive upper bound.
205 bss_end_ += sizeof(uint32_t);
206 }
207
Andreas Gampe049cff02015-12-01 23:27:12 -0800208 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800209}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800210
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100211// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
212// position by the number of bytes read, i.e. sizeof(T).
213// Return true on success, false if the read would go beyond the end of the OatFile.
214template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100215inline static bool ReadOatDexFileData(const OatFile& oat_file,
216 /*inout*/const uint8_t** oat,
217 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100218 DCHECK(oat != nullptr);
219 DCHECK(value != nullptr);
220 DCHECK_LE(*oat, oat_file.End());
221 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
222 return false;
223 }
224 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
225 typedef __attribute__((__aligned__(1))) T unaligned_type;
226 *value = *reinterpret_cast<const unaligned_type*>(*oat);
227 *oat += sizeof(T);
228 return true;
229}
230
Andreas Gampe049cff02015-12-01 23:27:12 -0800231bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700232 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800233 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100234 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
235 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800236 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700237 return false;
238 }
Ian Rogers13735952014-10-08 12:43:28 -0700239 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700241 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700242 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700243 return false;
244 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700245
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700246 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700247 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700248 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100249 "%p + %zu + %u <= %p",
250 GetLocation().c_str(),
251 Begin(),
252 sizeof(OatHeader),
253 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700254 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700255 return false;
256 }
257
Vladimir Marko09d09432015-09-08 13:47:48 +0100258 size_t pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100259 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100260 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
261 oat_dex_files_storage_.reserve(dex_file_count);
262 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100263 uint32_t dex_file_location_size;
264 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
265 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
266 "location size",
267 GetLocation().c_str(),
268 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700269 return false;
270 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100271 if (UNLIKELY(dex_file_location_size == 0U)) {
272 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
273 GetLocation().c_str(),
274 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700275 return false;
276 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000277 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100278 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
279 "location",
280 GetLocation().c_str(),
281 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700282 return false;
283 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000284 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
285 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700286
Richard Uhlere5fed032015-03-18 08:21:11 -0700287 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
288 abs_dex_location,
289 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700290
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100291 uint32_t dex_file_checksum;
292 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
293 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
294 "dex file checksum",
295 GetLocation().c_str(),
296 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700297 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700298 return false;
299 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100301 uint32_t dex_file_offset;
302 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
303 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
304 "after dex file offsets",
305 GetLocation().c_str(),
306 i,
307 dex_file_location.c_str());
308 return false;
309 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700310 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100311 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
312 "file offset",
313 GetLocation().c_str(),
314 i,
315 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700316 return false;
317 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700318 if (UNLIKELY(dex_file_offset > Size())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100319 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
320 "offset %u > %zu",
321 GetLocation().c_str(),
322 i,
323 dex_file_location.c_str(),
324 dex_file_offset,
325 Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700326 return false;
327 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000328 if (UNLIKELY(Size() - dex_file_offset < sizeof(DexFile::Header))) {
329 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
330 "offset %u of %zu but the size of dex file header is %zu",
331 GetLocation().c_str(),
332 i,
333 dex_file_location.c_str(),
334 dex_file_offset,
335 Size(),
336 sizeof(DexFile::Header));
337 return false;
338 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800339
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800340 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700341 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100342 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
343 "dex file magic '%s'",
344 GetLocation().c_str(),
345 i,
346 dex_file_location.c_str(),
347 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700348 return false;
349 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700350 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100351 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
352 "dex file version '%s'",
353 GetLocation().c_str(),
354 i,
355 dex_file_location.c_str(),
356 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700357 return false;
358 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800359 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000360 if (Size() - dex_file_offset < header->file_size_) {
361 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
362 "offset %u and size %u truncated at %zu",
363 GetLocation().c_str(),
364 i,
365 dex_file_location.c_str(),
366 dex_file_offset,
367 header->file_size_,
368 Size());
369 return false;
370 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300371
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000372 uint32_t class_offsets_offset;
373 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
374 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
375 "after class offsets offset",
376 GetLocation().c_str(),
377 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300378 dex_file_location.c_str());
379 return false;
380 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000381 if (UNLIKELY(class_offsets_offset > Size()) ||
382 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
383 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
384 "class offsets, offset %u of %zu, class defs %u",
385 GetLocation().c_str(),
386 i,
387 dex_file_location.c_str(),
388 class_offsets_offset,
389 Size(),
390 header->class_defs_size_);
391 return false;
392 }
393 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
394 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
395 "class offsets, offset %u",
396 GetLocation().c_str(),
397 i,
398 dex_file_location.c_str(),
399 class_offsets_offset);
400 return false;
401 }
402 const uint32_t* class_offsets_pointer =
403 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
404
405 uint32_t lookup_table_offset;
406 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
407 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
408 "after lookup table offset",
409 GetLocation().c_str(),
410 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300411 dex_file_location.c_str());
412 return false;
413 }
414 const uint8_t* lookup_table_data = lookup_table_offset != 0u
415 ? Begin() + lookup_table_offset
416 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000417 if (lookup_table_offset != 0u &&
418 (UNLIKELY(lookup_table_offset > Size()) ||
419 UNLIKELY(Size() - lookup_table_offset <
420 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100421 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000422 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100423 GetLocation().c_str(),
424 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000425 dex_file_location.c_str(),
426 lookup_table_offset,
427 Size(),
428 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700429 return false;
430 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700431
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100432 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100433 if (dex_cache_arrays != nullptr) {
434 DexCacheArraysLayout layout(pointer_size, *header);
435 if (layout.Size() != 0u) {
436 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100437 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
438 "truncated dex cache arrays, %zu < %zu.",
439 GetLocation().c_str(),
440 i,
441 dex_file_location.c_str(),
442 static_cast<size_t>(bss_end_ - dex_cache_arrays),
443 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100444 return false;
445 }
446 current_dex_cache_arrays = dex_cache_arrays;
447 dex_cache_arrays += layout.Size();
448 }
449 }
450
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100451 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
452
453 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100454 OatDexFile* oat_dex_file = new OatDexFile(this,
455 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100456 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100457 dex_file_checksum,
458 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300459 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000460 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100461 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100462 oat_dex_files_storage_.push_back(oat_dex_file);
463
464 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100465 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100466 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100467 if (canonical_location != dex_file_location) {
468 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
469 oat_dex_files_.Put(canonical_key, oat_dex_file);
470 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700471 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100472
473 if (dex_cache_arrays != bss_end_) {
474 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
475 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100476 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100477 GetLocation().c_str(),
478 static_cast<size_t>(bss_end_ - dex_cache_arrays));
479 return false;
480 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700481 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700482}
483
Andreas Gampe049cff02015-12-01 23:27:12 -0800484////////////////////////
485// OatFile via dlopen //
486////////////////////////
487
488static bool RegisterOatFileLocation(const std::string& location) {
489 if (!kIsTargetBuild) {
490 Runtime* const runtime = Runtime::Current();
491 if (runtime != nullptr && !runtime->IsAotCompiler()) {
492 return runtime->GetOatFileManager().RegisterOatFileLocation(location);
493 }
494 return false;
495 }
496 return true;
497}
498
499static void UnregisterOatFileLocation(const std::string& location) {
500 if (!kIsTargetBuild) {
501 Runtime* const runtime = Runtime::Current();
502 if (runtime != nullptr && !runtime->IsAotCompiler()) {
503 runtime->GetOatFileManager().UnRegisterOatFileLocation(location);
504 }
505 }
506}
507
508class DlOpenOatFile FINAL : public OatFileBase {
509 public:
510 DlOpenOatFile(const std::string& filename, bool executable)
511 : OatFileBase(filename, executable),
512 dlopen_handle_(nullptr),
513 first_oat_(RegisterOatFileLocation(filename)) {
514 }
515
516 ~DlOpenOatFile() {
517 if (dlopen_handle_ != nullptr) {
518 dlclose(dlopen_handle_);
519 }
520 UnregisterOatFileLocation(GetLocation());
521 }
522
523 protected:
524 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
525 std::string* error_msg) const OVERRIDE {
526 const uint8_t* ptr =
527 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
528 if (ptr == nullptr) {
529 *error_msg = dlerror();
530 }
531 return ptr;
532 }
533
534 bool Load(const std::string& elf_filename,
535 uint8_t* oat_file_begin,
536 bool writable,
537 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800538 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800539 std::string* error_msg) OVERRIDE;
540
541 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
542 void PreSetup(const std::string& elf_filename) OVERRIDE;
543
544 private:
545 bool Dlopen(const std::string& elf_filename,
546 uint8_t* oat_file_begin,
547 std::string* error_msg);
548
549 // dlopen handle during runtime.
550 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
551
552 // Dummy memory map objects corresponding to the regions mapped by dlopen.
553 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
554
555 // Track the registration status (= was this the first oat file) for the location.
556 const bool first_oat_;
557
558 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
559};
560
561bool DlOpenOatFile::Load(const std::string& elf_filename,
562 uint8_t* oat_file_begin,
563 bool writable,
564 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800565 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800566 std::string* error_msg) {
567 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
568 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
569 // !executable is a sign that we may want to patch), which may not be allowed for
570 // various reasons.
571 if (!kUseDlopen) {
572 *error_msg = "DlOpen is disabled.";
573 return false;
574 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800575 if (low_4gb) {
576 *error_msg = "DlOpen does not support low 4gb loading.";
577 return false;
578 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800579 if (writable) {
580 *error_msg = "DlOpen does not support writable loading.";
581 return false;
582 }
583 if (!executable) {
584 *error_msg = "DlOpen does not support non-executable loading.";
585 return false;
586 }
587
588 // dlopen always returns the same library if it is already opened on the host. For this reason
589 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
590 // the same library loaded multiple times at different addresses is required for class unloading
591 // and for having dex caches arrays in the .bss section.
592 if (!kIsTargetBuild) {
593 if (!kUseDlopenOnHost) {
594 *error_msg = "DlOpen disabled for host.";
595 return false;
596 }
597 // For RAII, tracking multiple loads is done in the constructor and destructor. The result is
598 // stored in the first_oat_ flag.
599 if (!first_oat_) {
600 *error_msg = "Loading oat files multiple times with dlopen not supported on host.";
601 return false;
602 }
603 }
604
605 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
606 DCHECK(dlopen_handle_ != nullptr || !success);
607
608 return success;
609}
610
611bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
612 uint8_t* oat_file_begin,
613 std::string* error_msg) {
614#ifdef __APPLE__
615 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
616 // but let's fallback to the custom loading code for the time being.
617 UNUSED(elf_filename, oat_file_begin);
618 *error_msg = "Dlopen unsupported on Mac.";
619 return false;
620#else
621 {
622 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
623 if (absolute_path == nullptr) {
624 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
625 return false;
626 }
627#ifdef __ANDROID__
628 android_dlextinfo extinfo;
629 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
630 // (open oat files multiple
631 // times).
632 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
633 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800634 if (oat_file_begin != nullptr) { //
635 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
636 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
637 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800638 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
639#else
640 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
641 UNUSED(oat_file_begin);
642#endif
643 }
644 if (dlopen_handle_ == nullptr) {
645 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
646 return false;
647 }
648 return true;
649#endif
650}
651
652void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800653#ifdef __APPLE__
654 UNUSED(elf_filename);
655 LOG(FATAL) << "Should not reach here.";
656 UNREACHABLE();
657#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800658 struct dl_iterate_context {
659 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
660 auto* context = reinterpret_cast<dl_iterate_context*>(data);
661 // See whether this callback corresponds to the file which we have just loaded.
662 bool contains_begin = false;
663 for (int i = 0; i < info->dlpi_phnum; i++) {
664 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
665 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
666 info->dlpi_phdr[i].p_vaddr);
667 size_t memsz = info->dlpi_phdr[i].p_memsz;
668 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
669 contains_begin = true;
670 break;
671 }
672 }
673 }
674 // Add dummy mmaps for this file.
675 if (contains_begin) {
676 for (int i = 0; i < info->dlpi_phnum; i++) {
677 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
678 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
679 info->dlpi_phdr[i].p_vaddr);
680 size_t memsz = info->dlpi_phdr[i].p_memsz;
681 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
682 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
683 }
684 }
685 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
686 }
687 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
688 }
689 const uint8_t* const begin_;
690 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
691 } context = { Begin(), &dlopen_mmaps_ };
692
693 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
694 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Roland Levillain91d65e02016-01-19 15:59:16 +0000695 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
Andreas Gampe049cff02015-12-01 23:27:12 -0800696 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800697#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800698}
699
700////////////////////////////////////////////////
701// OatFile via our own ElfFile implementation //
702////////////////////////////////////////////////
703
704class ElfOatFile FINAL : public OatFileBase {
705 public:
706 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
707
708 static ElfOatFile* OpenElfFile(File* file,
709 const std::string& location,
710 uint8_t* requested_base,
711 uint8_t* oat_file_begin, // Override base if not null
712 bool writable,
713 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800714 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800715 const char* abs_dex_location,
716 std::string* error_msg);
717
718 bool InitializeFromElfFile(ElfFile* elf_file,
719 const char* abs_dex_location,
720 std::string* error_msg);
721
722 protected:
723 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
724 std::string* error_msg) const OVERRIDE {
725 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
726 if (ptr == nullptr) {
727 *error_msg = "(Internal implementation could not find symbol)";
728 }
729 return ptr;
730 }
731
732 bool Load(const std::string& elf_filename,
733 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
734 bool writable,
735 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800736 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800737 std::string* error_msg) OVERRIDE;
738
739 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
740 }
741
742 private:
743 bool ElfFileOpen(File* file,
744 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
745 bool writable,
746 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800747 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800748 std::string* error_msg);
749
750 private:
751 // Backing memory map for oat file during cross compilation.
752 std::unique_ptr<ElfFile> elf_file_;
753
754 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
755};
756
757ElfOatFile* ElfOatFile::OpenElfFile(File* file,
758 const std::string& location,
759 uint8_t* requested_base,
760 uint8_t* oat_file_begin, // Override base if not null
761 bool writable,
762 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800763 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800764 const char* abs_dex_location,
765 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800766 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800767 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800768 bool success = oat_file->ElfFileOpen(file,
769 oat_file_begin,
770 writable,
771 low_4gb,
772 executable,
773 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800774 if (!success) {
775 CHECK(!error_msg->empty());
776 return nullptr;
777 }
778
779 // Complete the setup.
780 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
781 return nullptr;
782 }
783
784 if (!oat_file->Setup(abs_dex_location, error_msg)) {
785 return nullptr;
786 }
787
788 return oat_file.release();
789}
790
791bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
792 const char* abs_dex_location,
793 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800794 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800795 if (IsExecutable()) {
796 *error_msg = "Cannot initialize from elf file in executable mode.";
797 return false;
798 }
799 elf_file_.reset(elf_file);
800 uint64_t offset, size;
801 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
802 CHECK(has_section);
803 SetBegin(elf_file->Begin() + offset);
804 SetEnd(elf_file->Begin() + size + offset);
805 // Ignore the optional .bss section when opening non-executable.
806 return Setup(abs_dex_location, error_msg);
807}
808
809bool ElfOatFile::Load(const std::string& elf_filename,
810 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
811 bool writable,
812 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800813 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800814 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800815 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800816 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
817 if (file == nullptr) {
818 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
819 return false;
820 }
821 return ElfOatFile::ElfFileOpen(file.get(),
822 oat_file_begin,
823 writable,
824 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800825 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800826 error_msg);
827}
828
829bool ElfOatFile::ElfFileOpen(File* file,
830 uint8_t* oat_file_begin,
831 bool writable,
832 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800833 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800834 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800835 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800836 // TODO: rename requested_base to oat_data_begin
837 elf_file_.reset(ElfFile::Open(file,
838 writable,
839 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800840 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800841 error_msg,
842 oat_file_begin));
843 if (elf_file_ == nullptr) {
844 DCHECK(!error_msg->empty());
845 return false;
846 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800847 bool loaded = elf_file_->Load(executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800848 DCHECK(loaded || !error_msg->empty());
849 return loaded;
850}
851
852//////////////////////////
853// General OatFile code //
854//////////////////////////
855
856std::string OatFile::ResolveRelativeEncodedDexLocation(
857 const char* abs_dex_location, const std::string& rel_dex_location) {
858 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
859 // Strip :classes<N>.dex used for secondary multidex files.
860 std::string base = DexFile::GetBaseLocation(rel_dex_location);
861 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
862
863 // Check if the base is a suffix of the provided abs_dex_location.
864 std::string target_suffix = "/" + base;
865 std::string abs_location(abs_dex_location);
866 if (abs_location.size() > target_suffix.size()) {
867 size_t pos = abs_location.size() - target_suffix.size();
868 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
869 return abs_location + multidex_suffix;
870 }
871 }
872 }
873 return rel_dex_location;
874}
875
876static void CheckLocation(const std::string& location) {
877 CHECK(!location.empty());
878}
879
880OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
881 const std::string& location,
882 const char* abs_dex_location,
883 std::string* error_msg) {
884 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
885 return oat_file->InitializeFromElfFile(elf_file, abs_dex_location, error_msg)
886 ? oat_file.release()
887 : nullptr;
888}
889
890OatFile* OatFile::Open(const std::string& filename,
891 const std::string& location,
892 uint8_t* requested_base,
893 uint8_t* oat_file_begin,
894 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800895 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800896 const char* abs_dex_location,
897 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800898 ScopedTrace trace("Open oat file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800899 CHECK(!filename.empty()) << location;
900 CheckLocation(location);
901 std::unique_ptr<OatFile> ret;
902
903 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
904 // disabled.
905 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(filename,
906 location,
907 requested_base,
908 oat_file_begin,
909 false,
910 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800911 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800912 abs_dex_location,
913 error_msg);
914 if (with_dlopen != nullptr) {
915 return with_dlopen;
916 }
917 if (kPrintDlOpenErrorMessage) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800918 LOG(ERROR) << "Failed to dlopen: " << filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -0800919 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800920 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
921 //
922 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
923 //
924 // We use our own ELF loader for Quick to deal with legacy apps that
925 // open a generated dex file by name, remove the file, then open
926 // another generated dex file with the same name. http://b/10614658
927 //
928 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
929 //
930 //
931 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
932 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
933 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(filename,
934 location,
935 requested_base,
936 oat_file_begin,
937 false,
938 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800939 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800940 abs_dex_location,
941 error_msg);
942 return with_internal;
943}
944
945OatFile* OatFile::OpenWritable(File* file,
946 const std::string& location,
947 const char* abs_dex_location,
948 std::string* error_msg) {
949 CheckLocation(location);
950 return ElfOatFile::OpenElfFile(file,
951 location,
952 nullptr,
953 nullptr,
954 true,
955 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800956 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -0800957 abs_dex_location,
958 error_msg);
959}
960
961OatFile* OatFile::OpenReadable(File* file,
962 const std::string& location,
963 const char* abs_dex_location,
964 std::string* error_msg) {
965 CheckLocation(location);
966 return ElfOatFile::OpenElfFile(file,
967 location,
968 nullptr,
969 nullptr,
970 false,
971 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800972 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -0800973 abs_dex_location,
974 error_msg);
975}
976
977OatFile::OatFile(const std::string& location, bool is_executable)
978 : location_(location),
979 begin_(nullptr),
980 end_(nullptr),
981 bss_begin_(nullptr),
982 bss_end_(nullptr),
983 is_executable_(is_executable),
984 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
985 CHECK(!location_.empty());
986}
987
988OatFile::~OatFile() {
989 STLDeleteElements(&oat_dex_files_storage_);
990}
991
Brian Carlstrome24fa612011-09-29 00:53:55 -0700992const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800993 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700994}
995
Ian Rogers13735952014-10-08 12:43:28 -0700996const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700997 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800998 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700999}
1000
Ian Rogers13735952014-10-08 12:43:28 -07001001const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001002 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001003 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001004}
1005
Vladimir Marko5c42c292015-02-25 12:02:49 +00001006const uint8_t* OatFile::BssBegin() const {
1007 return bss_begin_;
1008}
1009
1010const uint8_t* OatFile::BssEnd() const {
1011 return bss_end_;
1012}
1013
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001014const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1015 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -08001016 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001017 // NOTE: We assume here that the canonical location for a given dex_location never
1018 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1019 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1020 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001021
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001022 // TODO: Additional analysis of usage patterns to see if this can be simplified
1023 // without any performance loss, for example by not doing the first lock-free lookup.
1024
1025 const OatFile::OatDexFile* oat_dex_file = nullptr;
1026 StringPiece key(dex_location);
1027 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1028 // directly mentioned in the oat file and doesn't require locking.
1029 auto primary_it = oat_dex_files_.find(key);
1030 if (primary_it != oat_dex_files_.end()) {
1031 oat_dex_file = primary_it->second;
1032 DCHECK(oat_dex_file != nullptr);
1033 } else {
1034 // This dex_location is not one of the dex locations directly mentioned in the
1035 // oat file. The correct lookup is via the canonical location but first see in
1036 // the secondary_oat_dex_files_ whether we've looked up this location before.
1037 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1038 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1039 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001040 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001041 } else {
1042 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001043 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001044 if (dex_canonical_location != dex_location) {
1045 StringPiece canonical_key(dex_canonical_location);
1046 auto canonical_it = oat_dex_files_.find(canonical_key);
1047 if (canonical_it != oat_dex_files_.end()) {
1048 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001049 } // else keep null.
1050 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001051
1052 // Copy the key to the string_cache_ and store the result in secondary map.
1053 string_cache_.emplace_back(key.data(), key.length());
1054 StringPiece key_copy(string_cache_.back());
1055 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001056 }
1057 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001058 if (oat_dex_file != nullptr &&
1059 (dex_location_checksum == nullptr ||
1060 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
1061 return oat_dex_file;
1062 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001063
1064 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001065 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001066 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -07001067 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001068 checksum = StringPrintf("0x%08x", *dex_location_checksum);
1069 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001070 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +01001071 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001072 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001073 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001074 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001075 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001076 << " contains OatDexFile " << odf->GetDexFileLocation()
1077 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
1078 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001079 }
Ian Rogers7fe2c692011-12-06 16:35:59 -08001080 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001081 }
Calin Juravle4e1d5792014-07-15 23:56:47 +01001082
Andreas Gampefa8429b2015-04-07 18:34:42 -07001083 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001084}
1085
Brian Carlstrome24fa612011-09-29 00:53:55 -07001086OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001087 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001088 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001089 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001090 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001091 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001092 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001093 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001094 : oat_file_(oat_file),
1095 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001096 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001097 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001098 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001099 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001100 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1101 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001102
1103OatFile::OatDexFile::~OatDexFile() {}
1104
Ian Rogers05f28c62012-10-23 18:12:13 -07001105size_t OatFile::OatDexFile::FileSize() const {
1106 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1107}
1108
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001109std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001110 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001111 return DexFile::Open(dex_file_pointer_,
1112 FileSize(),
1113 dex_file_location_,
1114 dex_file_location_checksum_,
1115 this,
1116 false /* verify */,
1117 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001118}
1119
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001120uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1121 return oat_class_offsets_pointer_[class_def_index];
1122}
1123
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001124OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001125 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001126
Ian Rogers13735952014-10-08 12:43:28 -07001127 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001128 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001129
Ian Rogers13735952014-10-08 12:43:28 -07001130 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001131 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1132 mirror::Class::Status status =
1133 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1134 CHECK_LT(status, mirror::Class::kStatusMax);
1135
Ian Rogers13735952014-10-08 12:43:28 -07001136 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001137 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1138 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1139 CHECK_LT(type, kOatClassMax);
1140
Ian Rogers13735952014-10-08 12:43:28 -07001141 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001142 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001143
Brian Carlstromcd937a92014-03-04 22:53:23 -08001144 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001145 const uint8_t* bitmap_pointer = nullptr;
1146 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001147 if (type != kOatClassNoneCompiled) {
1148 if (type == kOatClassSomeCompiled) {
1149 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1150 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1151 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1152 methods_pointer = bitmap_pointer + bitmap_size;
1153 } else {
1154 methods_pointer = after_type_pointer;
1155 }
1156 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001157 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001158
Richard Uhler07b3c232015-03-31 15:57:54 -07001159 return OatFile::OatClass(oat_file_,
1160 status,
1161 type,
1162 bitmap_size,
1163 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1164 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001165}
1166
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001167OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001168 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001169 OatClassType type,
1170 uint32_t bitmap_size,
1171 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001172 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001173 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001174 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001175 switch (type_) {
1176 case kOatClassAllCompiled: {
1177 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001178 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001179 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001180 break;
1181 }
1182 case kOatClassSomeCompiled: {
1183 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001184 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001185 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001186 break;
1187 }
1188 case kOatClassNoneCompiled: {
1189 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001190 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001191 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001192 break;
1193 }
1194 case kOatClassMax: {
1195 LOG(FATAL) << "Invalid OatClassType " << type_;
1196 break;
1197 }
1198 }
1199}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001200
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001201uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1202 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1203 if (oat_method_offsets == nullptr) {
1204 return 0u;
1205 }
1206 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1207}
1208
1209const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001210 // 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 -07001211 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001212 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001213 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001214 }
1215 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001216 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001217 CHECK_EQ(kOatClassAllCompiled, type_);
1218 methods_pointer_index = method_index;
1219 } else {
1220 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001221 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001222 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001223 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001224 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1225 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001226 }
1227 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001228 return &oat_method_offsets;
1229}
1230
1231const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1232 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1233 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001234 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001235 }
1236 if (oat_file_->IsExecutable() ||
1237 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001238 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001239 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001240 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001241 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1242 // version.
1243 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001244}
1245
Mathieu Chartiere401d142015-04-22 13:56:20 -07001246void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001247 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001248 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001249}
1250
Andreas Gampe7ba64962014-10-23 11:37:40 -07001251bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001252 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001253 // TODO: Check against oat_patches. b/18144996
1254}
1255
Sebastien Hertz0de11332015-05-13 12:14:05 +02001256bool OatFile::IsDebuggable() const {
1257 return GetOatHeader().IsDebuggable();
1258}
1259
David Brazdilce4b0ba2016-01-28 15:05:49 +00001260bool OatFile::IsExtractOnly() const {
1261 return GetOatHeader().IsExtractOnly();
1262}
1263
Calin Juravleb077e152016-02-18 18:47:37 +00001264bool OatFile::IsProfileGuideCompiled() const {
1265 return GetOatHeader().IsProfileGuideCompiled();
1266}
1267
Andreas Gampe7848da42015-04-09 11:15:04 -07001268static constexpr char kDexClassPathEncodingSeparator = '*';
1269
1270std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1271 std::ostringstream out;
1272
1273 for (const DexFile* dex_file : dex_files) {
1274 out << dex_file->GetLocation().c_str();
1275 out << kDexClassPathEncodingSeparator;
1276 out << dex_file->GetLocationChecksum();
1277 out << kDexClassPathEncodingSeparator;
1278 }
1279
1280 return out.str();
1281}
1282
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001283bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001284 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1285 // No dependencies.
1286 return true;
1287 }
1288
1289 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1290 // Split() instead of manual parsing of the combined char*.
1291 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001292 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001293 if (split.size() % 2 != 0) {
1294 // Expected pairs of location and checksum.
1295 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1296 return false;
1297 }
1298
1299 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1300 std::string& location = *it;
1301 std::string& checksum = *(it + 1);
1302 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1303 if (converted == 0) {
1304 // Conversion error.
1305 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1306 return false;
1307 }
1308
1309 uint32_t dex_checksum;
1310 std::string error_msg;
1311 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001312 &dex_checksum,
1313 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001314 if (converted != dex_checksum) {
1315 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1316 location.c_str(), converted, dex_checksum);
1317 return false;
1318 }
1319 } else {
1320 // Problem retrieving checksum.
1321 // TODO: odex files?
1322 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1323 error_msg.c_str());
1324 return false;
1325 }
1326 }
1327
1328 return true;
1329}
1330
1331bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001332 std::vector<std::string>* locations) {
1333 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001334 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1335 return true;
1336 }
1337
1338 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1339 // Split() instead of manual parsing of the combined char*.
1340 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001341 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001342 if (split.size() % 2 != 0) {
1343 // Expected pairs of location and checksum.
1344 return false;
1345 }
1346
1347 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1348 locations->push_back(*it);
1349 }
1350
1351 return true;
1352}
1353
Brian Carlstrome24fa612011-09-29 00:53:55 -07001354} // namespace art