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 | |
| 126 | const OatFile::OatDexFile& OatFile::GetOatDexFile(const DexFile& dex_file) { |
| 127 | Table::const_iterator it = oat_dex_files_.find(dex_file.GetLocation()); |
| 128 | if (it == oat_dex_files_.end()) { |
| 129 | LOG(FATAL) << "Failed to find OatDexFile for DexFile " << dex_file.GetLocation(); |
| 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()); |
| 149 | return OatClass(oat_file_, reinterpret_cast<const uint32_t*>(methods_pointer)); |
| 150 | } |
| 151 | |
| 152 | OatFile::OatClass::OatClass(const OatFile* oat_file, const uint32_t* methods_pointer) |
| 153 | : oat_file_(oat_file), methods_pointer_(methods_pointer) {} |
| 154 | |
| 155 | OatFile::OatClass::~OatClass() {} |
| 156 | |
| 157 | const void* OatFile::OatClass::GetMethodCode(uint32_t method_index) const { |
| 158 | uint32_t code_offset = methods_pointer_[method_index]; |
| 159 | if (code_offset == 0) { |
| 160 | return NULL; |
| 161 | } |
| 162 | const void* code_pointer = reinterpret_cast<const void*>(oat_file_->GetBase() + code_offset); |
| 163 | CHECK_LT(code_pointer, oat_file_->GetLimit()); |
| 164 | return code_pointer; |
| 165 | } |
| 166 | |
| 167 | } // namespace art |