blob: 32a8a861e1a0e97e49b6405c21ece188da417e7f [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
Brian Carlstrom265091e2013-01-30 14:08:26 -080047OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
48 const std::string& location) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049 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 Carlstrom265091e2013-01-30 14:08:26 -080072 */
Brian Carlstrom700c8d32012-11-05 10:42:02 -080073 OatFile* result = OpenDlopen(filename, location, requested_base);
74 if (result != NULL) {
75 return result;
76 }
Brian Carlstrom265091e2013-01-30 14:08:26 -080077 // On target, only used dlopen to load.
78 if (kIsTargetBuild) {
79 return NULL;
80 }
81 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
82 // This won't work for portable runtime execution because it doesn't process relocations.
Brian Carlstrom700c8d32012-11-05 10:42:02 -080083 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false, false));
84 if (file.get() == NULL) {
85 return NULL;
86 }
87 return OpenElfFile(file.get(), location, requested_base, false);
88}
89
Brian Carlstrom265091e2013-01-30 14:08:26 -080090OatFile* OatFile::OpenWritable(File* file, const std::string& location) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080091 CheckLocation(location);
Brian Carlstrom265091e2013-01-30 14:08:26 -080092 return OpenElfFile(file, location, NULL, true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080093}
94
95OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
96 const std::string& location,
97 byte* requested_base) {
98 UniquePtr<OatFile> oat_file(new OatFile(location));
99 bool success = oat_file->Dlopen(elf_filename, requested_base);
100 if (!success) {
101 return NULL;
102 }
103 return oat_file.release();
104}
105
106OatFile* OatFile::OpenElfFile(File* file,
107 const std::string& location,
108 byte* requested_base,
109 bool writable) {
110 UniquePtr<OatFile> oat_file(new OatFile(location));
111 bool success = oat_file->ElfFileOpen(file, requested_base, writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700112 if (!success) {
113 return NULL;
114 }
115 return oat_file.release();
116}
117
Logan Chien0c717dd2012-03-28 18:31:07 +0800118OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800119 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800120 CHECK(!location_.empty());
121}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122
123OatFile::~OatFile() {
124 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800125 if (dlopen_handle_ != NULL) {
126 dlclose(dlopen_handle_);
127 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128}
129
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800130bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base) {
131
132 char* absolute_path = realpath(elf_filename.c_str(), NULL);
133 if (absolute_path == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700134 return false;
135 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800136 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
137 free(absolute_path);
138 if (dlopen_handle_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139 return false;
140 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800141 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
142 if (begin_ == NULL) {
143 LOG(WARNING) << "Failed to find oatdata symbol in " << elf_filename << ": " << dlerror();
144 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700145 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800146 if (requested_base != NULL && begin_ != requested_base) {
147 std::string maps;
148 ReadFileToString("/proc/self/maps", &maps);
149 LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
150 << reinterpret_cast<const void*>(begin_) << " != expected="
151 << reinterpret_cast<const void*>(requested_base)
152 << " /proc/self/maps:\n" << maps;
153 return false;
154 }
155 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
156 if (end_ == NULL) {
157 LOG(WARNING) << "Failed to find oatlastword symbol in " << elf_filename << ": " << dlerror();
158 return false;
159 }
160 // Readjust to be non-inclusive upper bound.
161 end_ += sizeof(uint32_t);
162 Setup();
163 return true;
164}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800166bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable) {
167 elf_file_.reset(ElfFile::Open(file, writable, true));
168 if (elf_file_.get() == NULL) {
169 PLOG(WARNING) << "Failed to create ELF file for " << file->GetPath();
170 return false;
171 }
172 bool loaded = elf_file_->Load();
173 if (!loaded) {
174 LOG(WARNING) << "Failed to load ELF file " << file->GetPath();
175 return false;
176 }
177 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
178 if (begin_ == NULL) {
179 LOG(WARNING) << "Failed to find oatdata symbol in " << file->GetPath();
180 return false;
181 }
182 if (requested_base != NULL && begin_ != requested_base) {
183 std::string maps;
184 ReadFileToString("/proc/self/maps", &maps);
185 LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
186 << reinterpret_cast<const void*>(begin_) << " != expected="
187 << reinterpret_cast<const void*>(requested_base)
188 << " /proc/self/maps:\n" << maps;
189 return false;
190 }
191 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
192 if (end_ == NULL) {
193 LOG(WARNING) << "Failed to find oatlastword symbol in " << file->GetPath();
194 return false;
195 }
196 // Readjust to be non-inclusive upper bound.
197 end_ += sizeof(uint32_t);
198 Setup();
199 return true;
200}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800201
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800202void OatFile::Setup() {
203 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204 oat += sizeof(OatHeader);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800205 oat += GetOatHeader().GetImageFileLocationSize();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700206
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800207 CHECK_LE(oat, End())
208 << reinterpret_cast<const void*>(Begin())
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700209 << "+" << sizeof(OatHeader)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800210 << "+" << GetOatHeader().GetImageFileLocationSize()
211 << "<=" << reinterpret_cast<const void*>(End())
212 << " " << GetLocation();
213 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800215 CHECK_GT(dex_file_location_size, 0U) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 oat += sizeof(dex_file_location_size);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800217 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700218
219 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
220 oat += dex_file_location_size;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800221 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700222
223 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
224
225 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
226 oat += sizeof(dex_file_checksum);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800227 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228
Brian Carlstrom89521892011-12-07 22:05:07 -0800229 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800230 CHECK_GT(dex_file_offset, 0U) << GetLocation();
231 CHECK_LT(dex_file_offset, Size()) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800232 oat += sizeof(dex_file_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800233 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800234
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800235 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700236 CHECK(DexFile::IsMagicValid(dex_file_pointer))
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800237 << GetLocation() << " " << dex_file_pointer;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700238 CHECK(DexFile::IsVersionValid(dex_file_pointer))
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800239 << GetLocation() << " " << dex_file_pointer;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800240 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
241 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800242
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800243 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800244 CHECK_LE(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245
Elliott Hughesa0e18062012-04-13 15:59:59 -0700246 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
247 dex_file_location,
248 dex_file_checksum,
249 dex_file_pointer,
250 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700251 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700252}
253
254const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800255 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700256}
257
Ian Rogers30fab402012-01-23 15:43:46 -0800258const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800259 CHECK(begin_ != NULL);
260 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261}
262
Ian Rogers30fab402012-01-23 15:43:46 -0800263const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800264 CHECK(end_ != NULL);
265 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266}
267
Ian Rogers7fe2c692011-12-06 16:35:59 -0800268const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
269 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700270 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800272 if (warn_if_not_found) {
273 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
274 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700275 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700276 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700277 return it->second;
278}
279
280std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
281 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700282 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700283 result.push_back(it->second);
284 }
285 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700286}
287
288OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800289 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800290 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800291 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800292 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293 : oat_file_(oat_file),
294 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800295 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800296 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800297 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700298
299OatFile::OatDexFile::~OatDexFile() {}
300
Ian Rogers05f28c62012-10-23 18:12:13 -0700301size_t OatFile::OatDexFile::FileSize() const {
302 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
303}
304
Brian Carlstrom89521892011-12-07 22:05:07 -0800305const DexFile* OatFile::OatDexFile::OpenDexFile() const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700306 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700307 dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800308}
309
Brian Carlstromaded5f72011-10-07 17:15:04 -0700310const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800311 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
312
Ian Rogers30fab402012-01-23 15:43:46 -0800313 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
314 CHECK_LT(oat_class_pointer, oat_file_->End());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 mirror::Class::Status status = *reinterpret_cast<const mirror::Class::Status*>(oat_class_pointer);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800316
317 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800318 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800319
320 return new OatClass(oat_file_,
321 status,
322 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700323}
324
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800325OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800327 const OatMethodOffsets* methods_pointer)
328 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700329
330OatFile::OatClass::~OatClass() {}
331
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332mirror::Class::Status OatFile::OatClass::GetStatus() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800333 return status_;
334}
335
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700336const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
337 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
338 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800339 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800340 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700341 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700342 oat_method_offsets.core_spill_mask_,
343 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800344 oat_method_offsets.mapping_table_offset_,
345 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800346 oat_method_offsets.gc_map_offset_,
Logan Chien0c717dd2012-03-28 18:31:07 +0800347 oat_method_offsets.invoke_stub_offset_
Ian Rogersc928de92013-02-27 14:30:44 -0800348#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800349 , oat_method_offsets.proxy_stub_offset_
Logan Chien0c717dd2012-03-28 18:31:07 +0800350#endif
351 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700352}
353
Brian Carlstromae826982011-11-09 01:33:42 -0800354OatFile::OatMethod::OatMethod(const byte* base,
355 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700356 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700357 const uint32_t core_spill_mask,
358 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800359 const uint32_t mapping_table_offset,
360 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800361 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800362 const uint32_t invoke_stub_offset
Ian Rogersc928de92013-02-27 14:30:44 -0800363#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800364 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800365#endif
366 )
Ian Rogers30fab402012-01-23 15:43:46 -0800367 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800368 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700369 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700370 core_spill_mask_(core_spill_mask),
371 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800372 mapping_table_offset_(mapping_table_offset),
373 vmap_table_offset_(vmap_table_offset),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700374 native_gc_map_offset_(gc_map_offset),
Logan Chien0c717dd2012-03-28 18:31:07 +0800375 invoke_stub_offset_(invoke_stub_offset)
Ian Rogersc928de92013-02-27 14:30:44 -0800376#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800377 , proxy_stub_offset_(proxy_stub_offset)
Logan Chien0c717dd2012-03-28 18:31:07 +0800378#endif
379{
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700380#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800381 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
382 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700383 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
384 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800385 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700386 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
387 }
388 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800389 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700390 }
391#endif
392}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700393
394OatFile::OatMethod::~OatMethod() {}
395
Logan Chien0c717dd2012-03-28 18:31:07 +0800396const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800397 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800398}
399
400uint32_t OatFile::OatMethod::GetCodeSize() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800401#if defined(ART_USE_PORTABLE_COMPILER)
402 // TODO: With Quick, we store the size before the code. With
403 // Portable, the code is in a .o file we don't manage ourselves. ELF
404 // symbols do have a concept of size, so we could capture that and
405 // store it somewhere, such as the OatMethod.
406 return 0;
407#else
Logan Chien971bf3f2012-05-01 15:47:55 +0800408 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800409
Logan Chien971bf3f2012-05-01 15:47:55 +0800410 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800411 return 0;
412 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800413 // TODO: make this Thumb2 specific
414 code &= ~0x1;
415 return reinterpret_cast<uint32_t*>(code)[-1];
Brian Carlstrom265091e2013-01-30 14:08:26 -0800416#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800417}
418
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800419mirror::AbstractMethod::InvokeStub* OatFile::OatMethod::GetInvokeStub() const {
Ian Rogers1b09b092012-08-20 15:35:52 -0700420 const byte* stub = GetOatPointer<const byte*>(invoke_stub_offset_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 return reinterpret_cast<mirror::AbstractMethod::InvokeStub*>(const_cast<byte*>(stub));
Logan Chien0c717dd2012-03-28 18:31:07 +0800422}
423
424uint32_t OatFile::OatMethod::GetInvokeStubSize() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800425#if defined(ART_USE_PORTABLE_COMPILER)
426 return 0;
427#else
Logan Chien971bf3f2012-05-01 15:47:55 +0800428 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
429 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800430 return 0;
431 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800432 // TODO: make this Thumb2 specific
433 code &= ~0x1;
434 return reinterpret_cast<uint32_t*>(code)[-1];
Brian Carlstrom265091e2013-01-30 14:08:26 -0800435#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800436}
437
Ian Rogersc928de92013-02-27 14:30:44 -0800438#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127eead4ac2012-06-03 07:15:25 -0700439const void* OatFile::OatMethod::GetProxyStub() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800440 return GetOatPointer<const void*>(proxy_stub_offset_);
TDYa127eead4ac2012-06-03 07:15:25 -0700441}
442#endif
443
Brian Carlstrom265091e2013-01-30 14:08:26 -0800444void OatFile::OatMethod::LinkMethod(mirror::AbstractMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700445 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800446 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700447 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700448 method->SetCoreSpillMask(core_spill_mask_);
449 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800450 method->SetMappingTable(GetMappingTable());
451 method->SetVmapTable(GetVmapTable());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700452 method->SetNativeGcMap(GetNativeGcMap()); // Note, used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800453 method->SetInvokeStub(GetInvokeStub());
454}
455
Brian Carlstrome24fa612011-09-29 00:53:55 -0700456} // namespace art