blob: 9061bb3d55b070eba8abf9a14cf20df4e26bb769 [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>
Andreas Gampedaab38c2014-09-12 18:38:24 -070022#include <unistd.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080023
Brian Carlstromba150c32013-08-27 17:31:03 -070024#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080027#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070028#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070030#include "mirror/art_method.h"
31#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070034#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070035#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070037#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038
39namespace art {
40
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070042 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043}
44
Alex Light84d76052014-08-22 17:49:35 -070045OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
46 const std::string& location,
47 std::string* error_msg) {
48 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
49 oat_file->elf_file_.reset(elf_file);
Tong Shen62d1ca32014-09-03 17:24:56 -070050 uint64_t offset, size;
51 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
52 CHECK(has_section);
53 oat_file->begin_ = elf_file->Begin() + offset;
54 oat_file->end_ = elf_file->Begin() + size + offset;
Alex Light84d76052014-08-22 17:49:35 -070055 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
56}
57
Brian Carlstrom265091e2013-01-30 14:08:26 -080058OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070059 const std::string& location,
60 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061 CHECK(!oat_contents.empty()) << location;
62 CheckLocation(location);
Alex Light9dcc4572014-08-14 14:16:26 -070063 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080064 oat_file->begin_ = &oat_contents[0];
65 oat_file->end_ = &oat_contents[oat_contents.size()];
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080067}
68
69OatFile* OatFile::Open(const std::string& filename,
70 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -070071 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -070072 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070073 bool executable,
74 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080075 CHECK(!filename.empty()) << location;
Andreas Gampe7ba64962014-10-23 11:37:40 -070076 CheckLocation(location);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070077 std::unique_ptr<OatFile> ret;
Elliott Hughes956af0f2014-12-11 14:34:28 -080078 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
79 //
80 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
81 //
82 // We use our own ELF loader for Quick to deal with legacy apps that
83 // open a generated dex file by name, remove the file, then open
84 // another generated dex file with the same name. http://b/10614658
85 //
86 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
87 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
88 if (file.get() == NULL) {
89 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
90 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080091 }
Elliott Hughes956af0f2014-12-11 14:34:28 -080092 ret.reset(OpenElfFile(file.get(), location, requested_base, oat_file_begin, false, executable,
93 error_msg));
94
95 // It would be nice to unlink here. But we might have opened the file created by the
96 // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API
97 // to allow removal when we know the ELF must be borked.
Dave Allison69dfe512014-07-11 17:11:58 +000098 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080099}
100
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700101OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800102 CheckLocation(location);
Igor Murashkin46774762014-10-22 11:37:02 -0700103 return OpenElfFile(file, location, nullptr, nullptr, true, false, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104}
105
Alex Lighta59dd802014-07-02 16:28:08 -0700106OatFile* OatFile::OpenReadable(File* file, const std::string& location, std::string* error_msg) {
107 CheckLocation(location);
Igor Murashkin46774762014-10-22 11:37:02 -0700108 return OpenElfFile(file, location, nullptr, nullptr, false, false, error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700109}
110
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800111OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
112 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700113 uint8_t* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700114 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700115 std::unique_ptr<OatFile> oat_file(new OatFile(location, true));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700116 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800117 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700118 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800119 }
120 return oat_file.release();
121}
122
123OatFile* OatFile::OpenElfFile(File* file,
124 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700125 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700126 uint8_t* oat_file_begin,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700127 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700128 bool executable,
129 std::string* error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700130 std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
Igor Murashkin46774762014-10-22 11:37:02 -0700131 bool success = oat_file->ElfFileOpen(file, requested_base, oat_file_begin, writable, executable,
132 error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700133 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700134 CHECK(!error_msg->empty());
135 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700136 }
137 return oat_file.release();
138}
139
Alex Light9dcc4572014-08-14 14:16:26 -0700140OatFile::OatFile(const std::string& location, bool is_executable)
141 : location_(location), begin_(NULL), end_(NULL), is_executable_(is_executable),
142 dlopen_handle_(NULL),
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100143 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800144 CHECK(!location_.empty());
145}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146
147OatFile::~OatFile() {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100148 STLDeleteElements(&oat_dex_files_storage_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800149 if (dlopen_handle_ != NULL) {
150 dlclose(dlopen_handle_);
151 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152}
153
Ian Rogers13735952014-10-08 12:43:28 -0700154bool OatFile::Dlopen(const std::string& elf_filename, uint8_t* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700155 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 char* absolute_path = realpath(elf_filename.c_str(), NULL);
157 if (absolute_path == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700158 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700159 return false;
160 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800161 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
162 free(absolute_path);
163 if (dlopen_handle_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700164 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165 return false;
166 }
Ian Rogers13735952014-10-08 12:43:28 -0700167 begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatdata"));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800168 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700169 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
170 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800171 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700172 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800173 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700174 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
175 "oatdata=%p != expected=%p /proc/self/maps:\n",
176 begin_, requested_base);
177 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178 return false;
179 }
Ian Rogers13735952014-10-08 12:43:28 -0700180 end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatlastword"));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700182 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
183 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800184 return false;
185 }
186 // Readjust to be non-inclusive upper bound.
187 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700188 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800189}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700190
Igor Murashkin46774762014-10-22 11:37:02 -0700191bool OatFile::ElfFileOpen(File* file, uint8_t* requested_base, uint8_t* oat_file_begin,
192 bool writable, bool executable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700193 std::string* error_msg) {
Igor Murashkin46774762014-10-22 11:37:02 -0700194 // TODO: rename requested_base to oat_data_begin
195 elf_file_.reset(ElfFile::Open(file, writable, /*program_header_only*/true, error_msg,
196 oat_file_begin));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700197 if (elf_file_.get() == nullptr) {
198 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800199 return false;
200 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700201 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800202 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700203 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800204 return false;
205 }
206 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
207 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800209 return false;
210 }
211 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700212 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
213 "oatdata=%p != expected=%p /proc/self/maps:\n",
214 begin_, requested_base);
215 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800216 return false;
217 }
218 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
219 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700220 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800221 return false;
222 }
223 // Readjust to be non-inclusive upper bound.
224 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700225 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800226}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800227
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700228bool OatFile::Setup(std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700229 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800230 std::string cause = GetOatHeader().GetValidationErrorMessage();
231 *error_msg = StringPrintf("Invalid oat header for '%s': %s", GetLocation().c_str(),
232 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700233 return false;
234 }
Ian Rogers13735952014-10-08 12:43:28 -0700235 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700237 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700238 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700239 return false;
240 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700241
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700242 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700243 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700244 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700246 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700247 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700248 return false;
249 }
250
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100251 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
252 oat_dex_files_storage_.reserve(dex_file_count);
253 for (size_t i = 0; i < dex_file_count; i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700254 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700255 if (UNLIKELY(dex_file_location_size == 0U)) {
256 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
257 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700258 return false;
259 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700260 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700261 if (UNLIKELY(oat > End())) {
262 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
263 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700264 return false;
265 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266
267 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
268 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700269 if (UNLIKELY(oat > End())) {
270 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
271 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700272 return false;
273 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274
275 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
276
277 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
278 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700279 if (UNLIKELY(oat > End())) {
280 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
281 "dex file checksum", GetLocation().c_str(), i,
282 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700283 return false;
284 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700285
Brian Carlstrom89521892011-12-07 22:05:07 -0800286 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700287 if (UNLIKELY(dex_file_offset == 0U)) {
288 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
289 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700290 return false;
291 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700292 if (UNLIKELY(dex_file_offset > Size())) {
293 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
294 "offset %ud > %zd", GetLocation().c_str(), i,
295 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700296 return false;
297 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800298 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700299 if (UNLIKELY(oat > End())) {
300 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700301 "after dex file offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700302 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700303 return false;
304 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800305
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800306 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700307 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
308 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700309 "dex file magic '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700310 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700311 return false;
312 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700313 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
314 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700315 "dex file version '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700316 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700317 return false;
318 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800319 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
320 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800321
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800322 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700323 if (UNLIKELY(oat > End())) {
324 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700325 "method offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700326 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700327 return false;
328 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700329
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100330 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
331
332 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100333 OatDexFile* oat_dex_file = new OatDexFile(this,
334 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100335 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100336 dex_file_checksum,
337 dex_file_pointer,
338 methods_offsets_pointer);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100339 oat_dex_files_storage_.push_back(oat_dex_file);
340
341 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100342 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100343 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100344 if (canonical_location != dex_file_location) {
345 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
346 oat_dex_files_.Put(canonical_key, oat_dex_file);
347 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700348 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700349 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700350}
351
352const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800353 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354}
355
Ian Rogers13735952014-10-08 12:43:28 -0700356const uint8_t* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800357 CHECK(begin_ != NULL);
358 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359}
360
Ian Rogers13735952014-10-08 12:43:28 -0700361const uint8_t* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800362 CHECK(end_ != NULL);
363 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700364}
365
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700366const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
367 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800368 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100369 // NOTE: We assume here that the canonical location for a given dex_location never
370 // changes. If it does (i.e. some symlink used by the filename changes) we may return
371 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
372 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100373
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100374 // TODO: Additional analysis of usage patterns to see if this can be simplified
375 // without any performance loss, for example by not doing the first lock-free lookup.
376
377 const OatFile::OatDexFile* oat_dex_file = nullptr;
378 StringPiece key(dex_location);
379 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
380 // directly mentioned in the oat file and doesn't require locking.
381 auto primary_it = oat_dex_files_.find(key);
382 if (primary_it != oat_dex_files_.end()) {
383 oat_dex_file = primary_it->second;
384 DCHECK(oat_dex_file != nullptr);
385 } else {
386 // This dex_location is not one of the dex locations directly mentioned in the
387 // oat file. The correct lookup is via the canonical location but first see in
388 // the secondary_oat_dex_files_ whether we've looked up this location before.
389 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
390 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
391 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
392 oat_dex_file = secondary_lb->second; // May be nullptr.
393 } else {
394 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100395 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100396 if (dex_canonical_location != dex_location) {
397 StringPiece canonical_key(dex_canonical_location);
398 auto canonical_it = oat_dex_files_.find(canonical_key);
399 if (canonical_it != oat_dex_files_.end()) {
400 oat_dex_file = canonical_it->second;
401 } // else keep nullptr.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100402 } // else keep nullptr.
403
404 // Copy the key to the string_cache_ and store the result in secondary map.
405 string_cache_.emplace_back(key.data(), key.length());
406 StringPiece key_copy(string_cache_.back());
407 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700408 }
409 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100410 if (oat_dex_file != nullptr &&
411 (dex_location_checksum == nullptr ||
412 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
413 return oat_dex_file;
414 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700415
416 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100417 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800418 std::string checksum("<unspecified>");
419 if (dex_location_checksum != NULL) {
420 checksum = StringPrintf("0x%08x", *dex_location_checksum);
421 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700422 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100423 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800424 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700425 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100426 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700427 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100428 << " contains OatDexFile " << odf->GetDexFileLocation()
429 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
430 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700431 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800432 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700433 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100434
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700435 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700436}
437
Brian Carlstrome24fa612011-09-29 00:53:55 -0700438OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800439 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100440 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800441 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -0700442 const uint8_t* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800443 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 : oat_file_(oat_file),
445 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100446 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800447 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800448 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800449 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700450
451OatFile::OatDexFile::~OatDexFile() {}
452
Ian Rogers05f28c62012-10-23 18:12:13 -0700453size_t OatFile::OatDexFile::FileSize() const {
454 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
455}
456
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800457std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700458 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Andreas Gampefd9eb392014-11-06 16:52:58 -0800459 dex_file_location_checksum_, GetOatFile(), error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800460}
461
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700462uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
463 return oat_class_offsets_pointer_[class_def_index];
464}
465
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100466OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700467 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800468
Ian Rogers13735952014-10-08 12:43:28 -0700469 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700470 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800471
Ian Rogers13735952014-10-08 12:43:28 -0700472 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -0700473 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
474 mirror::Class::Status status =
475 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
476 CHECK_LT(status, mirror::Class::kStatusMax);
477
Ian Rogers13735952014-10-08 12:43:28 -0700478 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -0700479 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
480 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
481 CHECK_LT(type, kOatClassMax);
482
Ian Rogers13735952014-10-08 12:43:28 -0700483 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800484 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700485
Brian Carlstromcd937a92014-03-04 22:53:23 -0800486 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700487 const uint8_t* bitmap_pointer = nullptr;
488 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -0700489 if (type != kOatClassNoneCompiled) {
490 if (type == kOatClassSomeCompiled) {
491 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
492 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
493 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
494 methods_pointer = bitmap_pointer + bitmap_size;
495 } else {
496 methods_pointer = after_type_pointer;
497 }
498 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -0800499 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800500
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100501 return OatClass(oat_file_,
502 status,
503 type,
504 bitmap_size,
505 reinterpret_cast<const uint32_t*>(bitmap_pointer),
506 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700507}
508
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800509OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800510 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700511 OatClassType type,
512 uint32_t bitmap_size,
513 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800514 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700515 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100516 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700517 switch (type_) {
518 case kOatClassAllCompiled: {
519 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800520 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700521 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700522 break;
523 }
524 case kOatClassSomeCompiled: {
525 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800526 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700527 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700528 break;
529 }
530 case kOatClassNoneCompiled: {
531 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800532 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700533 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700534 break;
535 }
536 case kOatClassMax: {
537 LOG(FATAL) << "Invalid OatClassType " << type_;
538 break;
539 }
540 }
541}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700542
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700543uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
544 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
545 if (oat_method_offsets == nullptr) {
546 return 0u;
547 }
548 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
549}
550
551const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100552 // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700553 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700554 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700555 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700556 }
557 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700558 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700559 CHECK_EQ(kOatClassAllCompiled, type_);
560 methods_pointer_index = method_index;
561 } else {
562 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100563 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700564 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700565 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100566 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
567 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700568 }
569 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700570 return &oat_method_offsets;
571}
572
573const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
574 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
575 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800576 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700577 }
578 if (oat_file_->IsExecutable() ||
579 Runtime::Current() == nullptr || // This case applies for oatdump.
580 Runtime::Current()->IsCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800581 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -0700582 } else {
583 // We aren't allowed to use the compiled code. We just force it down the interpreted version.
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800584 return OatMethod(oat_file_->Begin(), 0);
Alex Light9dcc4572014-08-14 14:16:26 -0700585 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700586}
587
Brian Carlstromea46f952013-07-30 01:26:50 -0700588void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700589 CHECK(method != NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800590 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -0800591}
592
Andreas Gampe7ba64962014-10-23 11:37:40 -0700593bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -0700594 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -0700595 // TODO: Check against oat_patches. b/18144996
596}
597
Brian Carlstrome24fa612011-09-29 00:53:55 -0700598} // namespace art