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)) { |
| 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)); |
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 | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 105 | LOG(WARNING) << "Failed to map oat file " << 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 | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 109 | << GetLocation() << " " << reinterpret_cast<void*>(map->Begin()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 110 | DCHECK_EQ(0, memcmp(&oat_header, map->Begin(), sizeof(OatHeader))) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 111 | |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 112 | off_t code_offset = oat_header.GetExecutableOffset(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 113 | if (code_offset < file.Length()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 114 | byte* code_address = map->Begin() + code_offset; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 115 | size_t code_length = file.Length() - code_offset; |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 116 | if (mprotect(code_address, code_length, prot | PROT_EXEC) != 0) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 117 | PLOG(ERROR) << "Failed to make oat code executable in " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | } else { |
| 121 | // 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] | 122 | DCHECK_EQ(code_offset, RoundUp(file.Length(), kPageSize)) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 125 | const byte* oat = map->Begin(); |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 126 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 127 | oat += sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 128 | oat += oat_header.GetImageFileLocationSize(); |
| 129 | |
| 130 | CHECK_LE(oat, map->End()) |
| 131 | << reinterpret_cast<void*>(map->Begin()) |
| 132 | << "+" << sizeof(OatHeader) |
| 133 | << "+" << oat_header.GetImageFileLocationSize() |
| 134 | << "<=" << reinterpret_cast<void*>(map->End()) |
| 135 | << " " << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 136 | for (size_t i = 0; i < oat_header.GetDexFileCount(); i++) { |
| 137 | size_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 138 | CHECK_GT(dex_file_location_size, 0U) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 139 | oat += sizeof(dex_file_location_size); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 140 | CHECK_LT(oat, map->End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 141 | |
| 142 | const char* dex_file_location_data = reinterpret_cast<const char*>(oat); |
| 143 | oat += dex_file_location_size; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 144 | CHECK_LT(oat, map->End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 145 | |
| 146 | std::string dex_file_location(dex_file_location_data, dex_file_location_size); |
| 147 | |
| 148 | uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat); |
| 149 | oat += sizeof(dex_file_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 150 | CHECK_LT(oat, map->End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 151 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 152 | uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 153 | CHECK_GT(dex_file_offset, 0U) << GetLocation(); |
| 154 | CHECK_LT(dex_file_offset, static_cast<uint32_t>(file.Length())) << GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 155 | oat += sizeof(dex_file_offset); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 156 | CHECK_LT(oat, map->End()) << GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 157 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 158 | uint8_t* dex_file_pointer = map->Begin() + dex_file_offset; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 159 | CHECK(DexFile::IsMagicValid(dex_file_pointer)) << GetLocation() << " " << dex_file_pointer; |
| 160 | CHECK(DexFile::IsVersionValid(dex_file_pointer)) << GetLocation() << " " << dex_file_pointer; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 161 | const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); |
| 162 | const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 163 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 164 | oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 165 | CHECK_LE(oat, map->End()) << GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 166 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 167 | oat_dex_files_.Put(dex_file_location, new OatDexFile(this, |
| 168 | dex_file_location, |
| 169 | dex_file_checksum, |
| 170 | dex_file_pointer, |
| 171 | methods_offsets_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | mem_map_.reset(map.release()); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | const OatHeader& OatFile::GetOatHeader() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 179 | return *reinterpret_cast<const OatHeader*>(Begin()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 182 | const byte* OatFile::Begin() const { |
| 183 | CHECK(mem_map_->Begin() != NULL); |
| 184 | return mem_map_->Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 187 | const byte* OatFile::End() const { |
| 188 | CHECK(mem_map_->End() != NULL); |
| 189 | return mem_map_->End(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 192 | const OatFile::OatDexFile* OatFile::GetOatDexFile(const std::string& dex_file_location, |
| 193 | bool warn_if_not_found) const { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 194 | Table::const_iterator it = oat_dex_files_.find(dex_file_location); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 195 | if (it == oat_dex_files_.end()) { |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 196 | if (warn_if_not_found) { |
| 197 | LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_file_location; |
| 198 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 199 | return NULL; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 200 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 201 | return it->second; |
| 202 | } |
| 203 | |
| 204 | std::vector<const OatFile::OatDexFile*> OatFile::GetOatDexFiles() const { |
| 205 | std::vector<const OatFile::OatDexFile*> result; |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 206 | 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] | 207 | result.push_back(it->second); |
| 208 | } |
| 209 | return result; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 212 | void OatFile::RelocateExecutable() { |
Shih-wei Liao | ab646f9 | 2012-06-27 23:02:11 -0700 | [diff] [blame] | 213 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 214 | UNIMPLEMENTED(WARNING) << "Relocate the executable"; |
Shih-wei Liao | ab646f9 | 2012-06-27 23:02:11 -0700 | [diff] [blame] | 215 | #endif |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 216 | } |
| 217 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 218 | OatFile::OatDexFile::OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 219 | const std::string& dex_file_location, |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 220 | uint32_t dex_file_location_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 221 | byte* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 222 | const uint32_t* oat_class_offsets_pointer) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 223 | : oat_file_(oat_file), |
| 224 | dex_file_location_(dex_file_location), |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 225 | dex_file_location_checksum_(dex_file_location_checksum), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 226 | dex_file_pointer_(dex_file_pointer), |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 227 | oat_class_offsets_pointer_(oat_class_offsets_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 228 | |
| 229 | OatFile::OatDexFile::~OatDexFile() {} |
| 230 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 231 | const DexFile* OatFile::OatDexFile::OpenDexFile() const { |
| 232 | 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] | 233 | 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] | 234 | } |
| 235 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 236 | const OatFile::OatClass* OatFile::OatDexFile::GetOatClass(uint32_t class_def_index) const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 237 | uint32_t oat_class_offset = oat_class_offsets_pointer_[class_def_index]; |
| 238 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 239 | const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset; |
| 240 | CHECK_LT(oat_class_pointer, oat_file_->End()); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 241 | Class::Status status = *reinterpret_cast<const Class::Status*>(oat_class_pointer); |
| 242 | |
| 243 | const byte* methods_pointer = oat_class_pointer + sizeof(status); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 244 | CHECK_LT(methods_pointer, oat_file_->End()); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 245 | |
| 246 | return new OatClass(oat_file_, |
| 247 | status, |
| 248 | reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 251 | OatFile::OatClass::OatClass(const OatFile* oat_file, |
| 252 | Class::Status status, |
| 253 | const OatMethodOffsets* methods_pointer) |
| 254 | : oat_file_(oat_file), status_(status), methods_pointer_(methods_pointer) {} |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 255 | |
| 256 | OatFile::OatClass::~OatClass() {} |
| 257 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 258 | Class::Status OatFile::OatClass::GetStatus() const { |
| 259 | return status_; |
| 260 | } |
| 261 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 262 | const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { |
| 263 | const OatMethodOffsets& oat_method_offsets = methods_pointer_[method_index]; |
| 264 | return OatMethod( |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 265 | oat_file_->Begin(), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 266 | oat_method_offsets.code_offset_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 267 | oat_method_offsets.frame_size_in_bytes_, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 268 | oat_method_offsets.core_spill_mask_, |
| 269 | oat_method_offsets.fp_spill_mask_, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 270 | oat_method_offsets.mapping_table_offset_, |
| 271 | oat_method_offsets.vmap_table_offset_, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 272 | oat_method_offsets.gc_map_offset_, |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 273 | oat_method_offsets.invoke_stub_offset_ |
| 274 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 275 | , oat_method_offsets.proxy_stub_offset_ |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 276 | #endif |
| 277 | ); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 280 | OatFile::OatMethod::OatMethod(const byte* base, |
| 281 | const uint32_t code_offset, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 282 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 283 | const uint32_t core_spill_mask, |
| 284 | const uint32_t fp_spill_mask, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 285 | const uint32_t mapping_table_offset, |
| 286 | const uint32_t vmap_table_offset, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 287 | const uint32_t gc_map_offset, |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 288 | const uint32_t invoke_stub_offset |
| 289 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 290 | , const uint32_t proxy_stub_offset |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 291 | #endif |
| 292 | ) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 293 | : begin_(base), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 294 | code_offset_(code_offset), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 295 | frame_size_in_bytes_(frame_size_in_bytes), |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 296 | core_spill_mask_(core_spill_mask), |
| 297 | fp_spill_mask_(fp_spill_mask), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 298 | mapping_table_offset_(mapping_table_offset), |
| 299 | vmap_table_offset_(vmap_table_offset), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 300 | gc_map_offset_(gc_map_offset), |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 301 | invoke_stub_offset_(invoke_stub_offset) |
| 302 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 303 | , proxy_stub_offset_(proxy_stub_offset) |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 304 | #endif |
| 305 | { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 306 | #ifndef NDEBUG |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 307 | if (mapping_table_offset_ != 0) { // implies non-native, non-stub code |
| 308 | if (vmap_table_offset_ == 0) { |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 309 | DCHECK_EQ(0U, static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 310 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 311 | 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] | 312 | DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask_) + __builtin_popcount(fp_spill_mask_))); |
| 313 | } |
| 314 | } else { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 315 | DCHECK_EQ(vmap_table_offset_, 0U); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 316 | } |
| 317 | #endif |
| 318 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 319 | |
| 320 | OatFile::OatMethod::~OatMethod() {} |
| 321 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 322 | const void* OatFile::OatMethod::GetCode() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 323 | return GetOatPointer<const void*>(code_offset_); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | uint32_t OatFile::OatMethod::GetCodeSize() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 327 | uintptr_t code = reinterpret_cast<uint32_t>(GetCode()); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 328 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 329 | if (code == 0) { |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 330 | return 0; |
| 331 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 332 | // TODO: make this Thumb2 specific |
| 333 | code &= ~0x1; |
| 334 | return reinterpret_cast<uint32_t*>(code)[-1]; |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | const Method::InvokeStub* OatFile::OatMethod::GetInvokeStub() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 338 | return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_); |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | uint32_t OatFile::OatMethod::GetInvokeStubSize() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 342 | uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub()); |
| 343 | if (code == 0) { |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 344 | return 0; |
| 345 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 346 | // TODO: make this Thumb2 specific |
| 347 | code &= ~0x1; |
| 348 | return reinterpret_cast<uint32_t*>(code)[-1]; |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 349 | } |
| 350 | |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 351 | #if defined(ART_USE_LLVM_COMPILER) |
| 352 | const void* OatFile::OatMethod::GetProxyStub() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 353 | return GetOatPointer<const void*>(proxy_stub_offset_); |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 354 | } |
| 355 | #endif |
| 356 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 357 | void OatFile::OatMethod::LinkMethodPointers(Method* method) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 358 | CHECK(method != NULL); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 359 | method->SetCode(GetCode()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 360 | method->SetFrameSizeInBytes(frame_size_in_bytes_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 361 | method->SetCoreSpillMask(core_spill_mask_); |
| 362 | method->SetFpSpillMask(fp_spill_mask_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 363 | method->SetMappingTable(GetMappingTable()); |
| 364 | method->SetVmapTable(GetVmapTable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 365 | 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] | 366 | method->SetInvokeStub(GetInvokeStub()); |
| 367 | } |
| 368 | |
| 369 | void OatFile::OatMethod::LinkMethodOffsets(Method* method) const { |
| 370 | CHECK(method != NULL); |
| 371 | method->SetOatCodeOffset(GetCodeOffset()); |
| 372 | method->SetFrameSizeInBytes(GetFrameSizeInBytes()); |
| 373 | method->SetCoreSpillMask(GetCoreSpillMask()); |
| 374 | method->SetFpSpillMask(GetFpSpillMask()); |
| 375 | method->SetOatMappingTableOffset(GetMappingTableOffset()); |
| 376 | method->SetOatVmapTableOffset(GetVmapTableOffset()); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 377 | method->SetOatGcMapOffset(GetGcMapOffset()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 378 | method->SetOatInvokeStubOffset(GetInvokeStubOffset()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 381 | } // namespace art |