blob: c33a9cd6d1393e1d708178f24da54353a5de7321 [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.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010031#ifdef ART_TARGET_ANDROID
Andreas Gampefa8429b2015-04-07 18:34:42 -070032#include "android/dlext.h"
33#endif
34
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "art_method-inl.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070036#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080037#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080038#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080039#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070041#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000043#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070045#include "mirror/object-inl.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010046#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070047#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070048#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070049#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000050#include "type_lookup_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "utils.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010052#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070053
54namespace art {
55
Andreas Gampe049cff02015-12-01 23:27:12 -080056// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000057static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070058
Andreas Gampe049cff02015-12-01 23:27:12 -080059// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070060// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
61// multiple times). However, if/when we switch the above, we likely want to switch this, too,
62// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000063static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070064
65// For debugging, Open will print DlOpen error message if set to true.
66static constexpr bool kPrintDlOpenErrorMessage = false;
67
Andreas Gampe049cff02015-12-01 23:27:12 -080068// Note for OatFileBase and descendents:
69//
70// These are used in OatFile::Open to try all our loaders.
71//
72// The process is simple:
73//
74// 1) Allocate an instance through the standard constructor (location, executable)
75// 2) Load() to try to open the file.
76// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
77// 4) PreSetup() for any steps that should be done before the final setup.
78// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070079
Andreas Gampe049cff02015-12-01 23:27:12 -080080class OatFileBase : public OatFile {
81 public:
82 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070083
Andreas Gampe049cff02015-12-01 23:27:12 -080084 template <typename kOatFileBaseSubType>
85 static OatFileBase* OpenOatFile(const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -070086 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -080087 uint8_t* requested_base,
88 uint8_t* oat_file_begin,
89 bool writable,
90 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080091 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070092 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -080093 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080094
Andreas Gampe049cff02015-12-01 23:27:12 -080095 protected:
96 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -070097
Andreas Gampe049cff02015-12-01 23:27:12 -080098 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
99 std::string* error_msg) const = 0;
100
Andreas Gampe4075f832016-05-18 13:09:54 -0700101 virtual void PreLoad() = 0;
102
Andreas Gampe049cff02015-12-01 23:27:12 -0800103 virtual bool Load(const std::string& elf_filename,
104 uint8_t* oat_file_begin,
105 bool writable,
106 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800107 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800108 std::string* error_msg) = 0;
109
110 bool ComputeFields(uint8_t* requested_base,
111 const std::string& file_path,
112 std::string* error_msg);
113
114 virtual void PreSetup(const std::string& elf_filename) = 0;
115
116 bool Setup(const char* abs_dex_location, std::string* error_msg);
117
118 // Setters exposed for ElfOatFile.
119
120 void SetBegin(const uint8_t* begin) {
121 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700122 }
123
Andreas Gampe049cff02015-12-01 23:27:12 -0800124 void SetEnd(const uint8_t* end) {
125 end_ = end;
126 }
127
128 private:
129 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
130};
131
132template <typename kOatFileBaseSubType>
133OatFileBase* OatFileBase::OpenOatFile(const std::string& elf_filename,
134 const std::string& location,
135 uint8_t* requested_base,
136 uint8_t* oat_file_begin,
137 bool writable,
138 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800139 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800140 const char* abs_dex_location,
141 std::string* error_msg) {
142 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700143
144 ret->PreLoad();
145
Andreas Gampe049cff02015-12-01 23:27:12 -0800146 if (!ret->Load(elf_filename,
147 oat_file_begin,
148 writable,
149 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800150 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800151 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800152 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800153 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800154
Andreas Gampe049cff02015-12-01 23:27:12 -0800155 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
156 return nullptr;
157 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700158
Andreas Gampe049cff02015-12-01 23:27:12 -0800159 ret->PreSetup(elf_filename);
160
161 if (!ret->Setup(abs_dex_location, error_msg)) {
162 return nullptr;
163 }
164
Dave Allison69dfe512014-07-11 17:11:58 +0000165 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800166}
167
Andreas Gampe049cff02015-12-01 23:27:12 -0800168bool OatFileBase::ComputeFields(uint8_t* requested_base,
169 const std::string& file_path,
170 std::string* error_msg) {
171 std::string symbol_error_msg;
172 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700173 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800174 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
175 file_path.c_str(),
176 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700177 return false;
178 }
179 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000180 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700181 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000182 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
183 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700184 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800185 "oatdata=%p != expected=%p. See process maps in the log.",
186 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700187 return false;
188 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800189 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700190 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800191 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
192 file_path.c_str(),
193 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700194 return false;
195 }
196 // Readjust to be non-inclusive upper bound.
197 end_ += sizeof(uint32_t);
198
Andreas Gampe049cff02015-12-01 23:27:12 -0800199 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700200 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800201 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700202 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700203 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800204 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700205 if (bss_end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800206 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700207 return false;
208 }
209 // Readjust to be non-inclusive upper bound.
210 bss_end_ += sizeof(uint32_t);
211 }
212
Andreas Gampe049cff02015-12-01 23:27:12 -0800213 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800214}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800215
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100216// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
217// position by the number of bytes read, i.e. sizeof(T).
218// Return true on success, false if the read would go beyond the end of the OatFile.
219template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100220inline static bool ReadOatDexFileData(const OatFile& oat_file,
221 /*inout*/const uint8_t** oat,
222 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100223 DCHECK(oat != nullptr);
224 DCHECK(value != nullptr);
225 DCHECK_LE(*oat, oat_file.End());
226 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
227 return false;
228 }
229 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
230 typedef __attribute__((__aligned__(1))) T unaligned_type;
231 *value = *reinterpret_cast<const unaligned_type*>(*oat);
232 *oat += sizeof(T);
233 return true;
234}
235
Andreas Gampe049cff02015-12-01 23:27:12 -0800236bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700237 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800238 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100239 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
240 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800241 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700242 return false;
243 }
Ian Rogers13735952014-10-08 12:43:28 -0700244 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700246 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700247 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700248 return false;
249 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700250
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700251 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700252 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700253 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100254 "%p + %zu + %u <= %p",
255 GetLocation().c_str(),
256 Begin(),
257 sizeof(OatHeader),
258 GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700259 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700260 return false;
261 }
262
Vladimir Marko09d09432015-09-08 13:47:48 +0100263 size_t pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100264 uint8_t* dex_cache_arrays = bss_begin_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100265 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
266 oat_dex_files_storage_.reserve(dex_file_count);
267 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100268 uint32_t dex_file_location_size;
269 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
270 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
271 "location size",
272 GetLocation().c_str(),
273 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700274 return false;
275 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100276 if (UNLIKELY(dex_file_location_size == 0U)) {
277 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
278 GetLocation().c_str(),
279 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700280 return false;
281 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000282 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100283 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
284 "location",
285 GetLocation().c_str(),
286 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700287 return false;
288 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000289 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
290 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700291
Richard Uhlere5fed032015-03-18 08:21:11 -0700292 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
293 abs_dex_location,
294 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700295
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100296 uint32_t dex_file_checksum;
297 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
298 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
299 "dex file checksum",
300 GetLocation().c_str(),
301 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700302 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700303 return false;
304 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700305
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100306 uint32_t dex_file_offset;
307 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
308 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
309 "after dex file offsets",
310 GetLocation().c_str(),
311 i,
312 dex_file_location.c_str());
313 return false;
314 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700315 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100316 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with zero dex "
317 "file offset",
318 GetLocation().c_str(),
319 i,
320 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700321 return false;
322 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700323 if (UNLIKELY(dex_file_offset > Size())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100324 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
325 "offset %u > %zu",
326 GetLocation().c_str(),
327 i,
328 dex_file_location.c_str(),
329 dex_file_offset,
330 Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700331 return false;
332 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000333 if (UNLIKELY(Size() - dex_file_offset < sizeof(DexFile::Header))) {
334 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
335 "offset %u of %zu but the size of dex file header is %zu",
336 GetLocation().c_str(),
337 i,
338 dex_file_location.c_str(),
339 dex_file_offset,
340 Size(),
341 sizeof(DexFile::Header));
342 return false;
343 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800344
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800345 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700346 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100347 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
348 "dex file magic '%s'",
349 GetLocation().c_str(),
350 i,
351 dex_file_location.c_str(),
352 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700353 return false;
354 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700355 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100356 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
357 "dex file version '%s'",
358 GetLocation().c_str(),
359 i,
360 dex_file_location.c_str(),
361 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700362 return false;
363 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800364 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000365 if (Size() - dex_file_offset < header->file_size_) {
366 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
367 "offset %u and size %u truncated at %zu",
368 GetLocation().c_str(),
369 i,
370 dex_file_location.c_str(),
371 dex_file_offset,
372 header->file_size_,
373 Size());
374 return false;
375 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300376
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000377 uint32_t class_offsets_offset;
378 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
379 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
380 "after class offsets offset",
381 GetLocation().c_str(),
382 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300383 dex_file_location.c_str());
384 return false;
385 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000386 if (UNLIKELY(class_offsets_offset > Size()) ||
387 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
388 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
389 "class offsets, offset %u of %zu, class defs %u",
390 GetLocation().c_str(),
391 i,
392 dex_file_location.c_str(),
393 class_offsets_offset,
394 Size(),
395 header->class_defs_size_);
396 return false;
397 }
398 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
399 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
400 "class offsets, offset %u",
401 GetLocation().c_str(),
402 i,
403 dex_file_location.c_str(),
404 class_offsets_offset);
405 return false;
406 }
407 const uint32_t* class_offsets_pointer =
408 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
409
410 uint32_t lookup_table_offset;
411 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
412 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
413 "after lookup table offset",
414 GetLocation().c_str(),
415 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300416 dex_file_location.c_str());
417 return false;
418 }
419 const uint8_t* lookup_table_data = lookup_table_offset != 0u
420 ? Begin() + lookup_table_offset
421 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000422 if (lookup_table_offset != 0u &&
423 (UNLIKELY(lookup_table_offset > Size()) ||
424 UNLIKELY(Size() - lookup_table_offset <
425 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100426 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000427 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100428 GetLocation().c_str(),
429 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000430 dex_file_location.c_str(),
431 lookup_table_offset,
432 Size(),
433 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700434 return false;
435 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700436
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100437 uint8_t* current_dex_cache_arrays = nullptr;
Vladimir Marko09d09432015-09-08 13:47:48 +0100438 if (dex_cache_arrays != nullptr) {
439 DexCacheArraysLayout layout(pointer_size, *header);
440 if (layout.Size() != 0u) {
441 if (static_cast<size_t>(bss_end_ - dex_cache_arrays) < layout.Size()) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100442 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with "
443 "truncated dex cache arrays, %zu < %zu.",
444 GetLocation().c_str(),
445 i,
446 dex_file_location.c_str(),
447 static_cast<size_t>(bss_end_ - dex_cache_arrays),
448 layout.Size());
Vladimir Marko09d09432015-09-08 13:47:48 +0100449 return false;
450 }
451 current_dex_cache_arrays = dex_cache_arrays;
452 dex_cache_arrays += layout.Size();
453 }
454 }
455
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100456 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
457
458 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100459 OatDexFile* oat_dex_file = new OatDexFile(this,
460 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100461 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100462 dex_file_checksum,
463 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300464 lookup_table_data,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000465 class_offsets_pointer,
Vladimir Marko09d09432015-09-08 13:47:48 +0100466 current_dex_cache_arrays);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100467 oat_dex_files_storage_.push_back(oat_dex_file);
468
469 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100470 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100471 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100472 if (canonical_location != dex_file_location) {
473 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
474 oat_dex_files_.Put(canonical_key, oat_dex_file);
475 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700476 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100477
478 if (dex_cache_arrays != bss_end_) {
479 // We expect the bss section to be either empty (dex_cache_arrays and bss_end_
480 // both null) or contain just the dex cache arrays and nothing else.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100481 *error_msg = StringPrintf("In oat file '%s' found unexpected bss size bigger by %zu bytes.",
Vladimir Marko09d09432015-09-08 13:47:48 +0100482 GetLocation().c_str(),
483 static_cast<size_t>(bss_end_ - dex_cache_arrays));
484 return false;
485 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700486 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700487}
488
Andreas Gampe049cff02015-12-01 23:27:12 -0800489////////////////////////
490// OatFile via dlopen //
491////////////////////////
492
493static bool RegisterOatFileLocation(const std::string& location) {
494 if (!kIsTargetBuild) {
495 Runtime* const runtime = Runtime::Current();
496 if (runtime != nullptr && !runtime->IsAotCompiler()) {
497 return runtime->GetOatFileManager().RegisterOatFileLocation(location);
498 }
499 return false;
500 }
501 return true;
502}
503
504static void UnregisterOatFileLocation(const std::string& location) {
505 if (!kIsTargetBuild) {
506 Runtime* const runtime = Runtime::Current();
507 if (runtime != nullptr && !runtime->IsAotCompiler()) {
508 runtime->GetOatFileManager().UnRegisterOatFileLocation(location);
509 }
510 }
511}
512
513class DlOpenOatFile FINAL : public OatFileBase {
514 public:
515 DlOpenOatFile(const std::string& filename, bool executable)
516 : OatFileBase(filename, executable),
517 dlopen_handle_(nullptr),
Andreas Gampe4075f832016-05-18 13:09:54 -0700518 shared_objects_before_(0),
Andreas Gampe049cff02015-12-01 23:27:12 -0800519 first_oat_(RegisterOatFileLocation(filename)) {
520 }
521
522 ~DlOpenOatFile() {
523 if (dlopen_handle_ != nullptr) {
524 dlclose(dlopen_handle_);
525 }
526 UnregisterOatFileLocation(GetLocation());
527 }
528
529 protected:
530 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
531 std::string* error_msg) const OVERRIDE {
532 const uint8_t* ptr =
533 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
534 if (ptr == nullptr) {
535 *error_msg = dlerror();
536 }
537 return ptr;
538 }
539
Andreas Gampe4075f832016-05-18 13:09:54 -0700540 void PreLoad() OVERRIDE;
541
Andreas Gampe049cff02015-12-01 23:27:12 -0800542 bool Load(const std::string& elf_filename,
543 uint8_t* oat_file_begin,
544 bool writable,
545 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800546 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800547 std::string* error_msg) OVERRIDE;
548
549 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
550 void PreSetup(const std::string& elf_filename) OVERRIDE;
551
552 private:
553 bool Dlopen(const std::string& elf_filename,
554 uint8_t* oat_file_begin,
555 std::string* error_msg);
556
557 // dlopen handle during runtime.
558 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
559
560 // Dummy memory map objects corresponding to the regions mapped by dlopen.
561 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
562
Andreas Gampe4075f832016-05-18 13:09:54 -0700563 // The number of shared objects the linker told us about before loading. Used to
564 // (optimistically) optimize the PreSetup stage (see comment there).
565 size_t shared_objects_before_;
566
Andreas Gampe049cff02015-12-01 23:27:12 -0800567 // Track the registration status (= was this the first oat file) for the location.
568 const bool first_oat_;
569
570 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
571};
572
Andreas Gampe4075f832016-05-18 13:09:54 -0700573void DlOpenOatFile::PreLoad() {
574#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700575 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700576 LOG(FATAL) << "Should not reach here.";
577 UNREACHABLE();
578#else
579 // Count the entries in dl_iterate_phdr we get at this point in time.
580 struct dl_iterate_context {
581 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
582 size_t size ATTRIBUTE_UNUSED,
583 void *data) {
584 reinterpret_cast<dl_iterate_context*>(data)->count++;
585 return 0; // Continue iteration.
586 }
587 size_t count = 0;
588 } context;
589
590 dl_iterate_phdr(dl_iterate_context::callback, &context);
591 shared_objects_before_ = context.count;
592#endif
593}
594
Andreas Gampe049cff02015-12-01 23:27:12 -0800595bool DlOpenOatFile::Load(const std::string& elf_filename,
596 uint8_t* oat_file_begin,
597 bool writable,
598 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800599 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800600 std::string* error_msg) {
601 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
602 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
603 // !executable is a sign that we may want to patch), which may not be allowed for
604 // various reasons.
605 if (!kUseDlopen) {
606 *error_msg = "DlOpen is disabled.";
607 return false;
608 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800609 if (low_4gb) {
610 *error_msg = "DlOpen does not support low 4gb loading.";
611 return false;
612 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800613 if (writable) {
614 *error_msg = "DlOpen does not support writable loading.";
615 return false;
616 }
617 if (!executable) {
618 *error_msg = "DlOpen does not support non-executable loading.";
619 return false;
620 }
621
622 // dlopen always returns the same library if it is already opened on the host. For this reason
623 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
624 // the same library loaded multiple times at different addresses is required for class unloading
625 // and for having dex caches arrays in the .bss section.
626 if (!kIsTargetBuild) {
627 if (!kUseDlopenOnHost) {
628 *error_msg = "DlOpen disabled for host.";
629 return false;
630 }
631 // For RAII, tracking multiple loads is done in the constructor and destructor. The result is
632 // stored in the first_oat_ flag.
633 if (!first_oat_) {
634 *error_msg = "Loading oat files multiple times with dlopen not supported on host.";
635 return false;
636 }
637 }
638
639 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
640 DCHECK(dlopen_handle_ != nullptr || !success);
641
642 return success;
643}
644
645bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
646 uint8_t* oat_file_begin,
647 std::string* error_msg) {
648#ifdef __APPLE__
649 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
650 // but let's fallback to the custom loading code for the time being.
651 UNUSED(elf_filename, oat_file_begin);
652 *error_msg = "Dlopen unsupported on Mac.";
653 return false;
654#else
655 {
656 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
657 if (absolute_path == nullptr) {
658 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
659 return false;
660 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100661#ifdef ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800662 android_dlextinfo extinfo;
663 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
664 // (open oat files multiple
665 // times).
666 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
667 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -0800668 if (oat_file_begin != nullptr) { //
669 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
670 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
671 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -0800672 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
673#else
674 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
675 UNUSED(oat_file_begin);
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100676#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -0800677 }
678 if (dlopen_handle_ == nullptr) {
679 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
680 return false;
681 }
682 return true;
683#endif
684}
685
686void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -0800687#ifdef __APPLE__
688 UNUSED(elf_filename);
689 LOG(FATAL) << "Should not reach here.";
690 UNREACHABLE();
691#else
Andreas Gampe049cff02015-12-01 23:27:12 -0800692 struct dl_iterate_context {
693 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
694 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -0700695 context->shared_objects_seen++;
696 if (context->shared_objects_seen < context->shared_objects_before) {
697 // We haven't been called yet for anything we haven't seen before. Just continue.
698 // Note: this is aggressively optimistic. If another thread was unloading a library,
699 // we may miss out here. However, this does not happen often in practice.
700 return 0;
701 }
702
Andreas Gampe049cff02015-12-01 23:27:12 -0800703 // See whether this callback corresponds to the file which we have just loaded.
704 bool contains_begin = false;
705 for (int i = 0; i < info->dlpi_phnum; i++) {
706 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
707 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
708 info->dlpi_phdr[i].p_vaddr);
709 size_t memsz = info->dlpi_phdr[i].p_memsz;
710 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
711 contains_begin = true;
712 break;
713 }
714 }
715 }
716 // Add dummy mmaps for this file.
717 if (contains_begin) {
718 for (int i = 0; i < info->dlpi_phnum; i++) {
719 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
720 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
721 info->dlpi_phdr[i].p_vaddr);
722 size_t memsz = info->dlpi_phdr[i].p_memsz;
723 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
724 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
725 }
726 }
727 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
728 }
729 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
730 }
731 const uint8_t* const begin_;
732 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -0700733 const size_t shared_objects_before;
734 size_t shared_objects_seen;
735 };
736 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -0800737
738 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -0700739 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
740 // before giving up. This should be unusual.
741 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
742 << shared_objects_before_;
743 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
744 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
745 // OK, give up and print an error.
746 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
747 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
748 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800749 }
Andreas Gampe74f07b52015-12-02 11:53:26 -0800750#endif
Andreas Gampe049cff02015-12-01 23:27:12 -0800751}
752
753////////////////////////////////////////////////
754// OatFile via our own ElfFile implementation //
755////////////////////////////////////////////////
756
757class ElfOatFile FINAL : public OatFileBase {
758 public:
759 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
760
761 static ElfOatFile* OpenElfFile(File* file,
762 const std::string& location,
763 uint8_t* requested_base,
764 uint8_t* oat_file_begin, // Override base if not null
765 bool writable,
766 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800767 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800768 const char* abs_dex_location,
769 std::string* error_msg);
770
771 bool InitializeFromElfFile(ElfFile* elf_file,
772 const char* abs_dex_location,
773 std::string* error_msg);
774
775 protected:
776 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
777 std::string* error_msg) const OVERRIDE {
778 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
779 if (ptr == nullptr) {
780 *error_msg = "(Internal implementation could not find symbol)";
781 }
782 return ptr;
783 }
784
Andreas Gampe4075f832016-05-18 13:09:54 -0700785 void PreLoad() OVERRIDE {
786 }
787
Andreas Gampe049cff02015-12-01 23:27:12 -0800788 bool Load(const std::string& elf_filename,
789 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
790 bool writable,
791 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800792 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800793 std::string* error_msg) OVERRIDE;
794
795 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
796 }
797
798 private:
799 bool ElfFileOpen(File* file,
800 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
801 bool writable,
802 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800803 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800804 std::string* error_msg);
805
806 private:
807 // Backing memory map for oat file during cross compilation.
808 std::unique_ptr<ElfFile> elf_file_;
809
810 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
811};
812
813ElfOatFile* ElfOatFile::OpenElfFile(File* file,
814 const std::string& location,
815 uint8_t* requested_base,
816 uint8_t* oat_file_begin, // Override base if not null
817 bool writable,
818 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800819 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800820 const char* abs_dex_location,
821 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800822 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800823 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800824 bool success = oat_file->ElfFileOpen(file,
825 oat_file_begin,
826 writable,
827 low_4gb,
828 executable,
829 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800830 if (!success) {
831 CHECK(!error_msg->empty());
832 return nullptr;
833 }
834
835 // Complete the setup.
836 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
837 return nullptr;
838 }
839
840 if (!oat_file->Setup(abs_dex_location, error_msg)) {
841 return nullptr;
842 }
843
844 return oat_file.release();
845}
846
847bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
848 const char* abs_dex_location,
849 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800850 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800851 if (IsExecutable()) {
852 *error_msg = "Cannot initialize from elf file in executable mode.";
853 return false;
854 }
855 elf_file_.reset(elf_file);
856 uint64_t offset, size;
857 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
858 CHECK(has_section);
859 SetBegin(elf_file->Begin() + offset);
860 SetEnd(elf_file->Begin() + size + offset);
861 // Ignore the optional .bss section when opening non-executable.
862 return Setup(abs_dex_location, error_msg);
863}
864
865bool ElfOatFile::Load(const std::string& elf_filename,
866 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
867 bool writable,
868 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800869 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800870 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800871 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800872 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
873 if (file == nullptr) {
874 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
875 return false;
876 }
877 return ElfOatFile::ElfFileOpen(file.get(),
878 oat_file_begin,
879 writable,
880 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800881 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800882 error_msg);
883}
884
885bool ElfOatFile::ElfFileOpen(File* file,
886 uint8_t* oat_file_begin,
887 bool writable,
888 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800889 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800890 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800891 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -0800892 // TODO: rename requested_base to oat_data_begin
893 elf_file_.reset(ElfFile::Open(file,
894 writable,
895 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800896 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800897 error_msg,
898 oat_file_begin));
899 if (elf_file_ == nullptr) {
900 DCHECK(!error_msg->empty());
901 return false;
902 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800903 bool loaded = elf_file_->Load(executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800904 DCHECK(loaded || !error_msg->empty());
905 return loaded;
906}
907
908//////////////////////////
909// General OatFile code //
910//////////////////////////
911
912std::string OatFile::ResolveRelativeEncodedDexLocation(
913 const char* abs_dex_location, const std::string& rel_dex_location) {
914 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
915 // Strip :classes<N>.dex used for secondary multidex files.
916 std::string base = DexFile::GetBaseLocation(rel_dex_location);
917 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
918
919 // Check if the base is a suffix of the provided abs_dex_location.
920 std::string target_suffix = "/" + base;
921 std::string abs_location(abs_dex_location);
922 if (abs_location.size() > target_suffix.size()) {
923 size_t pos = abs_location.size() - target_suffix.size();
924 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
925 return abs_location + multidex_suffix;
926 }
927 }
928 }
929 return rel_dex_location;
930}
931
932static void CheckLocation(const std::string& location) {
933 CHECK(!location.empty());
934}
935
936OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
937 const std::string& location,
938 const char* abs_dex_location,
939 std::string* error_msg) {
940 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
941 return oat_file->InitializeFromElfFile(elf_file, abs_dex_location, error_msg)
942 ? oat_file.release()
943 : nullptr;
944}
945
946OatFile* OatFile::Open(const std::string& filename,
947 const std::string& location,
948 uint8_t* requested_base,
949 uint8_t* oat_file_begin,
950 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800951 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800952 const char* abs_dex_location,
953 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800954 ScopedTrace trace("Open oat file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -0800955 CHECK(!filename.empty()) << location;
956 CheckLocation(location);
957 std::unique_ptr<OatFile> ret;
958
959 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
960 // disabled.
961 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(filename,
962 location,
963 requested_base,
964 oat_file_begin,
965 false,
966 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800967 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800968 abs_dex_location,
969 error_msg);
970 if (with_dlopen != nullptr) {
971 return with_dlopen;
972 }
973 if (kPrintDlOpenErrorMessage) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800974 LOG(ERROR) << "Failed to dlopen: " << filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -0800975 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800976 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
977 //
978 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
979 //
980 // We use our own ELF loader for Quick to deal with legacy apps that
981 // open a generated dex file by name, remove the file, then open
982 // another generated dex file with the same name. http://b/10614658
983 //
984 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
985 //
986 //
987 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
988 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
989 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(filename,
990 location,
991 requested_base,
992 oat_file_begin,
993 false,
994 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800995 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800996 abs_dex_location,
997 error_msg);
998 return with_internal;
999}
1000
1001OatFile* OatFile::OpenWritable(File* file,
1002 const std::string& location,
1003 const char* abs_dex_location,
1004 std::string* error_msg) {
1005 CheckLocation(location);
1006 return ElfOatFile::OpenElfFile(file,
1007 location,
1008 nullptr,
1009 nullptr,
1010 true,
1011 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001012 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001013 abs_dex_location,
1014 error_msg);
1015}
1016
1017OatFile* OatFile::OpenReadable(File* file,
1018 const std::string& location,
1019 const char* abs_dex_location,
1020 std::string* error_msg) {
1021 CheckLocation(location);
1022 return ElfOatFile::OpenElfFile(file,
1023 location,
1024 nullptr,
1025 nullptr,
1026 false,
1027 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001028 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001029 abs_dex_location,
1030 error_msg);
1031}
1032
1033OatFile::OatFile(const std::string& location, bool is_executable)
1034 : location_(location),
1035 begin_(nullptr),
1036 end_(nullptr),
1037 bss_begin_(nullptr),
1038 bss_end_(nullptr),
1039 is_executable_(is_executable),
1040 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1041 CHECK(!location_.empty());
1042}
1043
1044OatFile::~OatFile() {
1045 STLDeleteElements(&oat_dex_files_storage_);
1046}
1047
Brian Carlstrome24fa612011-09-29 00:53:55 -07001048const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001049 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001050}
1051
Ian Rogers13735952014-10-08 12:43:28 -07001052const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001053 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001054 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001055}
1056
Ian Rogers13735952014-10-08 12:43:28 -07001057const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001058 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001059 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001060}
1061
Vladimir Marko5c42c292015-02-25 12:02:49 +00001062const uint8_t* OatFile::BssBegin() const {
1063 return bss_begin_;
1064}
1065
1066const uint8_t* OatFile::BssEnd() const {
1067 return bss_end_;
1068}
1069
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001070const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1071 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -08001072 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001073 // NOTE: We assume here that the canonical location for a given dex_location never
1074 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1075 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1076 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001077
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001078 // TODO: Additional analysis of usage patterns to see if this can be simplified
1079 // without any performance loss, for example by not doing the first lock-free lookup.
1080
1081 const OatFile::OatDexFile* oat_dex_file = nullptr;
1082 StringPiece key(dex_location);
1083 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1084 // directly mentioned in the oat file and doesn't require locking.
1085 auto primary_it = oat_dex_files_.find(key);
1086 if (primary_it != oat_dex_files_.end()) {
1087 oat_dex_file = primary_it->second;
1088 DCHECK(oat_dex_file != nullptr);
1089 } else {
1090 // This dex_location is not one of the dex locations directly mentioned in the
1091 // oat file. The correct lookup is via the canonical location but first see in
1092 // the secondary_oat_dex_files_ whether we've looked up this location before.
1093 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1094 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1095 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001096 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001097 } else {
1098 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001099 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001100 if (dex_canonical_location != dex_location) {
1101 StringPiece canonical_key(dex_canonical_location);
1102 auto canonical_it = oat_dex_files_.find(canonical_key);
1103 if (canonical_it != oat_dex_files_.end()) {
1104 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001105 } // else keep null.
1106 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001107
1108 // Copy the key to the string_cache_ and store the result in secondary map.
1109 string_cache_.emplace_back(key.data(), key.length());
1110 StringPiece key_copy(string_cache_.back());
1111 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001112 }
1113 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001114 if (oat_dex_file != nullptr &&
1115 (dex_location_checksum == nullptr ||
1116 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
1117 return oat_dex_file;
1118 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001119
1120 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001121 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001122 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -07001123 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001124 checksum = StringPrintf("0x%08x", *dex_location_checksum);
1125 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001126 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +01001127 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001128 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001129 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001130 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001131 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001132 << " contains OatDexFile " << odf->GetDexFileLocation()
1133 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
1134 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001135 }
Ian Rogers7fe2c692011-12-06 16:35:59 -08001136 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001137 }
Calin Juravle4e1d5792014-07-15 23:56:47 +01001138
Andreas Gampefa8429b2015-04-07 18:34:42 -07001139 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001140}
1141
Brian Carlstrome24fa612011-09-29 00:53:55 -07001142OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001143 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001144 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001145 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001146 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001147 const uint8_t* lookup_table_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001148 const uint32_t* oat_class_offsets_pointer,
Vladimir Marko06d7aaa2015-10-16 11:23:41 +01001149 uint8_t* dex_cache_arrays)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001150 : oat_file_(oat_file),
1151 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001152 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001153 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001154 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001155 lookup_table_data_(lookup_table_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001156 oat_class_offsets_pointer_(oat_class_offsets_pointer),
1157 dex_cache_arrays_(dex_cache_arrays) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001158
1159OatFile::OatDexFile::~OatDexFile() {}
1160
Ian Rogers05f28c62012-10-23 18:12:13 -07001161size_t OatFile::OatDexFile::FileSize() const {
1162 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1163}
1164
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001165std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001166 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe3a2bd292016-01-26 17:23:47 -08001167 return DexFile::Open(dex_file_pointer_,
1168 FileSize(),
1169 dex_file_location_,
1170 dex_file_location_checksum_,
1171 this,
1172 false /* verify */,
1173 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001174}
1175
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001176uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1177 return oat_class_offsets_pointer_[class_def_index];
1178}
1179
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001180OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001181 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001182
Ian Rogers13735952014-10-08 12:43:28 -07001183 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001184 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001185
Ian Rogers13735952014-10-08 12:43:28 -07001186 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001187 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
1188 mirror::Class::Status status =
1189 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
1190 CHECK_LT(status, mirror::Class::kStatusMax);
1191
Ian Rogers13735952014-10-08 12:43:28 -07001192 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001193 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1194 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1195 CHECK_LT(type, kOatClassMax);
1196
Ian Rogers13735952014-10-08 12:43:28 -07001197 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001198 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001199
Brian Carlstromcd937a92014-03-04 22:53:23 -08001200 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001201 const uint8_t* bitmap_pointer = nullptr;
1202 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001203 if (type != kOatClassNoneCompiled) {
1204 if (type == kOatClassSomeCompiled) {
1205 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1206 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1207 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1208 methods_pointer = bitmap_pointer + bitmap_size;
1209 } else {
1210 methods_pointer = after_type_pointer;
1211 }
1212 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001213 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001214
Richard Uhler07b3c232015-03-31 15:57:54 -07001215 return OatFile::OatClass(oat_file_,
1216 status,
1217 type,
1218 bitmap_size,
1219 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1220 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001221}
1222
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001223OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001224 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001225 OatClassType type,
1226 uint32_t bitmap_size,
1227 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001228 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001229 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001230 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001231 switch (type_) {
1232 case kOatClassAllCompiled: {
1233 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001234 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001235 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001236 break;
1237 }
1238 case kOatClassSomeCompiled: {
1239 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001240 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001241 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001242 break;
1243 }
1244 case kOatClassNoneCompiled: {
1245 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001246 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001247 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001248 break;
1249 }
1250 case kOatClassMax: {
1251 LOG(FATAL) << "Invalid OatClassType " << type_;
1252 break;
1253 }
1254 }
1255}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001256
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001257uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1258 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1259 if (oat_method_offsets == nullptr) {
1260 return 0u;
1261 }
1262 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1263}
1264
1265const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001266 // 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 -07001267 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001268 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001269 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001270 }
1271 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001272 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001273 CHECK_EQ(kOatClassAllCompiled, type_);
1274 methods_pointer_index = method_index;
1275 } else {
1276 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001277 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001278 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001279 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001280 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1281 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001282 }
1283 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284 return &oat_method_offsets;
1285}
1286
1287const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1288 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1289 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001290 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001291 }
1292 if (oat_file_->IsExecutable() ||
1293 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001294 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001295 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001296 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001297 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1298 // version.
1299 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001300}
1301
Mathieu Chartiere401d142015-04-22 13:56:20 -07001302void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001303 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001304 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001305}
1306
Richard Uhlerd1537b52016-03-29 13:27:41 -07001307bool OatFile::HasPatchInfo() const {
1308 return GetOatHeader().HasPatchInfo();
1309}
1310
Andreas Gampe7ba64962014-10-23 11:37:40 -07001311bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001312 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001313 // TODO: Check against oat_patches. b/18144996
1314}
1315
Sebastien Hertz0de11332015-05-13 12:14:05 +02001316bool OatFile::IsDebuggable() const {
1317 return GetOatHeader().IsDebuggable();
1318}
1319
Andreas Gampe29d38e72016-03-23 15:31:51 +00001320CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1321 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001322}
1323
Andreas Gampe7848da42015-04-09 11:15:04 -07001324static constexpr char kDexClassPathEncodingSeparator = '*';
1325
1326std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
1327 std::ostringstream out;
1328
1329 for (const DexFile* dex_file : dex_files) {
1330 out << dex_file->GetLocation().c_str();
1331 out << kDexClassPathEncodingSeparator;
1332 out << dex_file->GetLocationChecksum();
1333 out << kDexClassPathEncodingSeparator;
1334 }
1335
1336 return out.str();
1337}
1338
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001339bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001340 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1341 // No dependencies.
1342 return true;
1343 }
1344
1345 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1346 // Split() instead of manual parsing of the combined char*.
1347 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001348 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001349 if (split.size() % 2 != 0) {
1350 // Expected pairs of location and checksum.
1351 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
1352 return false;
1353 }
1354
1355 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1356 std::string& location = *it;
1357 std::string& checksum = *(it + 1);
1358 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
1359 if (converted == 0) {
1360 // Conversion error.
1361 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
1362 return false;
1363 }
1364
1365 uint32_t dex_checksum;
1366 std::string error_msg;
1367 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001368 &dex_checksum,
1369 &error_msg)) {
Andreas Gampe7848da42015-04-09 11:15:04 -07001370 if (converted != dex_checksum) {
1371 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
1372 location.c_str(), converted, dex_checksum);
1373 return false;
1374 }
1375 } else {
1376 // Problem retrieving checksum.
1377 // TODO: odex files?
1378 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
1379 error_msg.c_str());
1380 return false;
1381 }
1382 }
1383
1384 return true;
1385}
1386
1387bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001388 std::vector<std::string>* locations) {
1389 DCHECK(locations != nullptr);
Andreas Gampe7848da42015-04-09 11:15:04 -07001390 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
1391 return true;
1392 }
1393
1394 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
1395 // Split() instead of manual parsing of the combined char*.
1396 std::vector<std::string> split;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001397 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
Andreas Gampe7848da42015-04-09 11:15:04 -07001398 if (split.size() % 2 != 0) {
1399 // Expected pairs of location and checksum.
1400 return false;
1401 }
1402
1403 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
1404 locations->push_back(*it);
1405 }
1406
1407 return true;
1408}
1409
Brian Carlstrome24fa612011-09-29 00:53:55 -07001410} // namespace art