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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
Andreas Gampe | 639b2b1 | 2019-01-08 10:32:50 -0800 | [diff] [blame] | 23 | #include "arch/instruction_set.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | #include "arch/instruction_set_features.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 26 | #include "base/strlcpy.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 27 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 30 | using android::base::StringPrintf; |
| 31 | |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 32 | constexpr uint8_t OatHeader::kOatMagic[4]; |
| 33 | constexpr uint8_t OatHeader::kOatVersion[4]; |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 34 | constexpr const char OatHeader::kTrueValue[]; |
| 35 | constexpr const char OatHeader::kFalseValue[]; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 36 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 37 | static size_t ComputeOatHeaderSize(const SafeMap<std::string, std::string>* variable_data) { |
| 38 | size_t estimate = 0U; |
| 39 | if (variable_data != nullptr) { |
| 40 | SafeMap<std::string, std::string>::const_iterator it = variable_data->begin(); |
| 41 | SafeMap<std::string, std::string>::const_iterator end = variable_data->end(); |
| 42 | for ( ; it != end; ++it) { |
| 43 | estimate += it->first.length() + 1; |
| 44 | estimate += it->second.length() + 1; |
| 45 | } |
| 46 | } |
| 47 | return sizeof(OatHeader) + estimate; |
| 48 | } |
| 49 | |
| 50 | OatHeader* OatHeader::Create(InstructionSet instruction_set, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 51 | const InstructionSetFeatures* instruction_set_features, |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 52 | uint32_t dex_file_count, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 53 | const SafeMap<std::string, std::string>* variable_data) { |
| 54 | // Estimate size of optional data. |
| 55 | size_t needed_size = ComputeOatHeaderSize(variable_data); |
| 56 | |
| 57 | // Reserve enough memory. |
| 58 | void* memory = operator new (needed_size); |
| 59 | |
| 60 | // Create the OatHeader in-place. |
| 61 | return new (memory) OatHeader(instruction_set, |
| 62 | instruction_set_features, |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 63 | dex_file_count, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 64 | variable_data); |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 67 | OatHeader::OatHeader(InstructionSet instruction_set, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 68 | const InstructionSetFeatures* instruction_set_features, |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 69 | uint32_t dex_file_count, |
| 70 | const SafeMap<std::string, std::string>* variable_data) |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 71 | : oat_checksum_(0u), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 72 | instruction_set_(instruction_set), |
| 73 | instruction_set_features_bitmap_(instruction_set_features->AsBitmap()), |
| 74 | dex_file_count_(dex_file_count), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 75 | oat_dex_files_offset_(0), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 76 | executable_offset_(0), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 77 | jni_dlsym_lookup_offset_(0), |
| 78 | quick_generic_jni_trampoline_offset_(0), |
| 79 | quick_imt_conflict_trampoline_offset_(0), |
| 80 | quick_resolution_trampoline_offset_(0), |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 81 | quick_to_interpreter_bridge_offset_(0) { |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 82 | // Don't want asserts in header as they would be checked in each file that includes it. But the |
| 83 | // fields are private, so we check inside a method. |
| 84 | static_assert(sizeof(magic_) == sizeof(kOatMagic), |
| 85 | "Oat magic and magic_ have different lengths."); |
| 86 | static_assert(sizeof(version_) == sizeof(kOatVersion), |
| 87 | "Oat version and version_ have different lengths."); |
| 88 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 89 | memcpy(magic_, kOatMagic, sizeof(kOatMagic)); |
| 90 | memcpy(version_, kOatVersion, sizeof(kOatVersion)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 91 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 92 | CHECK_NE(instruction_set, InstructionSet::kNone); |
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); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | bool OatHeader::IsValid() const { |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 99 | if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame] | 102 | if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 103 | return false; |
| 104 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 105 | if (!IsAligned<kPageSize>(executable_offset_)) { |
| 106 | return false; |
| 107 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 108 | if (!IsValidInstructionSet(instruction_set_)) { |
| 109 | return false; |
| 110 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 114 | std::string OatHeader::GetValidationErrorMessage() const { |
| 115 | if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) { |
| 116 | static_assert(sizeof(kOatMagic) == 4, "kOatMagic has unexpected length"); |
| 117 | return StringPrintf("Invalid oat magic, expected 0x%x%x%x%x, got 0x%x%x%x%x.", |
| 118 | kOatMagic[0], kOatMagic[1], kOatMagic[2], kOatMagic[3], |
| 119 | magic_[0], magic_[1], magic_[2], magic_[3]); |
| 120 | } |
| 121 | if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) { |
| 122 | static_assert(sizeof(kOatVersion) == 4, "kOatVersion has unexpected length"); |
| 123 | return StringPrintf("Invalid oat version, expected 0x%x%x%x%x, got 0x%x%x%x%x.", |
| 124 | kOatVersion[0], kOatVersion[1], kOatVersion[2], kOatVersion[3], |
| 125 | version_[0], version_[1], version_[2], version_[3]); |
| 126 | } |
| 127 | if (!IsAligned<kPageSize>(executable_offset_)) { |
| 128 | return "Executable offset not page-aligned."; |
| 129 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 130 | if (!IsValidInstructionSet(instruction_set_)) { |
| 131 | return StringPrintf("Invalid instruction set, %d.", static_cast<int>(instruction_set_)); |
| 132 | } |
Andreas Gampe | 2bcb3b2 | 2014-12-12 15:25:14 -0800 | [diff] [blame] | 133 | return ""; |
| 134 | } |
| 135 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 136 | const char* OatHeader::GetMagic() const { |
| 137 | CHECK(IsValid()); |
| 138 | return reinterpret_cast<const char*>(magic_); |
| 139 | } |
| 140 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 141 | uint32_t OatHeader::GetChecksum() const { |
| 142 | CHECK(IsValid()); |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 143 | return oat_checksum_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 146 | void OatHeader::SetChecksum(uint32_t oat_checksum) { |
| 147 | oat_checksum_ = oat_checksum; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 150 | InstructionSet OatHeader::GetInstructionSet() const { |
| 151 | CHECK(IsValid()); |
| 152 | return instruction_set_; |
| 153 | } |
| 154 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 155 | uint32_t OatHeader::GetInstructionSetFeaturesBitmap() const { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 156 | CHECK(IsValid()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 157 | return instruction_set_features_bitmap_; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 160 | uint32_t OatHeader::GetOatDexFilesOffset() const { |
| 161 | DCHECK(IsValid()); |
| 162 | DCHECK_GT(oat_dex_files_offset_, sizeof(OatHeader)); |
| 163 | return oat_dex_files_offset_; |
| 164 | } |
| 165 | |
| 166 | void OatHeader::SetOatDexFilesOffset(uint32_t oat_dex_files_offset) { |
| 167 | DCHECK_GT(oat_dex_files_offset, sizeof(OatHeader)); |
| 168 | DCHECK(IsValid()); |
| 169 | DCHECK_EQ(oat_dex_files_offset_, 0u); |
| 170 | |
| 171 | oat_dex_files_offset_ = oat_dex_files_offset; |
| 172 | } |
| 173 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 174 | uint32_t OatHeader::GetExecutableOffset() const { |
| 175 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 176 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 177 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 178 | return executable_offset_; |
| 179 | } |
| 180 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 181 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
| 182 | DCHECK_ALIGNED(executable_offset, kPageSize); |
| 183 | CHECK_GT(executable_offset, sizeof(OatHeader)); |
| 184 | DCHECK(IsValid()); |
| 185 | DCHECK_EQ(executable_offset_, 0U); |
| 186 | |
| 187 | executable_offset_ = executable_offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame^] | 190 | static const void* GetTrampoline(const OatHeader& header, uint32_t offset) { |
| 191 | return (offset != 0u) ? reinterpret_cast<const uint8_t*>(&header) + offset : nullptr; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | const void* OatHeader::GetJniDlsymLookup() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame^] | 195 | return GetTrampoline(*this, GetJniDlsymLookupOffset()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | uint32_t OatHeader::GetJniDlsymLookupOffset() const { |
| 199 | DCHECK(IsValid()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 200 | return jni_dlsym_lookup_offset_; |
| 201 | } |
| 202 | |
| 203 | void OatHeader::SetJniDlsymLookupOffset(uint32_t offset) { |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 204 | DCHECK(IsValid()); |
| 205 | DCHECK_EQ(jni_dlsym_lookup_offset_, 0U) << offset; |
| 206 | |
| 207 | jni_dlsym_lookup_offset_ = offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 210 | const void* OatHeader::GetQuickGenericJniTrampoline() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame^] | 211 | return GetTrampoline(*this, GetQuickGenericJniTrampolineOffset()); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const { |
| 215 | DCHECK(IsValid()); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 216 | CHECK_GE(quick_generic_jni_trampoline_offset_, jni_dlsym_lookup_offset_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 217 | return quick_generic_jni_trampoline_offset_; |
| 218 | } |
| 219 | |
| 220 | void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 221 | CHECK(offset == 0 || offset >= jni_dlsym_lookup_offset_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 222 | DCHECK(IsValid()); |
| 223 | DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset; |
| 224 | |
| 225 | quick_generic_jni_trampoline_offset_ = offset; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 228 | const void* OatHeader::GetQuickImtConflictTrampoline() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame^] | 229 | return GetTrampoline(*this, GetQuickImtConflictTrampolineOffset()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const { |
| 233 | DCHECK(IsValid()); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 234 | CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 235 | return quick_imt_conflict_trampoline_offset_; |
| 236 | } |
| 237 | |
| 238 | void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) { |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 239 | CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 240 | DCHECK(IsValid()); |
| 241 | DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset; |
| 242 | |
| 243 | quick_imt_conflict_trampoline_offset_ = offset; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 246 | const void* OatHeader::GetQuickResolutionTrampoline() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame^] | 247 | return GetTrampoline(*this, GetQuickResolutionTrampolineOffset()); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const { |
| 251 | DCHECK(IsValid()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 252 | CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 253 | return quick_resolution_trampoline_offset_; |
| 254 | } |
| 255 | |
| 256 | void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 257 | CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 258 | DCHECK(IsValid()); |
| 259 | DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset; |
| 260 | |
| 261 | quick_resolution_trampoline_offset_ = offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 264 | const void* OatHeader::GetQuickToInterpreterBridge() const { |
Vladimir Marko | 98fb083 | 2019-03-13 16:52:11 +0000 | [diff] [blame^] | 265 | return GetTrampoline(*this, GetQuickToInterpreterBridgeOffset()); |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const { |
| 269 | DCHECK(IsValid()); |
| 270 | CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_); |
| 271 | return quick_to_interpreter_bridge_offset_; |
| 272 | } |
| 273 | |
| 274 | void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) { |
| 275 | CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_); |
| 276 | DCHECK(IsValid()); |
| 277 | DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset; |
| 278 | |
| 279 | quick_to_interpreter_bridge_offset_ = offset; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 282 | uint32_t OatHeader::GetKeyValueStoreSize() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 283 | CHECK(IsValid()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 284 | return key_value_store_size_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 287 | const uint8_t* OatHeader::GetKeyValueStore() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 288 | CHECK(IsValid()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 289 | return key_value_store_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 292 | // Advance start until it is either end or \0. |
| 293 | static const char* ParseString(const char* start, const char* end) { |
| 294 | while (start < end && *start != 0) { |
| 295 | start++; |
| 296 | } |
| 297 | return start; |
| 298 | } |
| 299 | |
| 300 | const char* OatHeader::GetStoreValueByKey(const char* key) const { |
| 301 | const char* ptr = reinterpret_cast<const char*>(&key_value_store_); |
| 302 | const char* end = ptr + key_value_store_size_; |
| 303 | |
| 304 | while (ptr < end) { |
| 305 | // Scan for a closing zero. |
| 306 | const char* str_end = ParseString(ptr, end); |
| 307 | if (str_end < end) { |
| 308 | if (strcmp(key, ptr) == 0) { |
| 309 | // Same as key. Check if value is OK. |
| 310 | if (ParseString(str_end + 1, end) < end) { |
| 311 | return str_end + 1; |
| 312 | } |
| 313 | } else { |
| 314 | // Different from key. Advance over the value. |
| 315 | ptr = ParseString(str_end + 1, end) + 1; |
| 316 | } |
| 317 | } else { |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | // Not found. |
| 322 | return nullptr; |
| 323 | } |
| 324 | |
| 325 | bool OatHeader::GetStoreKeyValuePairByIndex(size_t index, const char** key, |
| 326 | const char** value) const { |
| 327 | const char* ptr = reinterpret_cast<const char*>(&key_value_store_); |
| 328 | const char* end = ptr + key_value_store_size_; |
| 329 | ssize_t counter = static_cast<ssize_t>(index); |
| 330 | |
| 331 | while (ptr < end && counter >= 0) { |
| 332 | // Scan for a closing zero. |
| 333 | const char* str_end = ParseString(ptr, end); |
| 334 | if (str_end < end) { |
| 335 | const char* maybe_key = ptr; |
| 336 | ptr = ParseString(str_end + 1, end) + 1; |
| 337 | if (ptr <= end) { |
| 338 | if (counter == 0) { |
| 339 | *key = maybe_key; |
| 340 | *value = str_end + 1; |
| 341 | return true; |
| 342 | } else { |
| 343 | counter--; |
| 344 | } |
| 345 | } else { |
| 346 | return false; |
| 347 | } |
| 348 | } else { |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | // Not found. |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | size_t OatHeader::GetHeaderSize() const { |
| 357 | return sizeof(OatHeader) + key_value_store_size_; |
| 358 | } |
| 359 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 360 | bool OatHeader::IsDebuggable() const { |
| 361 | return IsKeyEnabled(OatHeader::kDebuggableKey); |
| 362 | } |
| 363 | |
Andreas Gampe | f8cd890 | 2017-01-18 16:05:01 -0800 | [diff] [blame] | 364 | bool OatHeader::IsConcurrentCopying() const { |
| 365 | return IsKeyEnabled(OatHeader::kConcurrentCopying); |
| 366 | } |
| 367 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 368 | bool OatHeader::IsNativeDebuggable() const { |
| 369 | return IsKeyEnabled(OatHeader::kNativeDebuggableKey); |
| 370 | } |
| 371 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 372 | CompilerFilter::Filter OatHeader::GetCompilerFilter() const { |
| 373 | CompilerFilter::Filter filter; |
| 374 | const char* key_value = GetStoreValueByKey(kCompilerFilter); |
| 375 | CHECK(key_value != nullptr) << "compiler-filter not found in oat header"; |
| 376 | CHECK(CompilerFilter::ParseCompilerFilter(key_value, &filter)) |
| 377 | << "Invalid compiler-filter in oat header: " << key_value; |
| 378 | return filter; |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | bool OatHeader::KeyHasValue(const char* key, const char* value, size_t value_size) const { |
| 382 | const char* key_value = GetStoreValueByKey(key); |
| 383 | return (key_value != nullptr && strncmp(key_value, value, value_size) == 0); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 386 | bool OatHeader::IsKeyEnabled(const char* key) const { |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 387 | return KeyHasValue(key, kTrueValue, sizeof(kTrueValue)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 390 | void OatHeader::Flatten(const SafeMap<std::string, std::string>* key_value_store) { |
| 391 | char* data_ptr = reinterpret_cast<char*>(&key_value_store_); |
| 392 | if (key_value_store != nullptr) { |
| 393 | SafeMap<std::string, std::string>::const_iterator it = key_value_store->begin(); |
| 394 | SafeMap<std::string, std::string>::const_iterator end = key_value_store->end(); |
| 395 | for ( ; it != end; ++it) { |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 396 | strlcpy(data_ptr, it->first.c_str(), it->first.length() + 1); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 397 | data_ptr += it->first.length() + 1; |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 398 | strlcpy(data_ptr, it->second.c_str(), it->second.length() + 1); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 399 | data_ptr += it->second.length() + 1; |
| 400 | } |
| 401 | } |
| 402 | key_value_store_size_ = data_ptr - reinterpret_cast<char*>(&key_value_store_); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 405 | OatMethodOffsets::OatMethodOffsets(uint32_t code_offset) : code_offset_(code_offset) { |
| 406 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 407 | |
| 408 | OatMethodOffsets::~OatMethodOffsets() {} |
| 409 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 410 | } // namespace art |