blob: fa2b485822798934c91943d0099f502bd6a72fe0 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_file.h"
18
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019#include <dlfcn.h>
20
Brian Carlstromba150c32013-08-27 17:31:03 -070021#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080022#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024#include "elf_file.h"
25#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_method.h"
27#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030#include "os.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070032#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033
34namespace art {
35
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070036std::string OatFile::DexFilenameToOdexFilename(const std::string& location) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070037 CHECK_GE(location.size(), 4U) << location; // must be at least .123
38 size_t dot_index = location.size() - 3 - 1; // 3=dex or zip or apk
39 CHECK_EQ('.', location[dot_index]) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070040 std::string odex_location(location);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070041 odex_location.resize(dot_index + 1);
42 CHECK_EQ('.', odex_location[odex_location.size()-1]) << location << " " << odex_location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070043 odex_location += "odex";
44 return odex_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070045}
46
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070048 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049}
50
Brian Carlstrom265091e2013-01-30 14:08:26 -080051OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070052 const std::string& location,
53 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080054 CHECK(!oat_contents.empty()) << location;
55 CheckLocation(location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080056 UniquePtr<OatFile> oat_file(new OatFile(location));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080057 oat_file->begin_ = &oat_contents[0];
58 oat_file->end_ = &oat_contents[oat_contents.size()];
Ian Rogers8d31bbd2013-10-13 10:44:14 -070059 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080060}
61
62OatFile* OatFile::Open(const std::string& filename,
63 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070064 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070065 bool executable,
66 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080067 CHECK(!filename.empty()) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070068 CheckLocation(filename);
Brian Carlstromeeb98882013-10-03 15:53:49 -070069#ifdef ART_USE_PORTABLE_COMPILER
70 // If we are using PORTABLE, use dlopen to deal with relocations.
71 //
72 // We use our own ELF loader for Quick to deal with legacy apps that
73 // open a generated dex file by name, remove the file, then open
74 // another generated dex file with the same name. http://b/10614658
Brian Carlstromf1d34552013-07-12 20:22:23 -070075 if (executable) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070076 return OpenDlopen(filename, location, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080077 }
Brian Carlstromeeb98882013-10-03 15:53:49 -070078#endif
Brian Carlstromf1d34552013-07-12 20:22:23 -070079 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
80 //
81 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
82 //
Brian Carlstrom265091e2013-01-30 14:08:26 -080083 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
84 // This won't work for portable runtime execution because it doesn't process relocations.
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070085 UniquePtr<File> file(OS::OpenFileForReading(filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086 if (file.get() == NULL) {
87 return NULL;
88 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070089 return OpenElfFile(file.get(), location, requested_base, false, executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080090}
91
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080093 CheckLocation(location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070094 return OpenElfFile(file, location, NULL, true, false, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080095}
96
97OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
98 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070099 byte* requested_base,
100 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800101 UniquePtr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700102 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800103 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700104 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800105 }
106 return oat_file.release();
107}
108
109OatFile* OatFile::OpenElfFile(File* file,
110 const std::string& location,
111 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700112 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700113 bool executable,
114 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800115 UniquePtr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700116 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700118 CHECK(!error_msg->empty());
119 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700120 }
121 return oat_file.release();
122}
123
Logan Chien0c717dd2012-03-28 18:31:07 +0800124OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800125 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800126 CHECK(!location_.empty());
127}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128
129OatFile::~OatFile() {
130 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800131 if (dlopen_handle_ != NULL) {
132 dlclose(dlopen_handle_);
133 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700134}
135
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700136bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
137 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800138 char* absolute_path = realpath(elf_filename.c_str(), NULL);
139 if (absolute_path == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700140 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700141 return false;
142 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
144 free(absolute_path);
145 if (dlopen_handle_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700146 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147 return false;
148 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800149 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
150 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700151 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
152 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800153 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700154 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800155 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700156 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
157 "oatdata=%p != expected=%p /proc/self/maps:\n",
158 begin_, requested_base);
159 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800160 return false;
161 }
162 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
163 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700164 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
165 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800166 return false;
167 }
168 // Readjust to be non-inclusive upper bound.
169 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700170 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800171}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700172
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700173bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
174 std::string* error_msg) {
175 elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
176 if (elf_file_.get() == nullptr) {
177 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178 return false;
179 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700180 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700182 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800183 return false;
184 }
185 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
186 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700187 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800188 return false;
189 }
190 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700191 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
192 "oatdata=%p != expected=%p /proc/self/maps:\n",
193 begin_, requested_base);
194 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800195 return false;
196 }
197 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
198 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700199 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800200 return false;
201 }
202 // Readjust to be non-inclusive upper bound.
203 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700204 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800205}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800206
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700207bool OatFile::Setup(std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700208 if (!GetOatHeader().IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700209 *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700210 return false;
211 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800212 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700213 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700214 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700216 return false;
217 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700218
Brian Carlstromfb331d72013-07-25 22:00:16 -0700219 oat += GetOatHeader().GetImageFileLocationSize();
220 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700221 *error_msg = StringPrintf("In oat file '%s' found truncated image file location: "
222 "%p + %zd + %ud <= %p", GetLocation().c_str(),
223 Begin(), sizeof(OatHeader), GetOatHeader().GetImageFileLocationSize(),
224 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700225 return false;
226 }
227
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800228 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700229 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230 if (UNLIKELY(dex_file_location_size == 0U)) {
231 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
232 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700233 return false;
234 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700235 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700236 if (UNLIKELY(oat > End())) {
237 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
238 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700239 return false;
240 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700241
242 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
243 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700244 if (UNLIKELY(oat > End())) {
245 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
246 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700247 return false;
248 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249
250 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
251
252 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
253 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700254 if (UNLIKELY(oat > End())) {
255 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
256 "dex file checksum", GetLocation().c_str(), i,
257 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700258 return false;
259 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700260
Brian Carlstrom89521892011-12-07 22:05:07 -0800261 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700262 if (UNLIKELY(dex_file_offset == 0U)) {
263 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
264 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700265 return false;
266 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 if (UNLIKELY(dex_file_offset > Size())) {
268 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
269 "offset %ud > %zd", GetLocation().c_str(), i,
270 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700271 return false;
272 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800273 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700274 if (UNLIKELY(oat > End())) {
275 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
276 " after dex file offsets", GetLocation().c_str(), i,
277 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700278 return false;
279 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800280
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800281 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700282 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
283 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
284 " dex file magic '%s'", GetLocation().c_str(), i,
285 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700286 return false;
287 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700288 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
289 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
290 " dex file version '%s'", GetLocation().c_str(), i,
291 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700292 return false;
293 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800294 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
295 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800296
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800297 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700298 if (UNLIKELY(oat > End())) {
299 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
300 " method offsets", GetLocation().c_str(), i,
301 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700302 return false;
303 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700304
Elliott Hughesa0e18062012-04-13 15:59:59 -0700305 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
306 dex_file_location,
307 dex_file_checksum,
308 dex_file_pointer,
309 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700310 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700311 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700312}
313
314const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800315 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700316}
317
Ian Rogers30fab402012-01-23 15:43:46 -0800318const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800319 CHECK(begin_ != NULL);
320 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700321}
322
Ian Rogers30fab402012-01-23 15:43:46 -0800323const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800324 CHECK(end_ != NULL);
325 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700326}
327
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700328const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
329 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800330 bool warn_if_not_found) const {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700331 Table::const_iterator it = oat_dex_files_.find(dex_location);
332 if (it != oat_dex_files_.end()) {
333 const OatFile::OatDexFile* oat_dex_file = it->second;
334 if (dex_location_checksum == NULL ||
335 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) {
336 return oat_dex_file;
337 }
338 }
339
340 if (warn_if_not_found) {
341 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
342 << " in OatFile " << GetLocation();
343 if (kIsDebugBuild) {
344 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
345 LOG(WARNING) << "OatFile " << GetLocation()
346 << " contains OatDexFile " << it->second->GetDexFileLocation();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700347 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800348 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700349 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700350 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700351}
352
353std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
354 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700355 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700356 result.push_back(it->second);
357 }
358 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359}
360
361OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800362 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800363 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800364 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800365 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700366 : oat_file_(oat_file),
367 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800368 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800369 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800370 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700371
372OatFile::OatDexFile::~OatDexFile() {}
373
Ian Rogers05f28c62012-10-23 18:12:13 -0700374size_t OatFile::OatDexFile::FileSize() const {
375 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
376}
377
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700378const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700379 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700380 dex_file_location_checksum_, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800381}
382
Ian Rogersee39a102013-09-19 02:56:49 -0700383const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800384 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
385
Ian Rogers30fab402012-01-23 15:43:46 -0800386 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700387 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800388
Brian Carlstromba150c32013-08-27 17:31:03 -0700389 const byte* status_pointer = oat_class_pointer;
390 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
391 mirror::Class::Status status =
392 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
393 CHECK_LT(status, mirror::Class::kStatusMax);
394
395 const byte* type_pointer = status_pointer + sizeof(uint16_t);
396 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
397 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
398 CHECK_LT(type, kOatClassMax);
399
400 const byte* bitmap_pointer = type_pointer + sizeof(int16_t);
401 CHECK_LT(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
402 uint32_t bitmap_size = 0;
403 if (type == kOatClassSomeCompiled) {
404 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(bitmap_pointer));
405 bitmap_pointer += sizeof(bitmap_size);
406 CHECK_LT(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
407 }
408
409 const byte* methods_pointer = bitmap_pointer + bitmap_size;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700410 CHECK_LT(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800411
412 return new OatClass(oat_file_,
413 status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700414 type,
415 bitmap_size,
416 reinterpret_cast<const uint32_t*>(bitmap_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800417 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700418}
419
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800420OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700422 OatClassType type,
423 uint32_t bitmap_size,
424 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800425 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700426 : oat_file_(oat_file), status_(status), type_(type),
427 bitmap_(NULL), methods_pointer_(methods_pointer) {
428 switch (type_) {
429 case kOatClassAllCompiled: {
430 CHECK_EQ(0U, bitmap_size);
431 break;
432 }
433 case kOatClassSomeCompiled: {
434 CHECK_NE(0U, bitmap_size);
435 bitmap_ = new BitVector(0, false, Allocator::GetNoopAllocator(), bitmap_size,
436 const_cast<uint32_t*>(bitmap_pointer));
437 break;
438 }
439 case kOatClassNoneCompiled: {
440 CHECK_EQ(0U, bitmap_size);
441 methods_pointer_ = NULL;
442 break;
443 }
444 case kOatClassMax: {
445 LOG(FATAL) << "Invalid OatClassType " << type_;
446 break;
447 }
448 }
449}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700450
Brian Carlstromba150c32013-08-27 17:31:03 -0700451OatFile::OatClass::~OatClass() {
452 delete bitmap_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800453}
454
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700455const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700456 if (methods_pointer_ == NULL) {
457 CHECK_EQ(kOatClassNoneCompiled, type_);
458 return OatMethod(NULL, 0, 0, 0, 0, 0, 0, 0);
459 }
460 size_t methods_pointer_index;
461 if (bitmap_ == NULL) {
462 CHECK_EQ(kOatClassAllCompiled, type_);
463 methods_pointer_index = method_index;
464 } else {
465 CHECK_EQ(kOatClassSomeCompiled, type_);
466 if (!bitmap_->IsBitSet(method_index)) {
467 return OatMethod(NULL, 0, 0, 0, 0, 0, 0, 0);
468 }
469 size_t num_set_bits = bitmap_->NumSetBits(method_index);
470 CHECK_NE(0U, num_set_bits);
471 methods_pointer_index = num_set_bits - 1;
472 }
473 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700474 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800475 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800476 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700478 oat_method_offsets.core_spill_mask_,
479 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800480 oat_method_offsets.mapping_table_offset_,
481 oat_method_offsets.vmap_table_offset_,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700482 oat_method_offsets.gc_map_offset_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700483}
484
Brian Carlstromae826982011-11-09 01:33:42 -0800485OatFile::OatMethod::OatMethod(const byte* base,
486 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700487 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700488 const uint32_t core_spill_mask,
489 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800490 const uint32_t mapping_table_offset,
491 const uint32_t vmap_table_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700492 const uint32_t gc_map_offset)
Ian Rogers30fab402012-01-23 15:43:46 -0800493 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800494 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700495 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700496 core_spill_mask_(core_spill_mask),
497 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800498 mapping_table_offset_(mapping_table_offset),
499 vmap_table_offset_(vmap_table_offset),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700500 native_gc_map_offset_(gc_map_offset) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700501#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800502 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
503 if (vmap_table_offset_ == 0) {
Brian Carlstromfb331d72013-07-25 22:00:16 -0700504 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) +
505 __builtin_popcount(fp_spill_mask_)));
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700506 } else {
Ian Rogers1809a722013-08-09 22:05:32 -0700507 VmapTable vmap_table(reinterpret_cast<const uint8_t*>(begin_ + vmap_table_offset_));
508
509 DCHECK_EQ(vmap_table.Size(), static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) +
510 __builtin_popcount(fp_spill_mask_)));
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700511 }
512 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800513 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700514 }
515#endif
516}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700517
518OatFile::OatMethod::~OatMethod() {}
519
Logan Chien0c717dd2012-03-28 18:31:07 +0800520const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800521 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800522}
523
524uint32_t OatFile::OatMethod::GetCodeSize() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800525#if defined(ART_USE_PORTABLE_COMPILER)
526 // TODO: With Quick, we store the size before the code. With
527 // Portable, the code is in a .o file we don't manage ourselves. ELF
528 // symbols do have a concept of size, so we could capture that and
529 // store it somewhere, such as the OatMethod.
530 return 0;
531#else
Logan Chien971bf3f2012-05-01 15:47:55 +0800532 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800533
Logan Chien971bf3f2012-05-01 15:47:55 +0800534 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800535 return 0;
536 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800537 // TODO: make this Thumb2 specific
538 code &= ~0x1;
539 return reinterpret_cast<uint32_t*>(code)[-1];
Brian Carlstrom265091e2013-01-30 14:08:26 -0800540#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800541}
542
Brian Carlstromea46f952013-07-30 01:26:50 -0700543void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700544 CHECK(method != NULL);
Jeff Haoaa4a7932013-05-13 11:28:27 -0700545 method->SetEntryPointFromCompiledCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700546 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700547 method->SetCoreSpillMask(core_spill_mask_);
548 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800549 method->SetMappingTable(GetMappingTable());
550 method->SetVmapTable(GetVmapTable());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700551 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800552}
553
Brian Carlstrome24fa612011-09-29 00:53:55 -0700554} // namespace art