blob: 225fee0a6d7562697cddc077027ff142336feb34 [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>
David Srbecky1baabf02015-06-16 17:12:34 +000024#ifndef __APPLE__
25#include <link.h> // for dl_iterate_phdr.
26#endif
Richard Uhlere5fed032015-03-18 08:21:11 -070027#include <sstream>
28
Andreas Gampefa8429b2015-04-07 18:34:42 -070029// dlopen_ext support from bionic.
30#ifdef HAVE_ANDROID_OS
31#include "android/dlext.h"
32#endif
33
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "art_method-inl.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070035#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080036#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080037#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070039#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000041#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070043#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070044#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070045#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070047#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070048
49namespace art {
50
Andreas Gampefa8429b2015-04-07 18:34:42 -070051// Whether OatFile::Open will try DlOpen() first. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000052static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070053
54// Whether OatFile::Open will try DlOpen() on the host. On the host we're not linking against
55// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
56// multiple times). However, if/when we switch the above, we likely want to switch this, too,
57// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000058static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070059
60// For debugging, Open will print DlOpen error message if set to true.
61static constexpr bool kPrintDlOpenErrorMessage = false;
62
Richard Uhlere5fed032015-03-18 08:21:11 -070063std::string OatFile::ResolveRelativeEncodedDexLocation(
64 const char* abs_dex_location, const std::string& rel_dex_location) {
65 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
66 // Strip :classes<N>.dex used for secondary multidex files.
67 std::string base = DexFile::GetBaseLocation(rel_dex_location);
68 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
69
70 // Check if the base is a suffix of the provided abs_dex_location.
71 std::string target_suffix = "/" + base;
72 std::string abs_location(abs_dex_location);
73 if (abs_location.size() > target_suffix.size()) {
74 size_t pos = abs_location.size() - target_suffix.size();
75 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
76 return abs_location + multidex_suffix;
77 }
78 }
79 }
80 return rel_dex_location;
81}
82
Brian Carlstrom700c8d32012-11-05 10:42:02 -080083void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070084 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085}
86
Alex Light84d76052014-08-22 17:49:35 -070087OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
88 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070089 const char* abs_dex_location,
Alex Light84d76052014-08-22 17:49:35 -070090 std::string* error_msg) {
91 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
92 oat_file->elf_file_.reset(elf_file);
Tong Shen62d1ca32014-09-03 17:24:56 -070093 uint64_t offset, size;
94 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
95 CHECK(has_section);
96 oat_file->begin_ = elf_file->Begin() + offset;
97 oat_file->end_ = elf_file->Begin() + size + offset;
Vladimir Marko5c42c292015-02-25 12:02:49 +000098 // Ignore the optional .bss section when opening non-executable.
Richard Uhlere5fed032015-03-18 08:21:11 -070099 return oat_file->Setup(abs_dex_location, error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800100}
101
102OatFile* OatFile::Open(const std::string& filename,
103 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700104 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700105 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700107 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700108 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800109 CHECK(!filename.empty()) << location;
Andreas Gampe7ba64962014-10-23 11:37:40 -0700110 CheckLocation(location);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700111 std::unique_ptr<OatFile> ret;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700112
113 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
114 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
115 // !executable is a sign that we may want to patch), which may not be allowed for
116 // various reasons.
117 if (kUseDlopen && (kIsTargetBuild || kUseDlopenOnHost) && executable) {
118 // Try to use dlopen. This may fail for various reasons, outlined below. We try dlopen, as
119 // this will register the oat file with the linker and allows libunwind to find our info.
120 ret.reset(OpenDlopen(filename, location, requested_base, abs_dex_location, error_msg));
121 if (ret.get() != nullptr) {
122 return ret.release();
123 }
124 if (kPrintDlOpenErrorMessage) {
125 LOG(ERROR) << "Failed to dlopen: " << *error_msg;
126 }
127 }
128
Elliott Hughes956af0f2014-12-11 14:34:28 -0800129 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
130 //
131 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
132 //
133 // We use our own ELF loader for Quick to deal with legacy apps that
134 // open a generated dex file by name, remove the file, then open
135 // another generated dex file with the same name. http://b/10614658
136 //
137 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700138 //
139 //
140 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
141 // 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 -0800142 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700143 if (file == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800144 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
145 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800146 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800147 ret.reset(OpenElfFile(file.get(), location, requested_base, oat_file_begin, false, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700148 abs_dex_location, error_msg));
Elliott Hughes956af0f2014-12-11 14:34:28 -0800149
150 // It would be nice to unlink here. But we might have opened the file created by the
151 // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API
152 // to allow removal when we know the ELF must be borked.
Dave Allison69dfe512014-07-11 17:11:58 +0000153 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800154}
155
Richard Uhlere5fed032015-03-18 08:21:11 -0700156OatFile* OatFile::OpenWritable(File* file, const std::string& location,
157 const char* abs_dex_location,
158 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 CheckLocation(location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700160 return OpenElfFile(file, location, nullptr, nullptr, true, false, abs_dex_location, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800161}
162
Richard Uhlere5fed032015-03-18 08:21:11 -0700163OatFile* OatFile::OpenReadable(File* file, const std::string& location,
164 const char* abs_dex_location,
165 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700166 CheckLocation(location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700167 return OpenElfFile(file, location, nullptr, nullptr, false, false, abs_dex_location, error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700168}
169
Andreas Gampefa8429b2015-04-07 18:34:42 -0700170OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
171 const std::string& location,
172 uint8_t* requested_base,
173 const char* abs_dex_location,
174 std::string* error_msg) {
175 std::unique_ptr<OatFile> oat_file(new OatFile(location, true));
176 bool success = oat_file->Dlopen(elf_filename, requested_base, abs_dex_location, error_msg);
177 if (!success) {
178 return nullptr;
179 }
180 return oat_file.release();
181}
182
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800183OatFile* OatFile::OpenElfFile(File* file,
184 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700185 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700186 uint8_t* oat_file_begin,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700187 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700188 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700189 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700190 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700191 std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
Igor Murashkin46774762014-10-22 11:37:02 -0700192 bool success = oat_file->ElfFileOpen(file, requested_base, oat_file_begin, writable, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700193 abs_dex_location, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700194 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700195 CHECK(!error_msg->empty());
196 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700197 }
198 return oat_file.release();
199}
200
Alex Light9dcc4572014-08-14 14:16:26 -0700201OatFile::OatFile(const std::string& location, bool is_executable)
Andreas Gampefa8429b2015-04-07 18:34:42 -0700202 : location_(location), begin_(nullptr), end_(nullptr), bss_begin_(nullptr), bss_end_(nullptr),
203 is_executable_(is_executable), dlopen_handle_(nullptr),
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100204 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800205 CHECK(!location_.empty());
206}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207
208OatFile::~OatFile() {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100209 STLDeleteElements(&oat_dex_files_storage_);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700210 if (dlopen_handle_ != nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800211 dlclose(dlopen_handle_);
212 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700213}
214
Andreas Gampefa8429b2015-04-07 18:34:42 -0700215bool OatFile::Dlopen(const std::string& elf_filename, uint8_t* requested_base,
216 const char* abs_dex_location, std::string* error_msg) {
David Srbecky1baabf02015-06-16 17:12:34 +0000217#ifdef __APPLE__
218 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
219 // but let's fallback to the custom loading code for the time being.
220 UNUSED(elf_filename);
221 UNUSED(requested_base);
222 UNUSED(abs_dex_location);
223 UNUSED(error_msg);
224 return false;
225#else
Andreas Gampefa8429b2015-04-07 18:34:42 -0700226 std::unique_ptr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
227 if (absolute_path == nullptr) {
228 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
229 return false;
230 }
231#ifdef HAVE_ANDROID_OS
232 android_dlextinfo extinfo;
David Srbecky1baabf02015-06-16 17:12:34 +0000233 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | ANDROID_DLEXT_FORCE_FIXED_VADDR;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700234 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
235#else
236 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
237#endif
238 if (dlopen_handle_ == nullptr) {
239 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
240 return false;
241 }
242 begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatdata"));
243 if (begin_ == nullptr) {
244 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
245 dlerror());
246 return false;
247 }
248 if (requested_base != nullptr && begin_ != requested_base) {
249 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
250 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
251 "oatdata=%p != expected=%p, %s. See process maps in the log.",
252 begin_, requested_base, elf_filename.c_str());
253 return false;
254 }
255 end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatlastword"));
256 if (end_ == nullptr) {
257 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
258 dlerror());
259 return false;
260 }
261 // Readjust to be non-inclusive upper bound.
262 end_ += sizeof(uint32_t);
263
264 bss_begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbss"));
265 if (bss_begin_ == nullptr) {
266 // No .bss section. Clear dlerror().
267 bss_end_ = nullptr;
268 dlerror();
269 } else {
270 bss_end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbsslastword"));
271 if (bss_end_ == nullptr) {
272 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'",
273 elf_filename.c_str());
274 return false;
275 }
276 // Readjust to be non-inclusive upper bound.
277 bss_end_ += sizeof(uint32_t);
278 }
279
David Srbecky1baabf02015-06-16 17:12:34 +0000280 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
281 struct dl_iterate_context {
282 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
283 auto* context = reinterpret_cast<dl_iterate_context*>(data);
David Srbeckyaa038702015-06-17 02:11:07 +0100284 // See whether this callback corresponds to the file which we have just loaded.
285 bool contains_begin = false;
286 for (int i = 0; i < info->dlpi_phnum; i++) {
287 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
288 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
289 info->dlpi_phdr[i].p_vaddr);
290 size_t memsz = info->dlpi_phdr[i].p_memsz;
291 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
292 contains_begin = true;
293 break;
David Srbecky1baabf02015-06-16 17:12:34 +0000294 }
295 }
296 }
David Srbeckyaa038702015-06-17 02:11:07 +0100297 // Add dummy mmaps for this file.
298 if (contains_begin) {
299 for (int i = 0; i < info->dlpi_phnum; i++) {
300 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
301 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
302 info->dlpi_phdr[i].p_vaddr);
303 size_t memsz = info->dlpi_phdr[i].p_memsz;
304 MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
305 }
306 }
307 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
308 }
309 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
David Srbecky1baabf02015-06-16 17:12:34 +0000310 }
David Srbeckyaa038702015-06-17 02:11:07 +0100311 const uint8_t* begin_;
312 } context = { begin_ };
David Srbecky1baabf02015-06-16 17:12:34 +0000313
David Srbeckyaa038702015-06-17 02:11:07 +0100314 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
David Srbecky5dedb802015-06-17 00:08:02 +0100315 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
316 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but can not find its mmaps.";
317 }
318
Andreas Gampefa8429b2015-04-07 18:34:42 -0700319 return Setup(abs_dex_location, error_msg);
David Srbecky1baabf02015-06-16 17:12:34 +0000320#endif // __APPLE__
Andreas Gampefa8429b2015-04-07 18:34:42 -0700321}
322
Igor Murashkin46774762014-10-22 11:37:02 -0700323bool OatFile::ElfFileOpen(File* file, uint8_t* requested_base, uint8_t* oat_file_begin,
324 bool writable, bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700325 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700326 std::string* error_msg) {
Igor Murashkin46774762014-10-22 11:37:02 -0700327 // TODO: rename requested_base to oat_data_begin
328 elf_file_.reset(ElfFile::Open(file, writable, /*program_header_only*/true, error_msg,
329 oat_file_begin));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700330 if (elf_file_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700331 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800332 return false;
333 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700334 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800335 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700336 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800337 return false;
338 }
339 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700340 if (begin_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700341 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800342 return false;
343 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700344 if (requested_base != nullptr && begin_ != requested_base) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800345 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700346 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800347 "oatdata=%p != expected=%p. See process maps in the log.",
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700348 begin_, requested_base);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800349 return false;
350 }
351 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700352 if (end_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700353 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800354 return false;
355 }
356 // Readjust to be non-inclusive upper bound.
357 end_ += sizeof(uint32_t);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000358
359 bss_begin_ = elf_file_->FindDynamicSymbolAddress("oatbss");
360 if (bss_begin_ == nullptr) {
361 // No .bss section. Clear dlerror().
362 bss_end_ = nullptr;
363 dlerror();
364 } else {
365 bss_end_ = elf_file_->FindDynamicSymbolAddress("oatbsslastword");
366 if (bss_end_ == nullptr) {
367 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'",
368 file->GetPath().c_str());
369 return false;
370 }
371 // Readjust to be non-inclusive upper bound.
372 bss_end_ += sizeof(uint32_t);
373 }
374
Richard Uhlere5fed032015-03-18 08:21:11 -0700375 return Setup(abs_dex_location, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800376}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800377
Richard Uhlere5fed032015-03-18 08:21:11 -0700378bool OatFile::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700379 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800380 std::string cause = GetOatHeader().GetValidationErrorMessage();
381 *error_msg = StringPrintf("Invalid oat header for '%s': %s", GetLocation().c_str(),
382 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700383 return false;
384 }
Ian Rogers13735952014-10-08 12:43:28 -0700385 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700386 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700387 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700388 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700389 return false;
390 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700391
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700392 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700393 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700394 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700395 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700396 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700397 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700398 return false;
399 }
400
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100401 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
402 oat_dex_files_storage_.reserve(dex_file_count);
403 for (size_t i = 0; i < dex_file_count; i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700404 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700405 if (UNLIKELY(dex_file_location_size == 0U)) {
406 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
407 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700408 return false;
409 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700410 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700411 if (UNLIKELY(oat > End())) {
412 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
413 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700414 return false;
415 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700416
417 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
418 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700419 if (UNLIKELY(oat > End())) {
420 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
421 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700422 return false;
423 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700424
Richard Uhlere5fed032015-03-18 08:21:11 -0700425 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
426 abs_dex_location,
427 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428
429 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
430 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700431 if (UNLIKELY(oat > End())) {
432 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
433 "dex file checksum", GetLocation().c_str(), i,
434 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700435 return false;
436 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700437
Brian Carlstrom89521892011-12-07 22:05:07 -0800438 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700439 if (UNLIKELY(dex_file_offset == 0U)) {
440 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
441 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700442 return false;
443 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700444 if (UNLIKELY(dex_file_offset > Size())) {
445 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
446 "offset %ud > %zd", GetLocation().c_str(), i,
447 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700448 return false;
449 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800450 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700451 if (UNLIKELY(oat > End())) {
452 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700453 "after dex file offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700454 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700455 return false;
456 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800457
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800458 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700459 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
460 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700461 "dex file magic '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700462 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700463 return false;
464 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700465 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
466 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700467 "dex file version '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700468 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700469 return false;
470 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800471 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
472 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800473
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800474 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700475 if (UNLIKELY(oat > End())) {
476 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700477 "method offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700478 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700479 return false;
480 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700481
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100482 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
483
484 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100485 OatDexFile* oat_dex_file = new OatDexFile(this,
486 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100487 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100488 dex_file_checksum,
489 dex_file_pointer,
490 methods_offsets_pointer);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100491 oat_dex_files_storage_.push_back(oat_dex_file);
492
493 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100494 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100495 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100496 if (canonical_location != dex_file_location) {
497 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
498 oat_dex_files_.Put(canonical_key, oat_dex_file);
499 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700500 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700501 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700502}
503
504const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800505 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700506}
507
Ian Rogers13735952014-10-08 12:43:28 -0700508const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700509 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800510 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700511}
512
Ian Rogers13735952014-10-08 12:43:28 -0700513const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700514 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800515 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700516}
517
Vladimir Marko5c42c292015-02-25 12:02:49 +0000518const uint8_t* OatFile::BssBegin() const {
519 return bss_begin_;
520}
521
522const uint8_t* OatFile::BssEnd() const {
523 return bss_end_;
524}
525
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700526const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
527 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800528 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100529 // NOTE: We assume here that the canonical location for a given dex_location never
530 // changes. If it does (i.e. some symlink used by the filename changes) we may return
531 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
532 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100533
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100534 // TODO: Additional analysis of usage patterns to see if this can be simplified
535 // without any performance loss, for example by not doing the first lock-free lookup.
536
537 const OatFile::OatDexFile* oat_dex_file = nullptr;
538 StringPiece key(dex_location);
539 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
540 // directly mentioned in the oat file and doesn't require locking.
541 auto primary_it = oat_dex_files_.find(key);
542 if (primary_it != oat_dex_files_.end()) {
543 oat_dex_file = primary_it->second;
544 DCHECK(oat_dex_file != nullptr);
545 } else {
546 // This dex_location is not one of the dex locations directly mentioned in the
547 // oat file. The correct lookup is via the canonical location but first see in
548 // the secondary_oat_dex_files_ whether we've looked up this location before.
549 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
550 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
551 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700552 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100553 } else {
554 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100555 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100556 if (dex_canonical_location != dex_location) {
557 StringPiece canonical_key(dex_canonical_location);
558 auto canonical_it = oat_dex_files_.find(canonical_key);
559 if (canonical_it != oat_dex_files_.end()) {
560 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700561 } // else keep null.
562 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100563
564 // Copy the key to the string_cache_ and store the result in secondary map.
565 string_cache_.emplace_back(key.data(), key.length());
566 StringPiece key_copy(string_cache_.back());
567 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700568 }
569 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100570 if (oat_dex_file != nullptr &&
571 (dex_location_checksum == nullptr ||
572 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
573 return oat_dex_file;
574 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700575
576 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100577 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800578 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700579 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800580 checksum = StringPrintf("0x%08x", *dex_location_checksum);
581 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700582 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100583 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800584 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700585 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100586 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700587 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100588 << " contains OatDexFile " << odf->GetDexFileLocation()
589 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
590 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700591 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800592 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700593 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100594
Andreas Gampefa8429b2015-04-07 18:34:42 -0700595 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700596}
597
Brian Carlstrome24fa612011-09-29 00:53:55 -0700598OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800599 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100600 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800601 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -0700602 const uint8_t* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800603 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700604 : oat_file_(oat_file),
605 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100606 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800607 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800608 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800609 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700610
611OatFile::OatDexFile::~OatDexFile() {}
612
Ian Rogers05f28c62012-10-23 18:12:13 -0700613size_t OatFile::OatDexFile::FileSize() const {
614 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
615}
616
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800617std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700618 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Richard Uhler07b3c232015-03-31 15:57:54 -0700619 dex_file_location_checksum_, this, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800620}
621
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700622uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
623 return oat_class_offsets_pointer_[class_def_index];
624}
625
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100626OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700627 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800628
Ian Rogers13735952014-10-08 12:43:28 -0700629 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700630 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800631
Ian Rogers13735952014-10-08 12:43:28 -0700632 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -0700633 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
634 mirror::Class::Status status =
635 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
636 CHECK_LT(status, mirror::Class::kStatusMax);
637
Ian Rogers13735952014-10-08 12:43:28 -0700638 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -0700639 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
640 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
641 CHECK_LT(type, kOatClassMax);
642
Ian Rogers13735952014-10-08 12:43:28 -0700643 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800644 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700645
Brian Carlstromcd937a92014-03-04 22:53:23 -0800646 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700647 const uint8_t* bitmap_pointer = nullptr;
648 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -0700649 if (type != kOatClassNoneCompiled) {
650 if (type == kOatClassSomeCompiled) {
651 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
652 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
653 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
654 methods_pointer = bitmap_pointer + bitmap_size;
655 } else {
656 methods_pointer = after_type_pointer;
657 }
658 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -0800659 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800660
Richard Uhler07b3c232015-03-31 15:57:54 -0700661 return OatFile::OatClass(oat_file_,
662 status,
663 type,
664 bitmap_size,
665 reinterpret_cast<const uint32_t*>(bitmap_pointer),
666 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700667}
668
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800669OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800670 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700671 OatClassType type,
672 uint32_t bitmap_size,
673 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800674 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700675 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100676 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700677 switch (type_) {
678 case kOatClassAllCompiled: {
679 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800680 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700681 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700682 break;
683 }
684 case kOatClassSomeCompiled: {
685 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800686 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700687 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700688 break;
689 }
690 case kOatClassNoneCompiled: {
691 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800692 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700693 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700694 break;
695 }
696 case kOatClassMax: {
697 LOG(FATAL) << "Invalid OatClassType " << type_;
698 break;
699 }
700 }
701}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700702
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700703uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
704 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
705 if (oat_method_offsets == nullptr) {
706 return 0u;
707 }
708 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
709}
710
711const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100712 // 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 -0700713 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700714 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700715 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700716 }
717 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700718 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700719 CHECK_EQ(kOatClassAllCompiled, type_);
720 methods_pointer_index = method_index;
721 } else {
722 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100723 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700724 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700725 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100726 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
727 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700728 }
729 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700730 return &oat_method_offsets;
731}
732
733const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
734 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
735 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800736 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700737 }
738 if (oat_file_->IsExecutable() ||
739 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800740 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800741 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -0700742 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800743 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
744 // version.
745 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700746}
747
Mathieu Chartiere401d142015-04-22 13:56:20 -0700748void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700749 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800750 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -0800751}
752
Andreas Gampe7ba64962014-10-23 11:37:40 -0700753bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -0700754 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -0700755 // TODO: Check against oat_patches. b/18144996
756}
757
Sebastien Hertz0de11332015-05-13 12:14:05 +0200758bool OatFile::IsDebuggable() const {
759 return GetOatHeader().IsDebuggable();
760}
761
Andreas Gampe7848da42015-04-09 11:15:04 -0700762static constexpr char kDexClassPathEncodingSeparator = '*';
763
764std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
765 std::ostringstream out;
766
767 for (const DexFile* dex_file : dex_files) {
768 out << dex_file->GetLocation().c_str();
769 out << kDexClassPathEncodingSeparator;
770 out << dex_file->GetLocationChecksum();
771 out << kDexClassPathEncodingSeparator;
772 }
773
774 return out.str();
775}
776
777bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg) {
778 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
779 // No dependencies.
780 return true;
781 }
782
783 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
784 // Split() instead of manual parsing of the combined char*.
785 std::vector<std::string> split;
786 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
787 if (split.size() % 2 != 0) {
788 // Expected pairs of location and checksum.
789 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
790 return false;
791 }
792
793 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
794 std::string& location = *it;
795 std::string& checksum = *(it + 1);
796 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
797 if (converted == 0) {
798 // Conversion error.
799 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
800 return false;
801 }
802
803 uint32_t dex_checksum;
804 std::string error_msg;
805 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
806 &dex_checksum,
807 &error_msg)) {
808 if (converted != dex_checksum) {
809 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
810 location.c_str(), converted, dex_checksum);
811 return false;
812 }
813 } else {
814 // Problem retrieving checksum.
815 // TODO: odex files?
816 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
817 error_msg.c_str());
818 return false;
819 }
820 }
821
822 return true;
823}
824
825bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
826 std::vector<std::string>* locations) {
827 DCHECK(locations != nullptr);
828 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
829 return true;
830 }
831
832 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
833 // Split() instead of manual parsing of the combined char*.
834 std::vector<std::string> split;
835 Split(dex_dependencies, kDexClassPathEncodingSeparator, &split);
836 if (split.size() % 2 != 0) {
837 // Expected pairs of location and checksum.
838 return false;
839 }
840
841 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
842 locations->push_back(*it);
843 }
844
845 return true;
846}
847
Brian Carlstrome24fa612011-09-29 00:53:55 -0700848} // namespace art