blob: 239239bdd4e185638d9c27d7147c62feccc7acb7 [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
Logan Chien0c717dd2012-03-28 18:31:07 +080023#if defined(ART_USE_LLVM_COMPILER)
24#include "compiler_llvm/elf_loader.h"
25#endif
26
Brian Carlstrome24fa612011-09-29 00:53:55 -070027namespace art {
28
jeffhao262bf462011-10-20 18:36:32 -070029std::string OatFile::DexFilenameToOatFilename(const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070030 CHECK(IsValidDexFilename(location) || IsValidZipFilename(location));
Brian Carlstroma6cc8932012-01-04 14:44:07 -080031 std::string oat_location(location);
32 oat_location += ".oat";
jeffhao262bf462011-10-20 18:36:32 -070033 return oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070034}
35
Brian Carlstrome24fa612011-09-29 00:53:55 -070036OatFile* OatFile::Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080037 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070038 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080039 RelocationBehavior reloc,
Brian Carlstromf5822582012-03-19 22:34:31 -070040 bool writable) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070041 CHECK(!filename.empty()) << location;
Brian Carlstromf5822582012-03-19 22:34:31 -070042 UniquePtr<File> file(OS::OpenFile(filename.c_str(), writable, false));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080043 if (file.get() == NULL) {
Brian Carlstromf5822582012-03-19 22:34:31 -070044 return NULL;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080045 }
Logan Chien0c717dd2012-03-28 18:31:07 +080046 return Open(*file.get(), location, requested_base, reloc, writable);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080047}
Brian Carlstrome24fa612011-09-29 00:53:55 -070048
Brian Carlstrom5b332c82012-02-01 15:02:31 -080049OatFile* OatFile::Open(File& file,
50 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070051 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080052 RelocationBehavior reloc,
Brian Carlstromf5822582012-03-19 22:34:31 -070053 bool writable) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070054 CHECK(!location.empty());
55 if (!IsValidOatFilename(location)) {
56 LOG(WARNING) << "Attempting to open dex file with unknown extension '" << location << "'";
57 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080058 UniquePtr<OatFile> oat_file(new OatFile(location));
Logan Chien0c717dd2012-03-28 18:31:07 +080059 bool success = oat_file->Map(file, requested_base, reloc, writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -070060 if (!success) {
61 return NULL;
62 }
63 return oat_file.release();
64}
65
Logan Chien0c717dd2012-03-28 18:31:07 +080066OatFile::OatFile(const std::string& location)
67 : location_(location)
68#if defined(ART_USE_LLVM_COMPILER)
69 , elf_loader_(new compiler_llvm::ElfLoader())
70#endif
71{
Brian Carlstroma004aa92012-02-08 18:05:09 -080072 CHECK(!location_.empty());
73}
Brian Carlstrome24fa612011-09-29 00:53:55 -070074
75OatFile::~OatFile() {
76 STLDeleteValues(&oat_dex_files_);
Logan Chien0c717dd2012-03-28 18:31:07 +080077#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +080078 STLDeleteElements(&oat_elf_images_);
Logan Chien0c717dd2012-03-28 18:31:07 +080079#endif
Brian Carlstrome24fa612011-09-29 00:53:55 -070080}
81
Logan Chien0c717dd2012-03-28 18:31:07 +080082bool OatFile::Map(File& file,
83 byte* requested_base,
84#if defined(ART_USE_LLVM_COMPILER)
85 RelocationBehavior reloc,
86#else
87 RelocationBehavior /*UNUSED*/,
88#endif
89 bool writable) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 OatHeader oat_header;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080091 bool success = file.ReadFully(&oat_header, sizeof(oat_header));
Brian Carlstrome24fa612011-09-29 00:53:55 -070092 if (!success || !oat_header.IsValid()) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080093 LOG(WARNING) << "Invalid oat header " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -070094 return false;
95 }
96
Brian Carlstromf5822582012-03-19 22:34:31 -070097 int flags = 0;
98 int prot = 0;
99 if (writable) {
100 flags |= MAP_SHARED; // So changes will write through to file
101 prot |= (PROT_READ | PROT_WRITE);
102 } else {
103 flags |= MAP_PRIVATE;
104 prot |= PROT_READ;
105 }
106 if (requested_base != NULL) {
107 flags |= MAP_FIXED;
108 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800109 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(requested_base,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800110 file.Length(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700111 prot,
Brian Carlstrom89521892011-12-07 22:05:07 -0800112 flags,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800113 file.Fd(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800114 0));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115 if (map.get() == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800116 LOG(WARNING) << "Failed to map oat file " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117 return false;
118 }
Ian Rogers30fab402012-01-23 15:43:46 -0800119 CHECK(requested_base == 0 || requested_base == map->Begin())
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700120 << GetLocation() << " " << reinterpret_cast<void*>(map->Begin());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800121 DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader))) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122
Elliott Hughese689d512012-01-18 23:39:47 -0800123 off_t code_offset = oat_header.GetExecutableOffset();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800124 if (code_offset < file.Length()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800125 byte* code_address = map->Begin() + code_offset;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800126 size_t code_length = file.Length() - code_offset;
Brian Carlstromf5822582012-03-19 22:34:31 -0700127 if (mprotect(code_address, code_length, prot | PROT_EXEC) != 0) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800128 PLOG(ERROR) << "Failed to make oat code executable in " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129 return false;
130 }
131 } else {
132 // its possible to have no code if all the methods were abstract, native, etc
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800133 DCHECK_EQ(code_offset, RoundUp(file.Length(), kPageSize)) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700134 }
135
Ian Rogers30fab402012-01-23 15:43:46 -0800136 const byte* oat = map->Begin();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800137
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 oat += sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700139 oat += oat_header.GetImageFileLocationSize();
140
141 CHECK_LE(oat, map->End())
142 << reinterpret_cast<void*>(map->Begin())
143 << "+" << sizeof(OatHeader)
144 << "+" << oat_header.GetImageFileLocationSize()
145 << "<=" << reinterpret_cast<void*>(map->End())
146 << " " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147 for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) {
148 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800149 CHECK_GT(dex_file_location_size, 0U) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700150 oat += sizeof(dex_file_location_size);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800151 CHECK_LT(oat, map->End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152
153 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
154 oat += dex_file_location_size;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800155 CHECK_LT(oat, map->End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700156
157 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
158
159 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
160 oat += sizeof(dex_file_checksum);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800161 CHECK_LT(oat, map->End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162
Brian Carlstrom89521892011-12-07 22:05:07 -0800163 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800164 CHECK_GT(dex_file_offset, 0U) << GetLocation();
165 CHECK_LT(dex_file_offset, static_cast<uint32_t>(file.Length())) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800166 oat += sizeof(dex_file_offset);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800167 CHECK_LT(oat, map->End()) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800168
Ian Rogers30fab402012-01-23 15:43:46 -0800169 uint8_t* dex_file_pointer = map->Begin() + dex_file_offset;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800170 CHECK(DexFile::IsMagicValid(dex_file_pointer)) << GetLocation() << " " << dex_file_pointer;
171 CHECK(DexFile::IsVersionValid(dex_file_pointer)) << GetLocation() << " " << dex_file_pointer;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800172 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
173 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800174
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800175 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800176 CHECK_LE(oat, map->End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177
Elliott Hughesa0e18062012-04-13 15:59:59 -0700178 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
179 dex_file_location,
180 dex_file_checksum,
181 dex_file_pointer,
182 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700183 }
184
Logan Chien0c717dd2012-03-28 18:31:07 +0800185#if !defined(ART_USE_LLVM_COMPILER)
186 CHECK_EQ(oat_header.GetElfImageTableOffset(), 0u);
187 CHECK_EQ(oat_header.GetElfImageCount(), 0u);
188
189#else
Logan Chien0cc6ab62012-03-20 22:57:52 +0800190 oat = map->Begin() + oat_header.GetElfImageTableOffset();
Logan Chien8b0c8162012-06-22 13:05:45 +0800191 CHECK_EQ((reinterpret_cast<uintptr_t>(oat) & 0x3), 0u);
Logan Chien0cc6ab62012-03-20 22:57:52 +0800192
193 for (uint32_t i = 0, end = oat_header.GetElfImageCount(); i < end; ++i) {
194 uint32_t elf_offset = *reinterpret_cast<const uint32_t*>(oat);
195 oat += sizeof(uint32_t);
196
197 uint32_t elf_size = *reinterpret_cast<const uint32_t*>(oat);
198 oat += sizeof(uint32_t);
199
Logan Chien0c717dd2012-03-28 18:31:07 +0800200 const byte* elf_begin = map->Begin() + elf_offset;
201
202 oat_elf_images_.push_back(new OatElfImage(this, elf_begin, elf_size));
203
204 if (!elf_loader_->LoadElfAt(i, ElfImage(elf_begin, elf_size), reloc)) {
205 LOG(ERROR) << "Failed to load ELF image. index: " << i;
206 }
Logan Chien0cc6ab62012-03-20 22:57:52 +0800207 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800208#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800209
Brian Carlstrome24fa612011-09-29 00:53:55 -0700210 mem_map_.reset(map.release());
211 return true;
212}
213
214const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800215 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216}
217
Ian Rogers30fab402012-01-23 15:43:46 -0800218const byte* OatFile::Begin() const {
219 CHECK(mem_map_->Begin() != NULL);
220 return mem_map_->Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221}
222
Ian Rogers30fab402012-01-23 15:43:46 -0800223const byte* OatFile::End() const {
224 CHECK(mem_map_->End() != NULL);
225 return mem_map_->End();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226}
227
Ian Rogers7fe2c692011-12-06 16:35:59 -0800228const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
229 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700230 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700231 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800232 if (warn_if_not_found) {
233 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
234 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700235 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 return it->second;
238}
239
240std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
241 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700242 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243 result.push_back(it->second);
244 }
245 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700246}
247
Logan Chien0c717dd2012-03-28 18:31:07 +0800248void OatFile::RelocateExecutable() {
249#if defined(ART_USE_LLVM_COMPILER)
250 elf_loader_->RelocateExecutable();
251#endif
252}
253
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800255 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800256 uint32_t dex_file_location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800257 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800258 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700259 : oat_file_(oat_file),
260 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800261 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800262 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800263 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264
265OatFile::OatDexFile::~OatDexFile() {}
266
Brian Carlstrom89521892011-12-07 22:05:07 -0800267const DexFile* OatFile::OatDexFile::OpenDexFile() const {
268 size_t length = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800269 return DexFile::Open(dex_file_pointer_, length, dex_file_location_, dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800270}
271
Brian Carlstromaded5f72011-10-07 17:15:04 -0700272const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800273 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
274
Ian Rogers30fab402012-01-23 15:43:46 -0800275 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
276 CHECK_LT(oat_class_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800277 Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer);
278
279 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800280 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800281
282 return new OatClass(oat_file_,
283 status,
284 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700285}
286
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800287OatFile::OatClass::OatClass(const OatFile* oat_file,
288 Class::Status status,
289 const OatMethodOffsets* methods_pointer)
290 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700291
292OatFile::OatClass::~OatClass() {}
293
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800294Class::Status OatFile::OatClass::GetStatus() const {
295 return status_;
296}
297
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700298const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
299 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
300 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800301 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800302 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700303 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700304 oat_method_offsets.core_spill_mask_,
305 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800306 oat_method_offsets.mapping_table_offset_,
307 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800308 oat_method_offsets.gc_map_offset_,
Logan Chien0c717dd2012-03-28 18:31:07 +0800309 oat_method_offsets.invoke_stub_offset_
310#if defined(ART_USE_LLVM_COMPILER)
311 , oat_file_->elf_loader_.get(),
312 oat_method_offsets.code_elf_idx_,
Logan Chien937105a2012-04-02 02:37:37 +0800313 oat_method_offsets.code_elf_func_idx_,
314 oat_method_offsets.invoke_stub_elf_idx_,
TDYa127eead4ac2012-06-03 07:15:25 -0700315 oat_method_offsets.invoke_stub_elf_func_idx_,
Logan Chien7a2a23a2012-06-06 11:01:00 +0800316 oat_method_offsets.proxy_stub_elf_idx_,
TDYa127eead4ac2012-06-03 07:15:25 -0700317 oat_method_offsets.proxy_stub_elf_func_idx_
Logan Chien0c717dd2012-03-28 18:31:07 +0800318#endif
319 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700320}
321
Brian Carlstromae826982011-11-09 01:33:42 -0800322OatFile::OatMethod::OatMethod(const byte* base,
323 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700324 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700325 const uint32_t core_spill_mask,
326 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800327 const uint32_t mapping_table_offset,
328 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800329 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800330 const uint32_t invoke_stub_offset
331#if defined(ART_USE_LLVM_COMPILER)
332 , const compiler_llvm::ElfLoader* elf_loader,
Logan Chien937105a2012-04-02 02:37:37 +0800333 const uint16_t code_elf_idx,
334 const uint16_t code_elf_func_idx,
335 const uint16_t invoke_stub_elf_idx,
TDYa127eead4ac2012-06-03 07:15:25 -0700336 const uint16_t invoke_stub_elf_func_idx,
Logan Chien7a2a23a2012-06-06 11:01:00 +0800337 const uint16_t proxy_stub_elf_idx,
TDYa127eead4ac2012-06-03 07:15:25 -0700338 const uint16_t proxy_stub_elf_func_idx
Logan Chien0c717dd2012-03-28 18:31:07 +0800339#endif
340 )
Ian Rogers30fab402012-01-23 15:43:46 -0800341 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800342 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700343 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700344 core_spill_mask_(core_spill_mask),
345 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800346 mapping_table_offset_(mapping_table_offset),
347 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800348 gc_map_offset_(gc_map_offset),
Logan Chien0c717dd2012-03-28 18:31:07 +0800349 invoke_stub_offset_(invoke_stub_offset)
350#if defined(ART_USE_LLVM_COMPILER)
351 , elf_loader_(elf_loader),
352 code_elf_idx_(code_elf_idx),
Logan Chien937105a2012-04-02 02:37:37 +0800353 code_elf_func_idx_(code_elf_func_idx),
354 invoke_stub_elf_idx_(invoke_stub_elf_idx),
TDYa127eead4ac2012-06-03 07:15:25 -0700355 invoke_stub_elf_func_idx_(invoke_stub_elf_func_idx),
Logan Chien7a2a23a2012-06-06 11:01:00 +0800356 proxy_stub_elf_idx_(proxy_stub_elf_idx),
TDYa127eead4ac2012-06-03 07:15:25 -0700357 proxy_stub_elf_func_idx_(proxy_stub_elf_func_idx)
Logan Chien0c717dd2012-03-28 18:31:07 +0800358#endif
359{
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700360#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800361 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
362 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700363 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
364 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800365 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700366 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
367 }
368 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800369 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700370 }
371#endif
372}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373
374OatFile::OatMethod::~OatMethod() {}
375
Logan Chien0c717dd2012-03-28 18:31:07 +0800376const void* OatFile::OatMethod::GetCode() const {
377 if (!IsCodeInElf()) {
378 return GetOatPointer<const void*>(code_offset_);
379 } else {
380#if !defined(ART_USE_LLVM_COMPILER)
381 UNIMPLEMENTED(FATAL);
382 return NULL;
383#else
384 CHECK(elf_loader_ != NULL);
Logan Chien937105a2012-04-02 02:37:37 +0800385 const void* code =
386 elf_loader_->GetMethodCodeAddr(code_elf_idx_, code_elf_func_idx_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800387 CHECK(code != NULL);
388 return code;
389#endif
390 }
391}
392
393uint32_t OatFile::OatMethod::GetCodeSize() const {
394 if (!IsCodeInElf()) {
395 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
396
397 if (code == 0) {
398 return 0;
399 }
400 // TODO: make this Thumb2 specific
401 code &= ~0x1;
402 return reinterpret_cast<uint32_t*>(code)[-1];
403 } else {
Logan Chien14924fe2012-04-03 18:33:37 +0800404#if !defined(ART_USE_LLVM_COMPILER)
Logan Chien0c717dd2012-03-28 18:31:07 +0800405 UNIMPLEMENTED(ERROR);
406 return 0;
Logan Chien14924fe2012-04-03 18:33:37 +0800407#else
408 CHECK(elf_loader_ != NULL);
409 return elf_loader_->GetCodeSize(code_elf_idx_, code_elf_func_idx_);
410#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800411 }
412}
413
414const Method::InvokeStub* OatFile::OatMethod::GetInvokeStub() const {
415 if (!IsInvokeStubInElf()) {
416 return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_);
417 } else {
418#if !defined(ART_USE_LLVM_COMPILER)
419 UNIMPLEMENTED(FATAL);
420 return NULL;
421#else
422 CHECK(elf_loader_ != NULL);
Logan Chien937105a2012-04-02 02:37:37 +0800423 const Method::InvokeStub* stub =
424 elf_loader_->GetMethodInvokeStubAddr(invoke_stub_elf_idx_,
425 invoke_stub_elf_func_idx_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800426 CHECK(stub != NULL);
427 return stub;
428#endif
429 }
430}
431
432uint32_t OatFile::OatMethod::GetInvokeStubSize() const {
433 if (!IsInvokeStubInElf()) {
434 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
435 if (code == 0) {
436 return 0;
437 }
Logan Chien4284bb92012-06-06 15:30:44 +0800438 // TODO: make this Thumb2 specific
439 code &= ~0x1;
Logan Chien0c717dd2012-03-28 18:31:07 +0800440 return reinterpret_cast<uint32_t*>(code)[-1];
441 } else {
Logan Chien14924fe2012-04-03 18:33:37 +0800442#if !defined(ART_USE_LLVM_COMPILER)
Logan Chien937105a2012-04-02 02:37:37 +0800443 UNIMPLEMENTED(WARNING);
Logan Chien0c717dd2012-03-28 18:31:07 +0800444 return 0;
Logan Chien14924fe2012-04-03 18:33:37 +0800445#else
446 CHECK(elf_loader_ != NULL);
447 return elf_loader_->GetCodeSize(invoke_stub_elf_idx_,
448 invoke_stub_elf_func_idx_);
449#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800450 }
451}
452
TDYa127eead4ac2012-06-03 07:15:25 -0700453#if defined(ART_USE_LLVM_COMPILER)
454const void* OatFile::OatMethod::GetProxyStub() const {
455 CHECK(elf_loader_ != NULL);
456 const void* stub = NULL;
457 if (proxy_stub_elf_func_idx_ != static_cast<uint16_t>(-1)) {
Logan Chien7a2a23a2012-06-06 11:01:00 +0800458 stub = elf_loader_->GetMethodCodeAddr(proxy_stub_elf_idx_,
TDYa127eead4ac2012-06-03 07:15:25 -0700459 proxy_stub_elf_func_idx_);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800460 CHECK(stub != NULL);
TDYa127eead4ac2012-06-03 07:15:25 -0700461 }
462 return stub;
463}
464#endif
465
Brian Carlstromae826982011-11-09 01:33:42 -0800466void OatFile::OatMethod::LinkMethodPointers(Method* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700467 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800468 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700469 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700470 method->SetCoreSpillMask(core_spill_mask_);
471 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800472 method->SetMappingTable(GetMappingTable());
473 method->SetVmapTable(GetVmapTable());
Ian Rogers19846512012-02-24 11:42:47 -0800474 method->SetGcMap(GetGcMap()); // Note, used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800475 method->SetInvokeStub(GetInvokeStub());
476}
477
478void OatFile::OatMethod::LinkMethodOffsets(Method* method) const {
479 CHECK(method != NULL);
480 method->SetOatCodeOffset(GetCodeOffset());
481 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
482 method->SetCoreSpillMask(GetCoreSpillMask());
483 method->SetFpSpillMask(GetFpSpillMask());
484 method->SetOatMappingTableOffset(GetMappingTableOffset());
485 method->SetOatVmapTableOffset(GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800486 method->SetOatGcMapOffset(GetGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800487 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700488}
489
Logan Chien937105a2012-04-02 02:37:37 +0800490#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800491OatFile::OatElfImage::OatElfImage(const OatFile* oat_file,
492 const byte* addr,
493 uint32_t size)
494 : oat_file_(oat_file), elf_addr_(addr), elf_size_(size) {
495}
Logan Chien937105a2012-04-02 02:37:37 +0800496#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800497
Brian Carlstrome24fa612011-09-29 00:53:55 -0700498} // namespace art