blob: c3895479b7ee2bb5726f54d62707a4da8d9454a8 [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,
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,
105 std::string* error_msg) = 0;
106
107 bool ComputeFields(uint8_t* requested_base,
108 const std::string& file_path,
109 std::string* error_msg);
110
111 virtual void PreSetup(const std::string& elf_filename) = 0;
112
113 bool Setup(const char* abs_dex_location, std::string* error_msg);
114
115 // Setters exposed for ElfOatFile.
116
117 void SetBegin(const uint8_t* begin) {
118 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700119 }
120
Andreas Gampe049cff02015-12-01 23:27:12 -0800121 void SetEnd(const uint8_t* end) {
122 end_ = end;
123 }
124
125 private:
126 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
127};
128
129template <typename kOatFileBaseSubType>
130OatFileBase* OatFileBase::OpenOatFile(const std::string& elf_filename,
131 const std::string& location,
132 uint8_t* requested_base,
133 uint8_t* oat_file_begin,
134 bool writable,
135 bool executable,
136 const char* abs_dex_location,
137 std::string* error_msg) {
138 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
139 if (!ret->Load(elf_filename,
140 oat_file_begin,
141 writable,
142 executable,
143 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800144 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800145 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800146
Andreas Gampe049cff02015-12-01 23:27:12 -0800147 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
148 return nullptr;
149 }
150
151 ret->PreSetup(elf_filename);
152
153 if (!ret->Setup(abs_dex_location, error_msg)) {
154 return nullptr;
155 }
156
Dave Allison69dfe512014-07-11 17:11:58 +0000157 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800158}
159
Andreas Gampe049cff02015-12-01 23:27:12 -0800160bool OatFileBase::ComputeFields(uint8_t* requested_base,
161 const std::string& file_path,
162 std::string* error_msg) {
163 std::string symbol_error_msg;
164 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700165 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800166 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
167 file_path.c_str(),
168 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700169 return false;
170 }
171 if (requested_base != nullptr && begin_ != requested_base) {
172 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
173 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800174 "oatdata=%p != expected=%p. See process maps in the log.",
175 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700176 return false;
177 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800178 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700179 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800180 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
181 file_path.c_str(),
182 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700183 return false;
184 }
185 // Readjust to be non-inclusive upper bound.
186 end_ += sizeof(uint32_t);
187
Andreas Gampe049cff02015-12-01 23:27:12 -0800188 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700189 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800190 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700191 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700192 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800193 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700194 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800195 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700196 return false;
197 }
198 // Readjust to be non-inclusive upper bound.
199 bss_end_ += sizeof(uint32_t);
200 }
201
Andreas Gampe049cff02015-12-01 23:27:12 -0800202 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800203}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800204
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100205// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
206// position by the number of bytes read, i.e. sizeof(T).
207// Return true on success, false if the read would go beyond the end of the OatFile.
208template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100209inline static bool ReadOatDexFileData(const OatFile& oat_file,
210 /*inout*/const uint8_t** oat,
211 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100212 DCHECK(oat != nullptr);
213 DCHECK(value != nullptr);
214 DCHECK_LE(*oat, oat_file.End());
215 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
216 return false;
217 }
218 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
219 typedef __attribute__((__aligned__(1))) T unaligned_type;
220 *value = *reinterpret_cast<const unaligned_type*>(*oat);
221 *oat += sizeof(T);
222 return true;
223}
224
Andreas Gampe049cff02015-12-01 23:27:12 -0800225bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700226 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800227 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100228 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
229 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800230 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700231 return false;
232 }
Ian Rogers13735952014-10-08 12:43:28 -0700233 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700235 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700236 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700237 return false;
238 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700239
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700240 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700241 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700242 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100243 "%p + %zu + %u <= %p",
244 GetLocation().c_str(),
245 Begin(),
246 sizeof(OatHeader),
247 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700248 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700249 return false;
250 }
251
Vladimir Marko09d09432015-09-08 13:47:48 +0100252 size_t pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100253 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100254 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
255 oat_dex_files_storage_.reserve(dex_file_count);
256 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100257 uint32_t dex_file_location_size;
258 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
259 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
260 "location size",
261 GetLocation().c_str(),
262 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700263 return false;
264 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100265 if (UNLIKELY(dex_file_location_size == 0U)) {
266 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
267 GetLocation().c_str(),
268 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700269 return false;
270 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000271 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100272 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
273 "location",
274 GetLocation().c_str(),
275 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700276 return false;
277 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000278 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
279 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700280
Richard Uhlere5fed032015-03-18 08:21:11 -0700281 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
282 abs_dex_location,
283 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700284
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100285 uint32_t dex_file_checksum;
286 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
287 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
288 "dex file checksum",
289 GetLocation().c_str(),
290 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700291 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700292 return false;
293 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100295 uint32_t dex_file_offset;
296 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
297 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
298 "after dex file offsets",
299 GetLocation().c_str(),
300 i,
301 dex_file_location.c_str());
302 return false;
303 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700304 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100305 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
306 "file offset",
307 GetLocation().c_str(),
308 i,
309 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700310 return false;
311 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700312 if (UNLIKELY(dex_file_offset > Size())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100313 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
314 "offset %u > %zu",
315 GetLocation().c_str(),
316 i,
317 dex_file_location.c_str(),
318 dex_file_offset,
319 Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700320 return false;
321 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000322 if (UNLIKELY(Size() - dex_file_offset < sizeof(DexFile::Header))) {
323 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
324 "offset %u of %zu but the size of dex file header is %zu",
325 GetLocation().c_str(),
326 i,
327 dex_file_location.c_str(),
328 dex_file_offset,
329 Size(),
330 sizeof(DexFile::Header));
331 return false;
332 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800333
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800334 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700335 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100336 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
337 "dex file magic '%s'",
338 GetLocation().c_str(),
339 i,
340 dex_file_location.c_str(),
341 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700342 return false;
343 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700344 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100345 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
346 "dex file version '%s'",
347 GetLocation().c_str(),
348 i,
349 dex_file_location.c_str(),
350 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700351 return false;
352 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800353 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000354 if (Size() - dex_file_offset < header->file_size_) {
355 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
356 "offset %u and size %u truncated at %zu",
357 GetLocation().c_str(),
358 i,
359 dex_file_location.c_str(),
360 dex_file_offset,
361 header->file_size_,
362 Size());
363 return false;
364 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300365
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000366 uint32_t class_offsets_offset;
367 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
368 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
369 "after class offsets offset",
370 GetLocation().c_str(),
371 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300372 dex_file_location.c_str());
373 return false;
374 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000375 if (UNLIKELY(class_offsets_offset > Size()) ||
376 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
377 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
378 "class offsets, offset %u of %zu, class defs %u",
379 GetLocation().c_str(),
380 i,
381 dex_file_location.c_str(),
382 class_offsets_offset,
383 Size(),
384 header->class_defs_size_);
385 return false;
386 }
387 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
388 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
389 "class offsets, offset %u",
390 GetLocation().c_str(),
391 i,
392 dex_file_location.c_str(),
393 class_offsets_offset);
394 return false;
395 }
396 const uint32_t* class_offsets_pointer =
397 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
398
399 uint32_t lookup_table_offset;
400 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
401 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
402 "after lookup table offset",
403 GetLocation().c_str(),
404 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300405 dex_file_location.c_str());
406 return false;
407 }
408 const uint8_t* lookup_table_data = lookup_table_offset != 0u
409 ? Begin() + lookup_table_offset
410 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000411 if (lookup_table_offset != 0u &&
412 (UNLIKELY(lookup_table_offset > Size()) ||
413 UNLIKELY(Size() - lookup_table_offset <
414 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100415 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000416 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100417 GetLocation().c_str(),
418 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000419 dex_file_location.c_str(),
420 lookup_table_offset,
421 Size(),
422 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700423 return false;
424 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700425
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100426 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100427 if (dex_cache_arrays != nullptr) {
428 DexCacheArraysLayout layout(pointer_size, *header);
429 if (layout.Size() != 0u) {
430 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100431 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
432 "truncated dex cache arrays, %zu < %zu.",
433 GetLocation().c_str(),
434 i,
435 dex_file_location.c_str(),
436 static_cast<size_t>(bss_end_ - dex_cache_arrays),
437 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100438 return false;
439 }
440 current_dex_cache_arrays = dex_cache_arrays;
441 dex_cache_arrays += layout.Size();
442 }
443 }
444
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100445 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
446
447 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100448 OatDexFile* oat_dex_file = new OatDexFile(this,
449 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100450 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100451 dex_file_checksum,
452 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300453 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000454 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100455 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100456 oat_dex_files_storage_.push_back(oat_dex_file);
457
458 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100459 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100460 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100461 if (canonical_location != dex_file_location) {
462 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
463 oat_dex_files_.Put(canonical_key, oat_dex_file);
464 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100466
467 if (dex_cache_arrays != bss_end_) {
468 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
469 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100470 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100471 GetLocation().c_str(),
472 static_cast<size_t>(bss_end_ - dex_cache_arrays));
473 return false;
474 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700475 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700476}
477
Andreas Gampe049cff02015-12-01 23:27:12 -0800478////////////////////////
479// OatFile via dlopen //
480////////////////////////
481
482static bool RegisterOatFileLocation(const std::string& location) {
483 if (!kIsTargetBuild) {
484 Runtime* const runtime = Runtime::Current();
485 if (runtime != nullptr && !runtime->IsAotCompiler()) {
486 return runtime->GetOatFileManager().RegisterOatFileLocation(location);
487 }
488 return false;
489 }
490 return true;
491}
492
493static void UnregisterOatFileLocation(const std::string& location) {
494 if (!kIsTargetBuild) {
495 Runtime* const runtime = Runtime::Current();
496 if (runtime != nullptr && !runtime->IsAotCompiler()) {
497 runtime->GetOatFileManager().UnRegisterOatFileLocation(location);
498 }
499 }
500}
501
502class DlOpenOatFile FINAL : public OatFileBase {
503 public:
504 DlOpenOatFile(const std::string& filename, bool executable)
505 : OatFileBase(filename, executable),
506 dlopen_handle_(nullptr),
507 first_oat_(RegisterOatFileLocation(filename)) {
508 }
509
510 ~DlOpenOatFile() {
511 if (dlopen_handle_ != nullptr) {
512 dlclose(dlopen_handle_);
513 }
514 UnregisterOatFileLocation(GetLocation());
515 }
516
517 protected:
518 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
519 std::string* error_msg) const OVERRIDE {
520 const uint8_t* ptr =
521 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
522 if (ptr == nullptr) {
523 *error_msg = dlerror();
524 }
525 return ptr;
526 }
527
528 bool Load(const std::string& elf_filename,
529 uint8_t* oat_file_begin,
530 bool writable,
531 bool executable,
532 std::string* error_msg) OVERRIDE;
533
534 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
535 void PreSetup(const std::string& elf_filename) OVERRIDE;
536
537 private:
538 bool Dlopen(const std::string& elf_filename,
539 uint8_t* oat_file_begin,
540 std::string* error_msg);
541
542 // dlopen handle during runtime.
543 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
544
545 // Dummy memory map objects corresponding to the regions mapped by dlopen.
546 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
547
548 // Track the registration status (= was this the first oat file) for the location.
549 const bool first_oat_;
550
551 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
552};
553
554bool DlOpenOatFile::Load(const std::string& elf_filename,
555 uint8_t* oat_file_begin,
556 bool writable,
557 bool executable,
558 std::string* error_msg) {
559 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
560 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
561 // !executable is a sign that we may want to patch), which may not be allowed for
562 // various reasons.
563 if (!kUseDlopen) {
564 *error_msg = "DlOpen is disabled.";
565 return false;
566 }
567 if (writable) {
568 *error_msg = "DlOpen does not support writable loading.";
569 return false;
570 }
571 if (!executable) {
572 *error_msg = "DlOpen does not support non-executable loading.";
573 return false;
574 }
575
576 // dlopen always returns the same library if it is already opened on the host. For this reason
577 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
578 // the same library loaded multiple times at different addresses is required for class unloading
579 // and for having dex caches arrays in the .bss section.
580 if (!kIsTargetBuild) {
581 if (!kUseDlopenOnHost) {
582 *error_msg = "DlOpen disabled for host.";
583 return false;
584 }
585 // For RAII, tracking multiple loads is done in the constructor and destructor. The result is
586 // stored in the first_oat_ flag.
587 if (!first_oat_) {
588 *error_msg = "Loading oat files multiple times with dlopen not supported on host.";
589 return false;
590 }
591 }
592
593 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
594 DCHECK(dlopen_handle_ != nullptr || !success);
595
596 return success;
597}
598
599bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
600 uint8_t* oat_file_begin,
601 std::string* error_msg) {
602#ifdef __APPLE__
603 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
604 // but let's fallback to the custom loading code for the time being.
605 UNUSED(elf_filename, oat_file_begin);
606 *error_msg = "Dlopen unsupported on Mac.";
607 return false;
608#else
609 {
610 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
611 if (absolute_path == nullptr) {
612 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
613 return false;
614 }
615#ifdef __ANDROID__
616 android_dlextinfo extinfo;
617 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
618 // (open oat files multiple
619 // times).
620 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
621 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800622 if (oat_file_begin != nullptr) { //
623 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
624 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
625 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800626 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
627#else
628 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
629 UNUSED(oat_file_begin);
630#endif
631 }
632 if (dlopen_handle_ == nullptr) {
633 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
634 return false;
635 }
636 return true;
637#endif
638}
639
640void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800641#ifdef __APPLE__
642 UNUSED(elf_filename);
643 LOG(FATAL) << "Should not reach here.";
644 UNREACHABLE();
645#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800646 struct dl_iterate_context {
647 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
648 auto* context = reinterpret_cast<dl_iterate_context*>(data);
649 // See whether this callback corresponds to the file which we have just loaded.
650 bool contains_begin = false;
651 for (int i = 0; i < info->dlpi_phnum; i++) {
652 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
653 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
654 info->dlpi_phdr[i].p_vaddr);
655 size_t memsz = info->dlpi_phdr[i].p_memsz;
656 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
657 contains_begin = true;
658 break;
659 }
660 }
661 }
662 // Add dummy mmaps for this file.
663 if (contains_begin) {
664 for (int i = 0; i < info->dlpi_phnum; i++) {
665 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
666 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
667 info->dlpi_phdr[i].p_vaddr);
668 size_t memsz = info->dlpi_phdr[i].p_memsz;
669 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
670 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
671 }
672 }
673 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
674 }
675 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
676 }
677 const uint8_t* const begin_;
678 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
679 } context = { Begin(), &dlopen_mmaps_ };
680
681 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
682 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Roland Levillain91d65e02016-01-19 15:59:16 +0000683 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
Andreas Gampe049cff02015-12-01 23:27:12 -0800684 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800685#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800686}
687
688////////////////////////////////////////////////
689// OatFile via our own ElfFile implementation //
690////////////////////////////////////////////////
691
692class ElfOatFile FINAL : public OatFileBase {
693 public:
694 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
695
696 static ElfOatFile* OpenElfFile(File* file,
697 const std::string& location,
698 uint8_t* requested_base,
699 uint8_t* oat_file_begin, // Override base if not null
700 bool writable,
701 bool executable,
702 const char* abs_dex_location,
703 std::string* error_msg);
704
705 bool InitializeFromElfFile(ElfFile* elf_file,
706 const char* abs_dex_location,
707 std::string* error_msg);
708
709 protected:
710 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
711 std::string* error_msg) const OVERRIDE {
712 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
713 if (ptr == nullptr) {
714 *error_msg = "(Internal implementation could not find symbol)";
715 }
716 return ptr;
717 }
718
719 bool Load(const std::string& elf_filename,
720 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
721 bool writable,
722 bool executable,
723 std::string* error_msg) OVERRIDE;
724
725 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
726 }
727
728 private:
729 bool ElfFileOpen(File* file,
730 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
731 bool writable,
732 bool executable,
733 std::string* error_msg);
734
735 private:
736 // Backing memory map for oat file during cross compilation.
737 std::unique_ptr<ElfFile> elf_file_;
738
739 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
740};
741
742ElfOatFile* ElfOatFile::OpenElfFile(File* file,
743 const std::string& location,
744 uint8_t* requested_base,
745 uint8_t* oat_file_begin, // Override base if not null
746 bool writable,
747 bool executable,
748 const char* abs_dex_location,
749 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800750 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800751 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
752 bool success = oat_file->ElfFileOpen(file, oat_file_begin, writable, executable, error_msg);
753 if (!success) {
754 CHECK(!error_msg->empty());
755 return nullptr;
756 }
757
758 // Complete the setup.
759 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
760 return nullptr;
761 }
762
763 if (!oat_file->Setup(abs_dex_location, error_msg)) {
764 return nullptr;
765 }
766
767 return oat_file.release();
768}
769
770bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
771 const char* abs_dex_location,
772 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800773 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800774 if (IsExecutable()) {
775 *error_msg = "Cannot initialize from elf file in executable mode.";
776 return false;
777 }
778 elf_file_.reset(elf_file);
779 uint64_t offset, size;
780 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
781 CHECK(has_section);
782 SetBegin(elf_file->Begin() + offset);
783 SetEnd(elf_file->Begin() + size + offset);
784 // Ignore the optional .bss section when opening non-executable.
785 return Setup(abs_dex_location, error_msg);
786}
787
788bool ElfOatFile::Load(const std::string& elf_filename,
789 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
790 bool writable,
791 bool executable,
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 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
795 if (file == nullptr) {
796 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
797 return false;
798 }
799 return ElfOatFile::ElfFileOpen(file.get(),
800 oat_file_begin,
801 writable,
802 executable,
803 error_msg);
804}
805
806bool ElfOatFile::ElfFileOpen(File* file,
807 uint8_t* oat_file_begin,
808 bool writable,
809 bool executable,
810 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800811 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800812 // TODO: rename requested_base to oat_data_begin
813 elf_file_.reset(ElfFile::Open(file,
814 writable,
815 /*program_header_only*/true,
816 error_msg,
817 oat_file_begin));
818 if (elf_file_ == nullptr) {
819 DCHECK(!error_msg->empty());
820 return false;
821 }
822 bool loaded = elf_file_->Load(executable, error_msg);
823 DCHECK(loaded || !error_msg->empty());
824 return loaded;
825}
826
827//////////////////////////
828// General OatFile code //
829//////////////////////////
830
831std::string OatFile::ResolveRelativeEncodedDexLocation(
832 const char* abs_dex_location, const std::string& rel_dex_location) {
833 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
834 // Strip :classes<N>.dex used for secondary multidex files.
835 std::string base = DexFile::GetBaseLocation(rel_dex_location);
836 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
837
838 // Check if the base is a suffix of the provided abs_dex_location.
839 std::string target_suffix = "/" + base;
840 std::string abs_location(abs_dex_location);
841 if (abs_location.size() > target_suffix.size()) {
842 size_t pos = abs_location.size() - target_suffix.size();
843 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
844 return abs_location + multidex_suffix;
845 }
846 }
847 }
848 return rel_dex_location;
849}
850
851static void CheckLocation(const std::string& location) {
852 CHECK(!location.empty());
853}
854
855OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
856 const std::string& location,
857 const char* abs_dex_location,
858 std::string* error_msg) {
859 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
860 return oat_file->InitializeFromElfFile(elf_file, abs_dex_location, error_msg)
861 ? oat_file.release()
862 : nullptr;
863}
864
865OatFile* OatFile::Open(const std::string& filename,
866 const std::string& location,
867 uint8_t* requested_base,
868 uint8_t* oat_file_begin,
869 bool executable,
870 const char* abs_dex_location,
871 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800872 ScopedTrace trace("Open oat file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800873 CHECK(!filename.empty()) << location;
874 CheckLocation(location);
875 std::unique_ptr<OatFile> ret;
876
877 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
878 // disabled.
879 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(filename,
880 location,
881 requested_base,
882 oat_file_begin,
883 false,
884 executable,
885 abs_dex_location,
886 error_msg);
887 if (with_dlopen != nullptr) {
888 return with_dlopen;
889 }
890 if (kPrintDlOpenErrorMessage) {
891 LOG(ERROR) << "Failed to dlopen: " << *error_msg;
892 }
893
894 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
895 //
896 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
897 //
898 // We use our own ELF loader for Quick to deal with legacy apps that
899 // open a generated dex file by name, remove the file, then open
900 // another generated dex file with the same name. http://b/10614658
901 //
902 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
903 //
904 //
905 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
906 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
907 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(filename,
908 location,
909 requested_base,
910 oat_file_begin,
911 false,
912 executable,
913 abs_dex_location,
914 error_msg);
915 return with_internal;
916}
917
918OatFile* OatFile::OpenWritable(File* file,
919 const std::string& location,
920 const char* abs_dex_location,
921 std::string* error_msg) {
922 CheckLocation(location);
923 return ElfOatFile::OpenElfFile(file,
924 location,
925 nullptr,
926 nullptr,
927 true,
928 false,
929 abs_dex_location,
930 error_msg);
931}
932
933OatFile* OatFile::OpenReadable(File* file,
934 const std::string& location,
935 const char* abs_dex_location,
936 std::string* error_msg) {
937 CheckLocation(location);
938 return ElfOatFile::OpenElfFile(file,
939 location,
940 nullptr,
941 nullptr,
942 false,
943 false,
944 abs_dex_location,
945 error_msg);
946}
947
948OatFile::OatFile(const std::string& location, bool is_executable)
949 : location_(location),
950 begin_(nullptr),
951 end_(nullptr),
952 bss_begin_(nullptr),
953 bss_end_(nullptr),
954 is_executable_(is_executable),
955 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
956 CHECK(!location_.empty());
957}
958
959OatFile::~OatFile() {
960 STLDeleteElements(&oat_dex_files_storage_);
961}
962
Brian Carlstrome24fa612011-09-29 00:53:55 -0700963const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800964 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700965}
966
Ian Rogers13735952014-10-08 12:43:28 -0700967const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700968 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800969 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700970}
971
Ian Rogers13735952014-10-08 12:43:28 -0700972const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700973 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800974 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700975}
976
Vladimir Marko5c42c292015-02-25 12:02:49 +0000977const uint8_t* OatFile::BssBegin() const {
978 return bss_begin_;
979}
980
981const uint8_t* OatFile::BssEnd() const {
982 return bss_end_;
983}
984
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700985const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
986 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800987 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100988 // NOTE: We assume here that the canonical location for a given dex_location never
989 // changes. If it does (i.e. some symlink used by the filename changes) we may return
990 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
991 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100992
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100993 // TODO: Additional analysis of usage patterns to see if this can be simplified
994 // without any performance loss, for example by not doing the first lock-free lookup.
995
996 const OatFile::OatDexFile* oat_dex_file = nullptr;
997 StringPiece key(dex_location);
998 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
999 // directly mentioned in the oat file and doesn't require locking.
1000 auto primary_it = oat_dex_files_.find(key);
1001 if (primary_it != oat_dex_files_.end()) {
1002 oat_dex_file = primary_it->second;
1003 DCHECK(oat_dex_file != nullptr);
1004 } else {
1005 // This dex_location is not one of the dex locations directly mentioned in the
1006 // oat file. The correct lookup is via the canonical location but first see in
1007 // the secondary_oat_dex_files_ whether we've looked up this location before.
1008 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1009 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1010 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001011 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001012 } else {
1013 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001014 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001015 if (dex_canonical_location != dex_location) {
1016 StringPiece canonical_key(dex_canonical_location);
1017 auto canonical_it = oat_dex_files_.find(canonical_key);
1018 if (canonical_it != oat_dex_files_.end()) {
1019 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001020 } // else keep null.
1021 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001022
1023 // Copy the key to the string_cache_ and store the result in secondary map.
1024 string_cache_.emplace_back(key.data(), key.length());
1025 StringPiece key_copy(string_cache_.back());
1026 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001027 }
1028 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001029 if (oat_dex_file != nullptr &&
1030 (dex_location_checksum == nullptr ||
1031 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
1032 return oat_dex_file;
1033 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001034
1035 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001036 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001037 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -07001038 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001039 checksum = StringPrintf("0x%08x", *dex_location_checksum);
1040 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001041 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +01001042 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001043 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001044 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001045 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001046 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001047 << " contains OatDexFile " << odf->GetDexFileLocation()
1048 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
1049 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001050 }
Ian Rogers7fe2c692011-12-06 16:35:59 -08001051 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001052 }
Calin Juravle4e1d5792014-07-15 23:56:47 +01001053
Andreas Gampefa8429b2015-04-07 18:34:42 -07001054 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001055}
1056
Brian Carlstrome24fa612011-09-29 00:53:55 -07001057OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001058 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001059 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001060 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001061 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001062 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001063 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001064 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001065 : oat_file_(oat_file),
1066 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001067 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001068 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001069 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001070 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001071 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1072 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001073
1074OatFile::OatDexFile::~OatDexFile() {}
1075
Ian Rogers05f28c62012-10-23 18:12:13 -07001076size_t OatFile::OatDexFile::FileSize() const {
1077 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1078}
1079
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001080std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001081 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001082 return DexFile::Open(dex_file_pointer_,
1083 FileSize(),
1084 dex_file_location_,
1085 dex_file_location_checksum_,
1086 this,
1087 false /* verify */,
1088 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001089}
1090
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001091uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1092 return oat_class_offsets_pointer_[class_def_index];
1093}
1094
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001095OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001096 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001097
Ian Rogers13735952014-10-08 12:43:28 -07001098 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001099 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001100
Ian Rogers13735952014-10-08 12:43:28 -07001101 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001102 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1103 mirror::Class::Status status =
1104 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1105 CHECK_LT(status, mirror::Class::kStatusMax);
1106
Ian Rogers13735952014-10-08 12:43:28 -07001107 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001108 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1109 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1110 CHECK_LT(type, kOatClassMax);
1111
Ian Rogers13735952014-10-08 12:43:28 -07001112 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001113 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001114
Brian Carlstromcd937a92014-03-04 22:53:23 -08001115 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001116 const uint8_t* bitmap_pointer = nullptr;
1117 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001118 if (type != kOatClassNoneCompiled) {
1119 if (type == kOatClassSomeCompiled) {
1120 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1121 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1122 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1123 methods_pointer = bitmap_pointer + bitmap_size;
1124 } else {
1125 methods_pointer = after_type_pointer;
1126 }
1127 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001128 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001129
Richard Uhler07b3c232015-03-31 15:57:54 -07001130 return OatFile::OatClass(oat_file_,
1131 status,
1132 type,
1133 bitmap_size,
1134 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1135 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001136}
1137
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001138OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001139 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001140 OatClassType type,
1141 uint32_t bitmap_size,
1142 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001143 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001144 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001145 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001146 switch (type_) {
1147 case kOatClassAllCompiled: {
1148 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001149 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001150 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001151 break;
1152 }
1153 case kOatClassSomeCompiled: {
1154 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001155 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001156 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001157 break;
1158 }
1159 case kOatClassNoneCompiled: {
1160 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001161 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001162 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001163 break;
1164 }
1165 case kOatClassMax: {
1166 LOG(FATAL) << "Invalid OatClassType " << type_;
1167 break;
1168 }
1169 }
1170}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001171
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001172uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1173 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1174 if (oat_method_offsets == nullptr) {
1175 return 0u;
1176 }
1177 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1178}
1179
1180const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001181 // 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 -07001182 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001183 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001184 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001185 }
1186 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001187 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001188 CHECK_EQ(kOatClassAllCompiled, type_);
1189 methods_pointer_index = method_index;
1190 } else {
1191 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001192 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001193 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001194 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001195 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1196 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001197 }
1198 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001199 return &oat_method_offsets;
1200}
1201
1202const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1203 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1204 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001205 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001206 }
1207 if (oat_file_->IsExecutable() ||
1208 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001209 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001210 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001211 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001212 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1213 // version.
1214 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001215}
1216
Mathieu Chartiere401d142015-04-22 13:56:20 -07001217void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001218 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001219 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001220}
1221
Andreas Gampe7ba64962014-10-23 11:37:40 -07001222bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001223 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001224 // TODO: Check against oat_patches. b/18144996
1225}
1226
Sebastien Hertz0de11332015-05-13 12:14:05 +02001227bool OatFile::IsDebuggable() const {
1228 return GetOatHeader().IsDebuggable();
1229}
1230
David Brazdilce4b0ba2016-01-28 15:05:49 +00001231bool OatFile::IsExtractOnly() const {
1232 return GetOatHeader().IsExtractOnly();
1233}
1234
Andreas Gampe7848da42015-04-09 11:15:04 -07001235static constexpr char kDexClassPathEncodingSeparator = '*';
1236
1237std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1238 std::ostringstream out;
1239
1240 for (const DexFile* dex_file : dex_files) {
1241 out << dex_file->GetLocation().c_str();
1242 out << kDexClassPathEncodingSeparator;
1243 out << dex_file->GetLocationChecksum();
1244 out << kDexClassPathEncodingSeparator;
1245 }
1246
1247 return out.str();
1248}
1249
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001250bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001251 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1252 // No dependencies.
1253 return true;
1254 }
1255
1256 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1257 // Split() instead of manual parsing of the combined char*.
1258 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001259 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001260 if (split.size() % 2 != 0) {
1261 // Expected pairs of location and checksum.
1262 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1263 return false;
1264 }
1265
1266 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1267 std::string& location = *it;
1268 std::string& checksum = *(it + 1);
1269 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1270 if (converted == 0) {
1271 // Conversion error.
1272 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1273 return false;
1274 }
1275
1276 uint32_t dex_checksum;
1277 std::string error_msg;
1278 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001279 &dex_checksum,
1280 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001281 if (converted != dex_checksum) {
1282 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1283 location.c_str(), converted, dex_checksum);
1284 return false;
1285 }
1286 } else {
1287 // Problem retrieving checksum.
1288 // TODO: odex files?
1289 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1290 error_msg.c_str());
1291 return false;
1292 }
1293 }
1294
1295 return true;
1296}
1297
1298bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001299 std::vector<std::string>* locations) {
1300 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001301 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1302 return true;
1303 }
1304
1305 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1306 // Split() instead of manual parsing of the combined char*.
1307 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001308 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001309 if (split.size() % 2 != 0) {
1310 // Expected pairs of location and checksum.
1311 return false;
1312 }
1313
1314 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1315 locations->push_back(*it);
1316 }
1317
1318 return true;
1319}
1320
Brian Carlstrome24fa612011-09-29 00:53:55 -07001321} // namespace art