blob: 5fbede597499b6869bf6f457898e9929caa8091e [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"
Elliott Hughes76160052012-12-12 16:31:20 -080038#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080039#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070040#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000042#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070044#include "mirror/object-inl.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010045#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070046#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070047#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070048#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010050#include "utils/dex_cache_arrays_layout-inl.h"
Ian Rogers1809a722013-08-09 22:05:32 -070051#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070052
53namespace art {
54
Andreas Gampe049cff02015-12-01 23:27:12 -080055// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000056static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070057
Andreas Gampe049cff02015-12-01 23:27:12 -080058// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070059// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
60// multiple times). However, if/when we switch the above, we likely want to switch this, too,
61// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000062static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070063
64// For debugging, Open will print DlOpen error message if set to true.
65static constexpr bool kPrintDlOpenErrorMessage = false;
66
Andreas Gampe049cff02015-12-01 23:27:12 -080067// Note for OatFileBase and descendents:
68//
69// These are used in OatFile::Open to try all our loaders.
70//
71// The process is simple:
72//
73// 1) Allocate an instance through the standard constructor (location, executable)
74// 2) Load() to try to open the file.
75// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
76// 4) PreSetup() for any steps that should be done before the final setup.
77// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070078
Andreas Gampe049cff02015-12-01 23:27:12 -080079class OatFileBase : public OatFile {
80 public:
81 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070082
Andreas Gampe049cff02015-12-01 23:27:12 -080083 template <typename kOatFileBaseSubType>
84 static OatFileBase* OpenOatFile(const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070085 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080086 uint8_t* requested_base,
87 uint8_t* oat_file_begin,
88 bool writable,
89 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -070090 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080091 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080092
Andreas Gampe049cff02015-12-01 23:27:12 -080093 protected:
94 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -070095
Andreas Gampe049cff02015-12-01 23:27:12 -080096 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
97 std::string* error_msg) const = 0;
98
99 virtual bool Load(const std::string& elf_filename,
100 uint8_t* oat_file_begin,
101 bool writable,
102 bool executable,
103 std::string* error_msg) = 0;
104
105 bool ComputeFields(uint8_t* requested_base,
106 const std::string& file_path,
107 std::string* error_msg);
108
109 virtual void PreSetup(const std::string& elf_filename) = 0;
110
111 bool Setup(const char* abs_dex_location, std::string* error_msg);
112
113 // Setters exposed for ElfOatFile.
114
115 void SetBegin(const uint8_t* begin) {
116 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700117 }
118
Andreas Gampe049cff02015-12-01 23:27:12 -0800119 void SetEnd(const uint8_t* end) {
120 end_ = end;
121 }
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
125};
126
127template <typename kOatFileBaseSubType>
128OatFileBase* OatFileBase::OpenOatFile(const std::string& elf_filename,
129 const std::string& location,
130 uint8_t* requested_base,
131 uint8_t* oat_file_begin,
132 bool writable,
133 bool executable,
134 const char* abs_dex_location,
135 std::string* error_msg) {
136 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
137 if (!ret->Load(elf_filename,
138 oat_file_begin,
139 writable,
140 executable,
141 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800142 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800144
Andreas Gampe049cff02015-12-01 23:27:12 -0800145 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
146 return nullptr;
147 }
148
149 ret->PreSetup(elf_filename);
150
151 if (!ret->Setup(abs_dex_location, error_msg)) {
152 return nullptr;
153 }
154
Dave Allison69dfe512014-07-11 17:11:58 +0000155 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156}
157
Andreas Gampe049cff02015-12-01 23:27:12 -0800158bool OatFileBase::ComputeFields(uint8_t* requested_base,
159 const std::string& file_path,
160 std::string* error_msg) {
161 std::string symbol_error_msg;
162 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700163 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800164 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
165 file_path.c_str(),
166 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700167 return false;
168 }
169 if (requested_base != nullptr && begin_ != requested_base) {
170 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
171 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800172 "oatdata=%p != expected=%p. See process maps in the log.",
173 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700174 return false;
175 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800176 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700177 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800178 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
179 file_path.c_str(),
180 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700181 return false;
182 }
183 // Readjust to be non-inclusive upper bound.
184 end_ += sizeof(uint32_t);
185
Andreas Gampe049cff02015-12-01 23:27:12 -0800186 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700187 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800188 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700189 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700190 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800191 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700192 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800193 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700194 return false;
195 }
196 // Readjust to be non-inclusive upper bound.
197 bss_end_ += sizeof(uint32_t);
198 }
199
Andreas Gampe049cff02015-12-01 23:27:12 -0800200 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800202
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100203// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
204// position by the number of bytes read, i.e. sizeof(T).
205// Return true on success, false if the read would go beyond the end of the OatFile.
206template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100207inline static bool ReadOatDexFileData(const OatFile& oat_file,
208 /*inout*/const uint8_t** oat,
209 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100210 DCHECK(oat != nullptr);
211 DCHECK(value != nullptr);
212 DCHECK_LE(*oat, oat_file.End());
213 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
214 return false;
215 }
216 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
217 typedef __attribute__((__aligned__(1))) T unaligned_type;
218 *value = *reinterpret_cast<const unaligned_type*>(*oat);
219 *oat += sizeof(T);
220 return true;
221}
222
Andreas Gampe049cff02015-12-01 23:27:12 -0800223bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700224 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800225 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100226 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
227 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800228 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700229 return false;
230 }
Ian Rogers13735952014-10-08 12:43:28 -0700231 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700232 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700233 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700234 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700235 return false;
236 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700237
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700238 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700239 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700240 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100241 "%p + %zu + %u <= %p",
242 GetLocation().c_str(),
243 Begin(),
244 sizeof(OatHeader),
245 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700246 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700247 return false;
248 }
249
Vladimir Marko09d09432015-09-08 13:47:48 +0100250 size_t pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100251 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100252 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
253 oat_dex_files_storage_.reserve(dex_file_count);
254 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100255 uint32_t dex_file_location_size;
256 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
257 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
258 "location size",
259 GetLocation().c_str(),
260 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700261 return false;
262 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100263 if (UNLIKELY(dex_file_location_size == 0U)) {
264 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
265 GetLocation().c_str(),
266 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700267 return false;
268 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700269
270 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
271 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700272 if (UNLIKELY(oat > End())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100273 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
274 "location",
275 GetLocation().c_str(),
276 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700277 return false;
278 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279
Richard Uhlere5fed032015-03-18 08:21:11 -0700280 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
281 abs_dex_location,
282 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700283
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100284 uint32_t dex_file_checksum;
285 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
286 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
287 "dex file checksum",
288 GetLocation().c_str(),
289 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700290 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700291 return false;
292 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100294 uint32_t dex_file_offset;
295 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
296 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
297 "after dex file offsets",
298 GetLocation().c_str(),
299 i,
300 dex_file_location.c_str());
301 return false;
302 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700303 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100304 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
305 "file offset",
306 GetLocation().c_str(),
307 i,
308 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700309 return false;
310 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700311 if (UNLIKELY(dex_file_offset > Size())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100312 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
313 "offset %u > %zu",
314 GetLocation().c_str(),
315 i,
316 dex_file_location.c_str(),
317 dex_file_offset,
318 Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700319 return false;
320 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800321
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800322 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700323 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100324 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
325 "dex file magic '%s'",
326 GetLocation().c_str(),
327 i,
328 dex_file_location.c_str(),
329 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700330 return false;
331 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100333 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
334 "dex file version '%s'",
335 GetLocation().c_str(),
336 i,
337 dex_file_location.c_str(),
338 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700339 return false;
340 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800341 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300342
343 if (UNLIKELY(oat > End())) {
344 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
345 "lookup table offset", GetLocation().c_str(), i,
346 dex_file_location.c_str());
347 return false;
348 }
349 uint32_t lookup_table_offset = *reinterpret_cast<const uint32_t*>(oat);
350 oat += sizeof(lookup_table_offset);
351 if (Begin() + lookup_table_offset > End()) {
352 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
353 "lookup table", GetLocation().c_str(), i,
354 dex_file_location.c_str());
355 return false;
356 }
357 const uint8_t* lookup_table_data = lookup_table_offset != 0u
358 ? Begin() + lookup_table_offset
359 : nullptr;
360
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800361 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800362
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800363 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700364 if (UNLIKELY(oat > End())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100365 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
366 "method offsets",
367 GetLocation().c_str(),
368 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700369 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700370 return false;
371 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100373 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100374 if (dex_cache_arrays != nullptr) {
375 DexCacheArraysLayout layout(pointer_size, *header);
376 if (layout.Size() != 0u) {
377 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100378 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
379 "truncated dex cache arrays, %zu < %zu.",
380 GetLocation().c_str(),
381 i,
382 dex_file_location.c_str(),
383 static_cast<size_t>(bss_end_ - dex_cache_arrays),
384 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100385 return false;
386 }
387 current_dex_cache_arrays = dex_cache_arrays;
388 dex_cache_arrays += layout.Size();
389 }
390 }
391
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100392 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
393
394 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100395 OatDexFile* oat_dex_file = new OatDexFile(this,
396 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100397 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100398 dex_file_checksum,
399 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300400 lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +0100401 methods_offsets_pointer,
402 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100403 oat_dex_files_storage_.push_back(oat_dex_file);
404
405 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100406 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100407 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100408 if (canonical_location != dex_file_location) {
409 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
410 oat_dex_files_.Put(canonical_key, oat_dex_file);
411 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700412 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100413
414 if (dex_cache_arrays != bss_end_) {
415 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
416 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100417 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100418 GetLocation().c_str(),
419 static_cast<size_t>(bss_end_ - dex_cache_arrays));
420 return false;
421 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700422 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700423}
424
Andreas Gampe049cff02015-12-01 23:27:12 -0800425////////////////////////
426// OatFile via dlopen //
427////////////////////////
428
429static bool RegisterOatFileLocation(const std::string& location) {
430 if (!kIsTargetBuild) {
431 Runtime* const runtime = Runtime::Current();
432 if (runtime != nullptr && !runtime->IsAotCompiler()) {
433 return runtime->GetOatFileManager().RegisterOatFileLocation(location);
434 }
435 return false;
436 }
437 return true;
438}
439
440static void UnregisterOatFileLocation(const std::string& location) {
441 if (!kIsTargetBuild) {
442 Runtime* const runtime = Runtime::Current();
443 if (runtime != nullptr && !runtime->IsAotCompiler()) {
444 runtime->GetOatFileManager().UnRegisterOatFileLocation(location);
445 }
446 }
447}
448
449class DlOpenOatFile FINAL : public OatFileBase {
450 public:
451 DlOpenOatFile(const std::string& filename, bool executable)
452 : OatFileBase(filename, executable),
453 dlopen_handle_(nullptr),
454 first_oat_(RegisterOatFileLocation(filename)) {
455 }
456
457 ~DlOpenOatFile() {
458 if (dlopen_handle_ != nullptr) {
459 dlclose(dlopen_handle_);
460 }
461 UnregisterOatFileLocation(GetLocation());
462 }
463
464 protected:
465 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
466 std::string* error_msg) const OVERRIDE {
467 const uint8_t* ptr =
468 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
469 if (ptr == nullptr) {
470 *error_msg = dlerror();
471 }
472 return ptr;
473 }
474
475 bool Load(const std::string& elf_filename,
476 uint8_t* oat_file_begin,
477 bool writable,
478 bool executable,
479 std::string* error_msg) OVERRIDE;
480
481 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
482 void PreSetup(const std::string& elf_filename) OVERRIDE;
483
484 private:
485 bool Dlopen(const std::string& elf_filename,
486 uint8_t* oat_file_begin,
487 std::string* error_msg);
488
489 // dlopen handle during runtime.
490 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
491
492 // Dummy memory map objects corresponding to the regions mapped by dlopen.
493 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
494
495 // Track the registration status (= was this the first oat file) for the location.
496 const bool first_oat_;
497
498 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
499};
500
501bool DlOpenOatFile::Load(const std::string& elf_filename,
502 uint8_t* oat_file_begin,
503 bool writable,
504 bool executable,
505 std::string* error_msg) {
506 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
507 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
508 // !executable is a sign that we may want to patch), which may not be allowed for
509 // various reasons.
510 if (!kUseDlopen) {
511 *error_msg = "DlOpen is disabled.";
512 return false;
513 }
514 if (writable) {
515 *error_msg = "DlOpen does not support writable loading.";
516 return false;
517 }
518 if (!executable) {
519 *error_msg = "DlOpen does not support non-executable loading.";
520 return false;
521 }
522
523 // dlopen always returns the same library if it is already opened on the host. For this reason
524 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
525 // the same library loaded multiple times at different addresses is required for class unloading
526 // and for having dex caches arrays in the .bss section.
527 if (!kIsTargetBuild) {
528 if (!kUseDlopenOnHost) {
529 *error_msg = "DlOpen disabled for host.";
530 return false;
531 }
532 // For RAII, tracking multiple loads is done in the constructor and destructor. The result is
533 // stored in the first_oat_ flag.
534 if (!first_oat_) {
535 *error_msg = "Loading oat files multiple times with dlopen not supported on host.";
536 return false;
537 }
538 }
539
540 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
541 DCHECK(dlopen_handle_ != nullptr || !success);
542
543 return success;
544}
545
546bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
547 uint8_t* oat_file_begin,
548 std::string* error_msg) {
549#ifdef __APPLE__
550 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
551 // but let's fallback to the custom loading code for the time being.
552 UNUSED(elf_filename, oat_file_begin);
553 *error_msg = "Dlopen unsupported on Mac.";
554 return false;
555#else
556 {
557 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
558 if (absolute_path == nullptr) {
559 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
560 return false;
561 }
562#ifdef __ANDROID__
563 android_dlextinfo extinfo;
564 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
565 // (open oat files multiple
566 // times).
567 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
568 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800569 if (oat_file_begin != nullptr) { //
570 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
571 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
572 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800573 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
574#else
575 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
576 UNUSED(oat_file_begin);
577#endif
578 }
579 if (dlopen_handle_ == nullptr) {
580 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
581 return false;
582 }
583 return true;
584#endif
585}
586
587void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
588 struct dl_iterate_context {
589 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
590 auto* context = reinterpret_cast<dl_iterate_context*>(data);
591 // See whether this callback corresponds to the file which we have just loaded.
592 bool contains_begin = false;
593 for (int i = 0; i < info->dlpi_phnum; i++) {
594 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
595 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
596 info->dlpi_phdr[i].p_vaddr);
597 size_t memsz = info->dlpi_phdr[i].p_memsz;
598 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
599 contains_begin = true;
600 break;
601 }
602 }
603 }
604 // Add dummy mmaps for this file.
605 if (contains_begin) {
606 for (int i = 0; i < info->dlpi_phnum; i++) {
607 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
608 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
609 info->dlpi_phdr[i].p_vaddr);
610 size_t memsz = info->dlpi_phdr[i].p_memsz;
611 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
612 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
613 }
614 }
615 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
616 }
617 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
618 }
619 const uint8_t* const begin_;
620 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
621 } context = { Begin(), &dlopen_mmaps_ };
622
623 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
624 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
625 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but can not find its mmaps.";
626 }
627}
628
629////////////////////////////////////////////////
630// OatFile via our own ElfFile implementation //
631////////////////////////////////////////////////
632
633class ElfOatFile FINAL : public OatFileBase {
634 public:
635 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
636
637 static ElfOatFile* OpenElfFile(File* file,
638 const std::string& location,
639 uint8_t* requested_base,
640 uint8_t* oat_file_begin, // Override base if not null
641 bool writable,
642 bool executable,
643 const char* abs_dex_location,
644 std::string* error_msg);
645
646 bool InitializeFromElfFile(ElfFile* elf_file,
647 const char* abs_dex_location,
648 std::string* error_msg);
649
650 protected:
651 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
652 std::string* error_msg) const OVERRIDE {
653 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
654 if (ptr == nullptr) {
655 *error_msg = "(Internal implementation could not find symbol)";
656 }
657 return ptr;
658 }
659
660 bool Load(const std::string& elf_filename,
661 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
662 bool writable,
663 bool executable,
664 std::string* error_msg) OVERRIDE;
665
666 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
667 }
668
669 private:
670 bool ElfFileOpen(File* file,
671 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
672 bool writable,
673 bool executable,
674 std::string* error_msg);
675
676 private:
677 // Backing memory map for oat file during cross compilation.
678 std::unique_ptr<ElfFile> elf_file_;
679
680 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
681};
682
683ElfOatFile* ElfOatFile::OpenElfFile(File* file,
684 const std::string& location,
685 uint8_t* requested_base,
686 uint8_t* oat_file_begin, // Override base if not null
687 bool writable,
688 bool executable,
689 const char* abs_dex_location,
690 std::string* error_msg) {
691 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
692 bool success = oat_file->ElfFileOpen(file, oat_file_begin, writable, executable, error_msg);
693 if (!success) {
694 CHECK(!error_msg->empty());
695 return nullptr;
696 }
697
698 // Complete the setup.
699 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
700 return nullptr;
701 }
702
703 if (!oat_file->Setup(abs_dex_location, error_msg)) {
704 return nullptr;
705 }
706
707 return oat_file.release();
708}
709
710bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
711 const char* abs_dex_location,
712 std::string* error_msg) {
713 if (IsExecutable()) {
714 *error_msg = "Cannot initialize from elf file in executable mode.";
715 return false;
716 }
717 elf_file_.reset(elf_file);
718 uint64_t offset, size;
719 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
720 CHECK(has_section);
721 SetBegin(elf_file->Begin() + offset);
722 SetEnd(elf_file->Begin() + size + offset);
723 // Ignore the optional .bss section when opening non-executable.
724 return Setup(abs_dex_location, error_msg);
725}
726
727bool ElfOatFile::Load(const std::string& elf_filename,
728 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
729 bool writable,
730 bool executable,
731 std::string* error_msg) {
732 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
733 if (file == nullptr) {
734 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
735 return false;
736 }
737 return ElfOatFile::ElfFileOpen(file.get(),
738 oat_file_begin,
739 writable,
740 executable,
741 error_msg);
742}
743
744bool ElfOatFile::ElfFileOpen(File* file,
745 uint8_t* oat_file_begin,
746 bool writable,
747 bool executable,
748 std::string* error_msg) {
749 // TODO: rename requested_base to oat_data_begin
750 elf_file_.reset(ElfFile::Open(file,
751 writable,
752 /*program_header_only*/true,
753 error_msg,
754 oat_file_begin));
755 if (elf_file_ == nullptr) {
756 DCHECK(!error_msg->empty());
757 return false;
758 }
759 bool loaded = elf_file_->Load(executable, error_msg);
760 DCHECK(loaded || !error_msg->empty());
761 return loaded;
762}
763
764//////////////////////////
765// General OatFile code //
766//////////////////////////
767
768std::string OatFile::ResolveRelativeEncodedDexLocation(
769 const char* abs_dex_location, const std::string& rel_dex_location) {
770 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
771 // Strip :classes<N>.dex used for secondary multidex files.
772 std::string base = DexFile::GetBaseLocation(rel_dex_location);
773 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
774
775 // Check if the base is a suffix of the provided abs_dex_location.
776 std::string target_suffix = "/" + base;
777 std::string abs_location(abs_dex_location);
778 if (abs_location.size() > target_suffix.size()) {
779 size_t pos = abs_location.size() - target_suffix.size();
780 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
781 return abs_location + multidex_suffix;
782 }
783 }
784 }
785 return rel_dex_location;
786}
787
788static void CheckLocation(const std::string& location) {
789 CHECK(!location.empty());
790}
791
792OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
793 const std::string& location,
794 const char* abs_dex_location,
795 std::string* error_msg) {
796 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
797 return oat_file->InitializeFromElfFile(elf_file, abs_dex_location, error_msg)
798 ? oat_file.release()
799 : nullptr;
800}
801
802OatFile* OatFile::Open(const std::string& filename,
803 const std::string& location,
804 uint8_t* requested_base,
805 uint8_t* oat_file_begin,
806 bool executable,
807 const char* abs_dex_location,
808 std::string* error_msg) {
809 CHECK(!filename.empty()) << location;
810 CheckLocation(location);
811 std::unique_ptr<OatFile> ret;
812
813 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
814 // disabled.
815 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(filename,
816 location,
817 requested_base,
818 oat_file_begin,
819 false,
820 executable,
821 abs_dex_location,
822 error_msg);
823 if (with_dlopen != nullptr) {
824 return with_dlopen;
825 }
826 if (kPrintDlOpenErrorMessage) {
827 LOG(ERROR) << "Failed to dlopen: " << *error_msg;
828 }
829
830 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
831 //
832 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
833 //
834 // We use our own ELF loader for Quick to deal with legacy apps that
835 // open a generated dex file by name, remove the file, then open
836 // another generated dex file with the same name. http://b/10614658
837 //
838 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
839 //
840 //
841 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
842 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
843 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(filename,
844 location,
845 requested_base,
846 oat_file_begin,
847 false,
848 executable,
849 abs_dex_location,
850 error_msg);
851 return with_internal;
852}
853
854OatFile* OatFile::OpenWritable(File* file,
855 const std::string& location,
856 const char* abs_dex_location,
857 std::string* error_msg) {
858 CheckLocation(location);
859 return ElfOatFile::OpenElfFile(file,
860 location,
861 nullptr,
862 nullptr,
863 true,
864 false,
865 abs_dex_location,
866 error_msg);
867}
868
869OatFile* OatFile::OpenReadable(File* file,
870 const std::string& location,
871 const char* abs_dex_location,
872 std::string* error_msg) {
873 CheckLocation(location);
874 return ElfOatFile::OpenElfFile(file,
875 location,
876 nullptr,
877 nullptr,
878 false,
879 false,
880 abs_dex_location,
881 error_msg);
882}
883
884OatFile::OatFile(const std::string& location, bool is_executable)
885 : location_(location),
886 begin_(nullptr),
887 end_(nullptr),
888 bss_begin_(nullptr),
889 bss_end_(nullptr),
890 is_executable_(is_executable),
891 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
892 CHECK(!location_.empty());
893}
894
895OatFile::~OatFile() {
896 STLDeleteElements(&oat_dex_files_storage_);
897}
898
Brian Carlstrome24fa612011-09-29 00:53:55 -0700899const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800900 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700901}
902
Ian Rogers13735952014-10-08 12:43:28 -0700903const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700904 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800905 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700906}
907
Ian Rogers13735952014-10-08 12:43:28 -0700908const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700909 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800910 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700911}
912
Vladimir Marko5c42c292015-02-25 12:02:49 +0000913const uint8_t* OatFile::BssBegin() const {
914 return bss_begin_;
915}
916
917const uint8_t* OatFile::BssEnd() const {
918 return bss_end_;
919}
920
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700921const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
922 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800923 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100924 // NOTE: We assume here that the canonical location for a given dex_location never
925 // changes. If it does (i.e. some symlink used by the filename changes) we may return
926 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
927 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100928
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100929 // TODO: Additional analysis of usage patterns to see if this can be simplified
930 // without any performance loss, for example by not doing the first lock-free lookup.
931
932 const OatFile::OatDexFile* oat_dex_file = nullptr;
933 StringPiece key(dex_location);
934 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
935 // directly mentioned in the oat file and doesn't require locking.
936 auto primary_it = oat_dex_files_.find(key);
937 if (primary_it != oat_dex_files_.end()) {
938 oat_dex_file = primary_it->second;
939 DCHECK(oat_dex_file != nullptr);
940 } else {
941 // This dex_location is not one of the dex locations directly mentioned in the
942 // oat file. The correct lookup is via the canonical location but first see in
943 // the secondary_oat_dex_files_ whether we've looked up this location before.
944 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
945 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
946 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700947 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100948 } else {
949 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100950 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100951 if (dex_canonical_location != dex_location) {
952 StringPiece canonical_key(dex_canonical_location);
953 auto canonical_it = oat_dex_files_.find(canonical_key);
954 if (canonical_it != oat_dex_files_.end()) {
955 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700956 } // else keep null.
957 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100958
959 // Copy the key to the string_cache_ and store the result in secondary map.
960 string_cache_.emplace_back(key.data(), key.length());
961 StringPiece key_copy(string_cache_.back());
962 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700963 }
964 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100965 if (oat_dex_file != nullptr &&
966 (dex_location_checksum == nullptr ||
967 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
968 return oat_dex_file;
969 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700970
971 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100972 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800973 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700974 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800975 checksum = StringPrintf("0x%08x", *dex_location_checksum);
976 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700977 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100978 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800979 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700980 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100981 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700982 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100983 << " contains OatDexFile " << odf->GetDexFileLocation()
984 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
985 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700986 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800987 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700988 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100989
Andreas Gampefa8429b2015-04-07 18:34:42 -0700990 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700991}
992
Brian Carlstrome24fa612011-09-29 00:53:55 -0700993OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800994 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100995 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800996 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -0700997 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300998 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +0100999 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001000 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001001 : oat_file_(oat_file),
1002 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001003 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001004 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001005 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001006 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001007 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1008 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001009
1010OatFile::OatDexFile::~OatDexFile() {}
1011
Ian Rogers05f28c62012-10-23 18:12:13 -07001012size_t OatFile::OatDexFile::FileSize() const {
1013 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1014}
1015
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001016std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -07001017 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001018 dex_file_location_checksum_, this, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001019}
1020
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001021uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1022 return oat_class_offsets_pointer_[class_def_index];
1023}
1024
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001025OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001026 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001027
Ian Rogers13735952014-10-08 12:43:28 -07001028 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001029 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001030
Ian Rogers13735952014-10-08 12:43:28 -07001031 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001032 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1033 mirror::Class::Status status =
1034 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1035 CHECK_LT(status, mirror::Class::kStatusMax);
1036
Ian Rogers13735952014-10-08 12:43:28 -07001037 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001038 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1039 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1040 CHECK_LT(type, kOatClassMax);
1041
Ian Rogers13735952014-10-08 12:43:28 -07001042 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001043 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001044
Brian Carlstromcd937a92014-03-04 22:53:23 -08001045 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001046 const uint8_t* bitmap_pointer = nullptr;
1047 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001048 if (type != kOatClassNoneCompiled) {
1049 if (type == kOatClassSomeCompiled) {
1050 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1051 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1052 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1053 methods_pointer = bitmap_pointer + bitmap_size;
1054 } else {
1055 methods_pointer = after_type_pointer;
1056 }
1057 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001058 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001059
Richard Uhler07b3c232015-03-31 15:57:54 -07001060 return OatFile::OatClass(oat_file_,
1061 status,
1062 type,
1063 bitmap_size,
1064 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1065 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001066}
1067
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001068OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001069 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001070 OatClassType type,
1071 uint32_t bitmap_size,
1072 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001073 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001074 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001075 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001076 switch (type_) {
1077 case kOatClassAllCompiled: {
1078 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001079 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001080 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001081 break;
1082 }
1083 case kOatClassSomeCompiled: {
1084 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001085 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001086 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001087 break;
1088 }
1089 case kOatClassNoneCompiled: {
1090 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001091 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001092 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001093 break;
1094 }
1095 case kOatClassMax: {
1096 LOG(FATAL) << "Invalid OatClassType " << type_;
1097 break;
1098 }
1099 }
1100}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001101
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001102uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1103 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1104 if (oat_method_offsets == nullptr) {
1105 return 0u;
1106 }
1107 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1108}
1109
1110const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001111 // 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 -07001112 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001113 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001114 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001115 }
1116 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001117 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001118 CHECK_EQ(kOatClassAllCompiled, type_);
1119 methods_pointer_index = method_index;
1120 } else {
1121 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001122 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001123 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001124 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001125 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1126 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001127 }
1128 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001129 return &oat_method_offsets;
1130}
1131
1132const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1133 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1134 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001135 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001136 }
1137 if (oat_file_->IsExecutable() ||
1138 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001139 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001140 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001141 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001142 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1143 // version.
1144 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001145}
1146
Mathieu Chartiere401d142015-04-22 13:56:20 -07001147void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001148 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001149 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001150}
1151
Andreas Gampe7ba64962014-10-23 11:37:40 -07001152bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001153 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001154 // TODO: Check against oat_patches. b/18144996
1155}
1156
Sebastien Hertz0de11332015-05-13 12:14:05 +02001157bool OatFile::IsDebuggable() const {
1158 return GetOatHeader().IsDebuggable();
1159}
1160
Andreas Gampe7848da42015-04-09 11:15:04 -07001161static constexpr char kDexClassPathEncodingSeparator = '*';
1162
1163std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1164 std::ostringstream out;
1165
1166 for (const DexFile* dex_file : dex_files) {
1167 out << dex_file->GetLocation().c_str();
1168 out << kDexClassPathEncodingSeparator;
1169 out << dex_file->GetLocationChecksum();
1170 out << kDexClassPathEncodingSeparator;
1171 }
1172
1173 return out.str();
1174}
1175
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001176bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001177 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1178 // No dependencies.
1179 return true;
1180 }
1181
1182 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1183 // Split() instead of manual parsing of the combined char*.
1184 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001185 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001186 if (split.size() % 2 != 0) {
1187 // Expected pairs of location and checksum.
1188 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1189 return false;
1190 }
1191
1192 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1193 std::string& location = *it;
1194 std::string& checksum = *(it + 1);
1195 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1196 if (converted == 0) {
1197 // Conversion error.
1198 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1199 return false;
1200 }
1201
1202 uint32_t dex_checksum;
1203 std::string error_msg;
1204 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001205 &dex_checksum,
1206 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001207 if (converted != dex_checksum) {
1208 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1209 location.c_str(), converted, dex_checksum);
1210 return false;
1211 }
1212 } else {
1213 // Problem retrieving checksum.
1214 // TODO: odex files?
1215 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1216 error_msg.c_str());
1217 return false;
1218 }
1219 }
1220
1221 return true;
1222}
1223
1224bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001225 std::vector<std::string>* locations) {
1226 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001227 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1228 return true;
1229 }
1230
1231 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1232 // Split() instead of manual parsing of the combined char*.
1233 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001234 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001235 if (split.size() % 2 != 0) {
1236 // Expected pairs of location and checksum.
1237 return false;
1238 }
1239
1240 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1241 locations->push_back(*it);
1242 }
1243
1244 return true;
1245}
1246
Brian Carlstrome24fa612011-09-29 00:53:55 -07001247} // namespace art