blob: 7d9922d72ac5c7b42f5309541339a2e784068299 [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>
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070020#include <sstream>
Calin Juravle4e1d5792014-07-15 23:56:47 +010021#include <string.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022
Brian Carlstromba150c32013-08-27 17:31:03 -070023#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080024#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080026#include "elf_file.h"
27#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method.h"
29#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070033#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070035#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070036
37namespace art {
38
Brian Carlstrom700c8d32012-11-05 10:42:02 -080039void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070040 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041}
42
Brian Carlstrom265091e2013-01-30 14:08:26 -080043OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070044 const std::string& location,
45 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046 CHECK(!oat_contents.empty()) << location;
47 CheckLocation(location);
Alex Light9dcc4572014-08-14 14:16:26 -070048 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049 oat_file->begin_ = &oat_contents[0];
50 oat_file->end_ = &oat_contents[oat_contents.size()];
Ian Rogers8d31bbd2013-10-13 10:44:14 -070051 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080052}
53
54OatFile* OatFile::Open(const std::string& filename,
55 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070056 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070057 bool executable,
58 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059 CHECK(!filename.empty()) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070060 CheckLocation(filename);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070061 std::unique_ptr<OatFile> ret;
62 if (kUsePortableCompiler && executable) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080063 // If we are using PORTABLE, use dlopen to deal with relocations.
64 //
65 // We use our own ELF loader for Quick to deal with legacy apps that
66 // open a generated dex file by name, remove the file, then open
67 // another generated dex file with the same name. http://b/10614658
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070068 ret.reset(OpenDlopen(filename, location, requested_base, error_msg));
69 } else {
70 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
71 //
72 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
73 //
74 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
75 // This won't work for portable runtime execution because it doesn't process relocations.
76 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
77 if (file.get() == NULL) {
78 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
79 return nullptr;
Ian Rogersef7d42f2014-01-06 12:55:46 -080080 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070081 ret.reset(OpenElfFile(file.get(), location, requested_base, false, executable, error_msg));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080082 }
Dave Allison69dfe512014-07-11 17:11:58 +000083 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080084}
85
Ian Rogers8d31bbd2013-10-13 10:44:14 -070086OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080087 CheckLocation(location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070088 return OpenElfFile(file, location, NULL, true, false, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080089}
90
Alex Lighta59dd802014-07-02 16:28:08 -070091OatFile* OatFile::OpenReadable(File* file, const std::string& location, std::string* error_msg) {
92 CheckLocation(location);
93 return OpenElfFile(file, location, NULL, false, false, error_msg);
94}
95
Brian Carlstrom700c8d32012-11-05 10:42:02 -080096OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
97 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098 byte* requested_base,
99 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700100 std::unique_ptr<OatFile> oat_file(new OatFile(location, true));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700101 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800102 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104 }
105 return oat_file.release();
106}
107
108OatFile* OatFile::OpenElfFile(File* file,
109 const std::string& location,
110 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700111 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700112 bool executable,
113 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700114 std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700115 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700116 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 CHECK(!error_msg->empty());
118 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 }
120 return oat_file.release();
121}
122
Alex Light9dcc4572014-08-14 14:16:26 -0700123OatFile::OatFile(const std::string& location, bool is_executable)
124 : location_(location), begin_(NULL), end_(NULL), is_executable_(is_executable),
125 dlopen_handle_(NULL),
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100126 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800127 CHECK(!location_.empty());
128}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129
130OatFile::~OatFile() {
131 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800132 if (dlopen_handle_ != NULL) {
133 dlclose(dlopen_handle_);
134 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135}
136
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700137bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
138 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800139 char* absolute_path = realpath(elf_filename.c_str(), NULL);
140 if (absolute_path == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700141 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 return false;
143 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800144 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
145 free(absolute_path);
146 if (dlopen_handle_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700147 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 return false;
149 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800150 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
151 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700152 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
153 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800154 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700155 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700157 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
158 "oatdata=%p != expected=%p /proc/self/maps:\n",
159 begin_, requested_base);
160 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800161 return false;
162 }
163 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
164 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700165 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
166 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800167 return false;
168 }
169 // Readjust to be non-inclusive upper bound.
170 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700171 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800172}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700173
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700174bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
175 std::string* error_msg) {
176 elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
177 if (elf_file_.get() == nullptr) {
178 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800179 return false;
180 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700181 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800182 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700183 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800184 return false;
185 }
186 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
187 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700188 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800189 return false;
190 }
191 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700192 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
193 "oatdata=%p != expected=%p /proc/self/maps:\n",
194 begin_, requested_base);
195 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800196 return false;
197 }
198 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
199 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201 return false;
202 }
203 // Readjust to be non-inclusive upper bound.
204 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700205 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800206}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800207
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208bool OatFile::Setup(std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700209 if (!GetOatHeader().IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700210 *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700211 return false;
212 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800213 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700215 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700216 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700217 return false;
218 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700219
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700220 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700221 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700222 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700223 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700224 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700225 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700226 return false;
227 }
228
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800229 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700230 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700231 if (UNLIKELY(dex_file_location_size == 0U)) {
232 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
233 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700234 return false;
235 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700237 if (UNLIKELY(oat > End())) {
238 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
239 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700240 return false;
241 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700242
243 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
244 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245 if (UNLIKELY(oat > End())) {
246 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
247 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700248 return false;
249 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250
251 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
252
253 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
254 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700255 if (UNLIKELY(oat > End())) {
256 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
257 "dex file checksum", GetLocation().c_str(), i,
258 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700259 return false;
260 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261
Brian Carlstrom89521892011-12-07 22:05:07 -0800262 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700263 if (UNLIKELY(dex_file_offset == 0U)) {
264 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
265 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700266 return false;
267 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700268 if (UNLIKELY(dex_file_offset > Size())) {
269 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
270 "offset %ud > %zd", GetLocation().c_str(), i,
271 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700272 return false;
273 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800274 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700275 if (UNLIKELY(oat > End())) {
276 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
277 " after dex file offsets", GetLocation().c_str(), i,
278 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700279 return false;
280 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800281
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800282 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700283 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
284 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
285 " dex file magic '%s'", GetLocation().c_str(), i,
286 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700287 return false;
288 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700289 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
290 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
291 " dex file version '%s'", GetLocation().c_str(), i,
292 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700293 return false;
294 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800295 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
296 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800297
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800298 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700299 if (UNLIKELY(oat > End())) {
300 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
301 " method offsets", GetLocation().c_str(), i,
302 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700303 return false;
304 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700305
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100306 // Create the OatDexFile and add it to the owning map indexed by the dex file location.
Vladimir Marko539690a2014-06-05 18:36:42 +0100307 OatDexFile* oat_dex_file = new OatDexFile(this,
308 dex_file_location,
309 dex_file_checksum,
310 dex_file_pointer,
311 methods_offsets_pointer);
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100312 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100313 oat_dex_files_.Put(key, oat_dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700314 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700315 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700316}
317
318const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800319 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320}
321
Ian Rogers30fab402012-01-23 15:43:46 -0800322const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800323 CHECK(begin_ != NULL);
324 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700325}
326
Ian Rogers30fab402012-01-23 15:43:46 -0800327const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800328 CHECK(end_ != NULL);
329 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700330}
331
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
333 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800334 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100335 // NOTE: We assume here that the canonical location for a given dex_location never
336 // changes. If it does (i.e. some symlink used by the filename changes) we may return
337 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
338 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100339
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100340 // TODO: Additional analysis of usage patterns to see if this can be simplified
341 // without any performance loss, for example by not doing the first lock-free lookup.
342
343 const OatFile::OatDexFile* oat_dex_file = nullptr;
344 StringPiece key(dex_location);
345 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
346 // directly mentioned in the oat file and doesn't require locking.
347 auto primary_it = oat_dex_files_.find(key);
348 if (primary_it != oat_dex_files_.end()) {
349 oat_dex_file = primary_it->second;
350 DCHECK(oat_dex_file != nullptr);
351 } else {
352 // This dex_location is not one of the dex locations directly mentioned in the
353 // oat file. The correct lookup is via the canonical location but first see in
354 // the secondary_oat_dex_files_ whether we've looked up this location before.
355 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
356 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
357 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
358 oat_dex_file = secondary_lb->second; // May be nullptr.
359 } else {
360 // We haven't seen this dex_location before, we must check the canonical location.
361 if (UNLIKELY(oat_dex_files_by_canonical_location_.empty())) {
362 // Lazily fill in the oat_dex_files_by_canonical_location_.
363 for (const auto& entry : oat_dex_files_) {
364 const std::string& dex_location = entry.second->GetDexFileLocation();
365 string_cache_.emplace_back(DexFile::GetDexCanonicalLocation(dex_location.c_str()));
366 StringPiece canonical_location_key(string_cache_.back());
367 oat_dex_files_by_canonical_location_.Put(canonical_location_key, entry.second);
368 }
369 }
370 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
371 StringPiece canonical_key(dex_canonical_location);
372 auto canonical_it = oat_dex_files_by_canonical_location_.find(canonical_key);
373 if (canonical_it != oat_dex_files_by_canonical_location_.end()) {
374 oat_dex_file = canonical_it->second;
375 } // else keep nullptr.
376
377 // Copy the key to the string_cache_ and store the result in secondary map.
378 string_cache_.emplace_back(key.data(), key.length());
379 StringPiece key_copy(string_cache_.back());
380 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700381 }
382 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100383 if (oat_dex_file != nullptr &&
384 (dex_location_checksum == nullptr ||
385 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
386 return oat_dex_file;
387 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700388
389 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100390 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800391 std::string checksum("<unspecified>");
392 if (dex_location_checksum != NULL) {
393 checksum = StringPrintf("0x%08x", *dex_location_checksum);
394 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700395 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100396 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800397 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700398 if (kIsDebugBuild) {
399 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
400 LOG(WARNING) << "OatFile " << GetLocation()
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800401 << " contains OatDexFile " << it->second->GetDexFileLocation()
Calin Juravle4e1d5792014-07-15 23:56:47 +0100402 << " (canonical path " << it->first << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800403 << " with checksum 0x" << std::hex << it->second->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700404 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800405 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700406 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100407
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700408 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700409}
410
411std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
412 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700413 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700414 result.push_back(it->second);
415 }
416 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417}
418
419OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800420 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800421 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800422 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800423 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700424 : oat_file_(oat_file),
425 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800426 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800427 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800428 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429
430OatFile::OatDexFile::~OatDexFile() {}
431
Ian Rogers05f28c62012-10-23 18:12:13 -0700432size_t OatFile::OatDexFile::FileSize() const {
433 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
434}
435
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700436const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700437 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700438 dex_file_location_checksum_, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800439}
440
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100441OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800442 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
443
Ian Rogers30fab402012-01-23 15:43:46 -0800444 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700445 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800446
Brian Carlstromba150c32013-08-27 17:31:03 -0700447 const byte* status_pointer = oat_class_pointer;
448 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
449 mirror::Class::Status status =
450 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
451 CHECK_LT(status, mirror::Class::kStatusMax);
452
453 const byte* type_pointer = status_pointer + sizeof(uint16_t);
454 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
455 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
456 CHECK_LT(type, kOatClassMax);
457
Brian Carlstromcd937a92014-03-04 22:53:23 -0800458 const byte* after_type_pointer = type_pointer + sizeof(int16_t);
459 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700460
Brian Carlstromcd937a92014-03-04 22:53:23 -0800461 uint32_t bitmap_size = 0;
462 const byte* bitmap_pointer = nullptr;
463 const byte* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -0700464 if (type != kOatClassNoneCompiled) {
465 if (type == kOatClassSomeCompiled) {
466 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
467 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
468 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
469 methods_pointer = bitmap_pointer + bitmap_size;
470 } else {
471 methods_pointer = after_type_pointer;
472 }
473 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -0800474 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800475
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100476 return OatClass(oat_file_,
477 status,
478 type,
479 bitmap_size,
480 reinterpret_cast<const uint32_t*>(bitmap_pointer),
481 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700482}
483
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800484OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700486 OatClassType type,
487 uint32_t bitmap_size,
488 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800489 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700490 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100491 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700492 switch (type_) {
493 case kOatClassAllCompiled: {
494 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800495 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700496 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700497 break;
498 }
499 case kOatClassSomeCompiled: {
500 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800501 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700502 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700503 break;
504 }
505 case kOatClassNoneCompiled: {
506 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800507 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700508 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700509 break;
510 }
511 case kOatClassMax: {
512 LOG(FATAL) << "Invalid OatClassType " << type_;
513 break;
514 }
515 }
516}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700517
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700518const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100519 // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index.
Brian Carlstromba150c32013-08-27 17:31:03 -0700520 if (methods_pointer_ == NULL) {
521 CHECK_EQ(kOatClassNoneCompiled, type_);
Vladimir Marko7624d252014-05-02 14:40:15 +0100522 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700523 }
524 size_t methods_pointer_index;
525 if (bitmap_ == NULL) {
526 CHECK_EQ(kOatClassAllCompiled, type_);
527 methods_pointer_index = method_index;
528 } else {
529 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100530 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100531 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700532 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100533 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
534 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700535 }
536 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Alex Light9dcc4572014-08-14 14:16:26 -0700537 if (oat_file_->IsExecutable() || Runtime::Current()->IsCompiler()) {
538 return OatMethod(
539 oat_file_->Begin(),
540 oat_method_offsets.code_offset_,
541 oat_method_offsets.gc_map_offset_);
542 } else {
543 // We aren't allowed to use the compiled code. We just force it down the interpreted version.
544 return OatMethod(oat_file_->Begin(), 0, 0);
545 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700546}
547
Logan Chien0c717dd2012-03-28 18:31:07 +0800548
Ian Rogersef7d42f2014-01-06 12:55:46 -0800549uint32_t OatFile::OatMethod::GetQuickCodeSize() const {
550 uintptr_t code = reinterpret_cast<uintptr_t>(GetQuickCode());
Logan Chien971bf3f2012-05-01 15:47:55 +0800551 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800552 return 0;
553 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800554 // TODO: make this Thumb2 specific
555 code &= ~0x1;
556 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800557}
558
Brian Carlstromea46f952013-07-30 01:26:50 -0700559void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700560 CHECK(method != NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800561 method->SetEntryPointFromPortableCompiledCode(GetPortableCode());
562 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700563 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800564}
565
Brian Carlstrome24fa612011-09-29 00:53:55 -0700566} // namespace art