blob: 81703b1b2ac36ead321a6c1aa67576e36f050568 [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
Richard Uhlere5fed032015-03-18 08:21:11 -070023#include <sstream>
24
Brian Carlstromba150c32013-08-27 17:31:03 -070025#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080026#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070029#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080030#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070031#include "mirror/art_method.h"
32#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070036#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070038#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070039
40namespace art {
41
Richard Uhlere5fed032015-03-18 08:21:11 -070042std::string OatFile::ResolveRelativeEncodedDexLocation(
43 const char* abs_dex_location, const std::string& rel_dex_location) {
44 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
45 // Strip :classes<N>.dex used for secondary multidex files.
46 std::string base = DexFile::GetBaseLocation(rel_dex_location);
47 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
48
49 // Check if the base is a suffix of the provided abs_dex_location.
50 std::string target_suffix = "/" + base;
51 std::string abs_location(abs_dex_location);
52 if (abs_location.size() > target_suffix.size()) {
53 size_t pos = abs_location.size() - target_suffix.size();
54 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
55 return abs_location + multidex_suffix;
56 }
57 }
58 }
59 return rel_dex_location;
60}
61
Brian Carlstrom700c8d32012-11-05 10:42:02 -080062void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070063 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080064}
65
Alex Light84d76052014-08-22 17:49:35 -070066OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
67 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070068 const char* abs_dex_location,
Alex Light84d76052014-08-22 17:49:35 -070069 std::string* error_msg) {
70 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
71 oat_file->elf_file_.reset(elf_file);
Tong Shen62d1ca32014-09-03 17:24:56 -070072 uint64_t offset, size;
73 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
74 CHECK(has_section);
75 oat_file->begin_ = elf_file->Begin() + offset;
76 oat_file->end_ = elf_file->Begin() + size + offset;
Vladimir Marko5c42c292015-02-25 12:02:49 +000077 // Ignore the optional .bss section when opening non-executable.
Richard Uhlere5fed032015-03-18 08:21:11 -070078 return oat_file->Setup(abs_dex_location, error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079}
80
81OatFile* OatFile::Open(const std::string& filename,
82 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -070083 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -070084 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070085 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -070086 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070087 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080088 CHECK(!filename.empty()) << location;
Andreas Gampe7ba64962014-10-23 11:37:40 -070089 CheckLocation(location);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070090 std::unique_ptr<OatFile> ret;
Elliott Hughes956af0f2014-12-11 14:34:28 -080091 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
92 //
93 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
94 //
95 // We use our own ELF loader for Quick to deal with legacy apps that
96 // open a generated dex file by name, remove the file, then open
97 // another generated dex file with the same name. http://b/10614658
98 //
99 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
100 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
101 if (file.get() == NULL) {
102 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
103 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800105 ret.reset(OpenElfFile(file.get(), location, requested_base, oat_file_begin, false, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700106 abs_dex_location, error_msg));
Elliott Hughes956af0f2014-12-11 14:34:28 -0800107
108 // It would be nice to unlink here. But we might have opened the file created by the
109 // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API
110 // to allow removal when we know the ELF must be borked.
Dave Allison69dfe512014-07-11 17:11:58 +0000111 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800112}
113
Richard Uhlere5fed032015-03-18 08:21:11 -0700114OatFile* OatFile::OpenWritable(File* file, const std::string& location,
115 const char* abs_dex_location,
116 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800117 CheckLocation(location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700118 return OpenElfFile(file, location, nullptr, nullptr, true, false, abs_dex_location, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800119}
120
Richard Uhlere5fed032015-03-18 08:21:11 -0700121OatFile* OatFile::OpenReadable(File* file, const std::string& location,
122 const char* abs_dex_location,
123 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700124 CheckLocation(location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700125 return OpenElfFile(file, location, nullptr, nullptr, false, false, abs_dex_location, error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700126}
127
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800128OatFile* OatFile::OpenElfFile(File* file,
129 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700130 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700131 uint8_t* oat_file_begin,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700132 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700133 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700134 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700135 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700136 std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
Igor Murashkin46774762014-10-22 11:37:02 -0700137 bool success = oat_file->ElfFileOpen(file, requested_base, oat_file_begin, writable, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700138 abs_dex_location, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700140 CHECK(!error_msg->empty());
141 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 }
143 return oat_file.release();
144}
145
Alex Light9dcc4572014-08-14 14:16:26 -0700146OatFile::OatFile(const std::string& location, bool is_executable)
Vladimir Marko5c42c292015-02-25 12:02:49 +0000147 : location_(location), begin_(NULL), end_(NULL), bss_begin_(nullptr), bss_end_(nullptr),
148 is_executable_(is_executable), dlopen_handle_(NULL),
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100149 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800150 CHECK(!location_.empty());
151}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152
153OatFile::~OatFile() {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100154 STLDeleteElements(&oat_dex_files_storage_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800155 if (dlopen_handle_ != NULL) {
156 dlclose(dlopen_handle_);
157 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700158}
159
Igor Murashkin46774762014-10-22 11:37:02 -0700160bool OatFile::ElfFileOpen(File* file, uint8_t* requested_base, uint8_t* oat_file_begin,
161 bool writable, bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700162 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700163 std::string* error_msg) {
Igor Murashkin46774762014-10-22 11:37:02 -0700164 // TODO: rename requested_base to oat_data_begin
165 elf_file_.reset(ElfFile::Open(file, writable, /*program_header_only*/true, error_msg,
166 oat_file_begin));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700167 if (elf_file_.get() == nullptr) {
168 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800169 return false;
170 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700171 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800172 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700173 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800174 return false;
175 }
176 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
177 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700178 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800179 return false;
180 }
181 if (requested_base != NULL && begin_ != requested_base) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800182 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700183 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800184 "oatdata=%p != expected=%p. See process maps in the log.",
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700185 begin_, requested_base);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800186 return false;
187 }
188 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
189 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700190 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800191 return false;
192 }
193 // Readjust to be non-inclusive upper bound.
194 end_ += sizeof(uint32_t);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000195
196 bss_begin_ = elf_file_->FindDynamicSymbolAddress("oatbss");
197 if (bss_begin_ == nullptr) {
198 // No .bss section. Clear dlerror().
199 bss_end_ = nullptr;
200 dlerror();
201 } else {
202 bss_end_ = elf_file_->FindDynamicSymbolAddress("oatbsslastword");
203 if (bss_end_ == nullptr) {
204 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'",
205 file->GetPath().c_str());
206 return false;
207 }
208 // Readjust to be non-inclusive upper bound.
209 bss_end_ += sizeof(uint32_t);
210 }
211
Richard Uhlere5fed032015-03-18 08:21:11 -0700212 return Setup(abs_dex_location, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800213}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800214
Richard Uhlere5fed032015-03-18 08:21:11 -0700215bool OatFile::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700216 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800217 std::string cause = GetOatHeader().GetValidationErrorMessage();
218 *error_msg = StringPrintf("Invalid oat header for '%s': %s", GetLocation().c_str(),
219 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700220 return false;
221 }
Ian Rogers13735952014-10-08 12:43:28 -0700222 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700223 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700224 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700225 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700226 return false;
227 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700228
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700229 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700230 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700231 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700232 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700233 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700234 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700235 return false;
236 }
237
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100238 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
239 oat_dex_files_storage_.reserve(dex_file_count);
240 for (size_t i = 0; i < dex_file_count; i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700241 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700242 if (UNLIKELY(dex_file_location_size == 0U)) {
243 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
244 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700245 return false;
246 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700247 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700248 if (UNLIKELY(oat > End())) {
249 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
250 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700251 return false;
252 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700253
254 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
255 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700256 if (UNLIKELY(oat > End())) {
257 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
258 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700259 return false;
260 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261
Richard Uhlere5fed032015-03-18 08:21:11 -0700262 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
263 abs_dex_location,
264 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700265
266 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
267 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700268 if (UNLIKELY(oat > End())) {
269 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
270 "dex file checksum", GetLocation().c_str(), i,
271 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700272 return false;
273 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274
Brian Carlstrom89521892011-12-07 22:05:07 -0800275 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700276 if (UNLIKELY(dex_file_offset == 0U)) {
277 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
278 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700279 return false;
280 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 if (UNLIKELY(dex_file_offset > Size())) {
282 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
283 "offset %ud > %zd", GetLocation().c_str(), i,
284 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700285 return false;
286 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800287 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700288 if (UNLIKELY(oat > End())) {
289 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700290 "after dex file offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700291 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700292 return false;
293 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800294
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800295 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700296 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
297 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700298 "dex file magic '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700299 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700300 return false;
301 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700302 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
303 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700304 "dex file version '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700305 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700306 return false;
307 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800308 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
309 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800310
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800311 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700312 if (UNLIKELY(oat > End())) {
313 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700314 "method offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700315 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700316 return false;
317 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700318
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100319 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
320
321 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100322 OatDexFile* oat_dex_file = new OatDexFile(this,
323 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100324 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100325 dex_file_checksum,
326 dex_file_pointer,
327 methods_offsets_pointer);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100328 oat_dex_files_storage_.push_back(oat_dex_file);
329
330 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100331 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100332 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100333 if (canonical_location != dex_file_location) {
334 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
335 oat_dex_files_.Put(canonical_key, oat_dex_file);
336 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700337 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700338 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700339}
340
341const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800342 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700343}
344
Ian Rogers13735952014-10-08 12:43:28 -0700345const uint8_t* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800346 CHECK(begin_ != NULL);
347 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700348}
349
Ian Rogers13735952014-10-08 12:43:28 -0700350const uint8_t* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800351 CHECK(end_ != NULL);
352 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700353}
354
Vladimir Marko5c42c292015-02-25 12:02:49 +0000355const uint8_t* OatFile::BssBegin() const {
356 return bss_begin_;
357}
358
359const uint8_t* OatFile::BssEnd() const {
360 return bss_end_;
361}
362
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700363const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
364 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800365 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100366 // NOTE: We assume here that the canonical location for a given dex_location never
367 // changes. If it does (i.e. some symlink used by the filename changes) we may return
368 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
369 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100370
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100371 // TODO: Additional analysis of usage patterns to see if this can be simplified
372 // without any performance loss, for example by not doing the first lock-free lookup.
373
374 const OatFile::OatDexFile* oat_dex_file = nullptr;
375 StringPiece key(dex_location);
376 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
377 // directly mentioned in the oat file and doesn't require locking.
378 auto primary_it = oat_dex_files_.find(key);
379 if (primary_it != oat_dex_files_.end()) {
380 oat_dex_file = primary_it->second;
381 DCHECK(oat_dex_file != nullptr);
382 } else {
383 // This dex_location is not one of the dex locations directly mentioned in the
384 // oat file. The correct lookup is via the canonical location but first see in
385 // the secondary_oat_dex_files_ whether we've looked up this location before.
386 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
387 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
388 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
389 oat_dex_file = secondary_lb->second; // May be nullptr.
390 } else {
391 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100392 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100393 if (dex_canonical_location != dex_location) {
394 StringPiece canonical_key(dex_canonical_location);
395 auto canonical_it = oat_dex_files_.find(canonical_key);
396 if (canonical_it != oat_dex_files_.end()) {
397 oat_dex_file = canonical_it->second;
398 } // else keep nullptr.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100399 } // else keep nullptr.
400
401 // Copy the key to the string_cache_ and store the result in secondary map.
402 string_cache_.emplace_back(key.data(), key.length());
403 StringPiece key_copy(string_cache_.back());
404 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700405 }
406 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100407 if (oat_dex_file != nullptr &&
408 (dex_location_checksum == nullptr ||
409 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
410 return oat_dex_file;
411 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700412
413 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100414 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800415 std::string checksum("<unspecified>");
416 if (dex_location_checksum != NULL) {
417 checksum = StringPrintf("0x%08x", *dex_location_checksum);
418 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700419 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100420 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800421 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700422 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100423 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700424 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100425 << " contains OatDexFile " << odf->GetDexFileLocation()
426 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
427 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700428 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800429 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100431
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700432 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700433}
434
Brian Carlstrome24fa612011-09-29 00:53:55 -0700435OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800436 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100437 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800438 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -0700439 const uint8_t* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800440 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700441 : oat_file_(oat_file),
442 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100443 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800444 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800445 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800446 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700447
448OatFile::OatDexFile::~OatDexFile() {}
449
Ian Rogers05f28c62012-10-23 18:12:13 -0700450size_t OatFile::OatDexFile::FileSize() const {
451 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
452}
453
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800454std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700455 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Richard Uhler07b3c232015-03-31 15:57:54 -0700456 dex_file_location_checksum_, this, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800457}
458
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700459uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
460 return oat_class_offsets_pointer_[class_def_index];
461}
462
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100463OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700464 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800465
Ian Rogers13735952014-10-08 12:43:28 -0700466 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700467 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800468
Ian Rogers13735952014-10-08 12:43:28 -0700469 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -0700470 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
471 mirror::Class::Status status =
472 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
473 CHECK_LT(status, mirror::Class::kStatusMax);
474
Ian Rogers13735952014-10-08 12:43:28 -0700475 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -0700476 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
477 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
478 CHECK_LT(type, kOatClassMax);
479
Ian Rogers13735952014-10-08 12:43:28 -0700480 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800481 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700482
Brian Carlstromcd937a92014-03-04 22:53:23 -0800483 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700484 const uint8_t* bitmap_pointer = nullptr;
485 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -0700486 if (type != kOatClassNoneCompiled) {
487 if (type == kOatClassSomeCompiled) {
488 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
489 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
490 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
491 methods_pointer = bitmap_pointer + bitmap_size;
492 } else {
493 methods_pointer = after_type_pointer;
494 }
495 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -0800496 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800497
Richard Uhler07b3c232015-03-31 15:57:54 -0700498 return OatFile::OatClass(oat_file_,
499 status,
500 type,
501 bitmap_size,
502 reinterpret_cast<const uint32_t*>(bitmap_pointer),
503 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700504}
505
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800506OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800507 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700508 OatClassType type,
509 uint32_t bitmap_size,
510 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800511 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700512 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100513 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700514 switch (type_) {
515 case kOatClassAllCompiled: {
516 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800517 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700518 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700519 break;
520 }
521 case kOatClassSomeCompiled: {
522 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800523 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700524 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700525 break;
526 }
527 case kOatClassNoneCompiled: {
528 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800529 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700530 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700531 break;
532 }
533 case kOatClassMax: {
534 LOG(FATAL) << "Invalid OatClassType " << type_;
535 break;
536 }
537 }
538}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700539
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700540uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
541 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
542 if (oat_method_offsets == nullptr) {
543 return 0u;
544 }
545 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
546}
547
548const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100549 // 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 -0700550 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700551 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700552 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700553 }
554 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700555 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700556 CHECK_EQ(kOatClassAllCompiled, type_);
557 methods_pointer_index = method_index;
558 } else {
559 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100560 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700561 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700562 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100563 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
564 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700565 }
566 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700567 return &oat_method_offsets;
568}
569
570const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
571 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
572 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800573 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700574 }
575 if (oat_file_->IsExecutable() ||
576 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800577 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800578 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -0700579 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800580 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
581 // version.
582 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700583}
584
Brian Carlstromea46f952013-07-30 01:26:50 -0700585void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700586 CHECK(method != NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800587 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -0800588}
589
Andreas Gampe7ba64962014-10-23 11:37:40 -0700590bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -0700591 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -0700592 // TODO: Check against oat_patches. b/18144996
593}
594
Brian Carlstrome24fa612011-09-29 00:53:55 -0700595} // namespace art