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