blob: 46a4d537eef06cd18f6c62eb44da4c94f9b1c92e [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>
20
Brian Carlstromba150c32013-08-27 17:31:03 -070021#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080022#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024#include "elf_file.h"
25#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_method.h"
27#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030#include "os.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070032#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033
34namespace art {
35
Brian Carlstrom700c8d32012-11-05 10:42:02 -080036void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070037 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038}
39
Brian Carlstrom265091e2013-01-30 14:08:26 -080040OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070041 const std::string& location,
42 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043 CHECK(!oat_contents.empty()) << location;
44 CheckLocation(location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080045 UniquePtr<OatFile> oat_file(new OatFile(location));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046 oat_file->begin_ = &oat_contents[0];
47 oat_file->end_ = &oat_contents[oat_contents.size()];
Ian Rogers8d31bbd2013-10-13 10:44:14 -070048 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049}
50
51OatFile* OatFile::Open(const std::string& filename,
52 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070053 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070054 bool executable,
55 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056 CHECK(!filename.empty()) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070057 CheckLocation(filename);
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 if (kUsePortableCompiler) {
59 // If we are using PORTABLE, use dlopen to deal with relocations.
60 //
61 // We use our own ELF loader for Quick to deal with legacy apps that
62 // open a generated dex file by name, remove the file, then open
63 // another generated dex file with the same name. http://b/10614658
64 if (executable) {
65 return OpenDlopen(filename, location, requested_base, error_msg);
66 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -080067 }
Brian Carlstromf1d34552013-07-12 20:22:23 -070068 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
69 //
70 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
71 //
Brian Carlstrom265091e2013-01-30 14:08:26 -080072 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
73 // This won't work for portable runtime execution because it doesn't process relocations.
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070074 UniquePtr<File> file(OS::OpenFileForReading(filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080075 if (file.get() == NULL) {
Brian Carlstrom6449c622014-02-10 23:48:36 -080076 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080077 return NULL;
78 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070079 return OpenElfFile(file.get(), location, requested_base, false, executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080080}
81
Ian Rogers8d31bbd2013-10-13 10:44:14 -070082OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080083 CheckLocation(location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070084 return OpenElfFile(file, location, NULL, true, false, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085}
86
87OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
88 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070089 byte* requested_base,
90 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080091 UniquePtr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080093 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070094 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080095 }
96 return oat_file.release();
97}
98
99OatFile* OatFile::OpenElfFile(File* file,
100 const std::string& location,
101 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700102 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 bool executable,
104 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800105 UniquePtr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700107 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700108 CHECK(!error_msg->empty());
109 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110 }
111 return oat_file.release();
112}
113
Logan Chien0c717dd2012-03-28 18:31:07 +0800114OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800115 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800116 CHECK(!location_.empty());
117}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118
119OatFile::~OatFile() {
120 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800121 if (dlopen_handle_ != NULL) {
122 dlclose(dlopen_handle_);
123 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124}
125
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700126bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
127 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800128 char* absolute_path = realpath(elf_filename.c_str(), NULL);
129 if (absolute_path == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700130 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 return false;
132 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800133 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
134 free(absolute_path);
135 if (dlopen_handle_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700136 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 return false;
138 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800139 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
140 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700141 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
142 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800145 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700146 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
147 "oatdata=%p != expected=%p /proc/self/maps:\n",
148 begin_, requested_base);
149 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800150 return false;
151 }
152 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
153 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700154 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
155 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 return false;
157 }
158 // Readjust to be non-inclusive upper bound.
159 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700160 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800161}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700163bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
164 std::string* error_msg) {
165 elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
166 if (elf_file_.get() == nullptr) {
167 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800168 return false;
169 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700170 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800171 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700172 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800173 return false;
174 }
175 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
176 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700177 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178 return false;
179 }
180 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700181 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
182 "oatdata=%p != expected=%p /proc/self/maps:\n",
183 begin_, requested_base);
184 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800185 return false;
186 }
187 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
188 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700189 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800190 return false;
191 }
192 // Readjust to be non-inclusive upper bound.
193 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700194 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800195}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800196
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700197bool OatFile::Setup(std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700198 if (!GetOatHeader().IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700199 *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700200 return false;
201 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800202 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700203 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700204 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700205 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700206 return false;
207 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700208
Brian Carlstromfb331d72013-07-25 22:00:16 -0700209 oat += GetOatHeader().GetImageFileLocationSize();
210 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700211 *error_msg = StringPrintf("In oat file '%s' found truncated image file location: "
212 "%p + %zd + %ud <= %p", GetLocation().c_str(),
213 Begin(), sizeof(OatHeader), GetOatHeader().GetImageFileLocationSize(),
214 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700215 return false;
216 }
217
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800218 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700219 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700220 if (UNLIKELY(dex_file_location_size == 0U)) {
221 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
222 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700223 return false;
224 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700226 if (UNLIKELY(oat > End())) {
227 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
228 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700229 return false;
230 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700231
232 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
233 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700234 if (UNLIKELY(oat > End())) {
235 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
236 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700237 return false;
238 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239
240 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
241
242 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
243 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700244 if (UNLIKELY(oat > End())) {
245 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
246 "dex file checksum", GetLocation().c_str(), i,
247 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700248 return false;
249 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250
Brian Carlstrom89521892011-12-07 22:05:07 -0800251 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700252 if (UNLIKELY(dex_file_offset == 0U)) {
253 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
254 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700255 return false;
256 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700257 if (UNLIKELY(dex_file_offset > Size())) {
258 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
259 "offset %ud > %zd", GetLocation().c_str(), i,
260 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700261 return false;
262 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800263 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700264 if (UNLIKELY(oat > End())) {
265 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
266 " after dex file offsets", GetLocation().c_str(), i,
267 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700268 return false;
269 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800270
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800271 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700272 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
273 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
274 " dex file magic '%s'", GetLocation().c_str(), i,
275 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700276 return false;
277 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700278 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
279 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
280 " dex file version '%s'", GetLocation().c_str(), i,
281 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700282 return false;
283 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800284 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
285 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800286
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800287 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
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' with truncated "
290 " method offsets", GetLocation().c_str(), i,
291 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700292 return false;
293 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294
Elliott Hughesa0e18062012-04-13 15:59:59 -0700295 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
296 dex_file_location,
297 dex_file_checksum,
298 dex_file_pointer,
299 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700301 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700302}
303
304const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800305 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700306}
307
Ian Rogers30fab402012-01-23 15:43:46 -0800308const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800309 CHECK(begin_ != NULL);
310 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700311}
312
Ian Rogers30fab402012-01-23 15:43:46 -0800313const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800314 CHECK(end_ != NULL);
315 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700316}
317
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700318const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
319 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800320 bool warn_if_not_found) const {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700321 Table::const_iterator it = oat_dex_files_.find(dex_location);
322 if (it != oat_dex_files_.end()) {
323 const OatFile::OatDexFile* oat_dex_file = it->second;
324 if (dex_location_checksum == NULL ||
325 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) {
326 return oat_dex_file;
327 }
328 }
329
330 if (warn_if_not_found) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800331 std::string checksum("<unspecified>");
332 if (dex_location_checksum != NULL) {
333 checksum = StringPrintf("0x%08x", *dex_location_checksum);
334 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700335 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800336 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700337 if (kIsDebugBuild) {
338 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
339 LOG(WARNING) << "OatFile " << GetLocation()
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800340 << " contains OatDexFile " << it->second->GetDexFileLocation()
341 << " with checksum 0x" << std::hex << it->second->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700342 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800343 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700344 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700345 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700346}
347
348std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
349 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700350 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700351 result.push_back(it->second);
352 }
353 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354}
355
356OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800357 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800358 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800359 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800360 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700361 : oat_file_(oat_file),
362 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800363 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800364 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800365 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700366
367OatFile::OatDexFile::~OatDexFile() {}
368
Ian Rogers05f28c62012-10-23 18:12:13 -0700369size_t OatFile::OatDexFile::FileSize() const {
370 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
371}
372
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700373const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700374 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700375 dex_file_location_checksum_, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800376}
377
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100378OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800379 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
380
Ian Rogers30fab402012-01-23 15:43:46 -0800381 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700382 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800383
Brian Carlstromba150c32013-08-27 17:31:03 -0700384 const byte* status_pointer = oat_class_pointer;
385 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
386 mirror::Class::Status status =
387 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
388 CHECK_LT(status, mirror::Class::kStatusMax);
389
390 const byte* type_pointer = status_pointer + sizeof(uint16_t);
391 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
392 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
393 CHECK_LT(type, kOatClassMax);
394
Brian Carlstromcd937a92014-03-04 22:53:23 -0800395 const byte* after_type_pointer = type_pointer + sizeof(int16_t);
396 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700397
Brian Carlstromcd937a92014-03-04 22:53:23 -0800398 uint32_t bitmap_size = 0;
399 const byte* bitmap_pointer = nullptr;
400 const byte* methods_pointer = nullptr;
401 if (type == kOatClassSomeCompiled) {
402 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
403 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
404 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
405 methods_pointer = bitmap_pointer + bitmap_size;
406 } else {
407 methods_pointer = after_type_pointer;
408 }
409 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800410
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100411 return OatClass(oat_file_,
412 status,
413 type,
414 bitmap_size,
415 reinterpret_cast<const uint32_t*>(bitmap_pointer),
416 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417}
418
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800419OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700421 OatClassType type,
422 uint32_t bitmap_size,
423 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800424 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700425 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100426 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromcd937a92014-03-04 22:53:23 -0800427 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700428 switch (type_) {
429 case kOatClassAllCompiled: {
430 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800431 CHECK(bitmap_pointer == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700432 break;
433 }
434 case kOatClassSomeCompiled: {
435 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800436 CHECK(bitmap_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700437 break;
438 }
439 case kOatClassNoneCompiled: {
440 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800441 CHECK(bitmap_pointer == nullptr);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100442 methods_pointer_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700443 break;
444 }
445 case kOatClassMax: {
446 LOG(FATAL) << "Invalid OatClassType " << type_;
447 break;
448 }
449 }
450}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700451
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700452const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100453 // 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 -0700454 if (methods_pointer_ == NULL) {
455 CHECK_EQ(kOatClassNoneCompiled, type_);
Vladimir Marko7624d252014-05-02 14:40:15 +0100456 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700457 }
458 size_t methods_pointer_index;
459 if (bitmap_ == NULL) {
460 CHECK_EQ(kOatClassAllCompiled, type_);
461 methods_pointer_index = method_index;
462 } else {
463 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100464 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100465 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700466 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100467 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
468 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700469 }
470 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700471 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800472 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800473 oat_method_offsets.code_offset_,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700474 oat_method_offsets.gc_map_offset_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475}
476
Brian Carlstromae826982011-11-09 01:33:42 -0800477OatFile::OatMethod::OatMethod(const byte* base,
478 const uint32_t code_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700479 const uint32_t gc_map_offset)
Ian Rogers30fab402012-01-23 15:43:46 -0800480 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800481 code_offset_(code_offset),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700482 native_gc_map_offset_(gc_map_offset) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700483}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700484
485OatFile::OatMethod::~OatMethod() {}
486
Logan Chien0c717dd2012-03-28 18:31:07 +0800487
Ian Rogersef7d42f2014-01-06 12:55:46 -0800488uint32_t OatFile::OatMethod::GetQuickCodeSize() const {
489 uintptr_t code = reinterpret_cast<uintptr_t>(GetQuickCode());
Logan Chien971bf3f2012-05-01 15:47:55 +0800490 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800491 return 0;
492 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800493 // TODO: make this Thumb2 specific
494 code &= ~0x1;
495 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800496}
497
Brian Carlstromea46f952013-07-30 01:26:50 -0700498void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700499 CHECK(method != NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800500 method->SetEntryPointFromPortableCompiledCode(GetPortableCode());
501 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700502 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800503}
504
Brian Carlstrome24fa612011-09-29 00:53:55 -0700505} // namespace art