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