blob: 7553dcca341fcdb4e3e2db914c180ddf1bd68fd0 [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
Elliott Hughes1aa246d2012-12-13 09:29:36 -080021#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080023#include "elf_file.h"
24#include "oat.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_method.h"
26#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070029#include "os.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070031#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032
33namespace art {
34
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070035std::string OatFile::DexFilenameToOdexFilename(const std::string& location) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070036 CHECK_GE(location.size(), 4U) << location; // must be at least .123
37 size_t dot_index = location.size() - 3 - 1; // 3=dex or zip or apk
38 CHECK_EQ('.', location[dot_index]) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070039 std::string odex_location(location);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070040 odex_location.resize(dot_index + 1);
41 CHECK_EQ('.', odex_location[odex_location.size()-1]) << location << " " << odex_location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070042 odex_location += "odex";
43 return odex_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070044}
45
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070047 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080048}
49
Brian Carlstrom265091e2013-01-30 14:08:26 -080050OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070051 const std::string& location,
52 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053 CHECK(!oat_contents.empty()) << location;
54 CheckLocation(location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080055 UniquePtr<OatFile> oat_file(new OatFile(location));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056 oat_file->begin_ = &oat_contents[0];
57 oat_file->end_ = &oat_contents[oat_contents.size()];
Ian Rogers8d31bbd2013-10-13 10:44:14 -070058 return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059}
60
61OatFile* OatFile::Open(const std::string& filename,
62 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070063 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070064 bool executable,
65 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080066 CHECK(!filename.empty()) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070067 CheckLocation(filename);
Brian Carlstromeeb98882013-10-03 15:53:49 -070068#ifdef ART_USE_PORTABLE_COMPILER
69 // If we are using PORTABLE, use dlopen to deal with relocations.
70 //
71 // We use our own ELF loader for Quick to deal with legacy apps that
72 // open a generated dex file by name, remove the file, then open
73 // another generated dex file with the same name. http://b/10614658
Brian Carlstromf1d34552013-07-12 20:22:23 -070074 if (executable) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070075 return OpenDlopen(filename, location, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076 }
Brian Carlstromeeb98882013-10-03 15:53:49 -070077#endif
Brian Carlstromf1d34552013-07-12 20:22:23 -070078 // 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 //
Brian Carlstrom265091e2013-01-30 14:08:26 -080082 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
83 // This won't work for portable runtime execution because it doesn't process relocations.
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070084 UniquePtr<File> file(OS::OpenFileForReading(filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085 if (file.get() == NULL) {
86 return NULL;
87 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070088 return OpenElfFile(file.get(), location, requested_base, false, executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080089}
90
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080092 CheckLocation(location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070093 return OpenElfFile(file, location, NULL, true, false, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080094}
95
96OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
97 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098 byte* requested_base,
99 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800100 UniquePtr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700101 bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800102 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104 }
105 return oat_file.release();
106}
107
108OatFile* OatFile::OpenElfFile(File* file,
109 const std::string& location,
110 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700111 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700112 bool executable,
113 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800114 UniquePtr<OatFile> oat_file(new OatFile(location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700115 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700116 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 CHECK(!error_msg->empty());
118 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 }
120 return oat_file.release();
121}
122
Logan Chien0c717dd2012-03-28 18:31:07 +0800123OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800124 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800125 CHECK(!location_.empty());
126}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127
128OatFile::~OatFile() {
129 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800130 if (dlopen_handle_ != NULL) {
131 dlclose(dlopen_handle_);
132 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700133}
134
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700135bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
136 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800137 char* absolute_path = realpath(elf_filename.c_str(), NULL);
138 if (absolute_path == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700139 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 return false;
141 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800142 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
143 free(absolute_path);
144 if (dlopen_handle_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700145 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 return false;
147 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800148 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
149 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700150 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
151 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800152 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800154 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700155 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
156 "oatdata=%p != expected=%p /proc/self/maps:\n",
157 begin_, requested_base);
158 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 return false;
160 }
161 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
162 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700163 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
164 dlerror());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800165 return false;
166 }
167 // Readjust to be non-inclusive upper bound.
168 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700169 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800170}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700171
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700172bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
173 std::string* error_msg) {
174 elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
175 if (elf_file_.get() == nullptr) {
176 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800177 return false;
178 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700179 bool loaded = elf_file_->Load(executable, error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800180 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700181 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800182 return false;
183 }
184 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
185 if (begin_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700186 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800187 return false;
188 }
189 if (requested_base != NULL && begin_ != requested_base) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700190 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
191 "oatdata=%p != expected=%p /proc/self/maps:\n",
192 begin_, requested_base);
193 ReadFileToString("/proc/self/maps", error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800194 return false;
195 }
196 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
197 if (end_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700198 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800199 return false;
200 }
201 // Readjust to be non-inclusive upper bound.
202 end_ += sizeof(uint32_t);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700203 return Setup(error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800204}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800205
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700206bool OatFile::Setup(std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700207 if (!GetOatHeader().IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700209 return false;
210 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800211 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700213 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700214 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700215 return false;
216 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700217
Brian Carlstromfb331d72013-07-25 22:00:16 -0700218 oat += GetOatHeader().GetImageFileLocationSize();
219 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700220 *error_msg = StringPrintf("In oat file '%s' found truncated image file location: "
221 "%p + %zd + %ud <= %p", GetLocation().c_str(),
222 Begin(), sizeof(OatHeader), GetOatHeader().GetImageFileLocationSize(),
223 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700224 return false;
225 }
226
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800227 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700229 if (UNLIKELY(dex_file_location_size == 0U)) {
230 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
231 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700232 return false;
233 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700235 if (UNLIKELY(oat > End())) {
236 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
237 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700238 return false;
239 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240
241 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
242 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700243 if (UNLIKELY(oat > End())) {
244 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
245 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700246 return false;
247 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248
249 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
250
251 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
252 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700253 if (UNLIKELY(oat > End())) {
254 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
255 "dex file checksum", GetLocation().c_str(), i,
256 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700257 return false;
258 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700259
Brian Carlstrom89521892011-12-07 22:05:07 -0800260 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700261 if (UNLIKELY(dex_file_offset == 0U)) {
262 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
263 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700264 return false;
265 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700266 if (UNLIKELY(dex_file_offset > Size())) {
267 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
268 "offset %ud > %zd", GetLocation().c_str(), i,
269 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700270 return false;
271 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800272 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700273 if (UNLIKELY(oat > End())) {
274 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
275 " after dex file offsets", GetLocation().c_str(), i,
276 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700277 return false;
278 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800279
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800280 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
282 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
283 " dex file magic '%s'", GetLocation().c_str(), i,
284 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700285 return false;
286 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700287 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
288 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
289 " dex file version '%s'", GetLocation().c_str(), i,
290 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700291 return false;
292 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800293 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
294 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800295
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800296 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700297 if (UNLIKELY(oat > End())) {
298 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
299 " method offsets", GetLocation().c_str(), i,
300 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700301 return false;
302 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700303
Elliott Hughesa0e18062012-04-13 15:59:59 -0700304 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
305 dex_file_location,
306 dex_file_checksum,
307 dex_file_pointer,
308 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700310 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700311}
312
313const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800314 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700315}
316
Ian Rogers30fab402012-01-23 15:43:46 -0800317const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800318 CHECK(begin_ != NULL);
319 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320}
321
Ian Rogers30fab402012-01-23 15:43:46 -0800322const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800323 CHECK(end_ != NULL);
324 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700325}
326
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700327const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
328 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800329 bool warn_if_not_found) const {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700330 Table::const_iterator it = oat_dex_files_.find(dex_location);
331 if (it != oat_dex_files_.end()) {
332 const OatFile::OatDexFile* oat_dex_file = it->second;
333 if (dex_location_checksum == NULL ||
334 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum) {
335 return oat_dex_file;
336 }
337 }
338
339 if (warn_if_not_found) {
340 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
341 << " in OatFile " << GetLocation();
342 if (kIsDebugBuild) {
343 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
344 LOG(WARNING) << "OatFile " << GetLocation()
345 << " contains OatDexFile " << it->second->GetDexFileLocation();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700346 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800347 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700348 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700349 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700350}
351
352std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
353 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700354 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700355 result.push_back(it->second);
356 }
357 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700358}
359
360OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800361 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800362 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800363 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800364 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700365 : oat_file_(oat_file),
366 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800367 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800368 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800369 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700370
371OatFile::OatDexFile::~OatDexFile() {}
372
Ian Rogers05f28c62012-10-23 18:12:13 -0700373size_t OatFile::OatDexFile::FileSize() const {
374 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
375}
376
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700377const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700378 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700379 dex_file_location_checksum_, error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800380}
381
Ian Rogersee39a102013-09-19 02:56:49 -0700382const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800383 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
384
Ian Rogers30fab402012-01-23 15:43:46 -0800385 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700386 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800387 mirror::Class::Status status = *reinterpret_cast<const mirror::Class::Status*>(oat_class_pointer);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800388
389 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700390 CHECK_LT(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800391
392 return new OatClass(oat_file_,
393 status,
394 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700395}
396
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800397OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800399 const OatMethodOffsets* methods_pointer)
400 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401
402OatFile::OatClass::~OatClass() {}
403
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800404mirror::Class::Status OatFile::OatClass::GetStatus() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800405 return status_;
406}
407
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700408const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
409 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
410 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800411 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800412 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700413 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700414 oat_method_offsets.core_spill_mask_,
415 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800416 oat_method_offsets.mapping_table_offset_,
417 oat_method_offsets.vmap_table_offset_,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700418 oat_method_offsets.gc_map_offset_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700419}
420
Brian Carlstromae826982011-11-09 01:33:42 -0800421OatFile::OatMethod::OatMethod(const byte* base,
422 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700423 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700424 const uint32_t core_spill_mask,
425 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800426 const uint32_t mapping_table_offset,
427 const uint32_t vmap_table_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700428 const uint32_t gc_map_offset)
Ian Rogers30fab402012-01-23 15:43:46 -0800429 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800430 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700431 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700432 core_spill_mask_(core_spill_mask),
433 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800434 mapping_table_offset_(mapping_table_offset),
435 vmap_table_offset_(vmap_table_offset),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700436 native_gc_map_offset_(gc_map_offset) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700437#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800438 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
439 if (vmap_table_offset_ == 0) {
Brian Carlstromfb331d72013-07-25 22:00:16 -0700440 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) +
441 __builtin_popcount(fp_spill_mask_)));
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700442 } else {
Ian Rogers1809a722013-08-09 22:05:32 -0700443 VmapTable vmap_table(reinterpret_cast<const uint8_t*>(begin_ + vmap_table_offset_));
444
445 DCHECK_EQ(vmap_table.Size(), static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) +
446 __builtin_popcount(fp_spill_mask_)));
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700447 }
448 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800449 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700450 }
451#endif
452}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700453
454OatFile::OatMethod::~OatMethod() {}
455
Logan Chien0c717dd2012-03-28 18:31:07 +0800456const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800457 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800458}
459
460uint32_t OatFile::OatMethod::GetCodeSize() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800461#if defined(ART_USE_PORTABLE_COMPILER)
462 // TODO: With Quick, we store the size before the code. With
463 // Portable, the code is in a .o file we don't manage ourselves. ELF
464 // symbols do have a concept of size, so we could capture that and
465 // store it somewhere, such as the OatMethod.
466 return 0;
467#else
Logan Chien971bf3f2012-05-01 15:47:55 +0800468 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800469
Logan Chien971bf3f2012-05-01 15:47:55 +0800470 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800471 return 0;
472 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800473 // TODO: make this Thumb2 specific
474 code &= ~0x1;
475 return reinterpret_cast<uint32_t*>(code)[-1];
Brian Carlstrom265091e2013-01-30 14:08:26 -0800476#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800477}
478
Brian Carlstromea46f952013-07-30 01:26:50 -0700479void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700480 CHECK(method != NULL);
Jeff Haoaa4a7932013-05-13 11:28:27 -0700481 method->SetEntryPointFromCompiledCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700483 method->SetCoreSpillMask(core_spill_mask_);
484 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800485 method->SetMappingTable(GetMappingTable());
486 method->SetVmapTable(GetVmapTable());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700487 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800488}
489
Brian Carlstrome24fa612011-09-29 00:53:55 -0700490} // namespace art