blob: 6fda790697d52709389c57c9767cc60c107d9ffd [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>
Andreas Gampedaab38c2014-09-12 18:38:24 -070021#include <unistd.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022
Andreas Gampe7848da42015-04-09 11:15:04 -070023#include <cstdlib>
Richard Uhlere5fed032015-03-18 08:21:11 -070024#include <sstream>
25
Andreas Gampefa8429b2015-04-07 18:34:42 -070026// dlopen_ext support from bionic.
27#ifdef HAVE_ANDROID_OS
28#include "android/dlext.h"
29#endif
30
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "art_method-inl.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070032#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080033#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080034#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080035#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070036#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080037#include "oat.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070039#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070040#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070041#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070043#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070044
45namespace art {
46
Andreas Gampefa8429b2015-04-07 18:34:42 -070047// Whether OatFile::Open will try DlOpen() first. Fallback is our own ELF loader.
48static constexpr bool kUseDlopen = false;
49
50// Whether OatFile::Open will try DlOpen() on the host. On the host we're not linking against
51// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
52// multiple times). However, if/when we switch the above, we likely want to switch this, too,
53// to get test coverage of the code paths.
54static constexpr bool kUseDlopenOnHost = false;
55
56// For debugging, Open will print DlOpen error message if set to true.
57static constexpr bool kPrintDlOpenErrorMessage = false;
58
Richard Uhlere5fed032015-03-18 08:21:11 -070059std::string OatFile::ResolveRelativeEncodedDexLocation(
60 const char* abs_dex_location, const std::string& rel_dex_location) {
61 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
62 // Strip :classes<N>.dex used for secondary multidex files.
63 std::string base = DexFile::GetBaseLocation(rel_dex_location);
64 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
65
66 // Check if the base is a suffix of the provided abs_dex_location.
67 std::string target_suffix = "/" + base;
68 std::string abs_location(abs_dex_location);
69 if (abs_location.size() > target_suffix.size()) {
70 size_t pos = abs_location.size() - target_suffix.size();
71 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
72 return abs_location + multidex_suffix;
73 }
74 }
75 }
76 return rel_dex_location;
77}
78
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070080 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080081}
82
Alex Light84d76052014-08-22 17:49:35 -070083OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
84 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070085 const char* abs_dex_location,
Alex Light84d76052014-08-22 17:49:35 -070086 std::string* error_msg) {
87 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
88 oat_file->elf_file_.reset(elf_file);
Tong Shen62d1ca32014-09-03 17:24:56 -070089 uint64_t offset, size;
90 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
91 CHECK(has_section);
92 oat_file->begin_ = elf_file->Begin() + offset;
93 oat_file->end_ = elf_file->Begin() + size + offset;
Vladimir Marko5c42c292015-02-25 12:02:49 +000094 // Ignore the optional .bss section when opening non-executable.
Richard Uhlere5fed032015-03-18 08:21:11 -070095 return oat_file->Setup(abs_dex_location, error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080096}
97
98OatFile* OatFile::Open(const std::string& filename,
99 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700100 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700101 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700102 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700103 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700104 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800105 CHECK(!filename.empty()) << location;
Andreas Gampe7ba64962014-10-23 11:37:40 -0700106 CheckLocation(location);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700107 std::unique_ptr<OatFile> ret;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700108
109 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
110 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
111 // !executable is a sign that we may want to patch), which may not be allowed for
112 // various reasons.
113 if (kUseDlopen && (kIsTargetBuild || kUseDlopenOnHost) && executable) {
114 // Try to use dlopen. This may fail for various reasons, outlined below. We try dlopen, as
115 // this will register the oat file with the linker and allows libunwind to find our info.
116 ret.reset(OpenDlopen(filename, location, requested_base, abs_dex_location, error_msg));
117 if (ret.get() != nullptr) {
118 return ret.release();
119 }
120 if (kPrintDlOpenErrorMessage) {
121 LOG(ERROR) << "Failed to dlopen: " << *error_msg;
122 }
123 }
124
Elliott Hughes956af0f2014-12-11 14:34:28 -0800125 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
126 //
127 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
128 //
129 // We use our own ELF loader for Quick to deal with legacy apps that
130 // open a generated dex file by name, remove the file, then open
131 // another generated dex file with the same name. http://b/10614658
132 //
133 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700134 //
135 //
136 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
137 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800138 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700139 if (file == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800140 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
141 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800142 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800143 ret.reset(OpenElfFile(file.get(), location, requested_base, oat_file_begin, false, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700144 abs_dex_location, error_msg));
Elliott Hughes956af0f2014-12-11 14:34:28 -0800145
146 // It would be nice to unlink here. But we might have opened the file created by the
147 // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API
148 // to allow removal when we know the ELF must be borked.
Dave Allison69dfe512014-07-11 17:11:58 +0000149 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800150}
151
Richard Uhlere5fed032015-03-18 08:21:11 -0700152OatFile* OatFile::OpenWritable(File* file, const std::string& location,
153 const char* abs_dex_location,
154 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800155 CheckLocation(location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700156 return OpenElfFile(file, location, nullptr, nullptr, true, false, abs_dex_location, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800157}
158
Richard Uhlere5fed032015-03-18 08:21:11 -0700159OatFile* OatFile::OpenReadable(File* file, const std::string& location,
160 const char* abs_dex_location,
161 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700162 CheckLocation(location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700163 return OpenElfFile(file, location, nullptr, nullptr, false, false, abs_dex_location, error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700164}
165
Andreas Gampefa8429b2015-04-07 18:34:42 -0700166OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
167 const std::string& location,
168 uint8_t* requested_base,
169 const char* abs_dex_location,
170 std::string* error_msg) {
171 std::unique_ptr<OatFile> oat_file(new OatFile(location, true));
172 bool success = oat_file->Dlopen(elf_filename, requested_base, abs_dex_location, error_msg);
173 if (!success) {
174 return nullptr;
175 }
176 return oat_file.release();
177}
178
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800179OatFile* OatFile::OpenElfFile(File* file,
180 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700181 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700182 uint8_t* oat_file_begin,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700183 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700184 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700185 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700186 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700187 std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
Igor Murashkin46774762014-10-22 11:37:02 -0700188 bool success = oat_file->ElfFileOpen(file, requested_base, oat_file_begin, writable, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700189 abs_dex_location, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700190 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700191 CHECK(!error_msg->empty());
192 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700193 }
194 return oat_file.release();
195}
196
Alex Light9dcc4572014-08-14 14:16:26 -0700197OatFile::OatFile(const std::string& location, bool is_executable)
Andreas Gampefa8429b2015-04-07 18:34:42 -0700198 : location_(location), begin_(nullptr), end_(nullptr), bss_begin_(nullptr), bss_end_(nullptr),
199 is_executable_(is_executable), dlopen_handle_(nullptr),
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100200 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800201 CHECK(!location_.empty());
202}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700203
204OatFile::~OatFile() {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100205 STLDeleteElements(&oat_dex_files_storage_);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700206 if (dlopen_handle_ != nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800207 dlclose(dlopen_handle_);
208 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209}
210
Andreas Gampefa8429b2015-04-07 18:34:42 -0700211bool OatFile::Dlopen(const std::string& elf_filename, uint8_t* requested_base,
212 const char* abs_dex_location, std::string* error_msg) {
213 std::unique_ptr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
214 if (absolute_path == nullptr) {
215 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
216 return false;
217 }
218#ifdef HAVE_ANDROID_OS
219 android_dlextinfo extinfo;
220 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
221 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
222#else
223 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
224#endif
225 if (dlopen_handle_ == nullptr) {
226 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
227 return false;
228 }
229 begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatdata"));
230 if (begin_ == nullptr) {
231 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
232 dlerror());
233 return false;
234 }
235 if (requested_base != nullptr && begin_ != requested_base) {
236 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
237 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
238 "oatdata=%p != expected=%p, %s. See process maps in the log.",
239 begin_, requested_base, elf_filename.c_str());
240 return false;
241 }
242 end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatlastword"));
243 if (end_ == nullptr) {
244 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
245 dlerror());
246 return false;
247 }
248 // Readjust to be non-inclusive upper bound.
249 end_ += sizeof(uint32_t);
250
251 bss_begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbss"));
252 if (bss_begin_ == nullptr) {
253 // No .bss section. Clear dlerror().
254 bss_end_ = nullptr;
255 dlerror();
256 } else {
257 bss_end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbsslastword"));
258 if (bss_end_ == nullptr) {
259 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'",
260 elf_filename.c_str());
261 return false;
262 }
263 // Readjust to be non-inclusive upper bound.
264 bss_end_ += sizeof(uint32_t);
265 }
266
267 return Setup(abs_dex_location, error_msg);
268}
269
Igor Murashkin46774762014-10-22 11:37:02 -0700270bool OatFile::ElfFileOpen(File* file, uint8_t* requested_base, uint8_t* oat_file_begin,
271 bool writable, bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700272 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700273 std::string* error_msg) {
Igor Murashkin46774762014-10-22 11:37:02 -0700274 // TODO: rename requested_base to oat_data_begin
275 elf_file_.reset(ElfFile::Open(file, writable, /*program_header_only*/true, error_msg,
276 oat_file_begin));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700277 if (elf_file_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700278 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800279 return false;
280 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800282 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700283 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800284 return false;
285 }
286 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700287 if (begin_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700288 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800289 return false;
290 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700291 if (requested_base != nullptr && begin_ != requested_base) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800292 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700293 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800294 "oatdata=%p != expected=%p. See process maps in the log.",
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700295 begin_, requested_base);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800296 return false;
297 }
298 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700299 if (end_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700300 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800301 return false;
302 }
303 // Readjust to be non-inclusive upper bound.
304 end_ += sizeof(uint32_t);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000305
306 bss_begin_ = elf_file_->FindDynamicSymbolAddress("oatbss");
307 if (bss_begin_ == nullptr) {
308 // No .bss section. Clear dlerror().
309 bss_end_ = nullptr;
310 dlerror();
311 } else {
312 bss_end_ = elf_file_->FindDynamicSymbolAddress("oatbsslastword");
313 if (bss_end_ == nullptr) {
314 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'",
315 file->GetPath().c_str());
316 return false;
317 }
318 // Readjust to be non-inclusive upper bound.
319 bss_end_ += sizeof(uint32_t);
320 }
321
Richard Uhlere5fed032015-03-18 08:21:11 -0700322 return Setup(abs_dex_location, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800323}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800324
Richard Uhlere5fed032015-03-18 08:21:11 -0700325bool OatFile::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700326 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800327 std::string cause = GetOatHeader().GetValidationErrorMessage();
328 *error_msg = StringPrintf("Invalid oat header for '%s': %s", GetLocation().c_str(),
329 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700330 return false;
331 }
Ian Rogers13735952014-10-08 12:43:28 -0700332 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700333 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700334 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700335 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700336 return false;
337 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700338
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700339 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700340 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700341 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700342 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700343 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700344 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700345 return false;
346 }
347
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100348 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
349 oat_dex_files_storage_.reserve(dex_file_count);
350 for (size_t i = 0; i < dex_file_count; i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700351 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700352 if (UNLIKELY(dex_file_location_size == 0U)) {
353 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
354 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700355 return false;
356 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700357 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700358 if (UNLIKELY(oat > End())) {
359 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
360 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700361 return false;
362 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700363
364 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
365 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700366 if (UNLIKELY(oat > End())) {
367 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
368 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700369 return false;
370 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700371
Richard Uhlere5fed032015-03-18 08:21:11 -0700372 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
373 abs_dex_location,
374 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700375
376 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
377 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700378 if (UNLIKELY(oat > End())) {
379 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
380 "dex file checksum", GetLocation().c_str(), i,
381 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700382 return false;
383 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700384
Brian Carlstrom89521892011-12-07 22:05:07 -0800385 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700386 if (UNLIKELY(dex_file_offset == 0U)) {
387 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
388 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700389 return false;
390 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700391 if (UNLIKELY(dex_file_offset > Size())) {
392 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
393 "offset %ud > %zd", GetLocation().c_str(), i,
394 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700395 return false;
396 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800397 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700398 if (UNLIKELY(oat > End())) {
399 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700400 "after dex file offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700401 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700402 return false;
403 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800404
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800405 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700406 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
407 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700408 "dex file magic '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700409 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700410 return false;
411 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700412 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
413 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700414 "dex file version '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700415 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700416 return false;
417 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800418 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
419 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800420
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800421 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700422 if (UNLIKELY(oat > End())) {
423 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700424 "method offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700425 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700426 return false;
427 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100429 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
430
431 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100432 OatDexFile* oat_dex_file = new OatDexFile(this,
433 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100434 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100435 dex_file_checksum,
436 dex_file_pointer,
437 methods_offsets_pointer);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100438 oat_dex_files_storage_.push_back(oat_dex_file);
439
440 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100441 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100442 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100443 if (canonical_location != dex_file_location) {
444 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
445 oat_dex_files_.Put(canonical_key, oat_dex_file);
446 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700447 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700448 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700449}
450
451const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800452 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700453}
454
Ian Rogers13735952014-10-08 12:43:28 -0700455const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700456 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800457 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700458}
459
Ian Rogers13735952014-10-08 12:43:28 -0700460const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700461 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800462 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463}
464
Vladimir Marko5c42c292015-02-25 12:02:49 +0000465const uint8_t* OatFile::BssBegin() const {
466 return bss_begin_;
467}
468
469const uint8_t* OatFile::BssEnd() const {
470 return bss_end_;
471}
472
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700473const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
474 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800475 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100476 // NOTE: We assume here that the canonical location for a given dex_location never
477 // changes. If it does (i.e. some symlink used by the filename changes) we may return
478 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
479 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100480
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100481 // TODO: Additional analysis of usage patterns to see if this can be simplified
482 // without any performance loss, for example by not doing the first lock-free lookup.
483
484 const OatFile::OatDexFile* oat_dex_file = nullptr;
485 StringPiece key(dex_location);
486 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
487 // directly mentioned in the oat file and doesn't require locking.
488 auto primary_it = oat_dex_files_.find(key);
489 if (primary_it != oat_dex_files_.end()) {
490 oat_dex_file = primary_it->second;
491 DCHECK(oat_dex_file != nullptr);
492 } else {
493 // This dex_location is not one of the dex locations directly mentioned in the
494 // oat file. The correct lookup is via the canonical location but first see in
495 // the secondary_oat_dex_files_ whether we've looked up this location before.
496 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
497 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
498 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700499 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100500 } else {
501 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100502 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100503 if (dex_canonical_location != dex_location) {
504 StringPiece canonical_key(dex_canonical_location);
505 auto canonical_it = oat_dex_files_.find(canonical_key);
506 if (canonical_it != oat_dex_files_.end()) {
507 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700508 } // else keep null.
509 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100510
511 // Copy the key to the string_cache_ and store the result in secondary map.
512 string_cache_.emplace_back(key.data(), key.length());
513 StringPiece key_copy(string_cache_.back());
514 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700515 }
516 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100517 if (oat_dex_file != nullptr &&
518 (dex_location_checksum == nullptr ||
519 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
520 return oat_dex_file;
521 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700522
523 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100524 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800525 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700526 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800527 checksum = StringPrintf("0x%08x", *dex_location_checksum);
528 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700529 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100530 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800531 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700532 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100533 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700534 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100535 << " contains OatDexFile " << odf->GetDexFileLocation()
536 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
537 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700538 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800539 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700540 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100541
Andreas Gampefa8429b2015-04-07 18:34:42 -0700542 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700543}
544
Brian Carlstrome24fa612011-09-29 00:53:55 -0700545OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800546 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100547 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800548 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -0700549 const uint8_t* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800550 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700551 : oat_file_(oat_file),
552 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100553 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800554 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800555 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800556 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700557
558OatFile::OatDexFile::~OatDexFile() {}
559
Ian Rogers05f28c62012-10-23 18:12:13 -0700560size_t OatFile::OatDexFile::FileSize() const {
561 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
562}
563
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800564std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700565 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Richard Uhler07b3c232015-03-31 15:57:54 -0700566 dex_file_location_checksum_, this, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800567}
568
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700569uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
570 return oat_class_offsets_pointer_[class_def_index];
571}
572
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100573OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700574 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800575
Ian Rogers13735952014-10-08 12:43:28 -0700576 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700577 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800578
Ian Rogers13735952014-10-08 12:43:28 -0700579 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -0700580 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
581 mirror::Class::Status status =
582 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
583 CHECK_LT(status, mirror::Class::kStatusMax);
584
Ian Rogers13735952014-10-08 12:43:28 -0700585 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -0700586 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
587 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
588 CHECK_LT(type, kOatClassMax);
589
Ian Rogers13735952014-10-08 12:43:28 -0700590 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800591 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700592
Brian Carlstromcd937a92014-03-04 22:53:23 -0800593 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700594 const uint8_t* bitmap_pointer = nullptr;
595 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -0700596 if (type != kOatClassNoneCompiled) {
597 if (type == kOatClassSomeCompiled) {
598 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
599 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
600 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
601 methods_pointer = bitmap_pointer + bitmap_size;
602 } else {
603 methods_pointer = after_type_pointer;
604 }
605 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -0800606 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800607
Richard Uhler07b3c232015-03-31 15:57:54 -0700608 return OatFile::OatClass(oat_file_,
609 status,
610 type,
611 bitmap_size,
612 reinterpret_cast<const uint32_t*>(bitmap_pointer),
613 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700614}
615
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800616OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700618 OatClassType type,
619 uint32_t bitmap_size,
620 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800621 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700622 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100623 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700624 switch (type_) {
625 case kOatClassAllCompiled: {
626 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800627 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700628 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700629 break;
630 }
631 case kOatClassSomeCompiled: {
632 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800633 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700634 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700635 break;
636 }
637 case kOatClassNoneCompiled: {
638 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800639 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700640 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700641 break;
642 }
643 case kOatClassMax: {
644 LOG(FATAL) << "Invalid OatClassType " << type_;
645 break;
646 }
647 }
648}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700649
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700650uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
651 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
652 if (oat_method_offsets == nullptr) {
653 return 0u;
654 }
655 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
656}
657
658const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100659 // 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 -0700660 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700661 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700662 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700663 }
664 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700665 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700666 CHECK_EQ(kOatClassAllCompiled, type_);
667 methods_pointer_index = method_index;
668 } else {
669 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100670 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700671 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700672 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100673 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
674 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700675 }
676 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700677 return &oat_method_offsets;
678}
679
680const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
681 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
682 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800683 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700684 }
685 if (oat_file_->IsExecutable() ||
686 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800687 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800688 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -0700689 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800690 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
691 // version.
692 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700693}
694
Mathieu Chartiere401d142015-04-22 13:56:20 -0700695void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700696 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800697 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -0800698}
699
Andreas Gampe7ba64962014-10-23 11:37:40 -0700700bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -0700701 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -0700702 // TODO: Check against oat_patches. b/18144996
703}
704
Sebastien Hertz0de11332015-05-13 12:14:05 +0200705bool OatFile::IsDebuggable() const {
706 return GetOatHeader().IsDebuggable();
707}
708
Andreas Gampe7848da42015-04-09 11:15:04 -0700709static constexpr char kDexClassPathEncodingSeparator = '*';
710
711std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
712 std::ostringstream out;
713
714 for (const DexFile* dex_file : dex_files) {
715 out << dex_file->GetLocation().c_str();
716 out << kDexClassPathEncodingSeparator;
717 out << dex_file->GetLocationChecksum();
718 out << kDexClassPathEncodingSeparator;
719 }
720
721 return out.str();
722}
723
724bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
725 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
726 // No dependencies.
727 return true;
728 }
729
730 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
731 // Split() instead of manual parsing of the combined char*.
732 std::vector<std::string> split;
733 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
734 if (split.size() % 2 != 0) {
735 // Expected pairs of location and checksum.
736 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
737 return false;
738 }
739
740 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
741 std::string& location = *it;
742 std::string& checksum = *(it + 1);
743 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
744 if (converted == 0) {
745 // Conversion error.
746 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
747 return false;
748 }
749
750 uint32_t dex_checksum;
751 std::string error_msg;
752 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
753 &dex_checksum,
754 &error_msg)) {
755 if (converted != dex_checksum) {
756 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
757 location.c_str(), converted, dex_checksum);
758 return false;
759 }
760 } else {
761 // Problem retrieving checksum.
762 // TODO: odex files?
763 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
764 error_msg.c_str());
765 return false;
766 }
767 }
768
769 return true;
770}
771
772bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
773 std::vector<std::string>* locations) {
774 DCHECK(locations != nullptr);
775 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
776 return true;
777 }
778
779 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
780 // Split() instead of manual parsing of the combined char*.
781 std::vector<std::string> split;
782 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
783 if (split.size() % 2 != 0) {
784 // Expected pairs of location and checksum.
785 return false;
786 }
787
788 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
789 locations->push_back(*it);
790 }
791
792 return true;
793}
794
Brian Carlstrome24fa612011-09-29 00:53:55 -0700795} // namespace art