blob: 86c1baef2743ffa355122ca5a3a31f37e073fc21 [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"
26#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method.h"
28#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070032#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070034#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035
36namespace art {
37
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070039 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040}
41
Brian Carlstrom265091e2013-01-30 14:08:26 -080042OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070043 const std::string& location,
44 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045 CHECK(!oat_contents.empty()) << location;
46 CheckLocation(location);
Ian Rogers700a4022014-05-19 16:49:03 -070047 std::unique_ptr<OatFile> oat_file(new OatFile(location));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080048 oat_file->begin_ = &oat_contents[0];
49 oat_file->end_ = &oat_contents[oat_contents.size()];
Ian Rogers8d31bbd2013-10-13 10:44:14 -070050 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051}
52
53OatFile* OatFile::Open(const std::string& filename,
54 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070055 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070056 bool executable,
57 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058 CHECK(!filename.empty()) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070059 CheckLocation(filename);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070060 std::unique_ptr<OatFile> ret;
61 if (kUsePortableCompiler && executable) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080062 // If we are using PORTABLE, use dlopen to deal with relocations.
63 //
64 // We use our own ELF loader for Quick to deal with legacy apps that
65 // open a generated dex file by name, remove the file, then open
66 // another generated dex file with the same name. http://b/10614658
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070067 ret.reset(OpenDlopen(filename, location, requested_base, error_msg));
68 } else {
69 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
70 //
71 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
72 //
73 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
74 // This won't work for portable runtime execution because it doesn't process relocations.
75 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
76 if (file.get() == NULL) {
77 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
78 return nullptr;
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070080 ret.reset(OpenElfFile(file.get(), location, requested_base, false, executable, error_msg));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080081 }
Dave Allison69dfe512014-07-11 17:11:58 +000082 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080083}
84
Ian Rogers8d31bbd2013-10-13 10:44:14 -070085OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086 CheckLocation(location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070087 return OpenElfFile(file, location, NULL, true, false, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080088}
89
90OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
91 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 byte* requested_base,
93 std::string* error_msg) {
Ian Rogers700a4022014-05-19 16:49:03 -070094 std::unique_ptr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070095 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080096 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070097 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080098 }
99 return oat_file.release();
100}
101
102OatFile* OatFile::OpenElfFile(File* file,
103 const std::string& location,
104 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700105 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106 bool executable,
107 std::string* error_msg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700108 std::unique_ptr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700109 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700111 CHECK(!error_msg->empty());
112 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700113 }
114 return oat_file.release();
115}
116
Logan Chien0c717dd2012-03-28 18:31:07 +0800117OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800118 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800119 CHECK(!location_.empty());
120}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121
122OatFile::~OatFile() {
123 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800124 if (dlopen_handle_ != NULL) {
125 dlclose(dlopen_handle_);
126 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127}
128
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700129bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
130 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800131 char* absolute_path = realpath(elf_filename.c_str(), NULL);
132 if (absolute_path == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700133 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700134 return false;
135 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800136 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
137 free(absolute_path);
138 if (dlopen_handle_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700139 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 return false;
141 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800142 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
143 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700144 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
145 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800146 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800148 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700149 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
150 "oatdata=%p != expected=%p /proc/self/maps:\n",
151 begin_, requested_base);
152 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800153 return false;
154 }
155 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
156 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700157 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
158 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 return false;
160 }
161 // Readjust to be non-inclusive upper bound.
162 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700163 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800164}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700166bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
167 std::string* error_msg) {
168 elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
169 if (elf_file_.get() == nullptr) {
170 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800171 return false;
172 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700173 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800174 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700175 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800176 return false;
177 }
178 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
179 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700180 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181 return false;
182 }
183 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700184 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
185 "oatdata=%p != expected=%p /proc/self/maps:\n",
186 begin_, requested_base);
187 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800188 return false;
189 }
190 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
191 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700192 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800193 return false;
194 }
195 // Readjust to be non-inclusive upper bound.
196 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700197 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800198}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800199
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200bool OatFile::Setup(std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700201 if (!GetOatHeader().IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700202 *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700203 return false;
204 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800205 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700207 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700209 return false;
210 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700211
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700212 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700213 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700214 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700216 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700217 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700218 return false;
219 }
220
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800221 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700222 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700223 if (UNLIKELY(dex_file_location_size == 0U)) {
224 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
225 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700226 return false;
227 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700229 if (UNLIKELY(oat > End())) {
230 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
231 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700232 return false;
233 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234
235 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
236 oat += 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 with truncated dex file "
239 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700240 return false;
241 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700242
243 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
244
245 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
246 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700247 if (UNLIKELY(oat > End())) {
248 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
249 "dex file checksum", GetLocation().c_str(), i,
250 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700251 return false;
252 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700253
Brian Carlstrom89521892011-12-07 22:05:07 -0800254 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700255 if (UNLIKELY(dex_file_offset == 0U)) {
256 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
257 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700258 return false;
259 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700260 if (UNLIKELY(dex_file_offset > Size())) {
261 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
262 "offset %ud > %zd", GetLocation().c_str(), i,
263 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700264 return false;
265 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800266 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 if (UNLIKELY(oat > End())) {
268 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
269 " after dex file offsets", GetLocation().c_str(), i,
270 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700271 return false;
272 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800273
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800274 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700275 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
276 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
277 " dex file magic '%s'", GetLocation().c_str(), i,
278 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700279 return false;
280 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
282 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
283 " dex file version '%s'", GetLocation().c_str(), i,
284 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700285 return false;
286 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800287 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
288 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800289
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800290 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700291 if (UNLIKELY(oat > End())) {
292 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
293 " method offsets", GetLocation().c_str(), i,
294 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700295 return false;
296 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297
Vladimir Marko539690a2014-06-05 18:36:42 +0100298 OatDexFile* oat_dex_file = new OatDexFile(this,
299 dex_file_location,
300 dex_file_checksum,
301 dex_file_pointer,
302 methods_offsets_pointer);
303 // Use a StringPiece backed by the oat_dex_file's internal std::string as the key.
304 StringPiece key(oat_dex_file->GetDexFileLocation());
305 oat_dex_files_.Put(key, oat_dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700306 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700307 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700308}
309
310const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800311 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700312}
313
Ian Rogers30fab402012-01-23 15:43:46 -0800314const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800315 CHECK(begin_ != NULL);
316 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700317}
318
Ian Rogers30fab402012-01-23 15:43:46 -0800319const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800320 CHECK(end_ != NULL);
321 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700322}
323
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700324const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
325 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800326 bool warn_if_not_found) const {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700327 Table::const_iterator it = oat_dex_files_.find(dex_location);
328 if (it != oat_dex_files_.end()) {
329 const OatFile::OatDexFile* oat_dex_file = it->second;
330 if (dex_location_checksum == NULL ||
331 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) {
332 return oat_dex_file;
333 }
334 }
335
336 if (warn_if_not_found) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800337 std::string checksum("<unspecified>");
338 if (dex_location_checksum != NULL) {
339 checksum = StringPrintf("0x%08x", *dex_location_checksum);
340 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700341 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800342 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700343 if (kIsDebugBuild) {
344 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
345 LOG(WARNING) << "OatFile " << GetLocation()
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800346 << " contains OatDexFile " << it->second->GetDexFileLocation()
347 << " with checksum 0x" << std::hex << it->second->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700348 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800349 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700350 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700351 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700352}
353
354std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
355 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700356 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700357 result.push_back(it->second);
358 }
359 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700360}
361
362OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800363 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800364 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800365 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800366 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 : oat_file_(oat_file),
368 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800369 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800370 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800371 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372
373OatFile::OatDexFile::~OatDexFile() {}
374
Ian Rogers05f28c62012-10-23 18:12:13 -0700375size_t OatFile::OatDexFile::FileSize() const {
376 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
377}
378
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700379const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700380 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700381 dex_file_location_checksum_, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800382}
383
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100384OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800385 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
386
Ian Rogers30fab402012-01-23 15:43:46 -0800387 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700388 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800389
Brian Carlstromba150c32013-08-27 17:31:03 -0700390 const byte* status_pointer = oat_class_pointer;
391 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
392 mirror::Class::Status status =
393 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
394 CHECK_LT(status, mirror::Class::kStatusMax);
395
396 const byte* type_pointer = status_pointer + sizeof(uint16_t);
397 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
398 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
399 CHECK_LT(type, kOatClassMax);
400
Brian Carlstromcd937a92014-03-04 22:53:23 -0800401 const byte* after_type_pointer = type_pointer + sizeof(int16_t);
402 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700403
Brian Carlstromcd937a92014-03-04 22:53:23 -0800404 uint32_t bitmap_size = 0;
405 const byte* bitmap_pointer = nullptr;
406 const byte* methods_pointer = nullptr;
407 if (type == kOatClassSomeCompiled) {
408 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
409 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
410 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
411 methods_pointer = bitmap_pointer + bitmap_size;
412 } else {
413 methods_pointer = after_type_pointer;
414 }
415 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800416
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100417 return OatClass(oat_file_,
418 status,
419 type,
420 bitmap_size,
421 reinterpret_cast<const uint32_t*>(bitmap_pointer),
422 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700423}
424
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800425OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700427 OatClassType type,
428 uint32_t bitmap_size,
429 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800430 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700431 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100432 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromcd937a92014-03-04 22:53:23 -0800433 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700434 switch (type_) {
435 case kOatClassAllCompiled: {
436 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800437 CHECK(bitmap_pointer == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700438 break;
439 }
440 case kOatClassSomeCompiled: {
441 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800442 CHECK(bitmap_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700443 break;
444 }
445 case kOatClassNoneCompiled: {
446 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800447 CHECK(bitmap_pointer == nullptr);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100448 methods_pointer_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700449 break;
450 }
451 case kOatClassMax: {
452 LOG(FATAL) << "Invalid OatClassType " << type_;
453 break;
454 }
455 }
456}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700457
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700458const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100459 // 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 -0700460 if (methods_pointer_ == NULL) {
461 CHECK_EQ(kOatClassNoneCompiled, type_);
Vladimir Marko7624d252014-05-02 14:40:15 +0100462 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700463 }
464 size_t methods_pointer_index;
465 if (bitmap_ == NULL) {
466 CHECK_EQ(kOatClassAllCompiled, type_);
467 methods_pointer_index = method_index;
468 } else {
469 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100470 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100471 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700472 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100473 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
474 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700475 }
476 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800478 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800479 oat_method_offsets.code_offset_,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700480 oat_method_offsets.gc_map_offset_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700481}
482
Brian Carlstromae826982011-11-09 01:33:42 -0800483OatFile::OatMethod::OatMethod(const byte* base,
484 const uint32_t code_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700485 const uint32_t gc_map_offset)
Ian Rogers30fab402012-01-23 15:43:46 -0800486 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800487 code_offset_(code_offset),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700488 native_gc_map_offset_(gc_map_offset) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700489}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700490
491OatFile::OatMethod::~OatMethod() {}
492
Logan Chien0c717dd2012-03-28 18:31:07 +0800493
Ian Rogersef7d42f2014-01-06 12:55:46 -0800494uint32_t OatFile::OatMethod::GetQuickCodeSize() const {
495 uintptr_t code = reinterpret_cast<uintptr_t>(GetQuickCode());
Logan Chien971bf3f2012-05-01 15:47:55 +0800496 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800497 return 0;
498 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800499 // TODO: make this Thumb2 specific
500 code &= ~0x1;
501 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800502}
503
Brian Carlstromea46f952013-07-30 01:26:50 -0700504void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700505 CHECK(method != NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800506 method->SetEntryPointFromPortableCompiledCode(GetPortableCode());
507 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700508 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800509}
510
Brian Carlstrome24fa612011-09-29 00:53:55 -0700511} // namespace art