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 | |
| 13 | OatFile* OatFile::Open(const std::string& filename, |
| 14 | const std::string& strip_location_prefix, |
| 15 | byte* requested_base) { |
| 16 | StringPiece location = filename; |
| 17 | if (!location.starts_with(strip_location_prefix)) { |
| 18 | LOG(ERROR) << filename << " does not start with " << strip_location_prefix; |
| 19 | return NULL; |
| 20 | } |
| 21 | location.remove_prefix(strip_location_prefix.size()); |
| 22 | |
| 23 | UniquePtr<OatFile> oat_file(new OatFile(location.ToString())); |
| 24 | bool success = oat_file->Read(filename, requested_base); |
| 25 | if (!success) { |
| 26 | return NULL; |
| 27 | } |
| 28 | return oat_file.release(); |
| 29 | } |
| 30 | |
| 31 | OatFile::OatFile(const std::string& filename) : location_(filename) {} |
| 32 | |
| 33 | OatFile::~OatFile() { |
| 34 | STLDeleteValues(&oat_dex_files_); |
| 35 | } |
| 36 | |
| 37 | bool OatFile::Read(const std::string& filename, byte* requested_base) { |
| 38 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), false)); |
| 39 | if (file.get() == NULL) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | OatHeader oat_header; |
| 44 | bool success = file->ReadFully(&oat_header, sizeof(oat_header)); |
| 45 | if (!success || !oat_header.IsValid()) { |
| 46 | LOG(WARNING) << "Invalid oat header " << filename; |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | UniquePtr<MemMap> map(MemMap::Map(requested_base, |
| 51 | file->Length(), |
| 52 | PROT_READ, |
| 53 | MAP_PRIVATE | ((requested_base != NULL) ? MAP_FIXED : 0), |
| 54 | file->Fd(), |
| 55 | 0)); |
| 56 | if (map.get() == NULL) { |
| 57 | LOG(WARNING) << "Failed to map oat file " << filename; |
| 58 | return false; |
| 59 | } |
| 60 | CHECK(requested_base == 0 || requested_base == map->GetAddress()) << map->GetAddress(); |
| 61 | DCHECK_EQ(0, memcmp(&oat_header, map->GetAddress(), sizeof(OatHeader))); |
| 62 | |
| 63 | off_t code_offset = oat_header.GetExecutableOffset(); |
| 64 | if (code_offset < file->Length()) { |
| 65 | byte* code_address = map->GetAddress() + code_offset; |
| 66 | size_t code_length = file->Length() - code_offset; |
| 67 | if (mprotect(code_address, code_length, PROT_READ | PROT_EXEC) != 0) { |
| 68 | PLOG(ERROR) << "Failed to make oat code executable."; |
| 69 | return false; |
| 70 | } |
| 71 | } else { |
| 72 | // its possible to have no code if all the methods were abstract, native, etc |
| 73 | DCHECK_EQ(code_offset, RoundUp(file->Length(), kPageSize)); |
| 74 | } |
| 75 | |
| 76 | const byte* oat = map->GetAddress(); |
| 77 | oat += sizeof(OatHeader); |
| 78 | CHECK_LT(oat, map->GetLimit()); |
| 79 | for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) { |
| 80 | size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
| 81 | oat += sizeof(dex_file_location_size); |
| 82 | CHECK_LT(oat, map->GetLimit()); |
| 83 | |
| 84 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 85 | oat += dex_file_location_size; |
| 86 | CHECK_LT(oat, map->GetLimit()); |
| 87 | |
| 88 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 89 | |
| 90 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 91 | oat += sizeof(dex_file_checksum); |
| 92 | CHECK_LT(oat, map->GetLimit()); |
| 93 | |
| 94 | uint32_t classes_offset = *reinterpret_cast<const uint32_t*>(oat); |
| 95 | CHECK_GT(classes_offset, 0U); |
| 96 | CHECK_LT(classes_offset, static_cast<uint32_t>(file->Length())); |
| 97 | oat += sizeof(classes_offset); |
| 98 | CHECK_LT(oat, map->GetLimit()); |
| 99 | |
| 100 | uint32_t* classes_pointer = reinterpret_cast<uint32_t*>(map->GetAddress() + classes_offset); |
| 101 | |
| 102 | oat_dex_files_[dex_file_location] = new OatDexFile(this, |
| 103 | dex_file_location, |
| 104 | dex_file_checksum, |
| 105 | classes_pointer); |
| 106 | } |
| 107 | |
| 108 | mem_map_.reset(map.release()); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | const OatHeader& OatFile::GetOatHeader() const { |
| 113 | return *reinterpret_cast<const OatHeader*>(GetBase()); |
| 114 | } |
| 115 | |
| 116 | const byte* OatFile::GetBase() const { |
| 117 | CHECK(mem_map_->GetAddress() != NULL); |
| 118 | return mem_map_->GetAddress(); |
| 119 | } |
| 120 | |
| 121 | const byte* OatFile::GetLimit() const { |
| 122 | CHECK(mem_map_->GetLimit() != NULL); |
| 123 | return mem_map_->GetLimit(); |
| 124 | } |
| 125 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 126 | const OatFile::OatDexFile& OatFile::GetOatDexFile(const std::string& dex_file_location) { |
| 127 | Table::const_iterator it = oat_dex_files_.find(dex_file_location); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 128 | if (it == oat_dex_files_.end()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 129 | LOG(FATAL) << "Failed to find OatDexFile for DexFile " << dex_file_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 130 | } |
| 131 | return *it->second; |
| 132 | } |
| 133 | |
| 134 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
| 135 | std::string dex_file_location, |
| 136 | uint32_t dex_file_checksum, |
| 137 | uint32_t* classes_pointer) |
| 138 | : oat_file_(oat_file), |
| 139 | dex_file_location_(dex_file_location), |
| 140 | dex_file_checksum_(dex_file_checksum), |
| 141 | classes_pointer_(classes_pointer) {} |
| 142 | |
| 143 | OatFile::OatDexFile::~OatDexFile() {} |
| 144 | |
| 145 | const OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const { |
| 146 | uint32_t methods_offset = classes_pointer_[class_def_index]; |
| 147 | const byte* methods_pointer = oat_file_->GetBase() + methods_offset; |
| 148 | CHECK_LT(methods_pointer, oat_file_->GetLimit()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame^] | 149 | return OatClass(oat_file_, reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame^] | 152 | OatFile::OatClass::OatClass(const OatFile* oat_file, const OatMethodOffsets* methods_pointer) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 153 | : oat_file_(oat_file), methods_pointer_(methods_pointer) {} |
| 154 | |
| 155 | OatFile::OatClass::~OatClass() {} |
| 156 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame^] | 157 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 158 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index]; |
| 159 | return OatMethod( |
| 160 | GetOatPointer<const void*>(oat_method_offsets.code_offset_), |
| 161 | oat_method_offsets.frame_size_in_bytes_, |
| 162 | oat_method_offsets.return_pc_offset_in_bytes_, |
| 163 | oat_method_offsets.core_spill_mask_, |
| 164 | oat_method_offsets.fp_spill_mask_, |
| 165 | GetOatPointer<const uint32_t*>(oat_method_offsets.mapping_table_offset_), |
| 166 | GetOatPointer<const uint16_t*>(oat_method_offsets.vmap_table_offset_), |
| 167 | GetOatPointer<const Method::InvokeStub*>(oat_method_offsets.invoke_stub_offset_)); |
| 168 | } |
| 169 | |
| 170 | OatFile::OatMethod::OatMethod(const void* code, |
| 171 | const size_t frame_size_in_bytes, |
| 172 | const size_t return_pc_offset_in_bytes, |
| 173 | const uint32_t core_spill_mask, |
| 174 | const uint32_t fp_spill_mask, |
| 175 | const uint32_t* mapping_table, |
| 176 | const uint16_t* vmap_table, |
| 177 | const Method::InvokeStub* invoke_stub) : |
| 178 | code_(code), |
| 179 | frame_size_in_bytes_(frame_size_in_bytes), |
| 180 | return_pc_offset_in_bytes_(return_pc_offset_in_bytes), |
| 181 | core_spill_mask_(core_spill_mask), |
| 182 | fp_spill_mask_(fp_spill_mask), |
| 183 | mapping_table_(mapping_table), |
| 184 | vmap_table_(vmap_table), |
| 185 | invoke_stub_(invoke_stub) {} |
| 186 | |
| 187 | OatFile::OatMethod::~OatMethod() {} |
| 188 | |
| 189 | void OatFile::OatMethod::LinkMethod(Method* method) { |
| 190 | CHECK(method != NULL); |
| 191 | method->SetCode(code_); |
| 192 | method->SetFrameSizeInBytes(frame_size_in_bytes_); |
| 193 | method->SetReturnPcOffsetInBytes(return_pc_offset_in_bytes_); |
| 194 | method->SetCoreSpillMask(core_spill_mask_); |
| 195 | method->SetFpSpillMask(fp_spill_mask_); |
| 196 | method->SetMappingTable(mapping_table_); |
| 197 | method->SetVmapTable(vmap_table_); |
| 198 | method->SetInvokeStub(invoke_stub_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | } // namespace art |