blob: 7bffc8cd52dc46667c62b11c887ab4addde457cb [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class.h"
26#include "mirror/abstract_method.h"
27#include "mirror/abstract_method-inl.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"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031
32namespace art {
33
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070034std::string OatFile::DexFilenameToOdexFilename(const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070035 CHECK(IsValidDexFilename(location) || IsValidZipFilename(location));
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070036 std::string odex_location(location);
37 odex_location.resize(odex_location.size() - 3); // 3=dex or zip or apk
38 CHECK_EQ('.', odex_location[odex_location.size()-1]);
39 odex_location += "odex";
40 return odex_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070041}
42
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070044 CHECK(!location.empty());
45 if (!IsValidOatFilename(location)) {
Brian Carlstromf852fb22012-10-19 11:01:58 -070046 LOG(WARNING) << "Attempting to open oat file with unknown extension '" << location << "'";
Brian Carlstrom7a967b32012-03-28 15:23:10 -070047 }
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,
51 const std::string& location) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080052 CHECK(!oat_contents.empty()) << location;
53 CheckLocation(location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080054 UniquePtr<OatFile> oat_file(new OatFile(location));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 oat_file->begin_ = &oat_contents[0];
56 oat_file->end_ = &oat_contents[oat_contents.size()];
Brian Carlstromf1b30302013-03-28 10:35:32 -070057 return oat_file->Setup() ? oat_file.release() : NULL;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058}
59
60OatFile* OatFile::Open(const std::string& filename,
61 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070062 byte* requested_base,
63 bool executable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080064 CHECK(!filename.empty()) << location;
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070065 CheckLocation(filename);
Brian Carlstromf1d34552013-07-12 20:22:23 -070066 // If we are trying to execute, we need to use dlopen.
67 if (executable) {
68 return OpenDlopen(filename, location, requested_base);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080069 }
Brian Carlstromf1d34552013-07-12 20:22:23 -070070 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
71 //
72 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
73 //
Brian Carlstrom265091e2013-01-30 14:08:26 -080074 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
75 // This won't work for portable runtime execution because it doesn't process relocations.
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false, false));
77 if (file.get() == NULL) {
78 return NULL;
79 }
Brian Carlstromf1d34552013-07-12 20:22:23 -070080 return OpenElfFile(file.get(), location, requested_base, false, executable);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080081}
82
Brian Carlstrom265091e2013-01-30 14:08:26 -080083OatFile* OatFile::OpenWritable(File* file, const std::string& location) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080084 CheckLocation(location);
Brian Carlstromf1d34552013-07-12 20:22:23 -070085 return OpenElfFile(file, location, NULL, true, false);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086}
87
88OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
89 const std::string& location,
90 byte* requested_base) {
91 UniquePtr<OatFile> oat_file(new OatFile(location));
92 bool success = oat_file->Dlopen(elf_filename, requested_base);
93 if (!success) {
94 return NULL;
95 }
96 return oat_file.release();
97}
98
99OatFile* OatFile::OpenElfFile(File* file,
100 const std::string& location,
101 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700102 bool writable,
103 bool executable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104 UniquePtr<OatFile> oat_file(new OatFile(location));
Brian Carlstromf1d34552013-07-12 20:22:23 -0700105 bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700106 if (!success) {
107 return NULL;
108 }
109 return oat_file.release();
110}
111
Logan Chien0c717dd2012-03-28 18:31:07 +0800112OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800113 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800114 CHECK(!location_.empty());
115}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700116
117OatFile::~OatFile() {
118 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800119 if (dlopen_handle_ != NULL) {
120 dlclose(dlopen_handle_);
121 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122}
123
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800124bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800125 char* absolute_path = realpath(elf_filename.c_str(), NULL);
126 if (absolute_path == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127 return false;
128 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800129 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
130 free(absolute_path);
131 if (dlopen_handle_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132 return false;
133 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800134 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
135 if (begin_ == NULL) {
136 LOG(WARNING) << "Failed to find oatdata symbol in " << elf_filename << ": " << dlerror();
137 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800139 if (requested_base != NULL && begin_ != requested_base) {
140 std::string maps;
141 ReadFileToString("/proc/self/maps", &maps);
142 LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
143 << reinterpret_cast<const void*>(begin_) << " != expected="
144 << reinterpret_cast<const void*>(requested_base)
145 << " /proc/self/maps:\n" << maps;
146 return false;
147 }
148 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
149 if (end_ == NULL) {
150 LOG(WARNING) << "Failed to find oatlastword symbol in " << elf_filename << ": " << dlerror();
151 return false;
152 }
153 // Readjust to be non-inclusive upper bound.
154 end_ += sizeof(uint32_t);
Brian Carlstromf1b30302013-03-28 10:35:32 -0700155 return Setup();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700157
Brian Carlstromf1d34552013-07-12 20:22:23 -0700158bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 elf_file_.reset(ElfFile::Open(file, writable, true));
160 if (elf_file_.get() == NULL) {
161 PLOG(WARNING) << "Failed to create ELF file for " << file->GetPath();
162 return false;
163 }
Brian Carlstromf1d34552013-07-12 20:22:23 -0700164 bool loaded = elf_file_->Load(executable);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800165 if (!loaded) {
166 LOG(WARNING) << "Failed to load ELF file " << file->GetPath();
167 return false;
168 }
169 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
170 if (begin_ == NULL) {
171 LOG(WARNING) << "Failed to find oatdata symbol in " << file->GetPath();
172 return false;
173 }
174 if (requested_base != NULL && begin_ != requested_base) {
175 std::string maps;
176 ReadFileToString("/proc/self/maps", &maps);
177 LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
178 << reinterpret_cast<const void*>(begin_) << " != expected="
179 << reinterpret_cast<const void*>(requested_base)
180 << " /proc/self/maps:\n" << maps;
181 return false;
182 }
183 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
184 if (end_ == NULL) {
185 LOG(WARNING) << "Failed to find oatlastword symbol in " << file->GetPath();
186 return false;
187 }
188 // Readjust to be non-inclusive upper bound.
189 end_ += sizeof(uint32_t);
Brian Carlstromf1b30302013-03-28 10:35:32 -0700190 return Setup();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800191}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800192
Brian Carlstromf1b30302013-03-28 10:35:32 -0700193bool OatFile::Setup() {
194 if (!GetOatHeader().IsValid()) {
195 LOG(WARNING) << "Invalid oat magic for " << GetLocation();
196 return false;
197 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800198 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700199 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700200 if (oat > End()) {
201 LOG(ERROR) << "In oat file " << GetLocation() << " found truncated OatHeader";
202 return false;
203 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700204
Brian Carlstromfb331d72013-07-25 22:00:16 -0700205 oat += GetOatHeader().GetImageFileLocationSize();
206 if (oat > End()) {
207 LOG(ERROR) << "In oat file " << GetLocation() << " found truncated image file location: "
208 << reinterpret_cast<const void*>(Begin())
209 << "+" << sizeof(OatHeader)
210 << "+" << GetOatHeader().GetImageFileLocationSize()
211 << "<=" << reinterpret_cast<const void*>(End());
212 return false;
213 }
214
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800215 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700217 if (dex_file_location_size == 0U) {
218 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
219 << " with empty location name";
220 return false;
221 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700222 oat += sizeof(dex_file_location_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700223 if (oat > End()) {
224 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
225 << " truncated after dex file location size";
226 return false;
227 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228
229 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
230 oat += dex_file_location_size;
Brian Carlstromfb331d72013-07-25 22:00:16 -0700231 if (oat > End()) {
232 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
233 << " with truncated dex file location";
234 return false;
235 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236
237 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
238
239 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
240 oat += sizeof(dex_file_checksum);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700241 if (oat > End()) {
242 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
243 << " for "<< dex_file_location
244 << " truncated after dex file checksum";
245 return false;
246 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700247
Brian Carlstrom89521892011-12-07 22:05:07 -0800248 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700249 if (dex_file_offset == 0U) {
250 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
251 << " for "<< dex_file_location
252 << " with zero dex file offset";
253 return false;
254 }
255 if (dex_file_offset > Size()) {
256 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
257 << " for "<< dex_file_location
258 << " with dex file offset" << dex_file_offset << " > " << Size();
259 return false;
260 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800261 oat += sizeof(dex_file_offset);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700262 if (oat > End()) {
263 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
264 << " for "<< dex_file_location
265 << " truncated after dex file offset";
266 return false;
267 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800268
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800269 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Brian Carlstromfb331d72013-07-25 22:00:16 -0700270 if (!DexFile::IsMagicValid(dex_file_pointer)) {
271 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
272 << " for "<< dex_file_location
273 << " with invalid dex file magic: " << dex_file_pointer;
274 return false;
275 }
276 if (!DexFile::IsVersionValid(dex_file_pointer)) {
277 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
278 << " for "<< dex_file_location
279 << " with invalid dex file version: " << dex_file_pointer;
280 return false;
281 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800282 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
283 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800284
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800285 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700286 if (oat > End()) {
287 LOG(ERROR) << "In oat file " << GetLocation() << " found OatDexFile # " << i
288 << " for "<< dex_file_location
289 << " with truncated method offsets";
290 return false;
291 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700292
Elliott Hughesa0e18062012-04-13 15:59:59 -0700293 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
294 dex_file_location,
295 dex_file_checksum,
296 dex_file_pointer,
297 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700298 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700299 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300}
301
302const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800303 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700304}
305
Ian Rogers30fab402012-01-23 15:43:46 -0800306const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800307 CHECK(begin_ != NULL);
308 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309}
310
Ian Rogers30fab402012-01-23 15:43:46 -0800311const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800312 CHECK(end_ != NULL);
313 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700314}
315
Ian Rogers7fe2c692011-12-06 16:35:59 -0800316const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
317 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700318 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700319 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800320 if (warn_if_not_found) {
321 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
322 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700323 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700324 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700325 return it->second;
326}
327
328std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
329 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700330 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700331 result.push_back(it->second);
332 }
333 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700334}
335
336OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800337 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800338 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800339 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800340 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700341 : oat_file_(oat_file),
342 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800343 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800344 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800345 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700346
347OatFile::OatDexFile::~OatDexFile() {}
348
Ian Rogers05f28c62012-10-23 18:12:13 -0700349size_t OatFile::OatDexFile::FileSize() const {
350 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
351}
352
Brian Carlstrom89521892011-12-07 22:05:07 -0800353const DexFile* OatFile::OatDexFile::OpenDexFile() const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700354 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700355 dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800356}
357
Brian Carlstromaded5f72011-10-07 17:15:04 -0700358const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800359 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
360
Ian Rogers30fab402012-01-23 15:43:46 -0800361 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
362 CHECK_LT(oat_class_pointer, oat_file_->End());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363 mirror::Class::Status status = *reinterpret_cast<const mirror::Class::Status*>(oat_class_pointer);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800364
365 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800366 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800367
368 return new OatClass(oat_file_,
369 status,
370 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700371}
372
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800373OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800375 const OatMethodOffsets* methods_pointer)
376 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700377
378OatFile::OatClass::~OatClass() {}
379
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380mirror::Class::Status OatFile::OatClass::GetStatus() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800381 return status_;
382}
383
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700384const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
385 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
386 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800387 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800388 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700389 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700390 oat_method_offsets.core_spill_mask_,
391 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800392 oat_method_offsets.mapping_table_offset_,
393 oat_method_offsets.vmap_table_offset_,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700394 oat_method_offsets.gc_map_offset_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700395}
396
Brian Carlstromae826982011-11-09 01:33:42 -0800397OatFile::OatMethod::OatMethod(const byte* base,
398 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700399 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700400 const uint32_t core_spill_mask,
401 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800402 const uint32_t mapping_table_offset,
403 const uint32_t vmap_table_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700404 const uint32_t gc_map_offset)
Ian Rogers30fab402012-01-23 15:43:46 -0800405 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800406 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700407 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700408 core_spill_mask_(core_spill_mask),
409 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800410 mapping_table_offset_(mapping_table_offset),
411 vmap_table_offset_(vmap_table_offset),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700412 native_gc_map_offset_(gc_map_offset) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700413#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800414 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
415 if (vmap_table_offset_ == 0) {
Brian Carlstromfb331d72013-07-25 22:00:16 -0700416 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) +
417 __builtin_popcount(fp_spill_mask_)));
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700418 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800419 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700420 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) +
421 __builtin_popcount(fp_spill_mask_)));
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700422 }
423 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800424 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700425 }
426#endif
427}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700428
429OatFile::OatMethod::~OatMethod() {}
430
Logan Chien0c717dd2012-03-28 18:31:07 +0800431const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800432 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800433}
434
435uint32_t OatFile::OatMethod::GetCodeSize() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800436#if defined(ART_USE_PORTABLE_COMPILER)
437 // TODO: With Quick, we store the size before the code. With
438 // Portable, the code is in a .o file we don't manage ourselves. ELF
439 // symbols do have a concept of size, so we could capture that and
440 // store it somewhere, such as the OatMethod.
441 return 0;
442#else
Logan Chien971bf3f2012-05-01 15:47:55 +0800443 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800444
Logan Chien971bf3f2012-05-01 15:47:55 +0800445 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800446 return 0;
447 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800448 // TODO: make this Thumb2 specific
449 code &= ~0x1;
450 return reinterpret_cast<uint32_t*>(code)[-1];
Brian Carlstrom265091e2013-01-30 14:08:26 -0800451#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800452}
453
Brian Carlstrom265091e2013-01-30 14:08:26 -0800454void OatFile::OatMethod::LinkMethod(mirror::AbstractMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700455 CHECK(method != NULL);
Jeff Haoaa4a7932013-05-13 11:28:27 -0700456 method->SetEntryPointFromCompiledCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700457 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700458 method->SetCoreSpillMask(core_spill_mask_);
459 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800460 method->SetMappingTable(GetMappingTable());
461 method->SetVmapTable(GetVmapTable());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700462 method->SetNativeGcMap(GetNativeGcMap()); // Used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800463}
464
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465} // namespace art