blob: 83e594b1695b07656262500399eb8c7e14ea96a5 [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) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800588#ifdef __APPLE__
589 UNUSED(elf_filename);
590 LOG(FATAL) << "Should not reach here.";
591 UNREACHABLE();
592#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800593 struct dl_iterate_context {
594 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
595 auto* context = reinterpret_cast<dl_iterate_context*>(data);
596 // See whether this callback corresponds to the file which we have just loaded.
597 bool contains_begin = false;
598 for (int i = 0; i < info->dlpi_phnum; i++) {
599 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
600 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
601 info->dlpi_phdr[i].p_vaddr);
602 size_t memsz = info->dlpi_phdr[i].p_memsz;
603 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
604 contains_begin = true;
605 break;
606 }
607 }
608 }
609 // Add dummy mmaps for this file.
610 if (contains_begin) {
611 for (int i = 0; i < info->dlpi_phnum; i++) {
612 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
613 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
614 info->dlpi_phdr[i].p_vaddr);
615 size_t memsz = info->dlpi_phdr[i].p_memsz;
616 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
617 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
618 }
619 }
620 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
621 }
622 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
623 }
624 const uint8_t* const begin_;
625 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
626 } context = { Begin(), &dlopen_mmaps_ };
627
628 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
629 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
630 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but can not find its mmaps.";
631 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800632#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800633}
634
635////////////////////////////////////////////////
636// OatFile via our own ElfFile implementation //
637////////////////////////////////////////////////
638
639class ElfOatFile FINAL : public OatFileBase {
640 public:
641 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
642
643 static ElfOatFile* OpenElfFile(File* file,
644 const std::string& location,
645 uint8_t* requested_base,
646 uint8_t* oat_file_begin, // Override base if not null
647 bool writable,
648 bool executable,
649 const char* abs_dex_location,
650 std::string* error_msg);
651
652 bool InitializeFromElfFile(ElfFile* elf_file,
653 const char* abs_dex_location,
654 std::string* error_msg);
655
656 protected:
657 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
658 std::string* error_msg) const OVERRIDE {
659 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
660 if (ptr == nullptr) {
661 *error_msg = "(Internal implementation could not find symbol)";
662 }
663 return ptr;
664 }
665
666 bool Load(const std::string& elf_filename,
667 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
668 bool writable,
669 bool executable,
670 std::string* error_msg) OVERRIDE;
671
672 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
673 }
674
675 private:
676 bool ElfFileOpen(File* file,
677 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
678 bool writable,
679 bool executable,
680 std::string* error_msg);
681
682 private:
683 // Backing memory map for oat file during cross compilation.
684 std::unique_ptr<ElfFile> elf_file_;
685
686 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
687};
688
689ElfOatFile* ElfOatFile::OpenElfFile(File* file,
690 const std::string& location,
691 uint8_t* requested_base,
692 uint8_t* oat_file_begin, // Override base if not null
693 bool writable,
694 bool executable,
695 const char* abs_dex_location,
696 std::string* error_msg) {
697 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
698 bool success = oat_file->ElfFileOpen(file, oat_file_begin, writable, executable, error_msg);
699 if (!success) {
700 CHECK(!error_msg->empty());
701 return nullptr;
702 }
703
704 // Complete the setup.
705 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
706 return nullptr;
707 }
708
709 if (!oat_file->Setup(abs_dex_location, error_msg)) {
710 return nullptr;
711 }
712
713 return oat_file.release();
714}
715
716bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
717 const char* abs_dex_location,
718 std::string* error_msg) {
719 if (IsExecutable()) {
720 *error_msg = "Cannot initialize from elf file in executable mode.";
721 return false;
722 }
723 elf_file_.reset(elf_file);
724 uint64_t offset, size;
725 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
726 CHECK(has_section);
727 SetBegin(elf_file->Begin() + offset);
728 SetEnd(elf_file->Begin() + size + offset);
729 // Ignore the optional .bss section when opening non-executable.
730 return Setup(abs_dex_location, error_msg);
731}
732
733bool ElfOatFile::Load(const std::string& elf_filename,
734 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
735 bool writable,
736 bool executable,
737 std::string* error_msg) {
738 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
739 if (file == nullptr) {
740 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
741 return false;
742 }
743 return ElfOatFile::ElfFileOpen(file.get(),
744 oat_file_begin,
745 writable,
746 executable,
747 error_msg);
748}
749
750bool ElfOatFile::ElfFileOpen(File* file,
751 uint8_t* oat_file_begin,
752 bool writable,
753 bool executable,
754 std::string* error_msg) {
755 // TODO: rename requested_base to oat_data_begin
756 elf_file_.reset(ElfFile::Open(file,
757 writable,
758 /*program_header_only*/true,
759 error_msg,
760 oat_file_begin));
761 if (elf_file_ == nullptr) {
762 DCHECK(!error_msg->empty());
763 return false;
764 }
765 bool loaded = elf_file_->Load(executable, error_msg);
766 DCHECK(loaded || !error_msg->empty());
767 return loaded;
768}
769
770//////////////////////////
771// General OatFile code //
772//////////////////////////
773
774std::string OatFile::ResolveRelativeEncodedDexLocation(
775 const char* abs_dex_location, const std::string& rel_dex_location) {
776 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
777 // Strip :classes<N>.dex used for secondary multidex files.
778 std::string base = DexFile::GetBaseLocation(rel_dex_location);
779 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
780
781 // Check if the base is a suffix of the provided abs_dex_location.
782 std::string target_suffix = "/" + base;
783 std::string abs_location(abs_dex_location);
784 if (abs_location.size() > target_suffix.size()) {
785 size_t pos = abs_location.size() - target_suffix.size();
786 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
787 return abs_location + multidex_suffix;
788 }
789 }
790 }
791 return rel_dex_location;
792}
793
794static void CheckLocation(const std::string& location) {
795 CHECK(!location.empty());
796}
797
798OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
799 const std::string& location,
800 const char* abs_dex_location,
801 std::string* error_msg) {
802 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
803 return oat_file->InitializeFromElfFile(elf_file, abs_dex_location, error_msg)
804 ? oat_file.release()
805 : nullptr;
806}
807
808OatFile* OatFile::Open(const std::string& filename,
809 const std::string& location,
810 uint8_t* requested_base,
811 uint8_t* oat_file_begin,
812 bool executable,
813 const char* abs_dex_location,
814 std::string* error_msg) {
815 CHECK(!filename.empty()) << location;
816 CheckLocation(location);
817 std::unique_ptr<OatFile> ret;
818
819 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
820 // disabled.
821 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(filename,
822 location,
823 requested_base,
824 oat_file_begin,
825 false,
826 executable,
827 abs_dex_location,
828 error_msg);
829 if (with_dlopen != nullptr) {
830 return with_dlopen;
831 }
832 if (kPrintDlOpenErrorMessage) {
833 LOG(ERROR) << "Failed to dlopen: " << *error_msg;
834 }
835
836 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
837 //
838 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
839 //
840 // We use our own ELF loader for Quick to deal with legacy apps that
841 // open a generated dex file by name, remove the file, then open
842 // another generated dex file with the same name. http://b/10614658
843 //
844 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
845 //
846 //
847 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
848 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
849 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(filename,
850 location,
851 requested_base,
852 oat_file_begin,
853 false,
854 executable,
855 abs_dex_location,
856 error_msg);
857 return with_internal;
858}
859
860OatFile* OatFile::OpenWritable(File* file,
861 const std::string& location,
862 const char* abs_dex_location,
863 std::string* error_msg) {
864 CheckLocation(location);
865 return ElfOatFile::OpenElfFile(file,
866 location,
867 nullptr,
868 nullptr,
869 true,
870 false,
871 abs_dex_location,
872 error_msg);
873}
874
875OatFile* OatFile::OpenReadable(File* file,
876 const std::string& location,
877 const char* abs_dex_location,
878 std::string* error_msg) {
879 CheckLocation(location);
880 return ElfOatFile::OpenElfFile(file,
881 location,
882 nullptr,
883 nullptr,
884 false,
885 false,
886 abs_dex_location,
887 error_msg);
888}
889
890OatFile::OatFile(const std::string& location, bool is_executable)
891 : location_(location),
892 begin_(nullptr),
893 end_(nullptr),
894 bss_begin_(nullptr),
895 bss_end_(nullptr),
896 is_executable_(is_executable),
897 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
898 CHECK(!location_.empty());
899}
900
901OatFile::~OatFile() {
902 STLDeleteElements(&oat_dex_files_storage_);
903}
904
Brian Carlstrome24fa612011-09-29 00:53:55 -0700905const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800906 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700907}
908
Ian Rogers13735952014-10-08 12:43:28 -0700909const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700910 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800911 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700912}
913
Ian Rogers13735952014-10-08 12:43:28 -0700914const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700915 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800916 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700917}
918
Vladimir Marko5c42c292015-02-25 12:02:49 +0000919const uint8_t* OatFile::BssBegin() const {
920 return bss_begin_;
921}
922
923const uint8_t* OatFile::BssEnd() const {
924 return bss_end_;
925}
926
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700927const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
928 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800929 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100930 // NOTE: We assume here that the canonical location for a given dex_location never
931 // changes. If it does (i.e. some symlink used by the filename changes) we may return
932 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
933 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100934
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100935 // TODO: Additional analysis of usage patterns to see if this can be simplified
936 // without any performance loss, for example by not doing the first lock-free lookup.
937
938 const OatFile::OatDexFile* oat_dex_file = nullptr;
939 StringPiece key(dex_location);
940 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
941 // directly mentioned in the oat file and doesn't require locking.
942 auto primary_it = oat_dex_files_.find(key);
943 if (primary_it != oat_dex_files_.end()) {
944 oat_dex_file = primary_it->second;
945 DCHECK(oat_dex_file != nullptr);
946 } else {
947 // This dex_location is not one of the dex locations directly mentioned in the
948 // oat file. The correct lookup is via the canonical location but first see in
949 // the secondary_oat_dex_files_ whether we've looked up this location before.
950 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
951 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
952 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700953 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100954 } else {
955 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100956 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100957 if (dex_canonical_location != dex_location) {
958 StringPiece canonical_key(dex_canonical_location);
959 auto canonical_it = oat_dex_files_.find(canonical_key);
960 if (canonical_it != oat_dex_files_.end()) {
961 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700962 } // else keep null.
963 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100964
965 // Copy the key to the string_cache_ and store the result in secondary map.
966 string_cache_.emplace_back(key.data(), key.length());
967 StringPiece key_copy(string_cache_.back());
968 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700969 }
970 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100971 if (oat_dex_file != nullptr &&
972 (dex_location_checksum == nullptr ||
973 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
974 return oat_dex_file;
975 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700976
977 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100978 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800979 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700980 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800981 checksum = StringPrintf("0x%08x", *dex_location_checksum);
982 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700983 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100984 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800985 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700986 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100987 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700988 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100989 << " contains OatDexFile " << odf->GetDexFileLocation()
990 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
991 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700992 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800993 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700994 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100995
Andreas Gampefa8429b2015-04-07 18:34:42 -0700996 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700997}
998
Brian Carlstrome24fa612011-09-29 00:53:55 -0700999OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001000 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001001 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001002 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001003 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001004 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001005 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001006 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001007 : oat_file_(oat_file),
1008 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001009 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001010 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001011 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001012 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001013 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1014 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001015
1016OatFile::OatDexFile::~OatDexFile() {}
1017
Ian Rogers05f28c62012-10-23 18:12:13 -07001018size_t OatFile::OatDexFile::FileSize() const {
1019 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1020}
1021
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001022std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -07001023 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001024 dex_file_location_checksum_, this, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001025}
1026
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001027uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1028 return oat_class_offsets_pointer_[class_def_index];
1029}
1030
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001031OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001032 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001033
Ian Rogers13735952014-10-08 12:43:28 -07001034 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001035 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001036
Ian Rogers13735952014-10-08 12:43:28 -07001037 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001038 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1039 mirror::Class::Status status =
1040 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1041 CHECK_LT(status, mirror::Class::kStatusMax);
1042
Ian Rogers13735952014-10-08 12:43:28 -07001043 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001044 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1045 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1046 CHECK_LT(type, kOatClassMax);
1047
Ian Rogers13735952014-10-08 12:43:28 -07001048 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001049 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001050
Brian Carlstromcd937a92014-03-04 22:53:23 -08001051 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001052 const uint8_t* bitmap_pointer = nullptr;
1053 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001054 if (type != kOatClassNoneCompiled) {
1055 if (type == kOatClassSomeCompiled) {
1056 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1057 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1058 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1059 methods_pointer = bitmap_pointer + bitmap_size;
1060 } else {
1061 methods_pointer = after_type_pointer;
1062 }
1063 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001064 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001065
Richard Uhler07b3c232015-03-31 15:57:54 -07001066 return OatFile::OatClass(oat_file_,
1067 status,
1068 type,
1069 bitmap_size,
1070 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1071 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001072}
1073
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001074OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001075 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001076 OatClassType type,
1077 uint32_t bitmap_size,
1078 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001079 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001080 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001081 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001082 switch (type_) {
1083 case kOatClassAllCompiled: {
1084 CHECK_EQ(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 kOatClassSomeCompiled: {
1090 CHECK_NE(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 kOatClassNoneCompiled: {
1096 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001097 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001098 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001099 break;
1100 }
1101 case kOatClassMax: {
1102 LOG(FATAL) << "Invalid OatClassType " << type_;
1103 break;
1104 }
1105 }
1106}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001107
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001108uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1109 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1110 if (oat_method_offsets == nullptr) {
1111 return 0u;
1112 }
1113 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1114}
1115
1116const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001117 // 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 -07001118 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001119 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001120 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001121 }
1122 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001123 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001124 CHECK_EQ(kOatClassAllCompiled, type_);
1125 methods_pointer_index = method_index;
1126 } else {
1127 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001128 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001129 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001130 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001131 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1132 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001133 }
1134 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001135 return &oat_method_offsets;
1136}
1137
1138const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1139 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1140 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001141 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001142 }
1143 if (oat_file_->IsExecutable() ||
1144 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001145 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001146 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001147 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001148 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1149 // version.
1150 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001151}
1152
Mathieu Chartiere401d142015-04-22 13:56:20 -07001153void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001154 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001155 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001156}
1157
Andreas Gampe7ba64962014-10-23 11:37:40 -07001158bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001159 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001160 // TODO: Check against oat_patches. b/18144996
1161}
1162
Sebastien Hertz0de11332015-05-13 12:14:05 +02001163bool OatFile::IsDebuggable() const {
1164 return GetOatHeader().IsDebuggable();
1165}
1166
Andreas Gampe7848da42015-04-09 11:15:04 -07001167static constexpr char kDexClassPathEncodingSeparator = '*';
1168
1169std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1170 std::ostringstream out;
1171
1172 for (const DexFile* dex_file : dex_files) {
1173 out << dex_file->GetLocation().c_str();
1174 out << kDexClassPathEncodingSeparator;
1175 out << dex_file->GetLocationChecksum();
1176 out << kDexClassPathEncodingSeparator;
1177 }
1178
1179 return out.str();
1180}
1181
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001182bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001183 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1184 // No dependencies.
1185 return true;
1186 }
1187
1188 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1189 // Split() instead of manual parsing of the combined char*.
1190 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001191 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001192 if (split.size() % 2 != 0) {
1193 // Expected pairs of location and checksum.
1194 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1195 return false;
1196 }
1197
1198 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1199 std::string& location = *it;
1200 std::string& checksum = *(it + 1);
1201 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1202 if (converted == 0) {
1203 // Conversion error.
1204 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1205 return false;
1206 }
1207
1208 uint32_t dex_checksum;
1209 std::string error_msg;
1210 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001211 &dex_checksum,
1212 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001213 if (converted != dex_checksum) {
1214 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1215 location.c_str(), converted, dex_checksum);
1216 return false;
1217 }
1218 } else {
1219 // Problem retrieving checksum.
1220 // TODO: odex files?
1221 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1222 error_msg.c_str());
1223 return false;
1224 }
1225 }
1226
1227 return true;
1228}
1229
1230bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001231 std::vector<std::string>* locations) {
1232 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001233 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1234 return true;
1235 }
1236
1237 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1238 // Split() instead of manual parsing of the combined char*.
1239 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001240 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001241 if (split.size() % 2 != 0) {
1242 // Expected pairs of location and checksum.
1243 return false;
1244 }
1245
1246 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1247 locations->push_back(*it);
1248 }
1249
1250 return true;
1251}
1252
Brian Carlstrome24fa612011-09-29 00:53:55 -07001253} // namespace art