blob: 9cefcb6b1ab193c3abda450467fc069b032858ac [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>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080021
Brian Carlstromba150c32013-08-27 17:31:03 -070022#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080023#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080025#include "elf_file.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070026#include "implicit_check_options.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080027#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);
Ian Rogers700a4022014-05-19 16:49:03 -070048 std::unique_ptr<OatFile> oat_file(new OatFile(location));
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 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070083
84 if (ret.get() == nullptr) {
85 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070087
88 // Embedded options check. Right now only implicit checks.
89 // TODO: Refactor to somewhere else?
90 const char* implicit_checks_value = ret->GetOatHeader().
91 GetStoreValueByKey(ImplicitCheckOptions::kImplicitChecksOatHeaderKey);
92
93 if (implicit_checks_value == nullptr) {
94 *error_msg = "Did not find implicit checks value.";
95 return nullptr;
96 }
97
98 bool explicit_null_checks, explicit_so_checks, explicit_suspend_checks;
99 if (ImplicitCheckOptions::Parse(implicit_checks_value, &explicit_null_checks,
100 &explicit_so_checks, &explicit_suspend_checks)) {
101 // Check whether the runtime agrees with the recorded checks.
102 if (ImplicitCheckOptions::CheckRuntimeSupport(executable, explicit_null_checks,
103 explicit_so_checks, explicit_suspend_checks,
104 error_msg)) {
105 return ret.release();
106 } else {
107 return nullptr;
108 }
109 } else {
110 *error_msg = "Failed parsing implicit check options.";
111 return nullptr;
112 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800113}
114
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700115OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800116 CheckLocation(location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 return OpenElfFile(file, location, NULL, true, false, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800118}
119
120OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
121 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700122 byte* requested_base,
123 std::string* error_msg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700124 std::unique_ptr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700125 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800126 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800128 }
129 return oat_file.release();
130}
131
132OatFile* OatFile::OpenElfFile(File* file,
133 const std::string& location,
134 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700135 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700136 bool executable,
137 std::string* error_msg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700138 std::unique_ptr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700139 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700141 CHECK(!error_msg->empty());
142 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 }
144 return oat_file.release();
145}
146
Logan Chien0c717dd2012-03-28 18:31:07 +0800147OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800148 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800149 CHECK(!location_.empty());
150}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151
152OatFile::~OatFile() {
153 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800154 if (dlopen_handle_ != NULL) {
155 dlclose(dlopen_handle_);
156 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700157}
158
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700159bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
160 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800161 char* absolute_path = realpath(elf_filename.c_str(), NULL);
162 if (absolute_path == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700163 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164 return false;
165 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800166 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
167 free(absolute_path);
168 if (dlopen_handle_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700169 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700170 return false;
171 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800172 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
173 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700174 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
175 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800176 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700179 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
180 "oatdata=%p != expected=%p /proc/self/maps:\n",
181 begin_, requested_base);
182 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800183 return false;
184 }
185 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
186 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700187 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
188 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800189 return false;
190 }
191 // Readjust to be non-inclusive upper bound.
192 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700193 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800194}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700195
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700196bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
197 std::string* error_msg) {
198 elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
199 if (elf_file_.get() == nullptr) {
200 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201 return false;
202 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700203 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800204 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700205 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800206 return false;
207 }
208 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
209 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700210 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800211 return false;
212 }
213 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700214 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
215 "oatdata=%p != expected=%p /proc/self/maps:\n",
216 begin_, requested_base);
217 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800218 return false;
219 }
220 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
221 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700222 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800223 return false;
224 }
225 // Readjust to be non-inclusive upper bound.
226 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700227 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800228}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800229
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230bool OatFile::Setup(std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700231 if (!GetOatHeader().IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700232 *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700233 return false;
234 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800235 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700237 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700238 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700239 return false;
240 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700241
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700242 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700243 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700244 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700246 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700247 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700248 return false;
249 }
250
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800251 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700252 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700253 if (UNLIKELY(dex_file_location_size == 0U)) {
254 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
255 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700256 return false;
257 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700258 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700259 if (UNLIKELY(oat > End())) {
260 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
261 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700262 return false;
263 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264
265 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
266 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 if (UNLIKELY(oat > End())) {
268 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
269 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700270 return false;
271 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700272
273 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
274
275 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
276 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277 if (UNLIKELY(oat > End())) {
278 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
279 "dex file checksum", GetLocation().c_str(), i,
280 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700281 return false;
282 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700283
Brian Carlstrom89521892011-12-07 22:05:07 -0800284 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700285 if (UNLIKELY(dex_file_offset == 0U)) {
286 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
287 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700288 return false;
289 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700290 if (UNLIKELY(dex_file_offset > Size())) {
291 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
292 "offset %ud > %zd", GetLocation().c_str(), i,
293 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700294 return false;
295 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800296 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700297 if (UNLIKELY(oat > End())) {
298 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
299 " after dex file offsets", GetLocation().c_str(), i,
300 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700301 return false;
302 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800303
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800304 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700305 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
306 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
307 " dex file magic '%s'", GetLocation().c_str(), i,
308 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700309 return false;
310 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700311 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
312 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
313 " dex file version '%s'", GetLocation().c_str(), i,
314 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700315 return false;
316 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800317 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
318 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800319
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800320 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700321 if (UNLIKELY(oat > End())) {
322 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
323 " method offsets", GetLocation().c_str(), i,
324 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700325 return false;
326 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700327
Vladimir Marko539690a2014-06-05 18:36:42 +0100328 OatDexFile* oat_dex_file = new OatDexFile(this,
329 dex_file_location,
330 dex_file_checksum,
331 dex_file_pointer,
332 methods_offsets_pointer);
333 // Use a StringPiece backed by the oat_dex_file's internal std::string as the key.
334 StringPiece key(oat_dex_file->GetDexFileLocation());
335 oat_dex_files_.Put(key, oat_dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700336 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700337 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700338}
339
340const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800341 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700342}
343
Ian Rogers30fab402012-01-23 15:43:46 -0800344const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800345 CHECK(begin_ != NULL);
346 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700347}
348
Ian Rogers30fab402012-01-23 15:43:46 -0800349const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800350 CHECK(end_ != NULL);
351 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352}
353
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700354const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
355 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800356 bool warn_if_not_found) const {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700357 Table::const_iterator it = oat_dex_files_.find(dex_location);
358 if (it != oat_dex_files_.end()) {
359 const OatFile::OatDexFile* oat_dex_file = it->second;
360 if (dex_location_checksum == NULL ||
361 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) {
362 return oat_dex_file;
363 }
364 }
365
366 if (warn_if_not_found) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800367 std::string checksum("<unspecified>");
368 if (dex_location_checksum != NULL) {
369 checksum = StringPrintf("0x%08x", *dex_location_checksum);
370 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700371 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800372 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700373 if (kIsDebugBuild) {
374 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
375 LOG(WARNING) << "OatFile " << GetLocation()
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800376 << " contains OatDexFile " << it->second->GetDexFileLocation()
377 << " with checksum 0x" << std::hex << it->second->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700378 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800379 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700380 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700381 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700382}
383
384std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
385 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700386 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700387 result.push_back(it->second);
388 }
389 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700390}
391
392OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800393 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800394 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800395 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800396 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700397 : oat_file_(oat_file),
398 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800399 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800400 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800401 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700402
403OatFile::OatDexFile::~OatDexFile() {}
404
Ian Rogers05f28c62012-10-23 18:12:13 -0700405size_t OatFile::OatDexFile::FileSize() const {
406 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
407}
408
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700409const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700410 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700411 dex_file_location_checksum_, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800412}
413
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100414OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800415 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
416
Ian Rogers30fab402012-01-23 15:43:46 -0800417 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700418 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800419
Brian Carlstromba150c32013-08-27 17:31:03 -0700420 const byte* status_pointer = oat_class_pointer;
421 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
422 mirror::Class::Status status =
423 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
424 CHECK_LT(status, mirror::Class::kStatusMax);
425
426 const byte* type_pointer = status_pointer + sizeof(uint16_t);
427 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
428 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
429 CHECK_LT(type, kOatClassMax);
430
Brian Carlstromcd937a92014-03-04 22:53:23 -0800431 const byte* after_type_pointer = type_pointer + sizeof(int16_t);
432 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700433
Brian Carlstromcd937a92014-03-04 22:53:23 -0800434 uint32_t bitmap_size = 0;
435 const byte* bitmap_pointer = nullptr;
436 const byte* methods_pointer = nullptr;
437 if (type == kOatClassSomeCompiled) {
438 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
439 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
440 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
441 methods_pointer = bitmap_pointer + bitmap_size;
442 } else {
443 methods_pointer = after_type_pointer;
444 }
445 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800446
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100447 return OatClass(oat_file_,
448 status,
449 type,
450 bitmap_size,
451 reinterpret_cast<const uint32_t*>(bitmap_pointer),
452 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700453}
454
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800455OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700457 OatClassType type,
458 uint32_t bitmap_size,
459 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800460 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700461 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100462 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromcd937a92014-03-04 22:53:23 -0800463 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700464 switch (type_) {
465 case kOatClassAllCompiled: {
466 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800467 CHECK(bitmap_pointer == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700468 break;
469 }
470 case kOatClassSomeCompiled: {
471 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800472 CHECK(bitmap_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700473 break;
474 }
475 case kOatClassNoneCompiled: {
476 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800477 CHECK(bitmap_pointer == nullptr);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100478 methods_pointer_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700479 break;
480 }
481 case kOatClassMax: {
482 LOG(FATAL) << "Invalid OatClassType " << type_;
483 break;
484 }
485 }
486}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700487
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700488const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100489 // 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 -0700490 if (methods_pointer_ == NULL) {
491 CHECK_EQ(kOatClassNoneCompiled, type_);
Vladimir Marko7624d252014-05-02 14:40:15 +0100492 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700493 }
494 size_t methods_pointer_index;
495 if (bitmap_ == NULL) {
496 CHECK_EQ(kOatClassAllCompiled, type_);
497 methods_pointer_index = method_index;
498 } else {
499 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100500 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100501 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700502 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100503 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
504 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700505 }
506 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800508 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800509 oat_method_offsets.code_offset_,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700510 oat_method_offsets.gc_map_offset_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700511}
512
Brian Carlstromae826982011-11-09 01:33:42 -0800513OatFile::OatMethod::OatMethod(const byte* base,
514 const uint32_t code_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700515 const uint32_t gc_map_offset)
Ian Rogers30fab402012-01-23 15:43:46 -0800516 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800517 code_offset_(code_offset),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700518 native_gc_map_offset_(gc_map_offset) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700519}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700520
521OatFile::OatMethod::~OatMethod() {}
522
Logan Chien0c717dd2012-03-28 18:31:07 +0800523
Ian Rogersef7d42f2014-01-06 12:55:46 -0800524uint32_t OatFile::OatMethod::GetQuickCodeSize() const {
525 uintptr_t code = reinterpret_cast<uintptr_t>(GetQuickCode());
Logan Chien971bf3f2012-05-01 15:47:55 +0800526 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800527 return 0;
528 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800529 // TODO: make this Thumb2 specific
530 code &= ~0x1;
531 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800532}
533
Brian Carlstromea46f952013-07-30 01:26:50 -0700534void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700535 CHECK(method != NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800536 method->SetEntryPointFromPortableCompiledCode(GetPortableCode());
537 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700538 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800539}
540
Brian Carlstrome24fa612011-09-29 00:53:55 -0700541} // namespace art