Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1 | // 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 | |
| 11 | namespace art { |
| 12 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 13 | std::string OatFile::DexFilenameToOatFilename(const std::string& location) { |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 14 | CHECK(IsValidDexFilename(location) || IsValidZipFilename(location)); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame^] | 15 | std::string oat_location(location); |
| 16 | oat_location += ".oat"; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 17 | return oat_location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 20 | OatFile* 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 | |
| 38 | OatFile::OatFile(const std::string& filename) : location_(filename) {} |
| 39 | |
| 40 | OatFile::~OatFile() { |
| 41 | STLDeleteValues(&oat_dex_files_); |
| 42 | } |
| 43 | |
| 44 | bool 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 Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 57 | 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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 64 | if (map.get() == NULL) { |
| 65 | LOG(WARNING) << "Failed to map oat file " << filename; |
| 66 | return false; |
| 67 | } |
| 68 | CHECK(requested_base == 0 || requested_base == map->GetAddress()) << map->GetAddress(); |
| 69 | DCHECK_EQ(0, memcmp(&oat_header, map->GetAddress(), sizeof(OatHeader))); |
| 70 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 71 | off64_t code_offset = oat_header.GetExecutableOffset(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 72 | if (code_offset < file->Length()) { |
| 73 | byte* code_address = map->GetAddress() + code_offset; |
| 74 | size_t code_length = file->Length() - code_offset; |
| 75 | if (mprotect(code_address, code_length, PROT_READ | PROT_EXEC) != 0) { |
| 76 | PLOG(ERROR) << "Failed to make oat code executable."; |
| 77 | return false; |
| 78 | } |
| 79 | } else { |
| 80 | // its possible to have no code if all the methods were abstract, native, etc |
| 81 | DCHECK_EQ(code_offset, RoundUp(file->Length(), kPageSize)); |
| 82 | } |
| 83 | |
| 84 | const byte* oat = map->GetAddress(); |
| 85 | oat += sizeof(OatHeader); |
| 86 | CHECK_LT(oat, map->GetLimit()); |
| 87 | for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) { |
| 88 | size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
| 89 | oat += sizeof(dex_file_location_size); |
| 90 | CHECK_LT(oat, map->GetLimit()); |
| 91 | |
| 92 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 93 | oat += dex_file_location_size; |
| 94 | CHECK_LT(oat, map->GetLimit()); |
| 95 | |
| 96 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 97 | |
| 98 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 99 | oat += sizeof(dex_file_checksum); |
| 100 | CHECK_LT(oat, map->GetLimit()); |
| 101 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 102 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
| 103 | CHECK_GT(dex_file_offset, 0U); |
| 104 | CHECK_LT(dex_file_offset, static_cast<uint32_t>(file->Length())); |
| 105 | oat += sizeof(dex_file_offset); |
| 106 | CHECK_LT(oat, map->GetLimit()); |
| 107 | |
| 108 | uint8_t* dex_file_pointer = map->GetAddress() + dex_file_offset; |
| 109 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 110 | uint32_t classes_offset = *reinterpret_cast<const uint32_t*>(oat); |
| 111 | CHECK_GT(classes_offset, 0U); |
| 112 | CHECK_LT(classes_offset, static_cast<uint32_t>(file->Length())); |
| 113 | oat += sizeof(classes_offset); |
| 114 | CHECK_LT(oat, map->GetLimit()); |
| 115 | |
| 116 | uint32_t* classes_pointer = reinterpret_cast<uint32_t*>(map->GetAddress() + classes_offset); |
| 117 | |
| 118 | oat_dex_files_[dex_file_location] = new OatDexFile(this, |
| 119 | dex_file_location, |
| 120 | dex_file_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 121 | dex_file_pointer, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 122 | classes_pointer); |
| 123 | } |
| 124 | |
| 125 | mem_map_.reset(map.release()); |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | const OatHeader& OatFile::GetOatHeader() const { |
| 130 | return *reinterpret_cast<const OatHeader*>(GetBase()); |
| 131 | } |
| 132 | |
| 133 | const byte* OatFile::GetBase() const { |
| 134 | CHECK(mem_map_->GetAddress() != NULL); |
| 135 | return mem_map_->GetAddress(); |
| 136 | } |
| 137 | |
| 138 | const byte* OatFile::GetLimit() const { |
| 139 | CHECK(mem_map_->GetLimit() != NULL); |
| 140 | return mem_map_->GetLimit(); |
| 141 | } |
| 142 | |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 143 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location, |
| 144 | bool warn_if_not_found) const { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 145 | Table::const_iterator it = oat_dex_files_.find(dex_file_location); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 146 | if (it == oat_dex_files_.end()) { |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 147 | if (warn_if_not_found) { |
| 148 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location; |
| 149 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 150 | return NULL; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 151 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 152 | return it->second; |
| 153 | } |
| 154 | |
| 155 | std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const { |
| 156 | std::vector<const OatFile::OatDexFile*> result; |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 157 | for (Table::const_iterator it = oat_dex_files_.begin(); it != oat_dex_files_.end(); ++it) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 158 | result.push_back(it->second); |
| 159 | } |
| 160 | return result; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
| 164 | std::string dex_file_location, |
| 165 | uint32_t dex_file_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 166 | byte* dex_file_pointer, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 167 | uint32_t* classes_pointer) |
| 168 | : oat_file_(oat_file), |
| 169 | dex_file_location_(dex_file_location), |
| 170 | dex_file_checksum_(dex_file_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 171 | dex_file_pointer_(dex_file_pointer), |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 172 | classes_pointer_(classes_pointer) {} |
| 173 | |
| 174 | OatFile::OatDexFile::~OatDexFile() {} |
| 175 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 176 | const DexFile* OatFile::OatDexFile::OpenDexFile() const { |
| 177 | size_t length = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 178 | return DexFile::Open(dex_file_pointer_, length, dex_file_location_); |
| 179 | } |
| 180 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 181 | const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 182 | uint32_t methods_offset = classes_pointer_[class_def_index]; |
| 183 | const byte* methods_pointer = oat_file_->GetBase() + methods_offset; |
| 184 | CHECK_LT(methods_pointer, oat_file_->GetLimit()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 185 | return new OatClass(oat_file_, reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 188 | OatFile::OatClass::OatClass(const OatFile* oat_file, const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 189 | : oat_file_(oat_file), methods_pointer_(methods_pointer) {} |
| 190 | |
| 191 | OatFile::OatClass::~OatClass() {} |
| 192 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 193 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 194 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index]; |
| 195 | return OatMethod( |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 196 | oat_file_->GetBase(), |
| 197 | oat_method_offsets.code_offset_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 198 | oat_method_offsets.frame_size_in_bytes_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 199 | oat_method_offsets.core_spill_mask_, |
| 200 | oat_method_offsets.fp_spill_mask_, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 201 | oat_method_offsets.mapping_table_offset_, |
| 202 | oat_method_offsets.vmap_table_offset_, |
| 203 | oat_method_offsets.invoke_stub_offset_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 206 | OatFile::OatMethod::OatMethod(const byte* base, |
| 207 | const uint32_t code_offset, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 208 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 209 | const uint32_t core_spill_mask, |
| 210 | const uint32_t fp_spill_mask, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 211 | const uint32_t mapping_table_offset, |
| 212 | const uint32_t vmap_table_offset, |
| 213 | const uint32_t invoke_stub_offset) |
| 214 | : base_(base), |
| 215 | code_offset_(code_offset), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 216 | frame_size_in_bytes_(frame_size_in_bytes), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 217 | core_spill_mask_(core_spill_mask), |
| 218 | fp_spill_mask_(fp_spill_mask), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 219 | mapping_table_offset_(mapping_table_offset), |
| 220 | vmap_table_offset_(vmap_table_offset), |
| 221 | invoke_stub_offset_(invoke_stub_offset) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 222 | |
| 223 | #ifndef NDEBUG |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 224 | if (mapping_table_offset_ != 0) { // implies non-native, non-stub code |
| 225 | if (vmap_table_offset_ == 0) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 226 | DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 227 | } else { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 228 | const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(base_ + vmap_table_offset_); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 229 | DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 230 | } |
| 231 | } else { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 232 | DCHECK(vmap_table_offset_ == 0); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 233 | } |
| 234 | #endif |
| 235 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 236 | |
| 237 | OatFile::OatMethod::~OatMethod() {} |
| 238 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 239 | void OatFile::OatMethod::LinkMethodPointers(Method* method) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 240 | CHECK(method != NULL); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 241 | method->SetCode(GetCode()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 242 | method->SetFrameSizeInBytes(frame_size_in_bytes_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 243 | method->SetCoreSpillMask(core_spill_mask_); |
| 244 | method->SetFpSpillMask(fp_spill_mask_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 245 | method->SetMappingTable(GetMappingTable()); |
| 246 | method->SetVmapTable(GetVmapTable()); |
| 247 | method->SetInvokeStub(GetInvokeStub()); |
| 248 | } |
| 249 | |
| 250 | void OatFile::OatMethod::LinkMethodOffsets(Method* method) const { |
| 251 | CHECK(method != NULL); |
| 252 | method->SetOatCodeOffset(GetCodeOffset()); |
| 253 | method->SetFrameSizeInBytes(GetFrameSizeInBytes()); |
| 254 | method->SetCoreSpillMask(GetCoreSpillMask()); |
| 255 | method->SetFpSpillMask(GetFpSpillMask()); |
| 256 | method->SetOatMappingTableOffset(GetMappingTableOffset()); |
| 257 | method->SetOatVmapTableOffset(GetVmapTableOffset()); |
| 258 | method->SetOatInvokeStubOffset(GetInvokeStubOffset()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | } // namespace art |