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 | |
| 19 | #include <sys/mman.h> |
| 20 | |
| 21 | #include "file.h" |
| 22 | #include "os.h" |
| 23 | #include "stl_util.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 27 | std::string OatFile::DexFilenameToOatFilename(const std::string& location) { |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 28 | CHECK(IsValidDexFilename(location) || IsValidZipFilename(location)); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 29 | std::string oat_location(location); |
| 30 | oat_location += ".oat"; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 31 | return oat_location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 34 | OatFile* OatFile::Open(const std::string& filename, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 35 | const std::string& location, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 36 | byte* requested_base, |
| 37 | bool writable) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame^] | 38 | CHECK(!filename.empty()) << location; |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 39 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), writable, false)); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 40 | if (file.get() == NULL) { |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 41 | return NULL; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 42 | } |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 43 | return Open(*file.get(), location, requested_base, writable); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 44 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 46 | OatFile* OatFile::Open(File& file, |
| 47 | const std::string& location, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 48 | byte* requested_base, |
| 49 | bool writable) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame^] | 50 | CHECK(!location.empty()); |
| 51 | if (!IsValidOatFilename(location)) { |
| 52 | LOG(WARNING) << "Attempting to open dex file with unknown extension '" << location << "'"; |
| 53 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 54 | UniquePtr<OatFile> oat_file(new OatFile(location)); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 55 | bool success = oat_file->Map(file, requested_base, 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 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 62 | OatFile::OatFile(const std::string& location) : location_(location) { |
| 63 | CHECK(!location_.empty()); |
| 64 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 65 | |
| 66 | OatFile::~OatFile() { |
| 67 | STLDeleteValues(&oat_dex_files_); |
Logan Chien | 0cc6ab6 | 2012-03-20 22:57:52 +0800 | [diff] [blame] | 68 | STLDeleteElements(&oat_elf_images_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 71 | bool OatFile::Map(File& file, byte* requested_base, 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, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 92 | file.Length(), |
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) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 98 | LOG(WARNING) << "Failed to map oat file " << 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()) |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 102 | << GetLocation() << " " << reinterpret_cast<void*>(map->Begin()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 103 | DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader))) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 104 | |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 105 | off_t code_offset = oat_header.GetExecutableOffset(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 106 | if (code_offset < file.Length()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 107 | byte* code_address = map->Begin() + code_offset; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 108 | size_t code_length = file.Length() - code_offset; |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 109 | if (mprotect(code_address, code_length, prot | PROT_EXEC) != 0) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 110 | PLOG(ERROR) << "Failed to make oat code executable in " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | } else { |
| 114 | // its possible to have no code if all the methods were abstract, native, etc |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 115 | DCHECK_EQ(code_offset, RoundUp(file.Length(), kPageSize)) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 118 | const byte* oat = map->Begin(); |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 119 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 120 | oat += sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 121 | oat += oat_header.GetImageFileLocationSize(); |
| 122 | |
| 123 | CHECK_LE(oat, map->End()) |
| 124 | << reinterpret_cast<void*>(map->Begin()) |
| 125 | << "+" << sizeof(OatHeader) |
| 126 | << "+" << oat_header.GetImageFileLocationSize() |
| 127 | << "<=" << reinterpret_cast<void*>(map->End()) |
| 128 | << " " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 129 | for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) { |
| 130 | size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 131 | CHECK_GT(dex_file_location_size, 0U) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 132 | oat += sizeof(dex_file_location_size); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 133 | CHECK_LT(oat, map->End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 134 | |
| 135 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 136 | oat += dex_file_location_size; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 137 | CHECK_LT(oat, map->End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 138 | |
| 139 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 140 | |
| 141 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 142 | oat += sizeof(dex_file_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 143 | CHECK_LT(oat, map->End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 144 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 145 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 146 | CHECK_GT(dex_file_offset, 0U) << GetLocation(); |
| 147 | CHECK_LT(dex_file_offset, static_cast<uint32_t>(file.Length())) << GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 148 | oat += sizeof(dex_file_offset); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 149 | CHECK_LT(oat, map->End()) << GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 150 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 151 | uint8_t* dex_file_pointer = map->Begin() + dex_file_offset; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 152 | CHECK(DexFile::IsMagicValid(dex_file_pointer)) << GetLocation() << " " << dex_file_pointer; |
| 153 | CHECK(DexFile::IsVersionValid(dex_file_pointer)) << GetLocation() << " " << dex_file_pointer; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 154 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
| 155 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 156 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 157 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 158 | CHECK_LE(oat, map->End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 159 | |
| 160 | oat_dex_files_[dex_file_location] = new OatDexFile(this, |
| 161 | dex_file_location, |
| 162 | dex_file_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 163 | dex_file_pointer, |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 164 | methods_offsets_pointer); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Logan Chien | 0cc6ab6 | 2012-03-20 22:57:52 +0800 | [diff] [blame] | 167 | oat = map->Begin() + oat_header.GetElfImageTableOffset(); |
| 168 | CHECK((reinterpret_cast<uintptr_t>(oat) & 0x3) == 0); |
| 169 | |
| 170 | for (uint32_t i = 0, end = oat_header.GetElfImageCount(); i < end; ++i) { |
| 171 | uint32_t elf_offset = *reinterpret_cast<const uint32_t*>(oat); |
| 172 | oat += sizeof(uint32_t); |
| 173 | |
| 174 | uint32_t elf_size = *reinterpret_cast<const uint32_t*>(oat); |
| 175 | oat += sizeof(uint32_t); |
| 176 | |
| 177 | oat_elf_images_.push_back( |
| 178 | new OatElfImage(this, map->Begin() + elf_offset, elf_size)); |
| 179 | } |
| 180 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 181 | mem_map_.reset(map.release()); |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 186 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 189 | const byte* OatFile::Begin() const { |
| 190 | CHECK(mem_map_->Begin() != NULL); |
| 191 | return mem_map_->Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 194 | const byte* OatFile::End() const { |
| 195 | CHECK(mem_map_->End() != NULL); |
| 196 | return mem_map_->End(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 199 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location, |
| 200 | bool warn_if_not_found) const { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 201 | Table::const_iterator it = oat_dex_files_.find(dex_file_location); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 202 | if (it == oat_dex_files_.end()) { |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 203 | if (warn_if_not_found) { |
| 204 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location; |
| 205 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 206 | return NULL; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 207 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 208 | return it->second; |
| 209 | } |
| 210 | |
| 211 | std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const { |
| 212 | std::vector<const OatFile::OatDexFile*> result; |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 213 | 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] | 214 | result.push_back(it->second); |
| 215 | } |
| 216 | return result; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 220 | const std::string& dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 221 | uint32_t dex_file_location_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 222 | byte* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 223 | const uint32_t* oat_class_offsets_pointer) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 224 | : oat_file_(oat_file), |
| 225 | dex_file_location_(dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 226 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 227 | dex_file_pointer_(dex_file_pointer), |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 228 | oat_class_offsets_pointer_(oat_class_offsets_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 229 | |
| 230 | OatFile::OatDexFile::~OatDexFile() {} |
| 231 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 232 | const DexFile* OatFile::OatDexFile::OpenDexFile() const { |
| 233 | size_t length = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 234 | return DexFile::Open(dex_file_pointer_, length, dex_file_location_, dex_file_location_checksum_); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 237 | const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 238 | uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index]; |
| 239 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 240 | const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
| 241 | CHECK_LT(oat_class_pointer, oat_file_->End()); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 242 | Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer); |
| 243 | |
| 244 | const byte* methods_pointer = oat_class_pointer + sizeof(status); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 245 | CHECK_LT(methods_pointer, oat_file_->End()); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 246 | |
| 247 | return new OatClass(oat_file_, |
| 248 | status, |
| 249 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 252 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
| 253 | Class::Status status, |
| 254 | const OatMethodOffsets* methods_pointer) |
| 255 | : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 256 | |
| 257 | OatFile::OatClass::~OatClass() {} |
| 258 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 259 | Class::Status OatFile::OatClass::GetStatus() const { |
| 260 | return status_; |
| 261 | } |
| 262 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 263 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 264 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index]; |
| 265 | return OatMethod( |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 266 | oat_file_->Begin(), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 267 | oat_method_offsets.code_offset_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 268 | oat_method_offsets.frame_size_in_bytes_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 269 | oat_method_offsets.core_spill_mask_, |
| 270 | oat_method_offsets.fp_spill_mask_, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 271 | oat_method_offsets.mapping_table_offset_, |
| 272 | oat_method_offsets.vmap_table_offset_, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 273 | oat_method_offsets.gc_map_offset_, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 274 | oat_method_offsets.invoke_stub_offset_); |
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, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 285 | const uint32_t invoke_stub_offset) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 286 | : begin_(base), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 287 | code_offset_(code_offset), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 288 | frame_size_in_bytes_(frame_size_in_bytes), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 289 | core_spill_mask_(core_spill_mask), |
| 290 | fp_spill_mask_(fp_spill_mask), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 291 | mapping_table_offset_(mapping_table_offset), |
| 292 | vmap_table_offset_(vmap_table_offset), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 293 | gc_map_offset_(gc_map_offset), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 294 | invoke_stub_offset_(invoke_stub_offset) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 295 | #ifndef NDEBUG |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 296 | if (mapping_table_offset_ != 0) { // implies non-native, non-stub code |
| 297 | if (vmap_table_offset_ == 0) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 298 | DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 299 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 300 | 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] | 301 | DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 302 | } |
| 303 | } else { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 304 | DCHECK_EQ(vmap_table_offset_, 0U); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 305 | } |
| 306 | #endif |
| 307 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 308 | |
| 309 | OatFile::OatMethod::~OatMethod() {} |
| 310 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 311 | void OatFile::OatMethod::LinkMethodPointers(Method* method) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 312 | CHECK(method != NULL); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 313 | method->SetCode(GetCode()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 314 | method->SetFrameSizeInBytes(frame_size_in_bytes_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 315 | method->SetCoreSpillMask(core_spill_mask_); |
| 316 | method->SetFpSpillMask(fp_spill_mask_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 317 | method->SetMappingTable(GetMappingTable()); |
| 318 | method->SetVmapTable(GetVmapTable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 319 | method->SetGcMap(GetGcMap()); // Note, used by native methods in work around JNI mode. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 320 | method->SetInvokeStub(GetInvokeStub()); |
| 321 | } |
| 322 | |
| 323 | void OatFile::OatMethod::LinkMethodOffsets(Method* method) const { |
| 324 | CHECK(method != NULL); |
| 325 | method->SetOatCodeOffset(GetCodeOffset()); |
| 326 | method->SetFrameSizeInBytes(GetFrameSizeInBytes()); |
| 327 | method->SetCoreSpillMask(GetCoreSpillMask()); |
| 328 | method->SetFpSpillMask(GetFpSpillMask()); |
| 329 | method->SetOatMappingTableOffset(GetMappingTableOffset()); |
| 330 | method->SetOatVmapTableOffset(GetVmapTableOffset()); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 331 | method->SetOatGcMapOffset(GetGcMapOffset()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 332 | method->SetOatInvokeStubOffset(GetInvokeStubOffset()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Logan Chien | 0cc6ab6 | 2012-03-20 22:57:52 +0800 | [diff] [blame] | 335 | OatFile::OatElfImage::OatElfImage(const OatFile* oat_file, |
| 336 | const byte* addr, |
| 337 | uint32_t size) |
| 338 | : oat_file_(oat_file), elf_addr_(addr), elf_size_(size) { |
| 339 | } |
| 340 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 341 | } // namespace art |