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' }; |
Mathieu Chartier | 5f9a954 | 2014-11-07 18:38:47 -0800 | [diff] [blame] | 28 | const uint8_t OatHeader::kOatVersion[] = { '0', '4', '6', '\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; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 108 | portable_imt_conflict_trampoline_offset_ = 0; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 109 | portable_resolution_trampoline_offset_ = 0; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 110 | portable_to_interpreter_bridge_offset_ = 0; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 111 | quick_generic_jni_trampoline_offset_ = 0; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 112 | quick_imt_conflict_trampoline_offset_ = 0; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 113 | quick_resolution_trampoline_offset_ = 0; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 114 | quick_to_interpreter_bridge_offset_ = 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool OatHeader::IsValid() const { |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 118 | if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 121 | if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 124 | if (!IsAligned<kPageSize>(executable_offset_)) { |
| 125 | return false; |
| 126 | } |
| 127 | if (!IsAligned<kPageSize>(image_patch_delta_)) { |
| 128 | return false; |
| 129 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
| 133 | const char* OatHeader::GetMagic() const { |
| 134 | CHECK(IsValid()); |
| 135 | return reinterpret_cast<const char*>(magic_); |
| 136 | } |
| 137 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 138 | uint32_t OatHeader::GetChecksum() const { |
| 139 | CHECK(IsValid()); |
| 140 | return adler32_checksum_; |
| 141 | } |
| 142 | |
| 143 | void OatHeader::UpdateChecksum(const void* data, size_t length) { |
| 144 | DCHECK(IsValid()); |
| 145 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 146 | adler32_checksum_ = adler32(adler32_checksum_, bytes, length); |
| 147 | } |
| 148 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 149 | InstructionSet OatHeader::GetInstructionSet() const { |
| 150 | CHECK(IsValid()); |
| 151 | return instruction_set_; |
| 152 | } |
| 153 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 154 | uint32_t OatHeader::GetInstructionSetFeaturesBitmap() const { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 155 | CHECK(IsValid()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 156 | return instruction_set_features_bitmap_; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 159 | uint32_t OatHeader::GetExecutableOffset() const { |
| 160 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 161 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 162 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 163 | return executable_offset_; |
| 164 | } |
| 165 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 166 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
| 167 | DCHECK_ALIGNED(executable_offset, kPageSize); |
| 168 | CHECK_GT(executable_offset, sizeof(OatHeader)); |
| 169 | DCHECK(IsValid()); |
| 170 | DCHECK_EQ(executable_offset_, 0U); |
| 171 | |
| 172 | executable_offset_ = executable_offset; |
| 173 | UpdateChecksum(&executable_offset_, sizeof(executable_offset)); |
| 174 | } |
| 175 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 176 | const void* OatHeader::GetInterpreterToInterpreterBridge() const { |
| 177 | return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToInterpreterBridgeOffset(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 180 | uint32_t OatHeader::GetInterpreterToInterpreterBridgeOffset() const { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 181 | DCHECK(IsValid()); |
Dave Allison | c6104ae | 2014-03-12 11:05:39 -0700 | [diff] [blame] | 182 | CHECK(interpreter_to_interpreter_bridge_offset_ == 0 || |
| 183 | interpreter_to_interpreter_bridge_offset_ >= executable_offset_); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 184 | return interpreter_to_interpreter_bridge_offset_; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 187 | void OatHeader::SetInterpreterToInterpreterBridgeOffset(uint32_t offset) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 188 | CHECK(offset == 0 || offset >= executable_offset_); |
| 189 | DCHECK(IsValid()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 190 | DCHECK_EQ(interpreter_to_interpreter_bridge_offset_, 0U) << offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 191 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 192 | interpreter_to_interpreter_bridge_offset_ = offset; |
| 193 | UpdateChecksum(&interpreter_to_interpreter_bridge_offset_, sizeof(offset)); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 196 | const void* OatHeader::GetInterpreterToCompiledCodeBridge() const { |
| 197 | return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToCompiledCodeBridgeOffset(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 200 | uint32_t OatHeader::GetInterpreterToCompiledCodeBridgeOffset() const { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 201 | DCHECK(IsValid()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 202 | CHECK_GE(interpreter_to_compiled_code_bridge_offset_, interpreter_to_interpreter_bridge_offset_); |
| 203 | return interpreter_to_compiled_code_bridge_offset_; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 206 | void OatHeader::SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset) { |
| 207 | CHECK(offset == 0 || offset >= interpreter_to_interpreter_bridge_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 208 | DCHECK(IsValid()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 209 | DCHECK_EQ(interpreter_to_compiled_code_bridge_offset_, 0U) << offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 210 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 211 | interpreter_to_compiled_code_bridge_offset_ = offset; |
| 212 | UpdateChecksum(&interpreter_to_compiled_code_bridge_offset_, sizeof(offset)); |
| 213 | } |
| 214 | |
| 215 | const void* OatHeader::GetJniDlsymLookup() const { |
| 216 | return reinterpret_cast<const uint8_t*>(this) + GetJniDlsymLookupOffset(); |
| 217 | } |
| 218 | |
| 219 | uint32_t OatHeader::GetJniDlsymLookupOffset() const { |
| 220 | DCHECK(IsValid()); |
| 221 | CHECK_GE(jni_dlsym_lookup_offset_, interpreter_to_compiled_code_bridge_offset_); |
| 222 | return jni_dlsym_lookup_offset_; |
| 223 | } |
| 224 | |
| 225 | void OatHeader::SetJniDlsymLookupOffset(uint32_t offset) { |
| 226 | CHECK(offset == 0 || offset >= interpreter_to_compiled_code_bridge_offset_); |
| 227 | DCHECK(IsValid()); |
| 228 | DCHECK_EQ(jni_dlsym_lookup_offset_, 0U) << offset; |
| 229 | |
| 230 | jni_dlsym_lookup_offset_ = offset; |
| 231 | UpdateChecksum(&jni_dlsym_lookup_offset_, sizeof(offset)); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 234 | const void* OatHeader::GetPortableImtConflictTrampoline() const { |
| 235 | return reinterpret_cast<const uint8_t*>(this) + GetPortableImtConflictTrampolineOffset(); |
| 236 | } |
| 237 | |
| 238 | uint32_t OatHeader::GetPortableImtConflictTrampolineOffset() const { |
| 239 | DCHECK(IsValid()); |
| 240 | CHECK_GE(portable_imt_conflict_trampoline_offset_, jni_dlsym_lookup_offset_); |
| 241 | return portable_imt_conflict_trampoline_offset_; |
| 242 | } |
| 243 | |
| 244 | void OatHeader::SetPortableImtConflictTrampolineOffset(uint32_t offset) { |
| 245 | CHECK(offset == 0 || offset >= jni_dlsym_lookup_offset_); |
| 246 | DCHECK(IsValid()); |
| 247 | DCHECK_EQ(portable_imt_conflict_trampoline_offset_, 0U) << offset; |
| 248 | |
| 249 | portable_imt_conflict_trampoline_offset_ = offset; |
| 250 | UpdateChecksum(&portable_imt_conflict_trampoline_offset_, sizeof(offset)); |
| 251 | } |
| 252 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 253 | const void* OatHeader::GetPortableResolutionTrampoline() const { |
| 254 | return reinterpret_cast<const uint8_t*>(this) + GetPortableResolutionTrampolineOffset(); |
| 255 | } |
| 256 | |
| 257 | uint32_t OatHeader::GetPortableResolutionTrampolineOffset() const { |
| 258 | DCHECK(IsValid()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 259 | CHECK_GE(portable_resolution_trampoline_offset_, portable_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 260 | return portable_resolution_trampoline_offset_; |
| 261 | } |
| 262 | |
| 263 | void OatHeader::SetPortableResolutionTrampolineOffset(uint32_t offset) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 264 | CHECK(offset == 0 || offset >= portable_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 265 | DCHECK(IsValid()); |
| 266 | DCHECK_EQ(portable_resolution_trampoline_offset_, 0U) << offset; |
| 267 | |
| 268 | portable_resolution_trampoline_offset_ = offset; |
| 269 | UpdateChecksum(&portable_resolution_trampoline_offset_, sizeof(offset)); |
| 270 | } |
| 271 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 272 | const void* OatHeader::GetPortableToInterpreterBridge() const { |
| 273 | return reinterpret_cast<const uint8_t*>(this) + GetPortableToInterpreterBridgeOffset(); |
| 274 | } |
| 275 | |
| 276 | uint32_t OatHeader::GetPortableToInterpreterBridgeOffset() const { |
| 277 | DCHECK(IsValid()); |
| 278 | CHECK_GE(portable_to_interpreter_bridge_offset_, portable_resolution_trampoline_offset_); |
| 279 | return portable_to_interpreter_bridge_offset_; |
| 280 | } |
| 281 | |
| 282 | void OatHeader::SetPortableToInterpreterBridgeOffset(uint32_t offset) { |
| 283 | CHECK(offset == 0 || offset >= portable_resolution_trampoline_offset_); |
| 284 | DCHECK(IsValid()); |
| 285 | DCHECK_EQ(portable_to_interpreter_bridge_offset_, 0U) << offset; |
| 286 | |
| 287 | portable_to_interpreter_bridge_offset_ = offset; |
| 288 | UpdateChecksum(&portable_to_interpreter_bridge_offset_, sizeof(offset)); |
| 289 | } |
| 290 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 291 | const void* OatHeader::GetQuickGenericJniTrampoline() const { |
| 292 | return reinterpret_cast<const uint8_t*>(this) + GetQuickGenericJniTrampolineOffset(); |
| 293 | } |
| 294 | |
| 295 | uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const { |
| 296 | DCHECK(IsValid()); |
| 297 | CHECK_GE(quick_generic_jni_trampoline_offset_, portable_to_interpreter_bridge_offset_); |
| 298 | return quick_generic_jni_trampoline_offset_; |
| 299 | } |
| 300 | |
| 301 | void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) { |
| 302 | CHECK(offset == 0 || offset >= portable_to_interpreter_bridge_offset_); |
| 303 | DCHECK(IsValid()); |
| 304 | DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset; |
| 305 | |
| 306 | quick_generic_jni_trampoline_offset_ = offset; |
| 307 | UpdateChecksum(&quick_generic_jni_trampoline_offset_, sizeof(offset)); |
| 308 | } |
| 309 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 310 | const void* OatHeader::GetQuickImtConflictTrampoline() const { |
| 311 | return reinterpret_cast<const uint8_t*>(this) + GetQuickImtConflictTrampolineOffset(); |
| 312 | } |
| 313 | |
| 314 | uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const { |
| 315 | DCHECK(IsValid()); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 316 | CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 317 | return quick_imt_conflict_trampoline_offset_; |
| 318 | } |
| 319 | |
| 320 | void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) { |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 321 | CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 322 | DCHECK(IsValid()); |
| 323 | DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset; |
| 324 | |
| 325 | quick_imt_conflict_trampoline_offset_ = offset; |
| 326 | UpdateChecksum(&quick_imt_conflict_trampoline_offset_, sizeof(offset)); |
| 327 | } |
| 328 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 329 | const void* OatHeader::GetQuickResolutionTrampoline() const { |
| 330 | return reinterpret_cast<const uint8_t*>(this) + GetQuickResolutionTrampolineOffset(); |
| 331 | } |
| 332 | |
| 333 | uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const { |
| 334 | DCHECK(IsValid()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 335 | CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 336 | return quick_resolution_trampoline_offset_; |
| 337 | } |
| 338 | |
| 339 | void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 340 | CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 341 | DCHECK(IsValid()); |
| 342 | DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset; |
| 343 | |
| 344 | quick_resolution_trampoline_offset_ = offset; |
| 345 | UpdateChecksum(&quick_resolution_trampoline_offset_, sizeof(offset)); |
| 346 | } |
| 347 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 348 | const void* OatHeader::GetQuickToInterpreterBridge() const { |
| 349 | return reinterpret_cast<const uint8_t*>(this) + GetQuickToInterpreterBridgeOffset(); |
| 350 | } |
| 351 | |
| 352 | uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const { |
| 353 | DCHECK(IsValid()); |
| 354 | CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_); |
| 355 | return quick_to_interpreter_bridge_offset_; |
| 356 | } |
| 357 | |
| 358 | void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) { |
| 359 | CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_); |
| 360 | DCHECK(IsValid()); |
| 361 | DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset; |
| 362 | |
| 363 | quick_to_interpreter_bridge_offset_ = offset; |
| 364 | UpdateChecksum(&quick_to_interpreter_bridge_offset_, sizeof(offset)); |
| 365 | } |
| 366 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 367 | int32_t OatHeader::GetImagePatchDelta() const { |
| 368 | CHECK(IsValid()); |
| 369 | return image_patch_delta_; |
| 370 | } |
| 371 | |
| 372 | void OatHeader::RelocateOat(off_t delta) { |
| 373 | CHECK(IsValid()); |
| 374 | CHECK_ALIGNED(delta, kPageSize); |
| 375 | image_patch_delta_ += delta; |
| 376 | if (image_file_location_oat_data_begin_ != 0) { |
| 377 | image_file_location_oat_data_begin_ += delta; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void OatHeader::SetImagePatchDelta(int32_t off) { |
| 382 | CHECK(IsValid()); |
| 383 | CHECK_ALIGNED(off, kPageSize); |
| 384 | image_patch_delta_ = off; |
| 385 | } |
| 386 | |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 387 | uint32_t OatHeader::GetImageFileLocationOatChecksum() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 388 | CHECK(IsValid()); |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 389 | return image_file_location_oat_checksum_; |
| 390 | } |
| 391 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 392 | uint32_t OatHeader::GetImageFileLocationOatDataBegin() const { |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 393 | CHECK(IsValid()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 394 | return image_file_location_oat_data_begin_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 397 | uint32_t OatHeader::GetKeyValueStoreSize() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 398 | CHECK(IsValid()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 399 | return key_value_store_size_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 402 | const uint8_t* OatHeader::GetKeyValueStore() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 403 | CHECK(IsValid()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 404 | return key_value_store_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 407 | // Advance start until it is either end or \0. |
| 408 | static const char* ParseString(const char* start, const char* end) { |
| 409 | while (start < end && *start != 0) { |
| 410 | start++; |
| 411 | } |
| 412 | return start; |
| 413 | } |
| 414 | |
| 415 | const char* OatHeader::GetStoreValueByKey(const char* key) const { |
| 416 | const char* ptr = reinterpret_cast<const char*>(&key_value_store_); |
| 417 | const char* end = ptr + key_value_store_size_; |
| 418 | |
| 419 | while (ptr < end) { |
| 420 | // Scan for a closing zero. |
| 421 | const char* str_end = ParseString(ptr, end); |
| 422 | if (str_end < end) { |
| 423 | if (strcmp(key, ptr) == 0) { |
| 424 | // Same as key. Check if value is OK. |
| 425 | if (ParseString(str_end + 1, end) < end) { |
| 426 | return str_end + 1; |
| 427 | } |
| 428 | } else { |
| 429 | // Different from key. Advance over the value. |
| 430 | ptr = ParseString(str_end + 1, end) + 1; |
| 431 | } |
| 432 | } else { |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | // Not found. |
| 437 | return nullptr; |
| 438 | } |
| 439 | |
| 440 | bool OatHeader::GetStoreKeyValuePairByIndex(size_t index, const char** key, |
| 441 | const char** value) const { |
| 442 | const char* ptr = reinterpret_cast<const char*>(&key_value_store_); |
| 443 | const char* end = ptr + key_value_store_size_; |
| 444 | ssize_t counter = static_cast<ssize_t>(index); |
| 445 | |
| 446 | while (ptr < end && counter >= 0) { |
| 447 | // Scan for a closing zero. |
| 448 | const char* str_end = ParseString(ptr, end); |
| 449 | if (str_end < end) { |
| 450 | const char* maybe_key = ptr; |
| 451 | ptr = ParseString(str_end + 1, end) + 1; |
| 452 | if (ptr <= end) { |
| 453 | if (counter == 0) { |
| 454 | *key = maybe_key; |
| 455 | *value = str_end + 1; |
| 456 | return true; |
| 457 | } else { |
| 458 | counter--; |
| 459 | } |
| 460 | } else { |
| 461 | return false; |
| 462 | } |
| 463 | } else { |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | // Not found. |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | size_t OatHeader::GetHeaderSize() const { |
| 472 | return sizeof(OatHeader) + key_value_store_size_; |
| 473 | } |
| 474 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 475 | bool OatHeader::IsPic() const { |
| 476 | const char* pic_string = GetStoreValueByKey(OatHeader::kPicKey); |
| 477 | static const char kTrue[] = "true"; |
| 478 | return (pic_string != nullptr && strncmp(pic_string, kTrue, sizeof(kTrue)) == 0); |
| 479 | } |
| 480 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 481 | void OatHeader::Flatten(const SafeMap<std::string, std::string>* key_value_store) { |
| 482 | char* data_ptr = reinterpret_cast<char*>(&key_value_store_); |
| 483 | if (key_value_store != nullptr) { |
| 484 | SafeMap<std::string, std::string>::const_iterator it = key_value_store->begin(); |
| 485 | SafeMap<std::string, std::string>::const_iterator end = key_value_store->end(); |
| 486 | for ( ; it != end; ++it) { |
| 487 | strcpy(data_ptr, it->first.c_str()); |
| 488 | data_ptr += it->first.length() + 1; |
| 489 | strcpy(data_ptr, it->second.c_str()); |
| 490 | data_ptr += it->second.length() + 1; |
| 491 | } |
| 492 | } |
| 493 | key_value_store_size_ = data_ptr - reinterpret_cast<char*>(&key_value_store_); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 496 | OatMethodOffsets::OatMethodOffsets() |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 497 | : code_offset_(0), |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame] | 498 | gc_map_offset_(0) |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 499 | {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 500 | |
| 501 | OatMethodOffsets::OatMethodOffsets(uint32_t code_offset, |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame] | 502 | uint32_t gc_map_offset |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 503 | ) |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 504 | : code_offset_(code_offset), |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame] | 505 | gc_map_offset_(gc_map_offset) |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 506 | {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 507 | |
| 508 | OatMethodOffsets::~OatMethodOffsets() {} |
| 509 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 510 | OatQuickMethodHeader::OatQuickMethodHeader() |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 511 | : mapping_table_offset_(0), |
| 512 | vmap_table_offset_(0), |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 513 | frame_info_(0, 0, 0), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 514 | code_size_(0) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 515 | {} |
| 516 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 517 | OatQuickMethodHeader::OatQuickMethodHeader( |
| 518 | uint32_t mapping_table_offset, uint32_t vmap_table_offset, uint32_t frame_size_in_bytes, |
| 519 | uint32_t core_spill_mask, uint32_t fp_spill_mask, uint32_t code_size) |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 520 | : mapping_table_offset_(mapping_table_offset), |
| 521 | vmap_table_offset_(vmap_table_offset), |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 522 | frame_info_(frame_size_in_bytes, core_spill_mask, fp_spill_mask), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 523 | code_size_(code_size) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 524 | {} |
| 525 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 526 | OatQuickMethodHeader::~OatQuickMethodHeader() {} |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 527 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 528 | } // namespace art |