blob: 4bcd9679f11a429d22ec290a448eb537e741704e [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
25namespace art {
26
jeffhao262bf462011-10-20 18:36:32 -070027std::string OatFile::DexFilenameToOatFilename(const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070028 CHECK(IsValidDexFilename(location) || IsValidZipFilename(location));
Brian Carlstroma6cc8932012-01-04 14:44:07 -080029 std::string oat_location(location);
30 oat_location += ".oat";
jeffhao262bf462011-10-20 18:36:32 -070031 return oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -070032}
33
Brian Carlstrome24fa612011-09-29 00:53:55 -070034OatFile* OatFile::Open(const std::string& filename,
35 const std::string& strip_location_prefix,
36 byte* requested_base) {
37 StringPiece location = filename;
38 if (!location.starts_with(strip_location_prefix)) {
39 LOG(ERROR) << filename << " does not start with " << strip_location_prefix;
40 return NULL;
41 }
42 location.remove_prefix(strip_location_prefix.size());
43
44 UniquePtr<OatFile> oat_file(new OatFile(location.ToString()));
45 bool success = oat_file->Read(filename, requested_base);
46 if (!success) {
47 return NULL;
48 }
49 return oat_file.release();
50}
51
52OatFile::OatFile(const std::string& filename) : location_(filename) {}
53
54OatFile::~OatFile() {
55 STLDeleteValues(&oat_dex_files_);
56}
57
58bool OatFile::Read(const std::string& filename, byte* requested_base) {
59 UniquePtr<File> file(OS::OpenFile(filename.c_str(), false));
60 if (file.get() == NULL) {
61 return false;
62 }
63
64 OatHeader oat_header;
65 bool success = file->ReadFully(&oat_header, sizeof(oat_header));
66 if (!success || !oat_header.IsValid()) {
67 LOG(WARNING) << "Invalid oat header " << filename;
68 return false;
69 }
70
Brian Carlstrom89521892011-12-07 22:05:07 -080071 int flags = MAP_PRIVATE | ((requested_base != NULL) ? MAP_FIXED : 0);
72 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(requested_base,
73 file->Length(),
74 PROT_READ,
75 flags,
76 file->Fd(),
77 0));
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 if (map.get() == NULL) {
79 LOG(WARNING) << "Failed to map oat file " << filename;
80 return false;
81 }
Ian Rogers30fab402012-01-23 15:43:46 -080082 CHECK(requested_base == 0 || requested_base == map->Begin())
83 << filename << " " << reinterpret_cast<void*>(map->Begin());
84 DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader))) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070085
Elliott Hughese689d512012-01-18 23:39:47 -080086 off_t code_offset = oat_header.GetExecutableOffset();
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 if (code_offset < file->Length()) {
Ian Rogers30fab402012-01-23 15:43:46 -080088 byte* code_address = map->Begin() + code_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -070089 size_t code_length = file->Length() - code_offset;
90 if (mprotect(code_address, code_length, PROT_READ | PROT_EXEC) != 0) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080091 PLOG(ERROR) << "Failed to make oat code executable in " << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070092 return false;
93 }
94 } else {
95 // its possible to have no code if all the methods were abstract, native, etc
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080096 DCHECK_EQ(code_offset, RoundUp(file->Length(), kPageSize)) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 }
98
Ian Rogers30fab402012-01-23 15:43:46 -080099 const byte* oat = map->Begin();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800100
Brian Carlstrome24fa612011-09-29 00:53:55 -0700101 oat += sizeof(OatHeader);
Ian Rogers30fab402012-01-23 15:43:46 -0800102 CHECK_LE(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103 for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) {
104 size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800105 CHECK_GT(dex_file_location_size, 0U) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700106 oat += sizeof(dex_file_location_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800107 CHECK_LT(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108
109 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
110 oat += dex_file_location_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800111 CHECK_LT(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700112
113 std::string dex_file_location(dex_file_location_data, dex_file_location_size);
114
115 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
116 oat += sizeof(dex_file_checksum);
Ian Rogers30fab402012-01-23 15:43:46 -0800117 CHECK_LT(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118
Brian Carlstrom89521892011-12-07 22:05:07 -0800119 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800120 CHECK_GT(dex_file_offset, 0U) << filename;
121 CHECK_LT(dex_file_offset, static_cast<uint32_t>(file->Length())) << filename;
Brian Carlstrom89521892011-12-07 22:05:07 -0800122 oat += sizeof(dex_file_offset);
Ian Rogers30fab402012-01-23 15:43:46 -0800123 CHECK_LT(oat, map->End()) << filename;
Brian Carlstrom89521892011-12-07 22:05:07 -0800124
Ian Rogers30fab402012-01-23 15:43:46 -0800125 uint8_t* dex_file_pointer = map->Begin() + dex_file_offset;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800126 CHECK(DexFile::IsMagicValid(dex_file_pointer)) << filename << " " << dex_file_pointer;
127 CHECK(DexFile::IsVersionValid(dex_file_pointer)) << filename << " " << dex_file_pointer;
128 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
129 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800130
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800131 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers30fab402012-01-23 15:43:46 -0800132 CHECK_LE(oat, map->End()) << filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700133
134 oat_dex_files_[dex_file_location] = new OatDexFile(this,
135 dex_file_location,
136 dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800137 dex_file_pointer,
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800138 methods_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139 }
140
141 mem_map_.reset(map.release());
142 return true;
143}
144
145const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800146 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147}
148
Ian Rogers30fab402012-01-23 15:43:46 -0800149const byte* OatFile::Begin() const {
150 CHECK(mem_map_->Begin() != NULL);
151 return mem_map_->Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152}
153
Ian Rogers30fab402012-01-23 15:43:46 -0800154const byte* OatFile::End() const {
155 CHECK(mem_map_->End() != NULL);
156 return mem_map_->End();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700157}
158
Ian Rogers7fe2c692011-12-06 16:35:59 -0800159const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location,
160 bool warn_if_not_found) const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700161 Table::const_iterator it = oat_dex_files_.find(dex_file_location);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162 if (it == oat_dex_files_.end()) {
Ian Rogers7fe2c692011-12-06 16:35:59 -0800163 if (warn_if_not_found) {
164 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location;
165 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 return NULL;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700168 return it->second;
169}
170
171std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const {
172 std::vector<const OatFile::OatDexFile*> result;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700173 for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700174 result.push_back(it->second);
175 }
176 return result;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177}
178
179OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800180 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700181 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800182 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800183 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184 : oat_file_(oat_file),
185 dex_file_location_(dex_file_location),
186 dex_file_checksum_(dex_file_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800187 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800188 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700189
190OatFile::OatDexFile::~OatDexFile() {}
191
Brian Carlstrom89521892011-12-07 22:05:07 -0800192const DexFile* OatFile::OatDexFile::OpenDexFile() const {
193 size_t length = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
194 return DexFile::Open(dex_file_pointer_, length, dex_file_location_);
195}
196
Brian Carlstromaded5f72011-10-07 17:15:04 -0700197const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800198 uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index];
199
Ian Rogers30fab402012-01-23 15:43:46 -0800200 const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
201 CHECK_LT(oat_class_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800202 Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer);
203
204 const byte* methods_pointer = oat_class_pointer + sizeof(status);
Ian Rogers30fab402012-01-23 15:43:46 -0800205 CHECK_LT(methods_pointer, oat_file_->End());
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800206
207 return new OatClass(oat_file_,
208 status,
209 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700210}
211
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800212OatFile::OatClass::OatClass(const OatFile* oat_file,
213 Class::Status status,
214 const OatMethodOffsets* methods_pointer)
215 : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216
217OatFile::OatClass::~OatClass() {}
218
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800219Class::Status OatFile::OatClass::GetStatus() const {
220 return status_;
221}
222
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700223const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
224 const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index];
225 return OatMethod(
Ian Rogers30fab402012-01-23 15:43:46 -0800226 oat_file_->Begin(),
Brian Carlstromae826982011-11-09 01:33:42 -0800227 oat_method_offsets.code_offset_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700228 oat_method_offsets.frame_size_in_bytes_,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700229 oat_method_offsets.core_spill_mask_,
230 oat_method_offsets.fp_spill_mask_,
Brian Carlstromae826982011-11-09 01:33:42 -0800231 oat_method_offsets.mapping_table_offset_,
232 oat_method_offsets.vmap_table_offset_,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800233 oat_method_offsets.gc_map_offset_,
Brian Carlstromae826982011-11-09 01:33:42 -0800234 oat_method_offsets.invoke_stub_offset_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700235}
236
Brian Carlstromae826982011-11-09 01:33:42 -0800237OatFile::OatMethod::OatMethod(const byte* base,
238 const uint32_t code_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700239 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700240 const uint32_t core_spill_mask,
241 const uint32_t fp_spill_mask,
Brian Carlstromae826982011-11-09 01:33:42 -0800242 const uint32_t mapping_table_offset,
243 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800244 const uint32_t gc_map_offset,
Brian Carlstromae826982011-11-09 01:33:42 -0800245 const uint32_t invoke_stub_offset)
Ian Rogers30fab402012-01-23 15:43:46 -0800246 : begin_(base),
Brian Carlstromae826982011-11-09 01:33:42 -0800247 code_offset_(code_offset),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700248 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700249 core_spill_mask_(core_spill_mask),
250 fp_spill_mask_(fp_spill_mask),
Brian Carlstromae826982011-11-09 01:33:42 -0800251 mapping_table_offset_(mapping_table_offset),
252 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800253 gc_map_offset_(gc_map_offset),
Brian Carlstromae826982011-11-09 01:33:42 -0800254 invoke_stub_offset_(invoke_stub_offset) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700255#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800256 if (mapping_table_offset_ != 0) { // implies non-native, non-stub code
257 if (vmap_table_offset_ == 0) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700258 DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
259 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800260 const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700261 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_)));
262 }
263 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800264 DCHECK_EQ(vmap_table_offset_, 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700265 }
266#endif
267}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700268
269OatFile::OatMethod::~OatMethod() {}
270
Brian Carlstromae826982011-11-09 01:33:42 -0800271void OatFile::OatMethod::LinkMethodPointers(Method* method) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700272 CHECK(method != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800273 method->SetCode(GetCode());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700274 method->SetFrameSizeInBytes(frame_size_in_bytes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700275 method->SetCoreSpillMask(core_spill_mask_);
276 method->SetFpSpillMask(fp_spill_mask_);
Brian Carlstromae826982011-11-09 01:33:42 -0800277 method->SetMappingTable(GetMappingTable());
278 method->SetVmapTable(GetVmapTable());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800279 method->SetGcMap(GetGcMap());
Brian Carlstromae826982011-11-09 01:33:42 -0800280 method->SetInvokeStub(GetInvokeStub());
281}
282
283void OatFile::OatMethod::LinkMethodOffsets(Method* method) const {
284 CHECK(method != NULL);
285 method->SetOatCodeOffset(GetCodeOffset());
286 method->SetFrameSizeInBytes(GetFrameSizeInBytes());
287 method->SetCoreSpillMask(GetCoreSpillMask());
288 method->SetFpSpillMask(GetFpSpillMask());
289 method->SetOatMappingTableOffset(GetMappingTableOffset());
290 method->SetOatVmapTableOffset(GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800291 method->SetOatGcMapOffset(GetGcMapOffset());
Brian Carlstromae826982011-11-09 01:33:42 -0800292 method->SetOatInvokeStubOffset(GetInvokeStubOffset());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293}
294
295} // namespace art