blob: 8229f630b13ff62025e21cd5d4ea6844884d9cf4 [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 Carlstrom700c8d32012-11-05 10:42:02 -080019#include <dlfcn.h>
20
Elliott Hughes1aa246d2012-12-13 09:29:36 -080021#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080023#include "elf_file.h"
24#include "oat.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070025#include "os.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070026
27namespace 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 Carlstrom700c8d32012-11-05 10:42:02 -080036void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070037 CHECK(!location.empty());
38 if (!IsValidOatFilename(location)) {
Brian Carlstromf852fb22012-10-19 11:01:58 -070039 LOG(WARNING) << "Attempting to open oat file with unknown extension '" << location << "'";
Brian Carlstrom7a967b32012-03-28 15:23:10 -070040 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041}
42
43OatFile* OatFile::Open(std::vector<uint8_t>& oat_contents,
44 const std::string& location) {
45 CHECK(!oat_contents.empty()) << location;
46 CheckLocation(location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080047 UniquePtr<OatFile> oat_file(new OatFile(location));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080048 oat_file->begin_ = &oat_contents[0];
49 oat_file->end_ = &oat_contents[oat_contents.size()];
50 oat_file->Setup();
51 return oat_file.release();
52}
53
54OatFile* OatFile::Open(const std::string& filename,
55 const std::string& location,
56 byte* requested_base) {
57 CHECK(!filename.empty()) << location;
58 CheckLocation(location);
59 OatFile* result = OpenDlopen(filename, location, requested_base);
60 if (result != NULL) {
61 return result;
62 }
63 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false, false));
64 if (file.get() == NULL) {
65 return NULL;
66 }
67 return OpenElfFile(file.get(), location, requested_base, false);
68}
69
70OatFile* OatFile::Open(File* file,
71 const std::string& location,
72 byte* requested_base,
73 bool writable) {
74 CheckLocation(location);
75 return OpenElfFile(file, location, requested_base, writable);
76}
77
78OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
79 const std::string& location,
80 byte* requested_base) {
81 UniquePtr<OatFile> oat_file(new OatFile(location));
82 bool success = oat_file->Dlopen(elf_filename, requested_base);
83 if (!success) {
84 return NULL;
85 }
86 return oat_file.release();
87}
88
89OatFile* OatFile::OpenElfFile(File* file,
90 const std::string& location,
91 byte* requested_base,
92 bool writable) {
93 UniquePtr<OatFile> oat_file(new OatFile(location));
94 bool success = oat_file->ElfFileOpen(file, requested_base, writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -070095 if (!success) {
96 return NULL;
97 }
98 return oat_file.release();
99}
100
Logan Chien0c717dd2012-03-28 18:31:07 +0800101OatFile::OatFile(const std::string& location)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800102 : location_(location), begin_(NULL), end_(NULL), dlopen_handle_(NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800103 CHECK(!location_.empty());
104}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105
106OatFile::~OatFile() {
107 STLDeleteValues(&oat_dex_files_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800108 if (dlopen_handle_ != NULL) {
109 dlclose(dlopen_handle_);
110 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111}
112
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800113bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base) {
114
115 char* absolute_path = realpath(elf_filename.c_str(), NULL);
116 if (absolute_path == NULL) {
117 PLOG(WARNING) << "Failed to create absolute path for " << elf_filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118 return false;
119 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800120 dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
121 free(absolute_path);
122 if (dlopen_handle_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123 return false;
124 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800125 begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
126 if (begin_ == NULL) {
127 LOG(WARNING) << "Failed to find oatdata symbol in " << elf_filename << ": " << dlerror();
128 return false;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800130 if (requested_base != NULL && begin_ != requested_base) {
131 std::string maps;
132 ReadFileToString("/proc/self/maps", &maps);
133 LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
134 << reinterpret_cast<const void*>(begin_) << " != expected="
135 << reinterpret_cast<const void*>(requested_base)
136 << " /proc/self/maps:\n" << maps;
137 return false;
138 }
139 end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
140 if (end_ == NULL) {
141 LOG(WARNING) << "Failed to find oatlastword symbol in " << elf_filename << ": " << dlerror();
142 return false;
143 }
144 // Readjust to be non-inclusive upper bound.
145 end_ += sizeof(uint32_t);
146 Setup();
147 return true;
148}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800150bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable) {
151 elf_file_.reset(ElfFile::Open(file, writable, true));
152 if (elf_file_.get() == NULL) {
153 PLOG(WARNING) << "Failed to create ELF file for " << file->GetPath();
154 return false;
155 }
156 bool loaded = elf_file_->Load();
157 if (!loaded) {
158 LOG(WARNING) << "Failed to load ELF file " << file->GetPath();
159 return false;
160 }
161 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
162 if (begin_ == NULL) {
163 LOG(WARNING) << "Failed to find oatdata symbol in " << file->GetPath();
164 return false;
165 }
166 if (requested_base != NULL && begin_ != requested_base) {
167 std::string maps;
168 ReadFileToString("/proc/self/maps", &maps);
169 LOG(WARNING) << "Failed to find oatdata symbol at expected address: oatdata="
170 << reinterpret_cast<const void*>(begin_) << " != expected="
171 << reinterpret_cast<const void*>(requested_base)
172 << " /proc/self/maps:\n" << maps;
173 return false;
174 }
175 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
176 if (end_ == NULL) {
177 LOG(WARNING) << "Failed to find oatlastword symbol in " << file->GetPath();
178 return false;
179 }
180 // Readjust to be non-inclusive upper bound.
181 end_ += sizeof(uint32_t);
182 Setup();
183 return true;
184}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800185
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800186void OatFile::Setup() {
187 const byte* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700188 oat += sizeof(OatHeader);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800189 oat += GetOatHeader().GetImageFileLocationSize();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700190
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800191 CHECK_LE(oat, End())
192 << reinterpret_cast<const void*>(Begin())
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700193 << "+" << sizeof(OatHeader)
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800194 << "+" << GetOatHeader().GetImageFileLocationSize()
195 << "<=" << reinterpret_cast<const void*>(End())
196 << " " << GetLocation();
197 for (size_t i = 0; i < GetOatHeader().GetDexFileCount(); i++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700198 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800199 CHECK_GT(dex_file_location_size, 0U) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700200 oat += sizeof(dex_file_location_size);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202
203 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
204 oat += dex_file_location_size;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800205 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206
207 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
208
209 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
210 oat += sizeof(dex_file_checksum);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800211 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212
Brian Carlstrom89521892011-12-07 22:05:07 -0800213 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800214 CHECK_GT(dex_file_offset, 0U) << GetLocation();
215 CHECK_LT(dex_file_offset, Size()) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800216 oat += sizeof(dex_file_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800217 CHECK_LT(oat, End()) << GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800218
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800219 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700220 CHECK(DexFile::IsMagicValid(dex_file_pointer))
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800221 << GetLocation() << " " << dex_file_pointer;
Brian Carlstromf852fb22012-10-19 11:01:58 -0700222 CHECK(DexFile::IsVersionValid(dex_file_pointer))
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800223 << GetLocation() << " " << dex_file_pointer;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800224 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
225 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800226
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800227 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800228 CHECK_LE(oat, End()) << GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700229
Elliott Hughesa0e18062012-04-13 15:59:59 -0700230 oat_dex_files_.Put(dex_file_location, new OatDexFile(this,
231 dex_file_location,
232 dex_file_checksum,
233 dex_file_pointer,
234 methods_offsets_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700235 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236}
237
238const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800239 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240}
241
Ian Rogers30fab402012-01-23 15:43:46 -0800242const byte* OatFile::Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800243 CHECK(begin_ != NULL);
244 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245}
246
Ian Rogers30fab402012-01-23 15:43:46 -0800247const byte* OatFile::End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800248 CHECK(end_ != NULL);
249 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250}
251
Ian Rogers7fe2c692011-12-06 16:35:59 -0800252const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
253 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700254 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700255 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800256 if (warn_if_not_found) {
257 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
258 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700259 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700260 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700261 return it->second;
262}
263
264std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
265 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700266 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700267 result.push_back(it->second);
268 }
269 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270}
271
272OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800273 const std::string& dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800274 uint32_t dex_file_location_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800275 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800276 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700277 : oat_file_(oat_file),
278 dex_file_location_(dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800279 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800280 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800281 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700282
283OatFile::OatDexFile::~OatDexFile() {}
284
Ian Rogers05f28c62012-10-23 18:12:13 -0700285size_t OatFile::OatDexFile::FileSize() const {
286 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
287}
288
Brian Carlstrom89521892011-12-07 22:05:07 -0800289const DexFile* OatFile::OatDexFile::OpenDexFile() const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700290 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700291 dex_file_location_checksum_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800292}
293
Brian Carlstromaded5f72011-10-07 17:15:04 -0700294const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800295 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
296
Ian Rogers30fab402012-01-23 15:43:46 -0800297 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
298 CHECK_LT(oat_class_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800299 Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer);
300
301 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800302 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800303
304 return new OatClass(oat_file_,
305 status,
306 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700307}
308
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800309OatFile::OatClass::OatClass(const OatFile* oat_file,
310 Class::Status status,
311 const OatMethodOffsets* methods_pointer)
312 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700313
314OatFile::OatClass::~OatClass() {}
315
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800316Class::Status OatFile::OatClass::GetStatus() const {
317 return status_;
318}
319
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700320const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
321 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
322 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800323 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800324 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700325 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700326 oat_method_offsets.core_spill_mask_,
327 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800328 oat_method_offsets.mapping_table_offset_,
329 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800330 oat_method_offsets.gc_map_offset_,
Logan Chien0c717dd2012-03-28 18:31:07 +0800331 oat_method_offsets.invoke_stub_offset_
332#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800333 , oat_method_offsets.proxy_stub_offset_
Logan Chien0c717dd2012-03-28 18:31:07 +0800334#endif
335 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700336}
337
Brian Carlstromae826982011-11-09 01:33:42 -0800338OatFile::OatMethod::OatMethod(const byte* base,
339 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700340 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700341 const uint32_t core_spill_mask,
342 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800343 const uint32_t mapping_table_offset,
344 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800345 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800346 const uint32_t invoke_stub_offset
347#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800348 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800349#endif
350 )
Ian Rogers30fab402012-01-23 15:43:46 -0800351 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800352 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700353 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700354 core_spill_mask_(core_spill_mask),
355 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800356 mapping_table_offset_(mapping_table_offset),
357 vmap_table_offset_(vmap_table_offset),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700358 native_gc_map_offset_(gc_map_offset),
Logan Chien0c717dd2012-03-28 18:31:07 +0800359 invoke_stub_offset_(invoke_stub_offset)
360#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800361 , proxy_stub_offset_(proxy_stub_offset)
Logan Chien0c717dd2012-03-28 18:31:07 +0800362#endif
363{
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700364#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800365 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
366 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700367 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
368 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800369 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700370 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
371 }
372 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800373 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700374 }
375#endif
376}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700377
378OatFile::OatMethod::~OatMethod() {}
379
Logan Chien0c717dd2012-03-28 18:31:07 +0800380const void* OatFile::OatMethod::GetCode() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800381 return GetOatPointer<const void*>(code_offset_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800382}
383
384uint32_t OatFile::OatMethod::GetCodeSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800385 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
Logan Chien0c717dd2012-03-28 18:31:07 +0800386
Logan Chien971bf3f2012-05-01 15:47:55 +0800387 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800388 return 0;
389 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800390 // TODO: make this Thumb2 specific
391 code &= ~0x1;
392 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800393}
394
Mathieu Chartier66f19252012-09-18 08:57:04 -0700395AbstractMethod::InvokeStub* OatFile::OatMethod::GetInvokeStub() const {
Ian Rogers1b09b092012-08-20 15:35:52 -0700396 const byte* stub = GetOatPointer<const byte*>(invoke_stub_offset_);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700397 return reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(stub));
Logan Chien0c717dd2012-03-28 18:31:07 +0800398}
399
400uint32_t OatFile::OatMethod::GetInvokeStubSize() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800401 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
402 if (code == 0) {
Logan Chien0c717dd2012-03-28 18:31:07 +0800403 return 0;
404 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800405 // TODO: make this Thumb2 specific
406 code &= ~0x1;
407 return reinterpret_cast<uint32_t*>(code)[-1];
Logan Chien0c717dd2012-03-28 18:31:07 +0800408}
409
TDYa127eead4ac2012-06-03 07:15:25 -0700410#if defined(ART_USE_LLVM_COMPILER)
411const void* OatFile::OatMethod::GetProxyStub() const {
Logan Chien971bf3f2012-05-01 15:47:55 +0800412 return GetOatPointer<const void*>(proxy_stub_offset_);
TDYa127eead4ac2012-06-03 07:15:25 -0700413}
414#endif
415
Mathieu Chartier66f19252012-09-18 08:57:04 -0700416void OatFile::OatMethod::LinkMethodPointers(AbstractMethod* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700417 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800418 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700419 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700420 method->SetCoreSpillMask(core_spill_mask_);
421 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800422 method->SetMappingTable(GetMappingTable());
423 method->SetVmapTable(GetVmapTable());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700424 method->SetNativeGcMap(GetNativeGcMap()); // Note, used by native methods in work around JNI mode.
Brian Carlstromae826982011-11-09 01:33:42 -0800425 method->SetInvokeStub(GetInvokeStub());
426}
427
Mathieu Chartier66f19252012-09-18 08:57:04 -0700428void OatFile::OatMethod::LinkMethodOffsets(AbstractMethod* method) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800429 CHECK(method != NULL);
430 method->SetOatCodeOffset(GetCodeOffset());
431 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
432 method->SetCoreSpillMask(GetCoreSpillMask());
433 method->SetFpSpillMask(GetFpSpillMask());
434 method->SetOatMappingTableOffset(GetMappingTableOffset());
435 method->SetOatVmapTableOffset(GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700436 method->SetOatNativeGcMapOffset(GetNativeGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800437 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700438}
439
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440} // namespace art