blob: 145d2e2d6207ed705ee4526cd4786e5ea080a703 [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,
Logan Chien0c717dd2012-03-28 18:31:07 +080035 RelocationBehavior reloc,
Brian Carlstromf5822582012-03-19 22:34:31 -070036 bool writable) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070037 CHECK(!filename.empty()) << location;
Brian Carlstromf5822582012-03-19 22:34:31 -070038 UniquePtr<File> file(OS::OpenFile(filename.c_str(), writable, false));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080039 if (file.get() == NULL) {
Brian Carlstromf5822582012-03-19 22:34:31 -070040 return NULL;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080041 }
Logan Chien0c717dd2012-03-28 18:31:07 +080042 return Open(*file.get(), location, requested_base, reloc, writable);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080043}
Brian Carlstrome24fa612011-09-29 00:53:55 -070044
Brian Carlstrom5b332c82012-02-01 15:02:31 -080045OatFile* OatFile::Open(File& file,
46 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070047 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080048 RelocationBehavior reloc,
Brian Carlstromf5822582012-03-19 22:34:31 -070049 bool writable) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070050 CHECK(!location.empty());
51 if (!IsValidOatFilename(location)) {
Brian Carlstromf852fb22012-10-19 11:01:58 -070052 LOG(WARNING) << "Attempting to open oat file with unknown extension '" << location << "'";
Brian Carlstrom7a967b32012-03-28 15:23:10 -070053 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080054 UniquePtr<OatFile> oat_file(new OatFile(location));
Logan Chien0c717dd2012-03-28 18:31:07 +080055 bool success = oat_file->Map(file, requested_base, reloc, writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 if (!success) {
57 return NULL;
58 }
59 return oat_file.release();
60}
61
Logan Chien0c717dd2012-03-28 18:31:07 +080062OatFile::OatFile(const std::string& location)
Logan Chien971bf3f2012-05-01 15:47:55 +080063 : location_(location) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080064 CHECK(!location_.empty());
65}
Brian Carlstrome24fa612011-09-29 00:53:55 -070066
67OatFile::~OatFile() {
68 STLDeleteValues(&oat_dex_files_);
69}
70
Logan Chien0c717dd2012-03-28 18:31:07 +080071bool OatFile::Map(File& file,
72 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080073 RelocationBehavior /*UNUSED*/,
Logan Chien0c717dd2012-03-28 18:31:07 +080074 bool writable) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 OatHeader oat_header;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080076 bool success = file.ReadFully(&oat_header, sizeof(oat_header));
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 if (!success || !oat_header.IsValid()) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -080078 LOG(WARNING) << "Invalid oat header " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 return false;
80 }
81
Brian Carlstromf5822582012-03-19 22:34:31 -070082 int flags = 0;
83 int prot = 0;
84 if (writable) {
85 flags |= MAP_SHARED; // So changes will write through to file
86 prot |= (PROT_READ | PROT_WRITE);
87 } else {
88 flags |= MAP_PRIVATE;
89 prot |= PROT_READ;
90 }
91 if (requested_base != NULL) {
92 flags |= MAP_FIXED;
93 }
Brian Carlstrom89521892011-12-07 22:05:07 -080094 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(requested_base,
Brian Carlstrom5b332c82012-02-01 15:02:31 -080095 file.Length(),
Brian Carlstromf5822582012-03-19 22:34:31 -070096 prot,
Brian Carlstrom89521892011-12-07 22:05:07 -080097 flags,
Brian Carlstrom5b332c82012-02-01 15:02:31 -080098 file.Fd(),
Brian Carlstrom89521892011-12-07 22:05:07 -080099 0));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700100 if (map.get() == NULL) {
Brian Carlstromf852fb22012-10-19 11:01:58 -0700101 LOG(WARNING) << "Failed to map oat file from " << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700102 return false;
103 }
Ian Rogers30fab402012-01-23 15:43:46 -0800104 CHECK(requested_base == 0 || requested_base == map->Begin())
Brian Carlstromf852fb22012-10-19 11:01:58 -0700105 << file.name() << " for " << GetLocation() << " " << reinterpret_cast<void*>(map->Begin());
106 DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader)))
107 << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108
Elliott Hughese689d512012-01-18 23:39:47 -0800109 off_t code_offset = oat_header.GetExecutableOffset();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800110 if (code_offset < file.Length()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800111 byte* code_address = map->Begin() + code_offset;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800112 size_t code_length = file.Length() - code_offset;
Brian Carlstromf5822582012-03-19 22:34:31 -0700113 if (mprotect(code_address, code_length, prot | PROT_EXEC) != 0) {
Brian Carlstromf852fb22012-10-19 11:01:58 -0700114 PLOG(ERROR) << "Failed to make oat code executable in "
115 << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700116 return false;
117 }
118 } else {
119 // its possible to have no code if all the methods were abstract, native, etc
Brian Carlstromf852fb22012-10-19 11:01:58 -0700120 DCHECK_EQ(code_offset, RoundUp(file.Length(), kPageSize))
121 << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122 }
123
Ian Rogers30fab402012-01-23 15:43:46 -0800124 const byte* oat = map->Begin();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800125
Brian Carlstrome24fa612011-09-29 00:53:55 -0700126 oat += sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700127 oat += oat_header.GetImageFileLocationSize();
128
129 CHECK_LE(oat, map->End())
130 << reinterpret_cast<void*>(map->Begin())
131 << "+" << sizeof(OatHeader)
132 << "+" << oat_header.GetImageFileLocationSize()
133 << "<=" << reinterpret_cast<void*>(map->End())
Brian Carlstromf852fb22012-10-19 11:01:58 -0700134 << " " << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135 for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) {
136 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700137 CHECK_GT(dex_file_location_size, 0U) << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 oat += sizeof(dex_file_location_size);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700139 CHECK_LT(oat, map->End()) << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140
141 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
142 oat += dex_file_location_size;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700143 CHECK_LT(oat, map->End()) << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144
145 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
146
147 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
148 oat += sizeof(dex_file_checksum);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700149 CHECK_LT(oat, map->End()) << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700150
Brian Carlstrom89521892011-12-07 22:05:07 -0800151 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700152 CHECK_GT(dex_file_offset, 0U) << file.name() << " for " << GetLocation();
153 CHECK_LT(dex_file_offset, static_cast<uint32_t>(file.Length()))
154 << file.name() << " for " << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800155 oat += sizeof(dex_file_offset);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700156 CHECK_LT(oat, map->End()) << file.name() << " for " << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800157
Ian Rogers30fab402012-01-23 15:43:46 -0800158 uint8_t* dex_file_pointer = map->Begin() + dex_file_offset;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700159 CHECK(DexFile::IsMagicValid(dex_file_pointer))
160 << file.name() << " for " << GetLocation() << " " << dex_file_pointer;
161 CHECK(DexFile::IsVersionValid(dex_file_pointer))
162 << file.name() << " for " << GetLocation() << " " << dex_file_pointer;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800163 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
164 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800165
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800166 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Brian Carlstromf852fb22012-10-19 11:01:58 -0700167 CHECK_LE(oat, map->End()) << file.name() << " for " << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700168
Elliott Hughesa0e18062012-04-13 15:59:59 -0700169 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
170 dex_file_location,
171 dex_file_checksum,
172 dex_file_pointer,
173 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174 }
175
176 mem_map_.reset(map.release());
177 return true;
178}
179
180const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800181 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700182}
183
Ian Rogers30fab402012-01-23 15:43:46 -0800184const byte* OatFile::Begin() const {
185 CHECK(mem_map_->Begin() != NULL);
186 return mem_map_->Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700187}
188
Ian Rogers30fab402012-01-23 15:43:46 -0800189const byte* OatFile::End() const {
190 CHECK(mem_map_->End() != NULL);
191 return mem_map_->End();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700192}
193
Ian Rogers7fe2c692011-12-06 16:35:59 -0800194const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
195 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700196 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700197 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800198 if (warn_if_not_found) {
199 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
200 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700201 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700203 return it->second;
204}
205
206std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
207 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700208 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700209 result.push_back(it->second);
210 }
211 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212}
213
Logan Chien0c717dd2012-03-28 18:31:07 +0800214void OatFile::RelocateExecutable() {
Shih-wei Liaoab646f92012-06-27 23:02:11 -0700215#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800216 UNIMPLEMENTED(WARNING) << "Relocate the executable";
Shih-wei Liaoab646f92012-06-27 23:02:11 -0700217#endif
Logan Chien0c717dd2012-03-28 18:31:07 +0800218}
219
Brian Carlstrome24fa612011-09-29 00:53:55 -0700220OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800221 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800222 uint32_t dex_file_location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800223 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800224 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225 : oat_file_(oat_file),
226 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800227 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800228 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800229 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700230
231OatFile::OatDexFile::~OatDexFile() {}
232
Ian Rogers05f28c62012-10-23 18:12:13 -0700233size_t OatFile::OatDexFile::FileSize() const {
234 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
235}
236
Brian Carlstrom89521892011-12-07 22:05:07 -0800237const DexFile* OatFile::OatDexFile::OpenDexFile() const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700238 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700239 dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800240}
241
Brian Carlstromaded5f72011-10-07 17:15:04 -0700242const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800243 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
244
Ian Rogers30fab402012-01-23 15:43:46 -0800245 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
246 CHECK_LT(oat_class_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800247 Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer);
248
249 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800250 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800251
252 return new OatClass(oat_file_,
253 status,
254 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700255}
256
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800257OatFile::OatClass::OatClass(const OatFile* oat_file,
258 Class::Status status,
259 const OatMethodOffsets* methods_pointer)
260 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261
262OatFile::OatClass::~OatClass() {}
263
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800264Class::Status OatFile::OatClass::GetStatus() const {
265 return status_;
266}
267
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700268const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
269 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
270 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800271 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800272 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700273 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700274 oat_method_offsets.core_spill_mask_,
275 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800276 oat_method_offsets.mapping_table_offset_,
277 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800278 oat_method_offsets.gc_map_offset_,
Logan Chien0c717dd2012-03-28 18:31:07 +0800279 oat_method_offsets.invoke_stub_offset_
280#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800281 , oat_method_offsets.proxy_stub_offset_
Logan Chien0c717dd2012-03-28 18:31:07 +0800282#endif
283 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700284}
285
Brian Carlstromae826982011-11-09 01:33:42 -0800286OatFile::OatMethod::OatMethod(const byte* base,
287 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700288 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700289 const uint32_t core_spill_mask,
290 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800291 const uint32_t mapping_table_offset,
292 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800293 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800294 const uint32_t invoke_stub_offset
295#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800296 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800297#endif
298 )
Ian Rogers30fab402012-01-23 15:43:46 -0800299 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800300 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700301 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700302 core_spill_mask_(core_spill_mask),
303 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800304 mapping_table_offset_(mapping_table_offset),
305 vmap_table_offset_(vmap_table_offset),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700306 native_gc_map_offset_(gc_map_offset),
Logan Chien0c717dd2012-03-28 18:31:07 +0800307 invoke_stub_offset_(invoke_stub_offset)
308#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800309 , proxy_stub_offset_(proxy_stub_offset)
Logan Chien0c717dd2012-03-28 18:31:07 +0800310#endif
311{
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700312#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800313 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
314 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700315 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
316 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800317 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700318 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
319 }
320 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800321 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700322 }
323#endif
324}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700325
326OatFile::OatMethod::~OatMethod() {}
327
Logan Chien0c717dd2012-03-28 18:31:07 +0800328const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800329 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800330}
331
332uint32_t OatFile::OatMethod::GetCodeSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800333 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800334
Logan Chien971bf3f2012-05-01 15:47:55 +0800335 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800336 return 0;
337 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800338 // TODO: make this Thumb2 specific
339 code &= ~0x1;
340 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800341}
342
Mathieu Chartier66f19252012-09-18 08:57:04 -0700343AbstractMethod::InvokeStub* OatFile::OatMethod::GetInvokeStub() const {
Ian Rogers1b09b092012-08-20 15:35:52 -0700344 const byte* stub = GetOatPointer<const byte*>(invoke_stub_offset_);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700345 return reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(stub));
Logan Chien0c717dd2012-03-28 18:31:07 +0800346}
347
348uint32_t OatFile::OatMethod::GetInvokeStubSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800349 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
350 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800351 return 0;
352 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800353 // TODO: make this Thumb2 specific
354 code &= ~0x1;
355 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800356}
357
TDYa127eead4ac2012-06-03 07:15:25 -0700358#if defined(ART_USE_LLVM_COMPILER)
359const void* OatFile::OatMethod::GetProxyStub() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800360 return GetOatPointer<const void*>(proxy_stub_offset_);
TDYa127eead4ac2012-06-03 07:15:25 -0700361}
362#endif
363
Mathieu Chartier66f19252012-09-18 08:57:04 -0700364void OatFile::OatMethod::LinkMethodPointers(AbstractMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700365 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800366 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700367 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 method->SetCoreSpillMask(core_spill_mask_);
369 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800370 method->SetMappingTable(GetMappingTable());
371 method->SetVmapTable(GetVmapTable());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700372 method->SetNativeGcMap(GetNativeGcMap()); // Note, used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800373 method->SetInvokeStub(GetInvokeStub());
374}
375
Mathieu Chartier66f19252012-09-18 08:57:04 -0700376void OatFile::OatMethod::LinkMethodOffsets(AbstractMethod* method) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800377 CHECK(method != NULL);
378 method->SetOatCodeOffset(GetCodeOffset());
379 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
380 method->SetCoreSpillMask(GetCoreSpillMask());
381 method->SetFpSpillMask(GetFpSpillMask());
382 method->SetOatMappingTableOffset(GetMappingTableOffset());
383 method->SetOatVmapTableOffset(GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700384 method->SetOatNativeGcMapOffset(GetNativeGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800385 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700386}
387
Brian Carlstrome24fa612011-09-29 00:53:55 -0700388} // namespace art