Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
| 17 | #include "oat_file.h" |
| 18 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 19 | #include "file.h" |
| 20 | #include "os.h" |
| 21 | #include "stl_util.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 25 | std::string OatFile::DexFilenameToOatFilename(const std::string& location) { |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 26 | CHECK(IsValidDexFilename(location) || IsValidZipFilename(location)); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 27 | std::string oat_location(location); |
| 28 | oat_location += ".oat"; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 29 | return oat_location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | OatFile* OatFile::Open(const std::string& filename, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 33 | const std::string& location, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 34 | byte* requested_base, |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 35 | RelocationBehavior reloc, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 36 | bool writable) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 37 | CHECK(!filename.empty()) << location; |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 38 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), writable, false)); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 39 | if (file.get() == NULL) { |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 40 | return NULL; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 41 | } |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 42 | return Open(*file.get(), location, requested_base, reloc, writable); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 43 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 44 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 45 | OatFile* OatFile::Open(File& file, |
| 46 | const std::string& location, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 47 | byte* requested_base, |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 48 | RelocationBehavior reloc, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 49 | bool writable) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 50 | CHECK(!location.empty()); |
| 51 | if (!IsValidOatFilename(location)) { |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 52 | LOG(WARNING) << "Attempting to open oat file with unknown extension '" << location << "'"; |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 53 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 54 | UniquePtr<OatFile> oat_file(new OatFile(location)); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 55 | bool success = oat_file->Map(file, requested_base, reloc, writable); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 56 | if (!success) { |
| 57 | return NULL; |
| 58 | } |
| 59 | return oat_file.release(); |
| 60 | } |
| 61 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 62 | OatFile::OatFile(const std::string& location) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 63 | : location_(location) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 64 | CHECK(!location_.empty()); |
| 65 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 66 | |
| 67 | OatFile::~OatFile() { |
| 68 | STLDeleteValues(&oat_dex_files_); |
| 69 | } |
| 70 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 71 | bool OatFile::Map(File& file, |
| 72 | byte* requested_base, |
| 73 | #if defined(ART_USE_LLVM_COMPILER) |
| 74 | RelocationBehavior reloc, |
| 75 | #else |
| 76 | RelocationBehavior /*UNUSED*/, |
| 77 | #endif |
| 78 | bool writable) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 79 | OatHeader oat_header; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 80 | bool success = file.ReadFully(&oat_header, sizeof(oat_header)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 81 | if (!success || !oat_header.IsValid()) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 82 | LOG(WARNING) << "Invalid oat header " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 86 | int flags = 0; |
| 87 | int prot = 0; |
| 88 | if (writable) { |
| 89 | flags |= MAP_SHARED; // So changes will write through to file |
| 90 | prot |= (PROT_READ | PROT_WRITE); |
| 91 | } else { |
| 92 | flags |= MAP_PRIVATE; |
| 93 | prot |= PROT_READ; |
| 94 | } |
| 95 | if (requested_base != NULL) { |
| 96 | flags |= MAP_FIXED; |
| 97 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 98 | UniquePtr<MemMap> map(MemMap::MapFileAtAddress(requested_base, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 99 | file.Length(), |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 100 | prot, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 101 | flags, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 102 | file.Fd(), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 103 | 0)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 104 | if (map.get() == NULL) { |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 105 | LOG(WARNING) << "Failed to map oat file from " << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 106 | return false; |
| 107 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 108 | CHECK(requested_base == 0 || requested_base == map->Begin()) |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 109 | << file.name() << " for " << GetLocation() << " " << reinterpret_cast<void*>(map->Begin()); |
| 110 | DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader))) |
| 111 | << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 112 | |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 113 | off_t code_offset = oat_header.GetExecutableOffset(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 114 | if (code_offset < file.Length()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 115 | byte* code_address = map->Begin() + code_offset; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 116 | size_t code_length = file.Length() - code_offset; |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 117 | if (mprotect(code_address, code_length, prot | PROT_EXEC) != 0) { |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 118 | PLOG(ERROR) << "Failed to make oat code executable in " |
| 119 | << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | } else { |
| 123 | // its possible to have no code if all the methods were abstract, native, etc |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 124 | DCHECK_EQ(code_offset, RoundUp(file.Length(), kPageSize)) |
| 125 | << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 128 | const byte* oat = map->Begin(); |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 129 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 130 | oat += sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 131 | oat += oat_header.GetImageFileLocationSize(); |
| 132 | |
| 133 | CHECK_LE(oat, map->End()) |
| 134 | << reinterpret_cast<void*>(map->Begin()) |
| 135 | << "+" << sizeof(OatHeader) |
| 136 | << "+" << oat_header.GetImageFileLocationSize() |
| 137 | << "<=" << reinterpret_cast<void*>(map->End()) |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 138 | << " " << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 139 | for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) { |
| 140 | size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 141 | CHECK_GT(dex_file_location_size, 0U) << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 142 | oat += sizeof(dex_file_location_size); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 143 | CHECK_LT(oat, map->End()) << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 144 | |
| 145 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 146 | oat += dex_file_location_size; |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 147 | CHECK_LT(oat, map->End()) << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 148 | |
| 149 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 150 | |
| 151 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 152 | oat += sizeof(dex_file_checksum); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 153 | CHECK_LT(oat, map->End()) << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 154 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 155 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 156 | CHECK_GT(dex_file_offset, 0U) << file.name() << " for " << GetLocation(); |
| 157 | CHECK_LT(dex_file_offset, static_cast<uint32_t>(file.Length())) |
| 158 | << file.name() << " for " << GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 159 | oat += sizeof(dex_file_offset); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 160 | CHECK_LT(oat, map->End()) << file.name() << " for " << GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 161 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 162 | uint8_t* dex_file_pointer = map->Begin() + dex_file_offset; |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 163 | CHECK(DexFile::IsMagicValid(dex_file_pointer)) |
| 164 | << file.name() << " for " << GetLocation() << " " << dex_file_pointer; |
| 165 | CHECK(DexFile::IsVersionValid(dex_file_pointer)) |
| 166 | << file.name() << " for " << GetLocation() << " " << dex_file_pointer; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 167 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
| 168 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 169 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 170 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 171 | CHECK_LE(oat, map->End()) << file.name() << " for " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 172 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 173 | oat_dex_files_.Put(dex_file_location, new OatDexFile(this, |
| 174 | dex_file_location, |
| 175 | dex_file_checksum, |
| 176 | dex_file_pointer, |
| 177 | methods_offsets_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | mem_map_.reset(map.release()); |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 185 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 188 | const byte* OatFile::Begin() const { |
| 189 | CHECK(mem_map_->Begin() != NULL); |
| 190 | return mem_map_->Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 193 | const byte* OatFile::End() const { |
| 194 | CHECK(mem_map_->End() != NULL); |
| 195 | return mem_map_->End(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 198 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location, |
| 199 | bool warn_if_not_found) const { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 200 | Table::const_iterator it = oat_dex_files_.find(dex_file_location); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 201 | if (it == oat_dex_files_.end()) { |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 202 | if (warn_if_not_found) { |
| 203 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location; |
| 204 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 205 | return NULL; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 206 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 207 | return it->second; |
| 208 | } |
| 209 | |
| 210 | std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const { |
| 211 | std::vector<const OatFile::OatDexFile*> result; |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 212 | 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] | 213 | result.push_back(it->second); |
| 214 | } |
| 215 | return result; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 218 | void OatFile::RelocateExecutable() { |
Shih-wei Liao | ab646f9 | 2012-06-27 23:02:11 -0700 | [diff] [blame] | 219 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 220 | UNIMPLEMENTED(WARNING) << "Relocate the executable"; |
Shih-wei Liao | ab646f9 | 2012-06-27 23:02:11 -0700 | [diff] [blame] | 221 | #endif |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 224 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 225 | const std::string& dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 226 | uint32_t dex_file_location_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 227 | byte* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 228 | const uint32_t* oat_class_offsets_pointer) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 229 | : oat_file_(oat_file), |
| 230 | dex_file_location_(dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 231 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 232 | dex_file_pointer_(dex_file_pointer), |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 233 | oat_class_offsets_pointer_(oat_class_offsets_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 234 | |
| 235 | OatFile::OatDexFile::~OatDexFile() {} |
| 236 | |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame^] | 237 | size_t OatFile::OatDexFile::FileSize() const { |
| 238 | return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
| 239 | } |
| 240 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 241 | const DexFile* OatFile::OatDexFile::OpenDexFile() const { |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame^] | 242 | return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 243 | dex_file_location_checksum_); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 246 | const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 247 | uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index]; |
| 248 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 249 | const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
| 250 | CHECK_LT(oat_class_pointer, oat_file_->End()); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 251 | Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer); |
| 252 | |
| 253 | const byte* methods_pointer = oat_class_pointer + sizeof(status); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 254 | CHECK_LT(methods_pointer, oat_file_->End()); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 255 | |
| 256 | return new OatClass(oat_file_, |
| 257 | status, |
| 258 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 261 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
| 262 | Class::Status status, |
| 263 | const OatMethodOffsets* methods_pointer) |
| 264 | : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 265 | |
| 266 | OatFile::OatClass::~OatClass() {} |
| 267 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 268 | Class::Status OatFile::OatClass::GetStatus() const { |
| 269 | return status_; |
| 270 | } |
| 271 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 272 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 273 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index]; |
| 274 | return OatMethod( |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 275 | oat_file_->Begin(), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 276 | oat_method_offsets.code_offset_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 277 | oat_method_offsets.frame_size_in_bytes_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 278 | oat_method_offsets.core_spill_mask_, |
| 279 | oat_method_offsets.fp_spill_mask_, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 280 | oat_method_offsets.mapping_table_offset_, |
| 281 | oat_method_offsets.vmap_table_offset_, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 282 | oat_method_offsets.gc_map_offset_, |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 283 | oat_method_offsets.invoke_stub_offset_ |
| 284 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 285 | , oat_method_offsets.proxy_stub_offset_ |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 286 | #endif |
| 287 | ); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 290 | OatFile::OatMethod::OatMethod(const byte* base, |
| 291 | const uint32_t code_offset, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 292 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 293 | const uint32_t core_spill_mask, |
| 294 | const uint32_t fp_spill_mask, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 295 | const uint32_t mapping_table_offset, |
| 296 | const uint32_t vmap_table_offset, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 297 | const uint32_t gc_map_offset, |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 298 | const uint32_t invoke_stub_offset |
| 299 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 300 | , const uint32_t proxy_stub_offset |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 301 | #endif |
| 302 | ) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 303 | : begin_(base), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 304 | code_offset_(code_offset), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 305 | frame_size_in_bytes_(frame_size_in_bytes), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 306 | core_spill_mask_(core_spill_mask), |
| 307 | fp_spill_mask_(fp_spill_mask), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 308 | mapping_table_offset_(mapping_table_offset), |
| 309 | vmap_table_offset_(vmap_table_offset), |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 310 | native_gc_map_offset_(gc_map_offset), |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 311 | invoke_stub_offset_(invoke_stub_offset) |
| 312 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 313 | , proxy_stub_offset_(proxy_stub_offset) |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 314 | #endif |
| 315 | { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 316 | #ifndef NDEBUG |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 317 | if (mapping_table_offset_ != 0) { // implies non-native, non-stub code |
| 318 | if (vmap_table_offset_ == 0) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 319 | DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 320 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 321 | const uint16_t* vmap_table_ = reinterpret_cast<const uint16_t*>(begin_ + vmap_table_offset_); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 322 | DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 323 | } |
| 324 | } else { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 325 | DCHECK_EQ(vmap_table_offset_, 0U); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 326 | } |
| 327 | #endif |
| 328 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 329 | |
| 330 | OatFile::OatMethod::~OatMethod() {} |
| 331 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 332 | const void* OatFile::OatMethod::GetCode() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 333 | return GetOatPointer<const void*>(code_offset_); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | uint32_t OatFile::OatMethod::GetCodeSize() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 337 | uintptr_t code = reinterpret_cast<uint32_t>(GetCode()); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 338 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 339 | if (code == 0) { |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 340 | return 0; |
| 341 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 342 | // TODO: make this Thumb2 specific |
| 343 | code &= ~0x1; |
| 344 | return reinterpret_cast<uint32_t*>(code)[-1]; |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 345 | } |
| 346 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 347 | AbstractMethod::InvokeStub* OatFile::OatMethod::GetInvokeStub() const { |
Ian Rogers | 1b09b09 | 2012-08-20 15:35:52 -0700 | [diff] [blame] | 348 | const byte* stub = GetOatPointer<const byte*>(invoke_stub_offset_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 349 | return reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(stub)); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | uint32_t OatFile::OatMethod::GetInvokeStubSize() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 353 | uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub()); |
| 354 | if (code == 0) { |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 355 | return 0; |
| 356 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 357 | // TODO: make this Thumb2 specific |
| 358 | code &= ~0x1; |
| 359 | return reinterpret_cast<uint32_t*>(code)[-1]; |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 360 | } |
| 361 | |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 362 | #if defined(ART_USE_LLVM_COMPILER) |
| 363 | const void* OatFile::OatMethod::GetProxyStub() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 364 | return GetOatPointer<const void*>(proxy_stub_offset_); |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 365 | } |
| 366 | #endif |
| 367 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 368 | void OatFile::OatMethod::LinkMethodPointers(AbstractMethod* method) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 369 | CHECK(method != NULL); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 370 | method->SetCode(GetCode()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 371 | method->SetFrameSizeInBytes(frame_size_in_bytes_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 372 | method->SetCoreSpillMask(core_spill_mask_); |
| 373 | method->SetFpSpillMask(fp_spill_mask_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 374 | method->SetMappingTable(GetMappingTable()); |
| 375 | method->SetVmapTable(GetVmapTable()); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 376 | method->SetNativeGcMap(GetNativeGcMap()); // Note, used by native methods in work around JNI mode. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 377 | method->SetInvokeStub(GetInvokeStub()); |
| 378 | } |
| 379 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 380 | void OatFile::OatMethod::LinkMethodOffsets(AbstractMethod* method) const { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 381 | CHECK(method != NULL); |
| 382 | method->SetOatCodeOffset(GetCodeOffset()); |
| 383 | method->SetFrameSizeInBytes(GetFrameSizeInBytes()); |
| 384 | method->SetCoreSpillMask(GetCoreSpillMask()); |
| 385 | method->SetFpSpillMask(GetFpSpillMask()); |
| 386 | method->SetOatMappingTableOffset(GetMappingTableOffset()); |
| 387 | method->SetOatVmapTableOffset(GetVmapTableOffset()); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 388 | method->SetOatNativeGcMapOffset(GetNativeGcMapOffset()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 389 | method->SetOatInvokeStubOffset(GetInvokeStubOffset()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 392 | } // namespace art |