blob: 21e20e9b747fbdccc1c48521ca38a56dbd4bd7ba [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat.h"
18
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070019#include <string.h>
Brian Carlstrome24fa612011-09-29 00:53:55 -070020#include <zlib.h>
21
Andreas Gampe46ee31b2016-12-14 10:11:49 -080022#include "android-base/stringprintf.h"
23
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set_features.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/bit_utils.h"
Andreas Gampef45d61c2017-06-07 10:29:33 -070026#include "base/strlcpy.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080027
Brian Carlstrome24fa612011-09-29 00:53:55 -070028namespace art {
29
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030using android::base::StringPrintf;
31
Andreas Gampe2bcb3b22014-12-12 15:25:14 -080032constexpr uint8_t OatHeader::kOatMagic[4];
33constexpr uint8_t OatHeader::kOatVersion[4];
Sebastien Hertz0de11332015-05-13 12:14:05 +020034constexpr const char OatHeader::kTrueValue[];
35constexpr const char OatHeader::kFalseValue[];
Brian Carlstrome24fa612011-09-29 00:53:55 -070036
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070037static 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
50OatHeader* OatHeader::Create(InstructionSet instruction_set,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070051 const InstructionSetFeatures* instruction_set_features,
Vladimir Marko49b0f452015-12-10 13:49:19 +000052 uint32_t dex_file_count,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070053 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 Marko49b0f452015-12-10 13:49:19 +000063 dex_file_count,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070064 variable_data);
Elliott Hughesa72ec822012-03-05 17:12:22 -080065}
66
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070067OatHeader::OatHeader(InstructionSet instruction_set,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070068 const InstructionSetFeatures* instruction_set_features,
Vladimir Marko49b0f452015-12-10 13:49:19 +000069 uint32_t dex_file_count,
70 const SafeMap<std::string, std::string>* variable_data)
71 : adler32_checksum_(adler32(0L, Z_NULL, 0)),
72 instruction_set_(instruction_set),
73 instruction_set_features_bitmap_(instruction_set_features->AsBitmap()),
74 dex_file_count_(dex_file_count),
Vladimir Marko0eb882b2017-05-15 13:39:18 +010075 oat_dex_files_offset_(0),
Vladimir Marko49b0f452015-12-10 13:49:19 +000076 executable_offset_(0),
77 interpreter_to_interpreter_bridge_offset_(0),
78 interpreter_to_compiled_code_bridge_offset_(0),
79 jni_dlsym_lookup_offset_(0),
80 quick_generic_jni_trampoline_offset_(0),
81 quick_imt_conflict_trampoline_offset_(0),
82 quick_resolution_trampoline_offset_(0),
83 quick_to_interpreter_bridge_offset_(0),
84 image_patch_delta_(0),
85 image_file_location_oat_checksum_(0),
86 image_file_location_oat_data_begin_(0) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -080087 // Don't want asserts in header as they would be checked in each file that includes it. But the
88 // fields are private, so we check inside a method.
89 static_assert(sizeof(magic_) == sizeof(kOatMagic),
90 "Oat magic and magic_ have different lengths.");
91 static_assert(sizeof(version_) == sizeof(kOatVersion),
92 "Oat version and version_ have different lengths.");
93
Brian Carlstrome24fa612011-09-29 00:53:55 -070094 memcpy(magic_, kOatMagic, sizeof(kOatMagic));
95 memcpy(version_, kOatVersion, sizeof(kOatVersion));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070096
Brian Carlstromf852fb22012-10-19 11:01:58 -070097 CHECK_NE(instruction_set, kNone);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070098
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070099 // Flatten the map. Will also update variable_size_data_size_.
100 Flatten(variable_data);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700101}
102
103bool OatHeader::IsValid() const {
Brian Carlstromf852fb22012-10-19 11:01:58 -0700104 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 return false;
106 }
Brian Carlstromf852fb22012-10-19 11:01:58 -0700107 if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108 return false;
109 }
Alex Lighta59dd802014-07-02 16:28:08 -0700110 if (!IsAligned<kPageSize>(executable_offset_)) {
111 return false;
112 }
113 if (!IsAligned<kPageSize>(image_patch_delta_)) {
114 return false;
115 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100116 if (!IsValidInstructionSet(instruction_set_)) {
117 return false;
118 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 return true;
120}
121
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800122std::string OatHeader::GetValidationErrorMessage() const {
123 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) {
124 static_assert(sizeof(kOatMagic) == 4, "kOatMagic has unexpected length");
125 return StringPrintf("Invalid oat magic, expected 0x%x%x%x%x, got 0x%x%x%x%x.",
126 kOatMagic[0], kOatMagic[1], kOatMagic[2], kOatMagic[3],
127 magic_[0], magic_[1], magic_[2], magic_[3]);
128 }
129 if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) {
130 static_assert(sizeof(kOatVersion) == 4, "kOatVersion has unexpected length");
131 return StringPrintf("Invalid oat version, expected 0x%x%x%x%x, got 0x%x%x%x%x.",
132 kOatVersion[0], kOatVersion[1], kOatVersion[2], kOatVersion[3],
133 version_[0], version_[1], version_[2], version_[3]);
134 }
135 if (!IsAligned<kPageSize>(executable_offset_)) {
136 return "Executable offset not page-aligned.";
137 }
138 if (!IsAligned<kPageSize>(image_patch_delta_)) {
139 return "Image patch delta not page-aligned.";
140 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100141 if (!IsValidInstructionSet(instruction_set_)) {
142 return StringPrintf("Invalid instruction set, %d.", static_cast<int>(instruction_set_));
143 }
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800144 return "";
145}
146
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147const char* OatHeader::GetMagic() const {
148 CHECK(IsValid());
149 return reinterpret_cast<const char*>(magic_);
150}
151
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152uint32_t OatHeader::GetChecksum() const {
153 CHECK(IsValid());
154 return adler32_checksum_;
155}
156
Vladimir Marko49b0f452015-12-10 13:49:19 +0000157void OatHeader::UpdateChecksumWithHeaderData() {
158 UpdateChecksum(&instruction_set_, sizeof(instruction_set_));
159 UpdateChecksum(&instruction_set_features_bitmap_, sizeof(instruction_set_features_bitmap_));
160 UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
161 UpdateChecksum(&image_file_location_oat_checksum_, sizeof(image_file_location_oat_checksum_));
162 UpdateChecksum(&image_file_location_oat_data_begin_, sizeof(image_file_location_oat_data_begin_));
163
164 // Update checksum for variable data size.
165 UpdateChecksum(&key_value_store_size_, sizeof(key_value_store_size_));
166
167 // Update for data, if existing.
168 if (key_value_store_size_ > 0U) {
169 UpdateChecksum(&key_value_store_, key_value_store_size_);
170 }
171
172 UpdateChecksum(&executable_offset_, sizeof(executable_offset_));
173 UpdateChecksum(&interpreter_to_interpreter_bridge_offset_,
174 sizeof(interpreter_to_interpreter_bridge_offset_));
175 UpdateChecksum(&interpreter_to_compiled_code_bridge_offset_,
176 sizeof(interpreter_to_compiled_code_bridge_offset_));
177 UpdateChecksum(&jni_dlsym_lookup_offset_, sizeof(jni_dlsym_lookup_offset_));
178 UpdateChecksum(&quick_generic_jni_trampoline_offset_,
179 sizeof(quick_generic_jni_trampoline_offset_));
180 UpdateChecksum(&quick_imt_conflict_trampoline_offset_,
181 sizeof(quick_imt_conflict_trampoline_offset_));
182 UpdateChecksum(&quick_resolution_trampoline_offset_,
183 sizeof(quick_resolution_trampoline_offset_));
184 UpdateChecksum(&quick_to_interpreter_bridge_offset_,
185 sizeof(quick_to_interpreter_bridge_offset_));
186}
187
Brian Carlstrome24fa612011-09-29 00:53:55 -0700188void OatHeader::UpdateChecksum(const void* data, size_t length) {
189 DCHECK(IsValid());
Jeff Hao2c6b4942016-05-23 19:17:04 -0700190 if (data != nullptr) {
191 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
192 adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
193 } else {
194 DCHECK_EQ(0U, length);
195 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700196}
197
Elliott Hughesa72ec822012-03-05 17:12:22 -0800198InstructionSet OatHeader::GetInstructionSet() const {
199 CHECK(IsValid());
200 return instruction_set_;
201}
202
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700203uint32_t OatHeader::GetInstructionSetFeaturesBitmap() const {
Dave Allison70202782013-10-22 17:52:19 -0700204 CHECK(IsValid());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700205 return instruction_set_features_bitmap_;
Dave Allison70202782013-10-22 17:52:19 -0700206}
207
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100208uint32_t OatHeader::GetOatDexFilesOffset() const {
209 DCHECK(IsValid());
210 DCHECK_GT(oat_dex_files_offset_, sizeof(OatHeader));
211 return oat_dex_files_offset_;
212}
213
214void OatHeader::SetOatDexFilesOffset(uint32_t oat_dex_files_offset) {
215 DCHECK_GT(oat_dex_files_offset, sizeof(OatHeader));
216 DCHECK(IsValid());
217 DCHECK_EQ(oat_dex_files_offset_, 0u);
218
219 oat_dex_files_offset_ = oat_dex_files_offset;
220}
221
Brian Carlstrome24fa612011-09-29 00:53:55 -0700222uint32_t OatHeader::GetExecutableOffset() const {
223 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700224 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225 CHECK_GT(executable_offset_, sizeof(OatHeader));
226 return executable_offset_;
227}
228
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700229void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
230 DCHECK_ALIGNED(executable_offset, kPageSize);
231 CHECK_GT(executable_offset, sizeof(OatHeader));
232 DCHECK(IsValid());
233 DCHECK_EQ(executable_offset_, 0U);
234
235 executable_offset_ = executable_offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700236}
237
Ian Rogers468532e2013-08-05 10:56:33 -0700238const void* OatHeader::GetInterpreterToInterpreterBridge() const {
239 return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToInterpreterBridgeOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700240}
241
Ian Rogers468532e2013-08-05 10:56:33 -0700242uint32_t OatHeader::GetInterpreterToInterpreterBridgeOffset() const {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700243 DCHECK(IsValid());
Dave Allisonc6104ae2014-03-12 11:05:39 -0700244 CHECK(interpreter_to_interpreter_bridge_offset_ == 0 ||
245 interpreter_to_interpreter_bridge_offset_ >= executable_offset_);
Ian Rogers468532e2013-08-05 10:56:33 -0700246 return interpreter_to_interpreter_bridge_offset_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700247}
248
Ian Rogers468532e2013-08-05 10:56:33 -0700249void OatHeader::SetInterpreterToInterpreterBridgeOffset(uint32_t offset) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700250 CHECK(offset == 0 || offset >= executable_offset_);
251 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700252 DCHECK_EQ(interpreter_to_interpreter_bridge_offset_, 0U) << offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700253
Ian Rogers468532e2013-08-05 10:56:33 -0700254 interpreter_to_interpreter_bridge_offset_ = offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700255}
256
Ian Rogers468532e2013-08-05 10:56:33 -0700257const void* OatHeader::GetInterpreterToCompiledCodeBridge() const {
258 return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToCompiledCodeBridgeOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700259}
260
Ian Rogers468532e2013-08-05 10:56:33 -0700261uint32_t OatHeader::GetInterpreterToCompiledCodeBridgeOffset() const {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700262 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700263 CHECK_GE(interpreter_to_compiled_code_bridge_offset_, interpreter_to_interpreter_bridge_offset_);
264 return interpreter_to_compiled_code_bridge_offset_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700265}
266
Ian Rogers468532e2013-08-05 10:56:33 -0700267void OatHeader::SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset) {
268 CHECK(offset == 0 || offset >= interpreter_to_interpreter_bridge_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700269 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700270 DCHECK_EQ(interpreter_to_compiled_code_bridge_offset_, 0U) << offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700271
Ian Rogers468532e2013-08-05 10:56:33 -0700272 interpreter_to_compiled_code_bridge_offset_ = offset;
Ian Rogers468532e2013-08-05 10:56:33 -0700273}
274
275const void* OatHeader::GetJniDlsymLookup() const {
276 return reinterpret_cast<const uint8_t*>(this) + GetJniDlsymLookupOffset();
277}
278
279uint32_t OatHeader::GetJniDlsymLookupOffset() const {
280 DCHECK(IsValid());
281 CHECK_GE(jni_dlsym_lookup_offset_, interpreter_to_compiled_code_bridge_offset_);
282 return jni_dlsym_lookup_offset_;
283}
284
285void OatHeader::SetJniDlsymLookupOffset(uint32_t offset) {
286 CHECK(offset == 0 || offset >= interpreter_to_compiled_code_bridge_offset_);
287 DCHECK(IsValid());
288 DCHECK_EQ(jni_dlsym_lookup_offset_, 0U) << offset;
289
290 jni_dlsym_lookup_offset_ = offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700291}
292
Andreas Gampe2da88232014-02-27 12:26:20 -0800293const void* OatHeader::GetQuickGenericJniTrampoline() const {
294 return reinterpret_cast<const uint8_t*>(this) + GetQuickGenericJniTrampolineOffset();
295}
296
297uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const {
298 DCHECK(IsValid());
Elliott Hughes956af0f2014-12-11 14:34:28 -0800299 CHECK_GE(quick_generic_jni_trampoline_offset_, jni_dlsym_lookup_offset_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800300 return quick_generic_jni_trampoline_offset_;
301}
302
303void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800304 CHECK(offset == 0 || offset >= jni_dlsym_lookup_offset_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800305 DCHECK(IsValid());
306 DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset;
307
308 quick_generic_jni_trampoline_offset_ = offset;
Andreas Gampe2da88232014-02-27 12:26:20 -0800309}
310
Jeff Hao88474b42013-10-23 16:24:40 -0700311const void* OatHeader::GetQuickImtConflictTrampoline() const {
312 return reinterpret_cast<const uint8_t*>(this) + GetQuickImtConflictTrampolineOffset();
313}
314
315uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const {
316 DCHECK(IsValid());
Andreas Gampe2da88232014-02-27 12:26:20 -0800317 CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_);
Jeff Hao88474b42013-10-23 16:24:40 -0700318 return quick_imt_conflict_trampoline_offset_;
319}
320
321void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800322 CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_);
Jeff Hao88474b42013-10-23 16:24:40 -0700323 DCHECK(IsValid());
324 DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset;
325
326 quick_imt_conflict_trampoline_offset_ = offset;
Jeff Hao88474b42013-10-23 16:24:40 -0700327}
328
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700329const void* OatHeader::GetQuickResolutionTrampoline() const {
330 return reinterpret_cast<const uint8_t*>(this) + GetQuickResolutionTrampolineOffset();
331}
332
333uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const {
334 DCHECK(IsValid());
Jeff Hao88474b42013-10-23 16:24:40 -0700335 CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700336 return quick_resolution_trampoline_offset_;
337}
338
339void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) {
Jeff Hao88474b42013-10-23 16:24:40 -0700340 CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700341 DCHECK(IsValid());
342 DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset;
343
344 quick_resolution_trampoline_offset_ = offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700345}
346
Ian Rogers468532e2013-08-05 10:56:33 -0700347const void* OatHeader::GetQuickToInterpreterBridge() const {
348 return reinterpret_cast<const uint8_t*>(this) + GetQuickToInterpreterBridgeOffset();
349}
350
351uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const {
352 DCHECK(IsValid());
353 CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_);
354 return quick_to_interpreter_bridge_offset_;
355}
356
357void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) {
358 CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_);
359 DCHECK(IsValid());
360 DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset;
361
362 quick_to_interpreter_bridge_offset_ = offset;
Ian Rogers468532e2013-08-05 10:56:33 -0700363}
364
Alex Lighta59dd802014-07-02 16:28:08 -0700365int32_t OatHeader::GetImagePatchDelta() const {
366 CHECK(IsValid());
367 return image_patch_delta_;
368}
369
370void OatHeader::RelocateOat(off_t delta) {
371 CHECK(IsValid());
372 CHECK_ALIGNED(delta, kPageSize);
373 image_patch_delta_ += delta;
374 if (image_file_location_oat_data_begin_ != 0) {
375 image_file_location_oat_data_begin_ += delta;
376 }
377}
378
379void OatHeader::SetImagePatchDelta(int32_t off) {
380 CHECK(IsValid());
381 CHECK_ALIGNED(off, kPageSize);
382 image_patch_delta_ = off;
383}
384
Brian Carlstrom28db0122012-10-18 16:20:41 -0700385uint32_t OatHeader::GetImageFileLocationOatChecksum() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700386 CHECK(IsValid());
Brian Carlstrom28db0122012-10-18 16:20:41 -0700387 return image_file_location_oat_checksum_;
388}
389
Vladimir Marko49b0f452015-12-10 13:49:19 +0000390void OatHeader::SetImageFileLocationOatChecksum(uint32_t image_file_location_oat_checksum) {
391 CHECK(IsValid());
392 image_file_location_oat_checksum_ = image_file_location_oat_checksum;
393}
394
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800395uint32_t OatHeader::GetImageFileLocationOatDataBegin() const {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700396 CHECK(IsValid());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800397 return image_file_location_oat_data_begin_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700398}
399
Vladimir Marko49b0f452015-12-10 13:49:19 +0000400void OatHeader::SetImageFileLocationOatDataBegin(uint32_t image_file_location_oat_data_begin) {
401 CHECK(IsValid());
402 CHECK_ALIGNED(image_file_location_oat_data_begin, kPageSize);
403 image_file_location_oat_data_begin_ = image_file_location_oat_data_begin;
404}
405
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700406uint32_t OatHeader::GetKeyValueStoreSize() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700407 CHECK(IsValid());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700408 return key_value_store_size_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700409}
410
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700411const uint8_t* OatHeader::GetKeyValueStore() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700412 CHECK(IsValid());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700413 return key_value_store_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700414}
415
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700416// Advance start until it is either end or \0.
417static const char* ParseString(const char* start, const char* end) {
418 while (start < end && *start != 0) {
419 start++;
420 }
421 return start;
422}
423
424const char* OatHeader::GetStoreValueByKey(const char* key) const {
425 const char* ptr = reinterpret_cast<const char*>(&key_value_store_);
426 const char* end = ptr + key_value_store_size_;
427
428 while (ptr < end) {
429 // Scan for a closing zero.
430 const char* str_end = ParseString(ptr, end);
431 if (str_end < end) {
432 if (strcmp(key, ptr) == 0) {
433 // Same as key. Check if value is OK.
434 if (ParseString(str_end + 1, end) < end) {
435 return str_end + 1;
436 }
437 } else {
438 // Different from key. Advance over the value.
439 ptr = ParseString(str_end + 1, end) + 1;
440 }
441 } else {
442 break;
443 }
444 }
445 // Not found.
446 return nullptr;
447}
448
449bool OatHeader::GetStoreKeyValuePairByIndex(size_t index, const char** key,
450 const char** value) const {
451 const char* ptr = reinterpret_cast<const char*>(&key_value_store_);
452 const char* end = ptr + key_value_store_size_;
453 ssize_t counter = static_cast<ssize_t>(index);
454
455 while (ptr < end && counter >= 0) {
456 // Scan for a closing zero.
457 const char* str_end = ParseString(ptr, end);
458 if (str_end < end) {
459 const char* maybe_key = ptr;
460 ptr = ParseString(str_end + 1, end) + 1;
461 if (ptr <= end) {
462 if (counter == 0) {
463 *key = maybe_key;
464 *value = str_end + 1;
465 return true;
466 } else {
467 counter--;
468 }
469 } else {
470 return false;
471 }
472 } else {
473 break;
474 }
475 }
476 // Not found.
477 return false;
478}
479
480size_t OatHeader::GetHeaderSize() const {
481 return sizeof(OatHeader) + key_value_store_size_;
482}
483
Igor Murashkin46774762014-10-22 11:37:02 -0700484bool OatHeader::IsPic() const {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200485 return IsKeyEnabled(OatHeader::kPicKey);
486}
487
488bool OatHeader::IsDebuggable() const {
489 return IsKeyEnabled(OatHeader::kDebuggableKey);
490}
491
Andreas Gampef8cd8902017-01-18 16:05:01 -0800492bool OatHeader::IsConcurrentCopying() const {
493 return IsKeyEnabled(OatHeader::kConcurrentCopying);
494}
495
David Srbecky5d950762016-03-07 20:47:29 +0000496bool OatHeader::IsNativeDebuggable() const {
497 return IsKeyEnabled(OatHeader::kNativeDebuggableKey);
498}
499
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000500CompilerFilter::Filter OatHeader::GetCompilerFilter() const {
501 CompilerFilter::Filter filter;
502 const char* key_value = GetStoreValueByKey(kCompilerFilter);
503 CHECK(key_value != nullptr) << "compiler-filter not found in oat header";
504 CHECK(CompilerFilter::ParseCompilerFilter(key_value, &filter))
505 << "Invalid compiler-filter in oat header: " << key_value;
506 return filter;
Calin Juravleb077e152016-02-18 18:47:37 +0000507}
508
509bool OatHeader::KeyHasValue(const char* key, const char* value, size_t value_size) const {
510 const char* key_value = GetStoreValueByKey(key);
511 return (key_value != nullptr && strncmp(key_value, value, value_size) == 0);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000512}
513
Sebastien Hertz0de11332015-05-13 12:14:05 +0200514bool OatHeader::IsKeyEnabled(const char* key) const {
Calin Juravleb077e152016-02-18 18:47:37 +0000515 return KeyHasValue(key, kTrueValue, sizeof(kTrueValue));
Igor Murashkin46774762014-10-22 11:37:02 -0700516}
517
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700518void OatHeader::Flatten(const SafeMap<std::string, std::string>* key_value_store) {
519 char* data_ptr = reinterpret_cast<char*>(&key_value_store_);
520 if (key_value_store != nullptr) {
521 SafeMap<std::string, std::string>::const_iterator it = key_value_store->begin();
522 SafeMap<std::string, std::string>::const_iterator end = key_value_store->end();
523 for ( ; it != end; ++it) {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700524 strlcpy(data_ptr, it->first.c_str(), it->first.length() + 1);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700525 data_ptr += it->first.length() + 1;
Andreas Gampef45d61c2017-06-07 10:29:33 -0700526 strlcpy(data_ptr, it->second.c_str(), it->second.length() + 1);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700527 data_ptr += it->second.length() + 1;
528 }
529 }
530 key_value_store_size_ = data_ptr - reinterpret_cast<char*>(&key_value_store_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700531}
532
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800533OatMethodOffsets::OatMethodOffsets(uint32_t code_offset) : code_offset_(code_offset) {
534}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700535
536OatMethodOffsets::~OatMethodOffsets() {}
537
Brian Carlstrome24fa612011-09-29 00:53:55 -0700538} // namespace art