blob: 7196222ecbbe849a62074478922187ad7fa894f7 [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
19#include <sys/mman.h>
20
21#include "file.h"
22#include "os.h"
23#include "stl_util.h"
24
Logan Chien0c717dd2012-03-28 18:31:07 +080025#if defined(ART_USE_LLVM_COMPILER)
26#include "compiler_llvm/elf_loader.h"
27#endif
28
Brian Carlstrome24fa612011-09-29 00:53:55 -070029namespace art {
30
jeffhao262bf462011-10-20 18:36:32 -070031std::string OatFile::DexFilenameToOatFilename(const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070032 CHECK(IsValidDexFilename(location) || IsValidZipFilename(location));
Brian Carlstroma6cc8932012-01-04 14:44:07 -080033 std::string oat_location(location);
34 oat_location += ".oat";
jeffhao262bf462011-10-20 18:36:32 -070035 return oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070036}
37
Brian Carlstrome24fa612011-09-29 00:53:55 -070038OatFile* OatFile::Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080039 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070040 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080041 RelocationBehavior reloc,
Brian Carlstromf5822582012-03-19 22:34:31 -070042 bool writable) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070043 CHECK(!filename.empty()) << location;
Brian Carlstromf5822582012-03-19 22:34:31 -070044 UniquePtr<File> file(OS::OpenFile(filename.c_str(), writable, false));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080045 if (file.get() == NULL) {
Brian Carlstromf5822582012-03-19 22:34:31 -070046 return NULL;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080047 }
Logan Chien0c717dd2012-03-28 18:31:07 +080048 return Open(*file.get(), location, requested_base, reloc, writable);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080049}
Brian Carlstrome24fa612011-09-29 00:53:55 -070050
Brian Carlstrom5b332c82012-02-01 15:02:31 -080051OatFile* OatFile::Open(File& file,
52 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070053 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080054 RelocationBehavior reloc,
Brian Carlstromf5822582012-03-19 22:34:31 -070055 bool writable) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070056 CHECK(!location.empty());
57 if (!IsValidOatFilename(location)) {
58 LOG(WARNING) << "Attempting to open dex file with unknown extension '" << location << "'";
59 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080060 UniquePtr<OatFile> oat_file(new OatFile(location));
Logan Chien0c717dd2012-03-28 18:31:07 +080061 bool success = oat_file->Map(file, requested_base, reloc, writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -070062 if (!success) {
63 return NULL;
64 }
65 return oat_file.release();
66}
67
Logan Chien0c717dd2012-03-28 18:31:07 +080068OatFile::OatFile(const std::string& location)
69 : location_(location)
70#if defined(ART_USE_LLVM_COMPILER)
71 , elf_loader_(new compiler_llvm::ElfLoader())
72#endif
73{
Brian Carlstroma004aa92012-02-08 18:05:09 -080074 CHECK(!location_.empty());
75}
Brian Carlstrome24fa612011-09-29 00:53:55 -070076
77OatFile::~OatFile() {
78 STLDeleteValues(&oat_dex_files_);
Logan Chien0c717dd2012-03-28 18:31:07 +080079#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +080080 STLDeleteElements(&oat_elf_images_);
Logan Chien0c717dd2012-03-28 18:31:07 +080081#endif
Brian Carlstrome24fa612011-09-29 00:53:55 -070082}
83
Logan Chien0c717dd2012-03-28 18:31:07 +080084bool OatFile::Map(File& file,
85 byte* requested_base,
86#if defined(ART_USE_LLVM_COMPILER)
87 RelocationBehavior reloc,
88#else
89 RelocationBehavior /*UNUSED*/,
90#endif
91 bool writable) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070092 OatHeader oat_header;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080093 bool success = file.ReadFully(&oat_header, sizeof(oat_header));
Brian Carlstrome24fa612011-09-29 00:53:55 -070094 if (!success || !oat_header.IsValid()) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080095 LOG(WARNING) << "Invalid oat header " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -070096 return false;
97 }
98
Brian Carlstromf5822582012-03-19 22:34:31 -070099 int flags = 0;
100 int prot = 0;
101 if (writable) {
102 flags |= MAP_SHARED; // So changes will write through to file
103 prot |= (PROT_READ | PROT_WRITE);
104 } else {
105 flags |= MAP_PRIVATE;
106 prot |= PROT_READ;
107 }
108 if (requested_base != NULL) {
109 flags |= MAP_FIXED;
110 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800111 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(requested_base,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800112 file.Length(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700113 prot,
Brian Carlstrom89521892011-12-07 22:05:07 -0800114 flags,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800115 file.Fd(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800116 0));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117 if (map.get() == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800118 LOG(WARNING) << "Failed to map oat file " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 return false;
120 }
Ian Rogers30fab402012-01-23 15:43:46 -0800121 CHECK(requested_base == 0 || requested_base == map->Begin())
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700122 << GetLocation() << " " << reinterpret_cast<void*>(map->Begin());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800123 DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader))) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124
Elliott Hughese689d512012-01-18 23:39:47 -0800125 off_t code_offset = oat_header.GetExecutableOffset();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800126 if (code_offset < file.Length()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800127 byte* code_address = map->Begin() + code_offset;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800128 size_t code_length = file.Length() - code_offset;
Brian Carlstromf5822582012-03-19 22:34:31 -0700129 if (mprotect(code_address, code_length, prot | PROT_EXEC) != 0) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800130 PLOG(ERROR) << "Failed to make oat code executable in " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 return false;
132 }
133 } else {
134 // its possible to have no code if all the methods were abstract, native, etc
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800135 DCHECK_EQ(code_offset, RoundUp(file.Length(), kPageSize)) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700136 }
137
Ian Rogers30fab402012-01-23 15:43:46 -0800138 const byte* oat = map->Begin();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800139
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 oat += sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700141 oat += oat_header.GetImageFileLocationSize();
142
143 CHECK_LE(oat, map->End())
144 << reinterpret_cast<void*>(map->Begin())
145 << "+" << sizeof(OatHeader)
146 << "+" << oat_header.GetImageFileLocationSize()
147 << "<=" << reinterpret_cast<void*>(map->End())
148 << " " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) {
150 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800151 CHECK_GT(dex_file_location_size, 0U) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152 oat += sizeof(dex_file_location_size);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800153 CHECK_LT(oat, map->End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700154
155 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
156 oat += dex_file_location_size;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800157 CHECK_LT(oat, map->End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700158
159 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
160
161 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
162 oat += sizeof(dex_file_checksum);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800163 CHECK_LT(oat, map->End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164
Brian Carlstrom89521892011-12-07 22:05:07 -0800165 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800166 CHECK_GT(dex_file_offset, 0U) << GetLocation();
167 CHECK_LT(dex_file_offset, static_cast<uint32_t>(file.Length())) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800168 oat += sizeof(dex_file_offset);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800169 CHECK_LT(oat, map->End()) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800170
Ian Rogers30fab402012-01-23 15:43:46 -0800171 uint8_t* dex_file_pointer = map->Begin() + dex_file_offset;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800172 CHECK(DexFile::IsMagicValid(dex_file_pointer)) << GetLocation() << " " << dex_file_pointer;
173 CHECK(DexFile::IsVersionValid(dex_file_pointer)) << GetLocation() << " " << dex_file_pointer;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800174 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
175 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800176
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800177 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800178 CHECK_LE(oat, map->End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179
Elliott Hughesa0e18062012-04-13 15:59:59 -0700180 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
181 dex_file_location,
182 dex_file_checksum,
183 dex_file_pointer,
184 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700185 }
186
Logan Chien0c717dd2012-03-28 18:31:07 +0800187#if !defined(ART_USE_LLVM_COMPILER)
188 CHECK_EQ(oat_header.GetElfImageTableOffset(), 0u);
189 CHECK_EQ(oat_header.GetElfImageCount(), 0u);
190
191#else
Logan Chien0cc6ab62012-03-20 22:57:52 +0800192 oat = map->Begin() + oat_header.GetElfImageTableOffset();
193 CHECK((reinterpret_cast<uintptr_t>(oat) & 0x3) == 0);
194
195 for (uint32_t i = 0, end = oat_header.GetElfImageCount(); i < end; ++i) {
196 uint32_t elf_offset = *reinterpret_cast<const uint32_t*>(oat);
197 oat += sizeof(uint32_t);
198
199 uint32_t elf_size = *reinterpret_cast<const uint32_t*>(oat);
200 oat += sizeof(uint32_t);
201
Logan Chien0c717dd2012-03-28 18:31:07 +0800202 const byte* elf_begin = map->Begin() + elf_offset;
203
204 oat_elf_images_.push_back(new OatElfImage(this, elf_begin, elf_size));
205
206 if (!elf_loader_->LoadElfAt(i, ElfImage(elf_begin, elf_size), reloc)) {
207 LOG(ERROR) << "Failed to load ELF image. index: " << i;
208 }
Logan Chien0cc6ab62012-03-20 22:57:52 +0800209 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800210#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800211
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212 mem_map_.reset(map.release());
213 return true;
214}
215
216const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800217 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700218}
219
Ian Rogers30fab402012-01-23 15:43:46 -0800220const byte* OatFile::Begin() const {
221 CHECK(mem_map_->Begin() != NULL);
222 return mem_map_->Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700223}
224
Ian Rogers30fab402012-01-23 15:43:46 -0800225const byte* OatFile::End() const {
226 CHECK(mem_map_->End() != NULL);
227 return mem_map_->End();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228}
229
Ian Rogers7fe2c692011-12-06 16:35:59 -0800230const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
231 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700232 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800234 if (warn_if_not_found) {
235 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
236 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700238 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700239 return it->second;
240}
241
242std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
243 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700244 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700245 result.push_back(it->second);
246 }
247 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248}
249
Logan Chien0c717dd2012-03-28 18:31:07 +0800250void OatFile::RelocateExecutable() {
251#if defined(ART_USE_LLVM_COMPILER)
252 elf_loader_->RelocateExecutable();
253#endif
254}
255
Brian Carlstrome24fa612011-09-29 00:53:55 -0700256OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800257 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800258 uint32_t dex_file_location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800259 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800260 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261 : oat_file_(oat_file),
262 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800263 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800264 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800265 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266
267OatFile::OatDexFile::~OatDexFile() {}
268
Brian Carlstrom89521892011-12-07 22:05:07 -0800269const DexFile* OatFile::OatDexFile::OpenDexFile() const {
270 size_t length = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800271 return DexFile::Open(dex_file_pointer_, length, dex_file_location_, dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800272}
273
Brian Carlstromaded5f72011-10-07 17:15:04 -0700274const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800275 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
276
Ian Rogers30fab402012-01-23 15:43:46 -0800277 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
278 CHECK_LT(oat_class_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800279 Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer);
280
281 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800282 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800283
284 return new OatClass(oat_file_,
285 status,
286 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700287}
288
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800289OatFile::OatClass::OatClass(const OatFile* oat_file,
290 Class::Status status,
291 const OatMethodOffsets* methods_pointer)
292 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293
294OatFile::OatClass::~OatClass() {}
295
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800296Class::Status OatFile::OatClass::GetStatus() const {
297 return status_;
298}
299
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700300const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
301 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
302 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800303 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800304 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700305 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700306 oat_method_offsets.core_spill_mask_,
307 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800308 oat_method_offsets.mapping_table_offset_,
309 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800310 oat_method_offsets.gc_map_offset_,
Logan Chien0c717dd2012-03-28 18:31:07 +0800311 oat_method_offsets.invoke_stub_offset_
312#if defined(ART_USE_LLVM_COMPILER)
313 , oat_file_->elf_loader_.get(),
314 oat_method_offsets.code_elf_idx_,
Logan Chien937105a2012-04-02 02:37:37 +0800315 oat_method_offsets.code_elf_func_idx_,
316 oat_method_offsets.invoke_stub_elf_idx_,
317 oat_method_offsets.invoke_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,
336 const uint16_t invoke_stub_elf_func_idx
Logan Chien0c717dd2012-03-28 18:31:07 +0800337#endif
338 )
Ian Rogers30fab402012-01-23 15:43:46 -0800339 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800340 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700341 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700342 core_spill_mask_(core_spill_mask),
343 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800344 mapping_table_offset_(mapping_table_offset),
345 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800346 gc_map_offset_(gc_map_offset),
Logan Chien0c717dd2012-03-28 18:31:07 +0800347 invoke_stub_offset_(invoke_stub_offset)
348#if defined(ART_USE_LLVM_COMPILER)
349 , elf_loader_(elf_loader),
350 code_elf_idx_(code_elf_idx),
Logan Chien937105a2012-04-02 02:37:37 +0800351 code_elf_func_idx_(code_elf_func_idx),
352 invoke_stub_elf_idx_(invoke_stub_elf_idx),
353 invoke_stub_elf_func_idx_(invoke_stub_elf_func_idx)
Logan Chien0c717dd2012-03-28 18:31:07 +0800354#endif
355{
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700356#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800357 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
358 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700359 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
360 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800361 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700362 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
363 }
364 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800365 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700366 }
367#endif
368}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700369
370OatFile::OatMethod::~OatMethod() {}
371
Logan Chien0c717dd2012-03-28 18:31:07 +0800372const void* OatFile::OatMethod::GetCode() const {
373 if (!IsCodeInElf()) {
374 return GetOatPointer<const void*>(code_offset_);
375 } else {
376#if !defined(ART_USE_LLVM_COMPILER)
377 UNIMPLEMENTED(FATAL);
378 return NULL;
379#else
380 CHECK(elf_loader_ != NULL);
Logan Chien937105a2012-04-02 02:37:37 +0800381 const void* code =
382 elf_loader_->GetMethodCodeAddr(code_elf_idx_, code_elf_func_idx_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800383 CHECK(code != NULL);
384 return code;
385#endif
386 }
387}
388
389uint32_t OatFile::OatMethod::GetCodeSize() const {
390 if (!IsCodeInElf()) {
391 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
392
393 if (code == 0) {
394 return 0;
395 }
396 // TODO: make this Thumb2 specific
397 code &= ~0x1;
398 return reinterpret_cast<uint32_t*>(code)[-1];
399 } else {
Logan Chien14924fe2012-04-03 18:33:37 +0800400#if !defined(ART_USE_LLVM_COMPILER)
Logan Chien0c717dd2012-03-28 18:31:07 +0800401 UNIMPLEMENTED(ERROR);
402 return 0;
Logan Chien14924fe2012-04-03 18:33:37 +0800403#else
404 CHECK(elf_loader_ != NULL);
405 return elf_loader_->GetCodeSize(code_elf_idx_, code_elf_func_idx_);
406#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800407 }
408}
409
410const Method::InvokeStub* OatFile::OatMethod::GetInvokeStub() const {
411 if (!IsInvokeStubInElf()) {
412 return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_);
413 } else {
414#if !defined(ART_USE_LLVM_COMPILER)
415 UNIMPLEMENTED(FATAL);
416 return NULL;
417#else
418 CHECK(elf_loader_ != NULL);
Logan Chien937105a2012-04-02 02:37:37 +0800419 const Method::InvokeStub* stub =
420 elf_loader_->GetMethodInvokeStubAddr(invoke_stub_elf_idx_,
421 invoke_stub_elf_func_idx_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800422 CHECK(stub != NULL);
423 return stub;
424#endif
425 }
426}
427
428uint32_t OatFile::OatMethod::GetInvokeStubSize() const {
429 if (!IsInvokeStubInElf()) {
430 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
431 if (code == 0) {
432 return 0;
433 }
434 return reinterpret_cast<uint32_t*>(code)[-1];
435 } else {
Logan Chien14924fe2012-04-03 18:33:37 +0800436#if !defined(ART_USE_LLVM_COMPILER)
Logan Chien937105a2012-04-02 02:37:37 +0800437 UNIMPLEMENTED(WARNING);
Logan Chien0c717dd2012-03-28 18:31:07 +0800438 return 0;
Logan Chien14924fe2012-04-03 18:33:37 +0800439#else
440 CHECK(elf_loader_ != NULL);
441 return elf_loader_->GetCodeSize(invoke_stub_elf_idx_,
442 invoke_stub_elf_func_idx_);
443#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800444 }
445}
446
Brian Carlstromae826982011-11-09 01:33:42 -0800447void OatFile::OatMethod::LinkMethodPointers(Method* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700448 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800449 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700450 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700451 method->SetCoreSpillMask(core_spill_mask_);
452 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800453 method->SetMappingTable(GetMappingTable());
454 method->SetVmapTable(GetVmapTable());
Ian Rogers19846512012-02-24 11:42:47 -0800455 method->SetGcMap(GetGcMap()); // Note, used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800456 method->SetInvokeStub(GetInvokeStub());
457}
458
459void OatFile::OatMethod::LinkMethodOffsets(Method* method) const {
460 CHECK(method != NULL);
461 method->SetOatCodeOffset(GetCodeOffset());
462 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
463 method->SetCoreSpillMask(GetCoreSpillMask());
464 method->SetFpSpillMask(GetFpSpillMask());
465 method->SetOatMappingTableOffset(GetMappingTableOffset());
466 method->SetOatVmapTableOffset(GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800467 method->SetOatGcMapOffset(GetGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800468 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700469}
470
Logan Chien937105a2012-04-02 02:37:37 +0800471#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800472OatFile::OatElfImage::OatElfImage(const OatFile* oat_file,
473 const byte* addr,
474 uint32_t size)
475 : oat_file_(oat_file), elf_addr_(addr), elf_size_(size) {
476}
Logan Chien937105a2012-04-02 02:37:37 +0800477#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800478
Brian Carlstrome24fa612011-09-29 00:53:55 -0700479} // namespace art