blob: bd21f9a19ccb1a5f3e7216cdcf41836b7eb4c671 [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 Carlstrome24fa612011-09-29 00:53:55 -070019#include "file.h"
20#include "os.h"
21#include "stl_util.h"
22
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,
Brian Carlstrom5b332c82012-02-01 15:02:31 -080092 file.Length(),
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) {
Brian Carlstromf852fb22012-10-19 11:01:58 -070098 LOG(WARNING) << "Failed to map oat file from " << file.name() << " 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())
Brian Carlstromf852fb22012-10-19 11:01:58 -0700102 << file.name() << " for " << GetLocation() << " " << reinterpret_cast<void*>(map->Begin());
103 DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader)))
104 << file.name() << " 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();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800107 if (code_offset < file.Length()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800108 byte* code_address = map->Begin() + code_offset;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800109 size_t code_length = file.Length() - 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 "
112 << file.name() << " 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
Brian Carlstromf852fb22012-10-19 11:01:58 -0700117 DCHECK_EQ(code_offset, RoundUp(file.Length(), kPageSize))
118 << file.name() << " 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())
Brian Carlstromf852fb22012-10-19 11:01:58 -0700131 << " " << file.name() << " 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);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700134 CHECK_GT(dex_file_location_size, 0U) << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135 oat += sizeof(dex_file_location_size);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700136 CHECK_LT(oat, map->End()) << file.name() << " 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;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700140 CHECK_LT(oat, map->End()) << file.name() << " 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);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700146 CHECK_LT(oat, map->End()) << file.name() << " 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);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700149 CHECK_GT(dex_file_offset, 0U) << file.name() << " for " << GetLocation();
150 CHECK_LT(dex_file_offset, static_cast<uint32_t>(file.Length()))
151 << file.name() << " for " << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800152 oat += sizeof(dex_file_offset);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700153 CHECK_LT(oat, map->End()) << file.name() << " 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))
157 << file.name() << " for " << GetLocation() << " " << dex_file_pointer;
158 CHECK(DexFile::IsVersionValid(dex_file_pointer))
159 << file.name() << " 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_);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700164 CHECK_LE(oat, map->End()) << file.name() << " 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
Logan Chien0c717dd2012-03-28 18:31:07 +0800211void OatFile::RelocateExecutable() {
Shih-wei Liaoab646f92012-06-27 23:02:11 -0700212#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800213 UNIMPLEMENTED(WARNING) << "Relocate the executable";
Shih-wei Liaoab646f92012-06-27 23:02:11 -0700214#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800215}
216
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800218 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800219 uint32_t dex_file_location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800220 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800221 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700222 : oat_file_(oat_file),
223 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800224 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800225 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800226 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227
228OatFile::OatDexFile::~OatDexFile() {}
229
Ian Rogers05f28c62012-10-23 18:12:13 -0700230size_t OatFile::OatDexFile::FileSize() const {
231 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
232}
233
Brian Carlstrom89521892011-12-07 22:05:07 -0800234const DexFile* OatFile::OatDexFile::OpenDexFile() const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700235 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700236 dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800237}
238
Brian Carlstromaded5f72011-10-07 17:15:04 -0700239const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800240 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
241
Ian Rogers30fab402012-01-23 15:43:46 -0800242 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
243 CHECK_LT(oat_class_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800244 Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer);
245
246 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800247 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800248
249 return new OatClass(oat_file_,
250 status,
251 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700252}
253
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800254OatFile::OatClass::OatClass(const OatFile* oat_file,
255 Class::Status status,
256 const OatMethodOffsets* methods_pointer)
257 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700258
259OatFile::OatClass::~OatClass() {}
260
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800261Class::Status OatFile::OatClass::GetStatus() const {
262 return status_;
263}
264
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700265const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
266 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
267 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800268 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800269 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700270 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700271 oat_method_offsets.core_spill_mask_,
272 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800273 oat_method_offsets.mapping_table_offset_,
274 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800275 oat_method_offsets.gc_map_offset_,
Logan Chien0c717dd2012-03-28 18:31:07 +0800276 oat_method_offsets.invoke_stub_offset_
277#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800278 , oat_method_offsets.proxy_stub_offset_
Logan Chien0c717dd2012-03-28 18:31:07 +0800279#endif
280 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700281}
282
Brian Carlstromae826982011-11-09 01:33:42 -0800283OatFile::OatMethod::OatMethod(const byte* base,
284 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700285 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700286 const uint32_t core_spill_mask,
287 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800288 const uint32_t mapping_table_offset,
289 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800290 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800291 const uint32_t invoke_stub_offset
292#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800293 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800294#endif
295 )
Ian Rogers30fab402012-01-23 15:43:46 -0800296 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800297 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700298 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700299 core_spill_mask_(core_spill_mask),
300 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800301 mapping_table_offset_(mapping_table_offset),
302 vmap_table_offset_(vmap_table_offset),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700303 native_gc_map_offset_(gc_map_offset),
Logan Chien0c717dd2012-03-28 18:31:07 +0800304 invoke_stub_offset_(invoke_stub_offset)
305#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800306 , proxy_stub_offset_(proxy_stub_offset)
Logan Chien0c717dd2012-03-28 18:31:07 +0800307#endif
308{
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700309#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800310 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
311 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700312 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
313 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800314 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700315 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
316 }
317 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800318 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700319 }
320#endif
321}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700322
323OatFile::OatMethod::~OatMethod() {}
324
Logan Chien0c717dd2012-03-28 18:31:07 +0800325const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800326 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800327}
328
329uint32_t OatFile::OatMethod::GetCodeSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800330 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800331
Logan Chien971bf3f2012-05-01 15:47:55 +0800332 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800333 return 0;
334 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800335 // TODO: make this Thumb2 specific
336 code &= ~0x1;
337 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800338}
339
Mathieu Chartier66f19252012-09-18 08:57:04 -0700340AbstractMethod::InvokeStub* OatFile::OatMethod::GetInvokeStub() const {
Ian Rogers1b09b092012-08-20 15:35:52 -0700341 const byte* stub = GetOatPointer<const byte*>(invoke_stub_offset_);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700342 return reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(stub));
Logan Chien0c717dd2012-03-28 18:31:07 +0800343}
344
345uint32_t OatFile::OatMethod::GetInvokeStubSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800346 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
347 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800348 return 0;
349 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800350 // TODO: make this Thumb2 specific
351 code &= ~0x1;
352 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800353}
354
TDYa127eead4ac2012-06-03 07:15:25 -0700355#if defined(ART_USE_LLVM_COMPILER)
356const void* OatFile::OatMethod::GetProxyStub() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800357 return GetOatPointer<const void*>(proxy_stub_offset_);
TDYa127eead4ac2012-06-03 07:15:25 -0700358}
359#endif
360
Mathieu Chartier66f19252012-09-18 08:57:04 -0700361void OatFile::OatMethod::LinkMethodPointers(AbstractMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700362 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800363 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700364 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700365 method->SetCoreSpillMask(core_spill_mask_);
366 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800367 method->SetMappingTable(GetMappingTable());
368 method->SetVmapTable(GetVmapTable());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700369 method->SetNativeGcMap(GetNativeGcMap()); // Note, used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800370 method->SetInvokeStub(GetInvokeStub());
371}
372
Mathieu Chartier66f19252012-09-18 08:57:04 -0700373void OatFile::OatMethod::LinkMethodOffsets(AbstractMethod* method) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800374 CHECK(method != NULL);
375 method->SetOatCodeOffset(GetCodeOffset());
376 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
377 method->SetCoreSpillMask(GetCoreSpillMask());
378 method->SetFpSpillMask(GetFpSpillMask());
379 method->SetOatMappingTableOffset(GetMappingTableOffset());
380 method->SetOatVmapTableOffset(GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700381 method->SetOatNativeGcMapOffset(GetNativeGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800382 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700383}
384
Brian Carlstrome24fa612011-09-29 00:53:55 -0700385} // namespace art