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.h" |
| 18 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 19 | #include <string.h> |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 20 | #include <zlib.h> |
| 21 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 22 | #include "arch/instruction_set_features.h" |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 23 | #include "base/stringprintf.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | #include "utils.h" |
| 25 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 26 | namespace art { |
| 27 | |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 28 | constexpr uint8_t OatHeader::kOatMagic[4]; |
| 29 | constexpr uint8_t OatHeader::kOatVersion[4]; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 30 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 31 | static size_t ComputeOatHeaderSize(const SafeMap<std::string, std::string>* variable_data) { |
| 32 | size_t estimate = 0U; |
| 33 | if (variable_data != nullptr) { |
| 34 | SafeMap<std::string, std::string>::const_iterator it = variable_data->begin(); |
| 35 | SafeMap<std::string, std::string>::const_iterator end = variable_data->end(); |
| 36 | for ( ; it != end; ++it) { |
| 37 | estimate += it->first.length() + 1; |
| 38 | estimate += it->second.length() + 1; |
| 39 | } |
| 40 | } |
| 41 | return sizeof(OatHeader) + estimate; |
| 42 | } |
| 43 | |
| 44 | OatHeader* OatHeader::Create(InstructionSet instruction_set, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 45 | const InstructionSetFeatures* instruction_set_features, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 46 | const std::vector<const DexFile*>* dex_files, |
| 47 | uint32_t image_file_location_oat_checksum, |
| 48 | uint32_t image_file_location_oat_data_begin, |
| 49 | const SafeMap<std::string, std::string>* variable_data) { |
| 50 | // Estimate size of optional data. |
| 51 | size_t needed_size = ComputeOatHeaderSize(variable_data); |
| 52 | |
| 53 | // Reserve enough memory. |
| 54 | void* memory = operator new (needed_size); |
| 55 | |
| 56 | // Create the OatHeader in-place. |
| 57 | return new (memory) OatHeader(instruction_set, |
| 58 | instruction_set_features, |
| 59 | dex_files, |
| 60 | image_file_location_oat_checksum, |
| 61 | image_file_location_oat_data_begin, |
| 62 | variable_data); |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 65 | OatHeader::OatHeader(InstructionSet instruction_set, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 66 | const InstructionSetFeatures* instruction_set_features, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 67 | const std::vector<const DexFile*>* dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 68 | uint32_t image_file_location_oat_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 69 | uint32_t image_file_location_oat_data_begin, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 70 | const SafeMap<std::string, std::string>* variable_data) { |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 71 | // Don't want asserts in header as they would be checked in each file that includes it. But the |
| 72 | // fields are private, so we check inside a method. |
| 73 | static_assert(sizeof(magic_) == sizeof(kOatMagic), |
| 74 | "Oat magic and magic_ have different lengths."); |
| 75 | static_assert(sizeof(version_) == sizeof(kOatVersion), |
| 76 | "Oat version and version_ have different lengths."); |
| 77 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 78 | memcpy(magic_, kOatMagic, sizeof(kOatMagic)); |
| 79 | memcpy(version_, kOatVersion, sizeof(kOatVersion)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 80 | executable_offset_ = 0; |
| 81 | image_patch_delta_ = 0; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 82 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 83 | adler32_checksum_ = adler32(0L, Z_NULL, 0); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 84 | |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 85 | CHECK_NE(instruction_set, kNone); |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 86 | instruction_set_ = instruction_set; |
| 87 | UpdateChecksum(&instruction_set_, sizeof(instruction_set_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 88 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 89 | instruction_set_features_bitmap_ = instruction_set_features->AsBitmap(); |
| 90 | UpdateChecksum(&instruction_set_features_bitmap_, sizeof(instruction_set_features_bitmap_)); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 91 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 92 | dex_file_count_ = dex_files->size(); |
| 93 | UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 94 | |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 95 | image_file_location_oat_checksum_ = image_file_location_oat_checksum; |
| 96 | UpdateChecksum(&image_file_location_oat_checksum_, sizeof(image_file_location_oat_checksum_)); |
| 97 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 98 | CHECK(IsAligned<kPageSize>(image_file_location_oat_data_begin)); |
| 99 | image_file_location_oat_data_begin_ = image_file_location_oat_data_begin; |
| 100 | UpdateChecksum(&image_file_location_oat_data_begin_, sizeof(image_file_location_oat_data_begin_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 101 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 102 | // Flatten the map. Will also update variable_size_data_size_. |
| 103 | Flatten(variable_data); |
| 104 | |
| 105 | // Update checksum for variable data size. |
| 106 | UpdateChecksum(&key_value_store_size_, sizeof(key_value_store_size_)); |
| 107 | |
| 108 | // Update for data, if existing. |
| 109 | if (key_value_store_size_ > 0U) { |
| 110 | UpdateChecksum(&key_value_store_, key_value_store_size_); |
| 111 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 112 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 113 | interpreter_to_interpreter_bridge_offset_ = 0; |
| 114 | interpreter_to_compiled_code_bridge_offset_ = 0; |
| 115 | jni_dlsym_lookup_offset_ = 0; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 116 | quick_generic_jni_trampoline_offset_ = 0; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 117 | quick_imt_conflict_trampoline_offset_ = 0; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 118 | quick_resolution_trampoline_offset_ = 0; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 119 | quick_to_interpreter_bridge_offset_ = 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | bool OatHeader::IsValid() const { |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 123 | if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 126 | if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 127 | return false; |
| 128 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 129 | if (!IsAligned<kPageSize>(executable_offset_)) { |
| 130 | return false; |
| 131 | } |
| 132 | if (!IsAligned<kPageSize>(image_patch_delta_)) { |
| 133 | return false; |
| 134 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 138 | std::string OatHeader::GetValidationErrorMessage() const { |
| 139 | if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) { |
| 140 | static_assert(sizeof(kOatMagic) == 4, "kOatMagic has unexpected length"); |
| 141 | return StringPrintf("Invalid oat magic, expected 0x%x%x%x%x, got 0x%x%x%x%x.", |
| 142 | kOatMagic[0], kOatMagic[1], kOatMagic[2], kOatMagic[3], |
| 143 | magic_[0], magic_[1], magic_[2], magic_[3]); |
| 144 | } |
| 145 | if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) { |
| 146 | static_assert(sizeof(kOatVersion) == 4, "kOatVersion has unexpected length"); |
| 147 | return StringPrintf("Invalid oat version, expected 0x%x%x%x%x, got 0x%x%x%x%x.", |
| 148 | kOatVersion[0], kOatVersion[1], kOatVersion[2], kOatVersion[3], |
| 149 | version_[0], version_[1], version_[2], version_[3]); |
| 150 | } |
| 151 | if (!IsAligned<kPageSize>(executable_offset_)) { |
| 152 | return "Executable offset not page-aligned."; |
| 153 | } |
| 154 | if (!IsAligned<kPageSize>(image_patch_delta_)) { |
| 155 | return "Image patch delta not page-aligned."; |
| 156 | } |
| 157 | return ""; |
| 158 | } |
| 159 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 160 | const char* OatHeader::GetMagic() const { |
| 161 | CHECK(IsValid()); |
| 162 | return reinterpret_cast<const char*>(magic_); |
| 163 | } |
| 164 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 165 | uint32_t OatHeader::GetChecksum() const { |
| 166 | CHECK(IsValid()); |
| 167 | return adler32_checksum_; |
| 168 | } |
| 169 | |
| 170 | void OatHeader::UpdateChecksum(const void* data, size_t length) { |
| 171 | DCHECK(IsValid()); |
| 172 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 173 | adler32_checksum_ = adler32(adler32_checksum_, bytes, length); |
| 174 | } |
| 175 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 176 | InstructionSet OatHeader::GetInstructionSet() const { |
| 177 | CHECK(IsValid()); |
| 178 | return instruction_set_; |
| 179 | } |
| 180 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 181 | uint32_t OatHeader::GetInstructionSetFeaturesBitmap() const { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 182 | CHECK(IsValid()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 183 | return instruction_set_features_bitmap_; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 186 | uint32_t OatHeader::GetExecutableOffset() const { |
| 187 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 188 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 189 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 190 | return executable_offset_; |
| 191 | } |
| 192 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 193 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
| 194 | DCHECK_ALIGNED(executable_offset, kPageSize); |
| 195 | CHECK_GT(executable_offset, sizeof(OatHeader)); |
| 196 | DCHECK(IsValid()); |
| 197 | DCHECK_EQ(executable_offset_, 0U); |
| 198 | |
| 199 | executable_offset_ = executable_offset; |
| 200 | UpdateChecksum(&executable_offset_, sizeof(executable_offset)); |
| 201 | } |
| 202 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 203 | const void* OatHeader::GetInterpreterToInterpreterBridge() const { |
| 204 | return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToInterpreterBridgeOffset(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 207 | uint32_t OatHeader::GetInterpreterToInterpreterBridgeOffset() const { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 208 | DCHECK(IsValid()); |
Dave Allison | c6104ae | 2014-03-12 11:05:39 -0700 | [diff] [blame] | 209 | CHECK(interpreter_to_interpreter_bridge_offset_ == 0 || |
| 210 | interpreter_to_interpreter_bridge_offset_ >= executable_offset_); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 211 | return interpreter_to_interpreter_bridge_offset_; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 214 | void OatHeader::SetInterpreterToInterpreterBridgeOffset(uint32_t offset) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 215 | CHECK(offset == 0 || offset >= executable_offset_); |
| 216 | DCHECK(IsValid()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 217 | DCHECK_EQ(interpreter_to_interpreter_bridge_offset_, 0U) << offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 218 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 219 | interpreter_to_interpreter_bridge_offset_ = offset; |
| 220 | UpdateChecksum(&interpreter_to_interpreter_bridge_offset_, sizeof(offset)); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 223 | const void* OatHeader::GetInterpreterToCompiledCodeBridge() const { |
| 224 | return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToCompiledCodeBridgeOffset(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 227 | uint32_t OatHeader::GetInterpreterToCompiledCodeBridgeOffset() const { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 228 | DCHECK(IsValid()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 229 | CHECK_GE(interpreter_to_compiled_code_bridge_offset_, interpreter_to_interpreter_bridge_offset_); |
| 230 | return interpreter_to_compiled_code_bridge_offset_; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 233 | void OatHeader::SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset) { |
| 234 | CHECK(offset == 0 || offset >= interpreter_to_interpreter_bridge_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 235 | DCHECK(IsValid()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 236 | DCHECK_EQ(interpreter_to_compiled_code_bridge_offset_, 0U) << offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 237 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 238 | interpreter_to_compiled_code_bridge_offset_ = offset; |
| 239 | UpdateChecksum(&interpreter_to_compiled_code_bridge_offset_, sizeof(offset)); |
| 240 | } |
| 241 | |
| 242 | const void* OatHeader::GetJniDlsymLookup() const { |
| 243 | return reinterpret_cast<const uint8_t*>(this) + GetJniDlsymLookupOffset(); |
| 244 | } |
| 245 | |
| 246 | uint32_t OatHeader::GetJniDlsymLookupOffset() const { |
| 247 | DCHECK(IsValid()); |
| 248 | CHECK_GE(jni_dlsym_lookup_offset_, interpreter_to_compiled_code_bridge_offset_); |
| 249 | return jni_dlsym_lookup_offset_; |
| 250 | } |
| 251 | |
| 252 | void OatHeader::SetJniDlsymLookupOffset(uint32_t offset) { |
| 253 | CHECK(offset == 0 || offset >= interpreter_to_compiled_code_bridge_offset_); |
| 254 | DCHECK(IsValid()); |
| 255 | DCHECK_EQ(jni_dlsym_lookup_offset_, 0U) << offset; |
| 256 | |
| 257 | jni_dlsym_lookup_offset_ = offset; |
| 258 | UpdateChecksum(&jni_dlsym_lookup_offset_, sizeof(offset)); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 261 | const void* OatHeader::GetQuickGenericJniTrampoline() const { |
| 262 | return reinterpret_cast<const uint8_t*>(this) + GetQuickGenericJniTrampolineOffset(); |
| 263 | } |
| 264 | |
| 265 | uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const { |
| 266 | DCHECK(IsValid()); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 267 | CHECK_GE(quick_generic_jni_trampoline_offset_, jni_dlsym_lookup_offset_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 268 | return quick_generic_jni_trampoline_offset_; |
| 269 | } |
| 270 | |
| 271 | void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 272 | CHECK(offset == 0 || offset >= jni_dlsym_lookup_offset_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 273 | DCHECK(IsValid()); |
| 274 | DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset; |
| 275 | |
| 276 | quick_generic_jni_trampoline_offset_ = offset; |
| 277 | UpdateChecksum(&quick_generic_jni_trampoline_offset_, sizeof(offset)); |
| 278 | } |
| 279 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 280 | const void* OatHeader::GetQuickImtConflictTrampoline() const { |
| 281 | return reinterpret_cast<const uint8_t*>(this) + GetQuickImtConflictTrampolineOffset(); |
| 282 | } |
| 283 | |
| 284 | uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const { |
| 285 | DCHECK(IsValid()); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 286 | CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 287 | return quick_imt_conflict_trampoline_offset_; |
| 288 | } |
| 289 | |
| 290 | void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) { |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 291 | CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 292 | DCHECK(IsValid()); |
| 293 | DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset; |
| 294 | |
| 295 | quick_imt_conflict_trampoline_offset_ = offset; |
| 296 | UpdateChecksum(&quick_imt_conflict_trampoline_offset_, sizeof(offset)); |
| 297 | } |
| 298 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 299 | const void* OatHeader::GetQuickResolutionTrampoline() const { |
| 300 | return reinterpret_cast<const uint8_t*>(this) + GetQuickResolutionTrampolineOffset(); |
| 301 | } |
| 302 | |
| 303 | uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const { |
| 304 | DCHECK(IsValid()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 305 | CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 306 | return quick_resolution_trampoline_offset_; |
| 307 | } |
| 308 | |
| 309 | void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 310 | CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 311 | DCHECK(IsValid()); |
| 312 | DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset; |
| 313 | |
| 314 | quick_resolution_trampoline_offset_ = offset; |
| 315 | UpdateChecksum(&quick_resolution_trampoline_offset_, sizeof(offset)); |
| 316 | } |
| 317 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 318 | const void* OatHeader::GetQuickToInterpreterBridge() const { |
| 319 | return reinterpret_cast<const uint8_t*>(this) + GetQuickToInterpreterBridgeOffset(); |
| 320 | } |
| 321 | |
| 322 | uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const { |
| 323 | DCHECK(IsValid()); |
| 324 | CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_); |
| 325 | return quick_to_interpreter_bridge_offset_; |
| 326 | } |
| 327 | |
| 328 | void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) { |
| 329 | CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_); |
| 330 | DCHECK(IsValid()); |
| 331 | DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset; |
| 332 | |
| 333 | quick_to_interpreter_bridge_offset_ = offset; |
| 334 | UpdateChecksum(&quick_to_interpreter_bridge_offset_, sizeof(offset)); |
| 335 | } |
| 336 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 337 | int32_t OatHeader::GetImagePatchDelta() const { |
| 338 | CHECK(IsValid()); |
| 339 | return image_patch_delta_; |
| 340 | } |
| 341 | |
| 342 | void OatHeader::RelocateOat(off_t delta) { |
| 343 | CHECK(IsValid()); |
| 344 | CHECK_ALIGNED(delta, kPageSize); |
| 345 | image_patch_delta_ += delta; |
| 346 | if (image_file_location_oat_data_begin_ != 0) { |
| 347 | image_file_location_oat_data_begin_ += delta; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void OatHeader::SetImagePatchDelta(int32_t off) { |
| 352 | CHECK(IsValid()); |
| 353 | CHECK_ALIGNED(off, kPageSize); |
| 354 | image_patch_delta_ = off; |
| 355 | } |
| 356 | |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 357 | uint32_t OatHeader::GetImageFileLocationOatChecksum() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 358 | CHECK(IsValid()); |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 359 | return image_file_location_oat_checksum_; |
| 360 | } |
| 361 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 362 | uint32_t OatHeader::GetImageFileLocationOatDataBegin() const { |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 363 | CHECK(IsValid()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 364 | return image_file_location_oat_data_begin_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 367 | uint32_t OatHeader::GetKeyValueStoreSize() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 368 | CHECK(IsValid()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 369 | return key_value_store_size_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 372 | const uint8_t* OatHeader::GetKeyValueStore() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 373 | CHECK(IsValid()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 374 | return key_value_store_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 377 | // Advance start until it is either end or \0. |
| 378 | static const char* ParseString(const char* start, const char* end) { |
| 379 | while (start < end && *start != 0) { |
| 380 | start++; |
| 381 | } |
| 382 | return start; |
| 383 | } |
| 384 | |
| 385 | const char* OatHeader::GetStoreValueByKey(const char* key) const { |
| 386 | const char* ptr = reinterpret_cast<const char*>(&key_value_store_); |
| 387 | const char* end = ptr + key_value_store_size_; |
| 388 | |
| 389 | while (ptr < end) { |
| 390 | // Scan for a closing zero. |
| 391 | const char* str_end = ParseString(ptr, end); |
| 392 | if (str_end < end) { |
| 393 | if (strcmp(key, ptr) == 0) { |
| 394 | // Same as key. Check if value is OK. |
| 395 | if (ParseString(str_end + 1, end) < end) { |
| 396 | return str_end + 1; |
| 397 | } |
| 398 | } else { |
| 399 | // Different from key. Advance over the value. |
| 400 | ptr = ParseString(str_end + 1, end) + 1; |
| 401 | } |
| 402 | } else { |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | // Not found. |
| 407 | return nullptr; |
| 408 | } |
| 409 | |
| 410 | bool OatHeader::GetStoreKeyValuePairByIndex(size_t index, const char** key, |
| 411 | const char** value) const { |
| 412 | const char* ptr = reinterpret_cast<const char*>(&key_value_store_); |
| 413 | const char* end = ptr + key_value_store_size_; |
| 414 | ssize_t counter = static_cast<ssize_t>(index); |
| 415 | |
| 416 | while (ptr < end && counter >= 0) { |
| 417 | // Scan for a closing zero. |
| 418 | const char* str_end = ParseString(ptr, end); |
| 419 | if (str_end < end) { |
| 420 | const char* maybe_key = ptr; |
| 421 | ptr = ParseString(str_end + 1, end) + 1; |
| 422 | if (ptr <= end) { |
| 423 | if (counter == 0) { |
| 424 | *key = maybe_key; |
| 425 | *value = str_end + 1; |
| 426 | return true; |
| 427 | } else { |
| 428 | counter--; |
| 429 | } |
| 430 | } else { |
| 431 | return false; |
| 432 | } |
| 433 | } else { |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | // Not found. |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | size_t OatHeader::GetHeaderSize() const { |
| 442 | return sizeof(OatHeader) + key_value_store_size_; |
| 443 | } |
| 444 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 445 | bool OatHeader::IsPic() const { |
| 446 | const char* pic_string = GetStoreValueByKey(OatHeader::kPicKey); |
| 447 | static const char kTrue[] = "true"; |
| 448 | return (pic_string != nullptr && strncmp(pic_string, kTrue, sizeof(kTrue)) == 0); |
| 449 | } |
| 450 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 451 | void OatHeader::Flatten(const SafeMap<std::string, std::string>* key_value_store) { |
| 452 | char* data_ptr = reinterpret_cast<char*>(&key_value_store_); |
| 453 | if (key_value_store != nullptr) { |
| 454 | SafeMap<std::string, std::string>::const_iterator it = key_value_store->begin(); |
| 455 | SafeMap<std::string, std::string>::const_iterator end = key_value_store->end(); |
| 456 | for ( ; it != end; ++it) { |
| 457 | strcpy(data_ptr, it->first.c_str()); |
| 458 | data_ptr += it->first.length() + 1; |
| 459 | strcpy(data_ptr, it->second.c_str()); |
| 460 | data_ptr += it->second.length() + 1; |
| 461 | } |
| 462 | } |
| 463 | key_value_store_size_ = data_ptr - reinterpret_cast<char*>(&key_value_store_); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 466 | OatMethodOffsets::OatMethodOffsets(uint32_t code_offset) : code_offset_(code_offset) { |
| 467 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 468 | |
| 469 | OatMethodOffsets::~OatMethodOffsets() {} |
| 470 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 471 | OatQuickMethodHeader::OatQuickMethodHeader( |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 472 | uint32_t mapping_table_offset, uint32_t vmap_table_offset, uint32_t gc_map_offset, |
| 473 | uint32_t frame_size_in_bytes, uint32_t core_spill_mask, uint32_t fp_spill_mask, |
| 474 | uint32_t code_size) |
| 475 | : mapping_table_offset_(mapping_table_offset), vmap_table_offset_(vmap_table_offset), |
| 476 | gc_map_offset_(gc_map_offset), |
| 477 | frame_info_(frame_size_in_bytes, core_spill_mask, fp_spill_mask), code_size_(code_size) { |
| 478 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 479 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 480 | OatQuickMethodHeader::~OatQuickMethodHeader() {} |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 481 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 482 | } // namespace art |