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