blob: b201630c6411921508ecaae56a1d37201930c44c [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
Elliott Hughes1aa246d2012-12-13 09:29:36 -080019#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080020#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include "os.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070022
23namespace art {
24
jeffhao262bf462011-10-20 18:36:32 -070025std::string OatFile::DexFilenameToOatFilename(const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070026 CHECK(IsValidDexFilename(location) || IsValidZipFilename(location));
Brian Carlstroma6cc8932012-01-04 14:44:07 -080027 std::string oat_location(location);
28 oat_location += ".oat";
jeffhao262bf462011-10-20 18:36:32 -070029 return oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070030}
31
Brian Carlstrome24fa612011-09-29 00:53:55 -070032OatFile* OatFile::Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080033 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070034 byte* requested_base,
35 bool writable) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070036 CHECK(!filename.empty()) << location;
Brian Carlstromf5822582012-03-19 22:34:31 -070037 UniquePtr<File> file(OS::OpenFile(filename.c_str(), writable, false));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080038 if (file.get() == NULL) {
Brian Carlstromf5822582012-03-19 22:34:31 -070039 return NULL;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080040 }
Brian Carlstrom1cac3432012-12-12 10:56:22 -080041 return Open(*file.get(), location, requested_base, writable);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080042}
Brian Carlstrome24fa612011-09-29 00:53:55 -070043
Brian Carlstrom5b332c82012-02-01 15:02:31 -080044OatFile* OatFile::Open(File& file,
45 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070046 byte* requested_base,
47 bool writable) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070048 CHECK(!location.empty());
49 if (!IsValidOatFilename(location)) {
Brian Carlstromf852fb22012-10-19 11:01:58 -070050 LOG(WARNING) << "Attempting to open oat file with unknown extension '" << location << "'";
Brian Carlstrom7a967b32012-03-28 15:23:10 -070051 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080052 UniquePtr<OatFile> oat_file(new OatFile(location));
Brian Carlstrom1cac3432012-12-12 10:56:22 -080053 bool success = oat_file->Map(file, requested_base, writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 if (!success) {
55 return NULL;
56 }
57 return oat_file.release();
58}
59
Logan Chien0c717dd2012-03-28 18:31:07 +080060OatFile::OatFile(const std::string& location)
Logan Chien971bf3f2012-05-01 15:47:55 +080061 : location_(location) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080062 CHECK(!location_.empty());
63}
Brian Carlstrome24fa612011-09-29 00:53:55 -070064
65OatFile::~OatFile() {
66 STLDeleteValues(&oat_dex_files_);
67}
68
Logan Chien0c717dd2012-03-28 18:31:07 +080069bool OatFile::Map(File& file,
70 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080071 bool writable) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 OatHeader oat_header;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080073 bool success = file.ReadFully(&oat_header, sizeof(oat_header));
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 if (!success || !oat_header.IsValid()) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080075 LOG(WARNING) << "Invalid oat header " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 return false;
77 }
78
Brian Carlstromf5822582012-03-19 22:34:31 -070079 int flags = 0;
80 int prot = 0;
81 if (writable) {
82 flags |= MAP_SHARED; // So changes will write through to file
83 prot |= (PROT_READ | PROT_WRITE);
84 } else {
85 flags |= MAP_PRIVATE;
86 prot |= PROT_READ;
87 }
88 if (requested_base != NULL) {
89 flags |= MAP_FIXED;
90 }
Brian Carlstrom89521892011-12-07 22:05:07 -080091 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(requested_base,
Elliott Hughes76160052012-12-12 16:31:20 -080092 file.GetLength(),
Brian Carlstromf5822582012-03-19 22:34:31 -070093 prot,
Brian Carlstrom89521892011-12-07 22:05:07 -080094 flags,
Brian Carlstrom5b332c82012-02-01 15:02:31 -080095 file.Fd(),
Brian Carlstrom89521892011-12-07 22:05:07 -080096 0));
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 if (map.get() == NULL) {
Elliott Hughes76160052012-12-12 16:31:20 -080098 LOG(WARNING) << "Failed to map oat file from " << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -070099 return false;
100 }
Ian Rogers30fab402012-01-23 15:43:46 -0800101 CHECK(requested_base == 0 || requested_base == map->Begin())
Elliott Hughes76160052012-12-12 16:31:20 -0800102 << file.GetPath() << " for " << GetLocation() << " " << reinterpret_cast<void*>(map->Begin());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700103 DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader)))
Elliott Hughes76160052012-12-12 16:31:20 -0800104 << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105
Elliott Hughese689d512012-01-18 23:39:47 -0800106 off_t code_offset = oat_header.GetExecutableOffset();
Elliott Hughes76160052012-12-12 16:31:20 -0800107 if (code_offset < file.GetLength()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800108 byte* code_address = map->Begin() + code_offset;
Elliott Hughes76160052012-12-12 16:31:20 -0800109 size_t code_length = file.GetLength() - code_offset;
Brian Carlstromf5822582012-03-19 22:34:31 -0700110 if (mprotect(code_address, code_length, prot | PROT_EXEC) != 0) {
Brian Carlstromf852fb22012-10-19 11:01:58 -0700111 PLOG(ERROR) << "Failed to make oat code executable in "
Elliott Hughes76160052012-12-12 16:31:20 -0800112 << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700113 return false;
114 }
115 } else {
116 // its possible to have no code if all the methods were abstract, native, etc
Elliott Hughes76160052012-12-12 16:31:20 -0800117 DCHECK_EQ(code_offset, RoundUp(file.GetLength(), kPageSize))
118 << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 }
120
Ian Rogers30fab402012-01-23 15:43:46 -0800121 const byte* oat = map->Begin();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800122
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123 oat += sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700124 oat += oat_header.GetImageFileLocationSize();
125
126 CHECK_LE(oat, map->End())
127 << reinterpret_cast<void*>(map->Begin())
128 << "+" << sizeof(OatHeader)
129 << "+" << oat_header.GetImageFileLocationSize()
130 << "<=" << reinterpret_cast<void*>(map->End())
Elliott Hughes76160052012-12-12 16:31:20 -0800131 << " " << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132 for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) {
133 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Elliott Hughes76160052012-12-12 16:31:20 -0800134 CHECK_GT(dex_file_location_size, 0U) << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135 oat += sizeof(dex_file_location_size);
Elliott Hughes76160052012-12-12 16:31:20 -0800136 CHECK_LT(oat, map->End()) << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137
138 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
139 oat += dex_file_location_size;
Elliott Hughes76160052012-12-12 16:31:20 -0800140 CHECK_LT(oat, map->End()) << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700141
142 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
143
144 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
145 oat += sizeof(dex_file_checksum);
Elliott Hughes76160052012-12-12 16:31:20 -0800146 CHECK_LT(oat, map->End()) << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147
Brian Carlstrom89521892011-12-07 22:05:07 -0800148 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Elliott Hughes76160052012-12-12 16:31:20 -0800149 CHECK_GT(dex_file_offset, 0U) << file.GetPath() << " for " << GetLocation();
150 CHECK_LT(dex_file_offset, static_cast<uint32_t>(file.GetLength()))
151 << file.GetPath() << " for " << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800152 oat += sizeof(dex_file_offset);
Elliott Hughes76160052012-12-12 16:31:20 -0800153 CHECK_LT(oat, map->End()) << file.GetPath() << " for " << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800154
Ian Rogers30fab402012-01-23 15:43:46 -0800155 uint8_t* dex_file_pointer = map->Begin() + dex_file_offset;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700156 CHECK(DexFile::IsMagicValid(dex_file_pointer))
Elliott Hughes76160052012-12-12 16:31:20 -0800157 << file.GetPath() << " for " << GetLocation() << " " << dex_file_pointer;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700158 CHECK(DexFile::IsVersionValid(dex_file_pointer))
Elliott Hughes76160052012-12-12 16:31:20 -0800159 << file.GetPath() << " for " << GetLocation() << " " << dex_file_pointer;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800160 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
161 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800162
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800163 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Elliott Hughes76160052012-12-12 16:31:20 -0800164 CHECK_LE(oat, map->End()) << file.GetPath() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165
Elliott Hughesa0e18062012-04-13 15:59:59 -0700166 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
167 dex_file_location,
168 dex_file_checksum,
169 dex_file_pointer,
170 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700171 }
172
173 mem_map_.reset(map.release());
174 return true;
175}
176
177const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800178 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179}
180
Ian Rogers30fab402012-01-23 15:43:46 -0800181const byte* OatFile::Begin() const {
182 CHECK(mem_map_->Begin() != NULL);
183 return mem_map_->Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184}
185
Ian Rogers30fab402012-01-23 15:43:46 -0800186const byte* OatFile::End() const {
187 CHECK(mem_map_->End() != NULL);
188 return mem_map_->End();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700189}
190
Ian Rogers7fe2c692011-12-06 16:35:59 -0800191const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
192 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700193 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700194 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800195 if (warn_if_not_found) {
196 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
197 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700198 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700199 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700200 return it->second;
201}
202
203std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
204 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700205 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700206 result.push_back(it->second);
207 }
208 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209}
210
211OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800212 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800213 uint32_t dex_file_location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800214 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800215 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 : oat_file_(oat_file),
217 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800218 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800219 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800220 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221
222OatFile::OatDexFile::~OatDexFile() {}
223
Ian Rogers05f28c62012-10-23 18:12:13 -0700224size_t OatFile::OatDexFile::FileSize() const {
225 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
226}
227
Brian Carlstrom89521892011-12-07 22:05:07 -0800228const DexFile* OatFile::OatDexFile::OpenDexFile() const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700229 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700230 dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800231}
232
Brian Carlstromaded5f72011-10-07 17:15:04 -0700233const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800234 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
235
Ian Rogers30fab402012-01-23 15:43:46 -0800236 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
237 CHECK_LT(oat_class_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800238 Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer);
239
240 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800241 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800242
243 return new OatClass(oat_file_,
244 status,
245 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700246}
247
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800248OatFile::OatClass::OatClass(const OatFile* oat_file,
249 Class::Status status,
250 const OatMethodOffsets* methods_pointer)
251 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700252
253OatFile::OatClass::~OatClass() {}
254
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800255Class::Status OatFile::OatClass::GetStatus() const {
256 return status_;
257}
258
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700259const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
260 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
261 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800262 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800263 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700264 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700265 oat_method_offsets.core_spill_mask_,
266 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800267 oat_method_offsets.mapping_table_offset_,
268 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800269 oat_method_offsets.gc_map_offset_,
Logan Chien0c717dd2012-03-28 18:31:07 +0800270 oat_method_offsets.invoke_stub_offset_
271#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800272 , oat_method_offsets.proxy_stub_offset_
Logan Chien0c717dd2012-03-28 18:31:07 +0800273#endif
274 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700275}
276
Brian Carlstromae826982011-11-09 01:33:42 -0800277OatFile::OatMethod::OatMethod(const byte* base,
278 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700279 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700280 const uint32_t core_spill_mask,
281 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800282 const uint32_t mapping_table_offset,
283 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800284 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800285 const uint32_t invoke_stub_offset
286#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800287 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800288#endif
289 )
Ian Rogers30fab402012-01-23 15:43:46 -0800290 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800291 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700292 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700293 core_spill_mask_(core_spill_mask),
294 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800295 mapping_table_offset_(mapping_table_offset),
296 vmap_table_offset_(vmap_table_offset),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700297 native_gc_map_offset_(gc_map_offset),
Logan Chien0c717dd2012-03-28 18:31:07 +0800298 invoke_stub_offset_(invoke_stub_offset)
299#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800300 , proxy_stub_offset_(proxy_stub_offset)
Logan Chien0c717dd2012-03-28 18:31:07 +0800301#endif
302{
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700303#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800304 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
305 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700306 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
307 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800308 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700309 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
310 }
311 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800312 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700313 }
314#endif
315}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700316
317OatFile::OatMethod::~OatMethod() {}
318
Logan Chien0c717dd2012-03-28 18:31:07 +0800319const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800320 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800321}
322
323uint32_t OatFile::OatMethod::GetCodeSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800324 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800325
Logan Chien971bf3f2012-05-01 15:47:55 +0800326 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800327 return 0;
328 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800329 // TODO: make this Thumb2 specific
330 code &= ~0x1;
331 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800332}
333
Mathieu Chartier66f19252012-09-18 08:57:04 -0700334AbstractMethod::InvokeStub* OatFile::OatMethod::GetInvokeStub() const {
Ian Rogers1b09b092012-08-20 15:35:52 -0700335 const byte* stub = GetOatPointer<const byte*>(invoke_stub_offset_);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700336 return reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(stub));
Logan Chien0c717dd2012-03-28 18:31:07 +0800337}
338
339uint32_t OatFile::OatMethod::GetInvokeStubSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800340 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
341 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800342 return 0;
343 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800344 // TODO: make this Thumb2 specific
345 code &= ~0x1;
346 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800347}
348
TDYa127eead4ac2012-06-03 07:15:25 -0700349#if defined(ART_USE_LLVM_COMPILER)
350const void* OatFile::OatMethod::GetProxyStub() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800351 return GetOatPointer<const void*>(proxy_stub_offset_);
TDYa127eead4ac2012-06-03 07:15:25 -0700352}
353#endif
354
Mathieu Chartier66f19252012-09-18 08:57:04 -0700355void OatFile::OatMethod::LinkMethodPointers(AbstractMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700356 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800357 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700358 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700359 method->SetCoreSpillMask(core_spill_mask_);
360 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800361 method->SetMappingTable(GetMappingTable());
362 method->SetVmapTable(GetVmapTable());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700363 method->SetNativeGcMap(GetNativeGcMap()); // Note, used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800364 method->SetInvokeStub(GetInvokeStub());
365}
366
Mathieu Chartier66f19252012-09-18 08:57:04 -0700367void OatFile::OatMethod::LinkMethodOffsets(AbstractMethod* method) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800368 CHECK(method != NULL);
369 method->SetOatCodeOffset(GetCodeOffset());
370 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
371 method->SetCoreSpillMask(GetCoreSpillMask());
372 method->SetFpSpillMask(GetFpSpillMask());
373 method->SetOatMappingTableOffset(GetMappingTableOffset());
374 method->SetOatVmapTableOffset(GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700375 method->SetOatNativeGcMapOffset(GetNativeGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800376 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700377}
378
Brian Carlstrome24fa612011-09-29 00:53:55 -0700379} // namespace art