blob: 5fd95be0dc1e7655edd1f055905a32c5998da555 [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"
Brian Carlstrome24fa612011-09-29 00:53:55 -070028#include "os.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "utils.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030
31namespace art {
32
jeffhao262bf462011-10-20 18:36:32 -070033std::string OatFile::DexFilenameToOatFilename(const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070034 CHECK(IsValidDexFilename(location) || IsValidZipFilename(location));
Brian Carlstroma6cc8932012-01-04 14:44:07 -080035 std::string oat_location(location);
36 oat_location += ".oat";
jeffhao262bf462011-10-20 18:36:32 -070037 return oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070038}
39
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070041 CHECK(!location.empty());
42 if (!IsValidOatFilename(location)) {
Brian Carlstromf852fb22012-10-19 11:01:58 -070043 LOG(WARNING) << "Attempting to open oat file with unknown extension '" << location << "'";
Brian Carlstrom7a967b32012-03-28 15:23:10 -070044 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045}
46
47OatFile* OatFile::Open(std::vector<uint8_t>& oat_contents,
48 const std::string& location) {
49 CHECK(!oat_contents.empty()) << location;
50 CheckLocation(location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080051 UniquePtr<OatFile> oat_file(new OatFile(location));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080052 oat_file->begin_ = &oat_contents[0];
53 oat_file->end_ = &oat_contents[oat_contents.size()];
54 oat_file->Setup();
55 return oat_file.release();
56}
57
58OatFile* OatFile::Open(const std::string& filename,
59 const std::string& location,
60 byte* requested_base) {
61 CHECK(!filename.empty()) << location;
62 CheckLocation(location);
Jeff Haof9e609f2013-02-15 16:36:29 -080063 /*
64 * TODO: Reenable dlopen when it works again on MIPS. It may have broken from this change:
65 * commit 818d98eb563ad5d7293b8b5c40f3dabf745e611f
66 * Author: Brian Carlstrom <bdc@google.com>
67 * Date: Sun Feb 10 21:38:12 2013 -0800
68 *
69 * Fix MIPS to use standard kPageSize=0x1000 section alignment for ELF sections
70 *
71 * Change-Id: I905f0c5f75921a65bd7426a54d6258c780d85d0e
Brian Carlstrom700c8d32012-11-05 10:42:02 -080072 OatFile* result = OpenDlopen(filename, location, requested_base);
73 if (result != NULL) {
74 return result;
75 }
Jeff Haof9e609f2013-02-15 16:36:29 -080076 */
Brian Carlstrom700c8d32012-11-05 10:42:02 -080077 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false, false));
78 if (file.get() == NULL) {
79 return NULL;
80 }
81 return OpenElfFile(file.get(), location, requested_base, false);
82}
83
84OatFile* OatFile::Open(File* file,
85 const std::string& location,
86 byte* requested_base,
87 bool writable) {
88 CheckLocation(location);
89 return OpenElfFile(file, location, requested_base, writable);
90}
91
92OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
93 const std::string& location,
94 byte* requested_base) {
95 UniquePtr<OatFile> oat_file(new OatFile(location));
96 bool success = oat_file->Dlopen(elf_filename, requested_base);
97 if (!success) {
98 return NULL;
99 }
100 return oat_file.release();
101}
102
103OatFile* OatFile::OpenElfFile(File* file,
104 const std::string& location,
105 byte* requested_base,
106 bool writable) {
107 UniquePtr<OatFile> oat_file(new OatFile(location));
108 bool success = oat_file->ElfFileOpen(file, requested_base, writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109 if (!success) {
110 return NULL;
111 }
112 return oat_file.release();
113}
114
Logan Chien0c717dd2012-03-28 18:31:07 +0800115OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800116 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800117 CHECK(!location_.empty());
118}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119
120OatFile::~OatFile() {
121 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800122 if (dlopen_handle_ != NULL) {
123 dlclose(dlopen_handle_);
124 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125}
126
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800127bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base) {
128
129 char* absolute_path = realpath(elf_filename.c_str(), NULL);
130 if (absolute_path == NULL) {
131 PLOG(WARNING) << "Failed to create absolute path for " << elf_filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132 return false;
133 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800134 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
135 free(absolute_path);
136 if (dlopen_handle_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 return false;
138 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800139 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
140 if (begin_ == NULL) {
141 LOG(WARNING) << "Failed to find oatdata symbol in " << elf_filename << ": " << dlerror();
142 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800144 if (requested_base != NULL && begin_ != requested_base) {
145 std::string maps;
146 ReadFileToString("/proc/self/maps", &maps);
147 LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
148 << reinterpret_cast<const void*>(begin_) << " != expected="
149 << reinterpret_cast<const void*>(requested_base)
150 << " /proc/self/maps:\n" << maps;
151 return false;
152 }
153 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
154 if (end_ == NULL) {
155 LOG(WARNING) << "Failed to find oatlastword symbol in " << elf_filename << ": " << dlerror();
156 return false;
157 }
158 // Readjust to be non-inclusive upper bound.
159 end_ += sizeof(uint32_t);
160 Setup();
161 return true;
162}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800164bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable) {
165 elf_file_.reset(ElfFile::Open(file, writable, true));
166 if (elf_file_.get() == NULL) {
167 PLOG(WARNING) << "Failed to create ELF file for " << file->GetPath();
168 return false;
169 }
170 bool loaded = elf_file_->Load();
171 if (!loaded) {
172 LOG(WARNING) << "Failed to load ELF file " << file->GetPath();
173 return false;
174 }
175 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
176 if (begin_ == NULL) {
177 LOG(WARNING) << "Failed to find oatdata symbol in " << file->GetPath();
178 return false;
179 }
180 if (requested_base != NULL && begin_ != requested_base) {
181 std::string maps;
182 ReadFileToString("/proc/self/maps", &maps);
183 LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
184 << reinterpret_cast<const void*>(begin_) << " != expected="
185 << reinterpret_cast<const void*>(requested_base)
186 << " /proc/self/maps:\n" << maps;
187 return false;
188 }
189 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
190 if (end_ == NULL) {
191 LOG(WARNING) << "Failed to find oatlastword symbol in " << file->GetPath();
192 return false;
193 }
194 // Readjust to be non-inclusive upper bound.
195 end_ += sizeof(uint32_t);
196 Setup();
197 return true;
198}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800199
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800200void OatFile::Setup() {
201 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202 oat += sizeof(OatHeader);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800203 oat += GetOatHeader().GetImageFileLocationSize();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700204
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800205 CHECK_LE(oat, End())
206 << reinterpret_cast<const void*>(Begin())
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700207 << "+" << sizeof(OatHeader)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800208 << "+" << GetOatHeader().GetImageFileLocationSize()
209 << "<=" << reinterpret_cast<const void*>(End())
210 << " " << GetLocation();
211 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800213 CHECK_GT(dex_file_location_size, 0U) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214 oat += sizeof(dex_file_location_size);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800215 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216
217 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
218 oat += dex_file_location_size;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800219 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700220
221 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
222
223 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
224 oat += sizeof(dex_file_checksum);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800225 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226
Brian Carlstrom89521892011-12-07 22:05:07 -0800227 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800228 CHECK_GT(dex_file_offset, 0U) << GetLocation();
229 CHECK_LT(dex_file_offset, Size()) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800230 oat += sizeof(dex_file_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800231 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800232
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800233 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700234 CHECK(DexFile::IsMagicValid(dex_file_pointer))
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800235 << GetLocation() << " " << dex_file_pointer;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700236 CHECK(DexFile::IsVersionValid(dex_file_pointer))
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800237 << GetLocation() << " " << dex_file_pointer;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800238 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
239 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800240
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800241 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800242 CHECK_LE(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700243
Elliott Hughesa0e18062012-04-13 15:59:59 -0700244 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
245 dex_file_location,
246 dex_file_checksum,
247 dex_file_pointer,
248 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250}
251
252const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800253 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254}
255
Ian Rogers30fab402012-01-23 15:43:46 -0800256const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800257 CHECK(begin_ != NULL);
258 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700259}
260
Ian Rogers30fab402012-01-23 15:43:46 -0800261const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800262 CHECK(end_ != NULL);
263 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264}
265
Ian Rogers7fe2c692011-12-06 16:35:59 -0800266const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
267 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700268 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700269 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800270 if (warn_if_not_found) {
271 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
272 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700273 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700275 return it->second;
276}
277
278std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
279 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700280 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700281 result.push_back(it->second);
282 }
283 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700284}
285
286OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800287 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800288 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800289 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800290 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700291 : oat_file_(oat_file),
292 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800293 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800294 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800295 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700296
297OatFile::OatDexFile::~OatDexFile() {}
298
Ian Rogers05f28c62012-10-23 18:12:13 -0700299size_t OatFile::OatDexFile::FileSize() const {
300 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
301}
302
Brian Carlstrom89521892011-12-07 22:05:07 -0800303const DexFile* OatFile::OatDexFile::OpenDexFile() const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700304 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700305 dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800306}
307
Brian Carlstromaded5f72011-10-07 17:15:04 -0700308const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800309 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
310
Ian Rogers30fab402012-01-23 15:43:46 -0800311 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
312 CHECK_LT(oat_class_pointer, oat_file_->End());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 mirror::Class::Status status = *reinterpret_cast<const mirror::Class::Status*>(oat_class_pointer);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800314
315 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800316 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800317
318 return new OatClass(oat_file_,
319 status,
320 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700321}
322
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800323OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800325 const OatMethodOffsets* methods_pointer)
326 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700327
328OatFile::OatClass::~OatClass() {}
329
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330mirror::Class::Status OatFile::OatClass::GetStatus() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800331 return status_;
332}
333
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700334const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
335 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
336 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800337 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800338 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700339 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700340 oat_method_offsets.core_spill_mask_,
341 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800342 oat_method_offsets.mapping_table_offset_,
343 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800344 oat_method_offsets.gc_map_offset_,
Logan Chien0c717dd2012-03-28 18:31:07 +0800345 oat_method_offsets.invoke_stub_offset_
Ian Rogersc928de92013-02-27 14:30:44 -0800346#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800347 , oat_method_offsets.proxy_stub_offset_
Logan Chien0c717dd2012-03-28 18:31:07 +0800348#endif
349 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700350}
351
Brian Carlstromae826982011-11-09 01:33:42 -0800352OatFile::OatMethod::OatMethod(const byte* base,
353 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700355 const uint32_t core_spill_mask,
356 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800357 const uint32_t mapping_table_offset,
358 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800359 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800360 const uint32_t invoke_stub_offset
Ian Rogersc928de92013-02-27 14:30:44 -0800361#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800362 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800363#endif
364 )
Ian Rogers30fab402012-01-23 15:43:46 -0800365 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800366 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700367 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700368 core_spill_mask_(core_spill_mask),
369 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800370 mapping_table_offset_(mapping_table_offset),
371 vmap_table_offset_(vmap_table_offset),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700372 native_gc_map_offset_(gc_map_offset),
Logan Chien0c717dd2012-03-28 18:31:07 +0800373 invoke_stub_offset_(invoke_stub_offset)
Ian Rogersc928de92013-02-27 14:30:44 -0800374#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800375 , proxy_stub_offset_(proxy_stub_offset)
Logan Chien0c717dd2012-03-28 18:31:07 +0800376#endif
377{
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700378#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800379 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
380 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700381 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
382 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800383 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700384 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
385 }
386 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800387 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700388 }
389#endif
390}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700391
392OatFile::OatMethod::~OatMethod() {}
393
Logan Chien0c717dd2012-03-28 18:31:07 +0800394const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800395 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800396}
397
398uint32_t OatFile::OatMethod::GetCodeSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800399 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800400
Logan Chien971bf3f2012-05-01 15:47:55 +0800401 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800402 return 0;
403 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800404 // TODO: make this Thumb2 specific
405 code &= ~0x1;
406 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800407}
408
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409mirror::AbstractMethod::InvokeStub* OatFile::OatMethod::GetInvokeStub() const {
Ian Rogers1b09b092012-08-20 15:35:52 -0700410 const byte* stub = GetOatPointer<const byte*>(invoke_stub_offset_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 return reinterpret_cast<mirror::AbstractMethod::InvokeStub*>(const_cast<byte*>(stub));
Logan Chien0c717dd2012-03-28 18:31:07 +0800412}
413
414uint32_t OatFile::OatMethod::GetInvokeStubSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800415 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
416 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800417 return 0;
418 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800419 // TODO: make this Thumb2 specific
420 code &= ~0x1;
421 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800422}
423
Ian Rogersc928de92013-02-27 14:30:44 -0800424#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127eead4ac2012-06-03 07:15:25 -0700425const void* OatFile::OatMethod::GetProxyStub() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800426 return GetOatPointer<const void*>(proxy_stub_offset_);
TDYa127eead4ac2012-06-03 07:15:25 -0700427}
428#endif
429
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430void OatFile::OatMethod::LinkMethodPointers(mirror::AbstractMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700431 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800432 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700433 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700434 method->SetCoreSpillMask(core_spill_mask_);
435 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800436 method->SetMappingTable(GetMappingTable());
437 method->SetVmapTable(GetVmapTable());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700438 method->SetNativeGcMap(GetNativeGcMap()); // Note, used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800439 method->SetInvokeStub(GetInvokeStub());
440}
441
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800442void OatFile::OatMethod::LinkMethodOffsets(mirror::AbstractMethod* method) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800443 CHECK(method != NULL);
444 method->SetOatCodeOffset(GetCodeOffset());
445 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
446 method->SetCoreSpillMask(GetCoreSpillMask());
447 method->SetFpSpillMask(GetFpSpillMask());
448 method->SetOatMappingTableOffset(GetMappingTableOffset());
449 method->SetOatVmapTableOffset(GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700450 method->SetOatNativeGcMapOffset(GetNativeGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800451 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700452}
453
Brian Carlstrome24fa612011-09-29 00:53:55 -0700454} // namespace art