blob: cf1c6e1918fb9dfa0a6f7cfe05cc1e5474cd4903 [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"
Alex Light84d76052014-08-22 17:49:35 -070027#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070029#include "mirror/art_method.h"
30#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070034#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070036#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037
38namespace art {
39
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070041 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042}
43
Alex Light84d76052014-08-22 17:49:35 -070044OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
45 const std::string& location,
46 std::string* error_msg) {
47 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
48 oat_file->elf_file_.reset(elf_file);
49 Elf32_Shdr* hdr = elf_file->FindSectionByName(".rodata");
50 oat_file->begin_ = elf_file->Begin() + hdr->sh_offset;
51 oat_file->end_ = elf_file->Begin() + hdr->sh_size + hdr->sh_offset;
52 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
53}
54
Brian Carlstrom265091e2013-01-30 14:08:26 -080055OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070056 const std::string& location,
57 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058 CHECK(!oat_contents.empty()) << location;
59 CheckLocation(location);
Alex Light9dcc4572014-08-14 14:16:26 -070060 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061 oat_file->begin_ = &oat_contents[0];
62 oat_file->end_ = &oat_contents[oat_contents.size()];
Ian Rogers8d31bbd2013-10-13 10:44:14 -070063 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080064}
65
66OatFile* OatFile::Open(const std::string& filename,
67 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070068 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070069 bool executable,
70 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080071 CHECK(!filename.empty()) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070072 CheckLocation(filename);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070073 std::unique_ptr<OatFile> ret;
74 if (kUsePortableCompiler && executable) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 // If we are using PORTABLE, use dlopen to deal with relocations.
76 //
77 // We use our own ELF loader for Quick to deal with legacy apps that
78 // open a generated dex file by name, remove the file, then open
79 // another generated dex file with the same name. http://b/10614658
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070080 ret.reset(OpenDlopen(filename, location, requested_base, error_msg));
81 } else {
82 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
83 //
84 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
85 //
86 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
87 // This won't work for portable runtime execution because it doesn't process relocations.
88 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
89 if (file.get() == NULL) {
90 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
91 return nullptr;
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070093 ret.reset(OpenElfFile(file.get(), location, requested_base, false, executable, error_msg));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080094 }
Dave Allison69dfe512014-07-11 17:11:58 +000095 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080096}
97
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080099 CheckLocation(location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700100 return OpenElfFile(file, location, NULL, true, false, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800101}
102
Alex Lighta59dd802014-07-02 16:28:08 -0700103OatFile* OatFile::OpenReadable(File* file, const std::string& location, std::string* error_msg) {
104 CheckLocation(location);
105 return OpenElfFile(file, location, NULL, false, false, error_msg);
106}
107
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800108OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
109 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700110 byte* requested_base,
111 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700112 std::unique_ptr<OatFile> oat_file(new OatFile(location, true));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700113 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800114 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700115 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800116 }
117 return oat_file.release();
118}
119
120OatFile* OatFile::OpenElfFile(File* file,
121 const std::string& location,
122 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700123 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700124 bool executable,
125 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700126 std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700129 CHECK(!error_msg->empty());
130 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 }
132 return oat_file.release();
133}
134
Alex Light9dcc4572014-08-14 14:16:26 -0700135OatFile::OatFile(const std::string& location, bool is_executable)
136 : location_(location), begin_(NULL), end_(NULL), is_executable_(is_executable),
137 dlopen_handle_(NULL),
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100138 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800139 CHECK(!location_.empty());
140}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700141
142OatFile::~OatFile() {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100143 STLDeleteElements(&oat_dex_files_storage_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800144 if (dlopen_handle_ != NULL) {
145 dlclose(dlopen_handle_);
146 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147}
148
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700149bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
150 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800151 char* absolute_path = realpath(elf_filename.c_str(), NULL);
152 if (absolute_path == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700153 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700154 return false;
155 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
157 free(absolute_path);
158 if (dlopen_handle_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700159 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700160 return false;
161 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800162 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
163 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700164 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
165 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800166 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800168 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700169 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
170 "oatdata=%p != expected=%p /proc/self/maps:\n",
171 begin_, requested_base);
172 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800173 return false;
174 }
175 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
176 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700177 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
178 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800179 return false;
180 }
181 // Readjust to be non-inclusive upper bound.
182 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700183 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800184}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700185
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700186bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
187 std::string* error_msg) {
188 elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
189 if (elf_file_.get() == nullptr) {
190 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800191 return false;
192 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700193 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800194 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700195 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800196 return false;
197 }
198 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
199 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201 return false;
202 }
203 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700204 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
205 "oatdata=%p != expected=%p /proc/self/maps:\n",
206 begin_, requested_base);
207 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800208 return false;
209 }
210 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
211 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700212 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800213 return false;
214 }
215 // Readjust to be non-inclusive upper bound.
216 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700217 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800218}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800219
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700220bool OatFile::Setup(std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700221 if (!GetOatHeader().IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700222 *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700223 return false;
224 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800225 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700227 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700228 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700229 return false;
230 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700231
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700232 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700233 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700234 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700235 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700236 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700237 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700238 return false;
239 }
240
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100241 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
242 oat_dex_files_storage_.reserve(dex_file_count);
243 for (size_t i = 0; i < dex_file_count; i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700244 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245 if (UNLIKELY(dex_file_location_size == 0U)) {
246 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
247 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700248 return false;
249 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700251 if (UNLIKELY(oat > End())) {
252 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
253 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700254 return false;
255 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700256
257 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
258 oat += 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 with truncated dex file "
261 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700262 return false;
263 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264
265 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
266
267 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
268 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700269 if (UNLIKELY(oat > End())) {
270 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
271 "dex file checksum", GetLocation().c_str(), i,
272 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700273 return false;
274 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275
Brian Carlstrom89521892011-12-07 22:05:07 -0800276 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277 if (UNLIKELY(dex_file_offset == 0U)) {
278 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
279 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700280 return false;
281 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700282 if (UNLIKELY(dex_file_offset > Size())) {
283 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
284 "offset %ud > %zd", GetLocation().c_str(), i,
285 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700286 return false;
287 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800288 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700289 if (UNLIKELY(oat > End())) {
290 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
291 " after dex file offsets", GetLocation().c_str(), i,
292 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700293 return false;
294 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800295
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800296 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700297 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
298 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
299 " dex file magic '%s'", GetLocation().c_str(), i,
300 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700301 return false;
302 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700303 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
304 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
305 " dex file version '%s'", GetLocation().c_str(), i,
306 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700307 return false;
308 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800309 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
310 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800311
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800312 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700313 if (UNLIKELY(oat > End())) {
314 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
315 " method offsets", GetLocation().c_str(), i,
316 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700317 return false;
318 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700319
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100320 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
321
322 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100323 OatDexFile* oat_dex_file = new OatDexFile(this,
324 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100325 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100326 dex_file_checksum,
327 dex_file_pointer,
328 methods_offsets_pointer);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100329 oat_dex_files_storage_.push_back(oat_dex_file);
330
331 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100332 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100333 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100334 if (canonical_location != dex_file_location) {
335 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
336 oat_dex_files_.Put(canonical_key, oat_dex_file);
337 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700338 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700339 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700340}
341
342const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800343 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700344}
345
Ian Rogers30fab402012-01-23 15:43:46 -0800346const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800347 CHECK(begin_ != NULL);
348 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700349}
350
Ian Rogers30fab402012-01-23 15:43:46 -0800351const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800352 CHECK(end_ != NULL);
353 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354}
355
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700356const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
357 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800358 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100359 // NOTE: We assume here that the canonical location for a given dex_location never
360 // changes. If it does (i.e. some symlink used by the filename changes) we may return
361 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
362 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100363
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100364 // TODO: Additional analysis of usage patterns to see if this can be simplified
365 // without any performance loss, for example by not doing the first lock-free lookup.
366
367 const OatFile::OatDexFile* oat_dex_file = nullptr;
368 StringPiece key(dex_location);
369 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
370 // directly mentioned in the oat file and doesn't require locking.
371 auto primary_it = oat_dex_files_.find(key);
372 if (primary_it != oat_dex_files_.end()) {
373 oat_dex_file = primary_it->second;
374 DCHECK(oat_dex_file != nullptr);
375 } else {
376 // This dex_location is not one of the dex locations directly mentioned in the
377 // oat file. The correct lookup is via the canonical location but first see in
378 // the secondary_oat_dex_files_ whether we've looked up this location before.
379 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
380 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
381 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
382 oat_dex_file = secondary_lb->second; // May be nullptr.
383 } else {
384 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100385 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100386 if (dex_canonical_location != dex_location) {
387 StringPiece canonical_key(dex_canonical_location);
388 auto canonical_it = oat_dex_files_.find(canonical_key);
389 if (canonical_it != oat_dex_files_.end()) {
390 oat_dex_file = canonical_it->second;
391 } // else keep nullptr.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100392 } // else keep nullptr.
393
394 // Copy the key to the string_cache_ and store the result in secondary map.
395 string_cache_.emplace_back(key.data(), key.length());
396 StringPiece key_copy(string_cache_.back());
397 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700398 }
399 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100400 if (oat_dex_file != nullptr &&
401 (dex_location_checksum == nullptr ||
402 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
403 return oat_dex_file;
404 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700405
406 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100407 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800408 std::string checksum("<unspecified>");
409 if (dex_location_checksum != NULL) {
410 checksum = StringPrintf("0x%08x", *dex_location_checksum);
411 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700412 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100413 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800414 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700415 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100416 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700417 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100418 << " contains OatDexFile " << odf->GetDexFileLocation()
419 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
420 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700421 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800422 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700423 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100424
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700425 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700426}
427
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800429 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100430 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800431 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800432 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800433 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700434 : oat_file_(oat_file),
435 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100436 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800437 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800438 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800439 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440
441OatFile::OatDexFile::~OatDexFile() {}
442
Ian Rogers05f28c62012-10-23 18:12:13 -0700443size_t OatFile::OatDexFile::FileSize() const {
444 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
445}
446
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700447const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700448 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700449 dex_file_location_checksum_, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800450}
451
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100452OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800453 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
454
Ian Rogers30fab402012-01-23 15:43:46 -0800455 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700456 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800457
Brian Carlstromba150c32013-08-27 17:31:03 -0700458 const byte* status_pointer = oat_class_pointer;
459 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
460 mirror::Class::Status status =
461 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
462 CHECK_LT(status, mirror::Class::kStatusMax);
463
464 const byte* type_pointer = status_pointer + sizeof(uint16_t);
465 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
466 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
467 CHECK_LT(type, kOatClassMax);
468
Brian Carlstromcd937a92014-03-04 22:53:23 -0800469 const byte* after_type_pointer = type_pointer + sizeof(int16_t);
470 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700471
Brian Carlstromcd937a92014-03-04 22:53:23 -0800472 uint32_t bitmap_size = 0;
473 const byte* bitmap_pointer = nullptr;
474 const byte* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -0700475 if (type != kOatClassNoneCompiled) {
476 if (type == kOatClassSomeCompiled) {
477 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
478 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
479 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
480 methods_pointer = bitmap_pointer + bitmap_size;
481 } else {
482 methods_pointer = after_type_pointer;
483 }
484 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -0800485 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800486
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100487 return OatClass(oat_file_,
488 status,
489 type,
490 bitmap_size,
491 reinterpret_cast<const uint32_t*>(bitmap_pointer),
492 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700493}
494
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800495OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800496 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700497 OatClassType type,
498 uint32_t bitmap_size,
499 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800500 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700501 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100502 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700503 switch (type_) {
504 case kOatClassAllCompiled: {
505 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800506 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700507 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700508 break;
509 }
510 case kOatClassSomeCompiled: {
511 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800512 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700513 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700514 break;
515 }
516 case kOatClassNoneCompiled: {
517 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800518 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700519 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700520 break;
521 }
522 case kOatClassMax: {
523 LOG(FATAL) << "Invalid OatClassType " << type_;
524 break;
525 }
526 }
527}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700528
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700529const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100530 // 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 -0700531 if (methods_pointer_ == NULL) {
532 CHECK_EQ(kOatClassNoneCompiled, type_);
Vladimir Marko7624d252014-05-02 14:40:15 +0100533 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700534 }
535 size_t methods_pointer_index;
536 if (bitmap_ == NULL) {
537 CHECK_EQ(kOatClassAllCompiled, type_);
538 methods_pointer_index = method_index;
539 } else {
540 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100541 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100542 return OatMethod(NULL, 0, 0);
Brian Carlstromba150c32013-08-27 17:31:03 -0700543 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100544 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
545 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700546 }
547 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Nicolas Geoffray4361bef2014-08-20 04:59:12 +0100548 if (oat_file_->IsExecutable()
549 || (Runtime::Current() == nullptr)
550 || Runtime::Current()->IsCompiler()) {
Alex Light9dcc4572014-08-14 14:16:26 -0700551 return OatMethod(
552 oat_file_->Begin(),
553 oat_method_offsets.code_offset_,
554 oat_method_offsets.gc_map_offset_);
555 } else {
556 // We aren't allowed to use the compiled code. We just force it down the interpreted version.
557 return OatMethod(oat_file_->Begin(), 0, 0);
558 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700559}
560
Logan Chien0c717dd2012-03-28 18:31:07 +0800561
Ian Rogersef7d42f2014-01-06 12:55:46 -0800562uint32_t OatFile::OatMethod::GetQuickCodeSize() const {
563 uintptr_t code = reinterpret_cast<uintptr_t>(GetQuickCode());
Logan Chien971bf3f2012-05-01 15:47:55 +0800564 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800565 return 0;
566 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800567 // TODO: make this Thumb2 specific
568 code &= ~0x1;
569 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800570}
571
Brian Carlstromea46f952013-07-30 01:26:50 -0700572void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700573 CHECK(method != NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800574 method->SetEntryPointFromPortableCompiledCode(GetPortableCode());
575 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700576 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800577}
578
Brian Carlstrome24fa612011-09-29 00:53:55 -0700579} // namespace art