blob: 168f0e34f336517dafb1a13f789d3d817df14a1c [file] [log] [blame]
Brian Carlstrome24fa612011-09-29 00:53:55 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "oat_file.h"
4
5#include <sys/mman.h>
6
7#include "file.h"
8#include "os.h"
9#include "stl_util.h"
10
11namespace art {
12
jeffhao262bf462011-10-20 18:36:32 -070013std::string OatFile::DexFilenameToOatFilename(const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070014 CHECK(IsValidDexFilename(location) || IsValidZipFilename(location));
Brian Carlstroma6cc8932012-01-04 14:44:07 -080015 std::string oat_location(location);
16 oat_location += ".oat";
jeffhao262bf462011-10-20 18:36:32 -070017 return oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070018}
19
Brian Carlstrome24fa612011-09-29 00:53:55 -070020OatFile* OatFile::Open(const std::string& filename,
21 const std::string& strip_location_prefix,
22 byte* requested_base) {
23 StringPiece location = filename;
24 if (!location.starts_with(strip_location_prefix)) {
25 LOG(ERROR) << filename << " does not start with " << strip_location_prefix;
26 return NULL;
27 }
28 location.remove_prefix(strip_location_prefix.size());
29
30 UniquePtr<OatFile> oat_file(new OatFile(location.ToString()));
31 bool success = oat_file->Read(filename, requested_base);
32 if (!success) {
33 return NULL;
34 }
35 return oat_file.release();
36}
37
38OatFile::OatFile(const std::string& filename) : location_(filename) {}
39
40OatFile::~OatFile() {
41 STLDeleteValues(&oat_dex_files_);
42}
43
44bool OatFile::Read(const std::string& filename, byte* requested_base) {
45 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
46 if (file.get() == NULL) {
47 return false;
48 }
49
50 OatHeader oat_header;
51 bool success = file->ReadFully(&oat_header, sizeof(oat_header));
52 if (!success || !oat_header.IsValid()) {
53 LOG(WARNING) << "Invalid oat header " << filename;
54 return false;
55 }
56
Brian Carlstrom89521892011-12-07 22:05:07 -080057 int flags = MAP_PRIVATE | ((requested_base != NULL) ? MAP_FIXED : 0);
58 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(requested_base,
59 file->Length(),
60 PROT_READ,
61 flags,
62 file->Fd(),
63 0));
Brian Carlstrome24fa612011-09-29 00:53:55 -070064 if (map.get() == NULL) {
65 LOG(WARNING) << "Failed to map oat file " << filename;
66 return false;
67 }
Ian Rogers30fab402012-01-23 15:43:46 -080068 CHECK(requested_base == 0 || requested_base == map->Begin())
69 << filename << " " << reinterpret_cast<void*>(map->Begin());
70 DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader))) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070071
Elliott Hughese689d512012-01-18 23:39:47 -080072 off_t code_offset = oat_header.GetExecutableOffset();
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 if (code_offset < file->Length()) {
Ian Rogers30fab402012-01-23 15:43:46 -080074 byte* code_address = map->Begin() + code_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 size_t code_length = file->Length() - code_offset;
76 if (mprotect(code_address, code_length, PROT_READ | PROT_EXEC) != 0) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080077 PLOG(ERROR) << "Failed to make oat code executable in " << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 return false;
79 }
80 } else {
81 // its possible to have no code if all the methods were abstract, native, etc
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080082 DCHECK_EQ(code_offset, RoundUp(file->Length(), kPageSize)) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 }
84
Ian Rogers30fab402012-01-23 15:43:46 -080085 const byte* oat = map->Begin();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080086
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 oat += sizeof(OatHeader);
Ian Rogers30fab402012-01-23 15:43:46 -080088 CHECK_LE(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070089 for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) {
90 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080091 CHECK_GT(dex_file_location_size, 0U) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070092 oat += sizeof(dex_file_location_size);
Ian Rogers30fab402012-01-23 15:43:46 -080093 CHECK_LT(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070094
95 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
96 oat += dex_file_location_size;
Ian Rogers30fab402012-01-23 15:43:46 -080097 CHECK_LT(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070098
99 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
100
101 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
102 oat += sizeof(dex_file_checksum);
Ian Rogers30fab402012-01-23 15:43:46 -0800103 CHECK_LT(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700104
Brian Carlstrom89521892011-12-07 22:05:07 -0800105 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800106 CHECK_GT(dex_file_offset, 0U) << filename;
107 CHECK_LT(dex_file_offset, static_cast<uint32_t>(file->Length())) << filename;
Brian Carlstrom89521892011-12-07 22:05:07 -0800108 oat += sizeof(dex_file_offset);
Ian Rogers30fab402012-01-23 15:43:46 -0800109 CHECK_LT(oat, map->End()) << filename;
Brian Carlstrom89521892011-12-07 22:05:07 -0800110
Ian Rogers30fab402012-01-23 15:43:46 -0800111 uint8_t* dex_file_pointer = map->Begin() + dex_file_offset;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800112 CHECK(DexFile::IsMagicValid(dex_file_pointer)) << filename << " " << dex_file_pointer;
113 CHECK(DexFile::IsVersionValid(dex_file_pointer)) << filename << " " << dex_file_pointer;
114 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
115 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800116
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800117 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers30fab402012-01-23 15:43:46 -0800118 CHECK_LE(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119
120 oat_dex_files_[dex_file_location] = new OatDexFile(this,
121 dex_file_location,
122 dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800123 dex_file_pointer,
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800124 methods_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125 }
126
127 mem_map_.reset(map.release());
128 return true;
129}
130
131const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800132 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700133}
134
Ian Rogers30fab402012-01-23 15:43:46 -0800135const byte* OatFile::Begin() const {
136 CHECK(mem_map_->Begin() != NULL);
137 return mem_map_->Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138}
139
Ian Rogers30fab402012-01-23 15:43:46 -0800140const byte* OatFile::End() const {
141 CHECK(mem_map_->End() != NULL);
142 return mem_map_->End();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143}
144
Ian Rogers7fe2c692011-12-06 16:35:59 -0800145const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
146 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700147 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800149 if (warn_if_not_found) {
150 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
151 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700154 return it->second;
155}
156
157std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
158 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700159 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700160 result.push_back(it->second);
161 }
162 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163}
164
165OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800166 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800168 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800169 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700170 : oat_file_(oat_file),
171 dex_file_location_(dex_file_location),
172 dex_file_checksum_(dex_file_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800173 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800174 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700175
176OatFile::OatDexFile::~OatDexFile() {}
177
Brian Carlstrom89521892011-12-07 22:05:07 -0800178const DexFile* OatFile::OatDexFile::OpenDexFile() const {
179 size_t length = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
180 return DexFile::Open(dex_file_pointer_, length, dex_file_location_);
181}
182
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800184 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
185
Ian Rogers30fab402012-01-23 15:43:46 -0800186 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
187 CHECK_LT(oat_class_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800188 Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer);
189
190 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800191 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800192
193 return new OatClass(oat_file_,
194 status,
195 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700196}
197
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800198OatFile::OatClass::OatClass(const OatFile* oat_file,
199 Class::Status status,
200 const OatMethodOffsets* methods_pointer)
201 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202
203OatFile::OatClass::~OatClass() {}
204
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800205Class::Status OatFile::OatClass::GetStatus() const {
206 return status_;
207}
208
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700209const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
210 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
211 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800212 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800213 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700214 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700215 oat_method_offsets.core_spill_mask_,
216 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800217 oat_method_offsets.mapping_table_offset_,
218 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800219 oat_method_offsets.gc_map_offset_,
Brian Carlstromae826982011-11-09 01:33:42 -0800220 oat_method_offsets.invoke_stub_offset_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700221}
222
Brian Carlstromae826982011-11-09 01:33:42 -0800223OatFile::OatMethod::OatMethod(const byte* base,
224 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700225 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700226 const uint32_t core_spill_mask,
227 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800228 const uint32_t mapping_table_offset,
229 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800230 const uint32_t gc_map_offset,
Brian Carlstromae826982011-11-09 01:33:42 -0800231 const uint32_t invoke_stub_offset)
Ian Rogers30fab402012-01-23 15:43:46 -0800232 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800233 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700234 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700235 core_spill_mask_(core_spill_mask),
236 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800237 mapping_table_offset_(mapping_table_offset),
238 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800239 gc_map_offset_(gc_map_offset),
Brian Carlstromae826982011-11-09 01:33:42 -0800240 invoke_stub_offset_(invoke_stub_offset) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700241#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800242 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
243 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700244 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
245 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800246 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700247 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
248 }
249 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800250 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700251 }
252#endif
253}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700254
255OatFile::OatMethod::~OatMethod() {}
256
Brian Carlstromae826982011-11-09 01:33:42 -0800257void OatFile::OatMethod::LinkMethodPointers(Method* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700258 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800259 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700260 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700261 method->SetCoreSpillMask(core_spill_mask_);
262 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800263 method->SetMappingTable(GetMappingTable());
264 method->SetVmapTable(GetVmapTable());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800265 method->SetGcMap(GetGcMap());
Brian Carlstromae826982011-11-09 01:33:42 -0800266 method->SetInvokeStub(GetInvokeStub());
267}
268
269void OatFile::OatMethod::LinkMethodOffsets(Method* method) const {
270 CHECK(method != NULL);
271 method->SetOatCodeOffset(GetCodeOffset());
272 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
273 method->SetCoreSpillMask(GetCoreSpillMask());
274 method->SetFpSpillMask(GetFpSpillMask());
275 method->SetOatMappingTableOffset(GetMappingTableOffset());
276 method->SetOatVmapTableOffset(GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800277 method->SetOatGcMapOffset(GetGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800278 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279}
280
281} // namespace art