blob: ed99cbabbd3c32701faf0bbe22b6cd48fcadce70 [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
Ian Rogersd582fa42014-11-05 23:46:43 -080022#include "arch/instruction_set_features.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/bit_utils.h"
Andreas Gampe2bcb3b22014-12-12 15:25:14 -080024#include "base/stringprintf.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080025
Brian Carlstrome24fa612011-09-29 00:53:55 -070026namespace art {
27
Andreas Gampe2bcb3b22014-12-12 15:25:14 -080028constexpr uint8_t OatHeader::kOatMagic[4];
29constexpr uint8_t OatHeader::kOatVersion[4];
Sebastien Hertz0de11332015-05-13 12:14:05 +020030constexpr const char OatHeader::kTrueValue[];
31constexpr const char OatHeader::kFalseValue[];
Calin Juravleb077e152016-02-18 18:47:37 +000032constexpr const char OatHeader::kExtractOnlyValue[];
33constexpr const char OatHeader::kProfileGuideCompiledValue[];
Brian Carlstrome24fa612011-09-29 00:53:55 -070034
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070035static 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
48OatHeader* OatHeader::Create(InstructionSet instruction_set,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070049 const InstructionSetFeatures* instruction_set_features,
Vladimir Marko49b0f452015-12-10 13:49:19 +000050 uint32_t dex_file_count,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070051 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 Marko49b0f452015-12-10 13:49:19 +000061 dex_file_count,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070062 variable_data);
Elliott Hughesa72ec822012-03-05 17:12:22 -080063}
64
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070065OatHeader::OatHeader(InstructionSet instruction_set,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070066 const InstructionSetFeatures* instruction_set_features,
Vladimir Marko49b0f452015-12-10 13:49:19 +000067 uint32_t dex_file_count,
68 const SafeMap<std::string, std::string>* variable_data)
69 : adler32_checksum_(adler32(0L, Z_NULL, 0)),
70 instruction_set_(instruction_set),
71 instruction_set_features_bitmap_(instruction_set_features->AsBitmap()),
72 dex_file_count_(dex_file_count),
73 executable_offset_(0),
74 interpreter_to_interpreter_bridge_offset_(0),
75 interpreter_to_compiled_code_bridge_offset_(0),
76 jni_dlsym_lookup_offset_(0),
77 quick_generic_jni_trampoline_offset_(0),
78 quick_imt_conflict_trampoline_offset_(0),
79 quick_resolution_trampoline_offset_(0),
80 quick_to_interpreter_bridge_offset_(0),
81 image_patch_delta_(0),
82 image_file_location_oat_checksum_(0),
83 image_file_location_oat_data_begin_(0) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -080084 // Don't want asserts in header as they would be checked in each file that includes it. But the
85 // fields are private, so we check inside a method.
86 static_assert(sizeof(magic_) == sizeof(kOatMagic),
87 "Oat magic and magic_ have different lengths.");
88 static_assert(sizeof(version_) == sizeof(kOatVersion),
89 "Oat version and version_ have different lengths.");
90
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 memcpy(magic_, kOatMagic, sizeof(kOatMagic));
92 memcpy(version_, kOatVersion, sizeof(kOatVersion));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070093
Brian Carlstromf852fb22012-10-19 11:01:58 -070094 CHECK_NE(instruction_set, kNone);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070095
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070096 // Flatten the map. Will also update variable_size_data_size_.
97 Flatten(variable_data);
Brian Carlstrome24fa612011-09-29 00:53:55 -070098}
99
100bool OatHeader::IsValid() const {
Brian Carlstromf852fb22012-10-19 11:01:58 -0700101 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700102 return false;
103 }
Brian Carlstromf852fb22012-10-19 11:01:58 -0700104 if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 return false;
106 }
Alex Lighta59dd802014-07-02 16:28:08 -0700107 if (!IsAligned<kPageSize>(executable_offset_)) {
108 return false;
109 }
110 if (!IsAligned<kPageSize>(image_patch_delta_)) {
111 return false;
112 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100113 if (!IsValidInstructionSet(instruction_set_)) {
114 return false;
115 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700116 return true;
117}
118
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800119std::string OatHeader::GetValidationErrorMessage() const {
120 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) {
121 static_assert(sizeof(kOatMagic) == 4, "kOatMagic has unexpected length");
122 return StringPrintf("Invalid oat magic, expected 0x%x%x%x%x, got 0x%x%x%x%x.",
123 kOatMagic[0], kOatMagic[1], kOatMagic[2], kOatMagic[3],
124 magic_[0], magic_[1], magic_[2], magic_[3]);
125 }
126 if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) {
127 static_assert(sizeof(kOatVersion) == 4, "kOatVersion has unexpected length");
128 return StringPrintf("Invalid oat version, expected 0x%x%x%x%x, got 0x%x%x%x%x.",
129 kOatVersion[0], kOatVersion[1], kOatVersion[2], kOatVersion[3],
130 version_[0], version_[1], version_[2], version_[3]);
131 }
132 if (!IsAligned<kPageSize>(executable_offset_)) {
133 return "Executable offset not page-aligned.";
134 }
135 if (!IsAligned<kPageSize>(image_patch_delta_)) {
136 return "Image patch delta not page-aligned.";
137 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100138 if (!IsValidInstructionSet(instruction_set_)) {
139 return StringPrintf("Invalid instruction set, %d.", static_cast<int>(instruction_set_));
140 }
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800141 return "";
142}
143
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144const char* OatHeader::GetMagic() const {
145 CHECK(IsValid());
146 return reinterpret_cast<const char*>(magic_);
147}
148
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149uint32_t OatHeader::GetChecksum() const {
150 CHECK(IsValid());
151 return adler32_checksum_;
152}
153
Vladimir Marko49b0f452015-12-10 13:49:19 +0000154void OatHeader::UpdateChecksumWithHeaderData() {
155 UpdateChecksum(&instruction_set_, sizeof(instruction_set_));
156 UpdateChecksum(&instruction_set_features_bitmap_, sizeof(instruction_set_features_bitmap_));
157 UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
158 UpdateChecksum(&image_file_location_oat_checksum_, sizeof(image_file_location_oat_checksum_));
159 UpdateChecksum(&image_file_location_oat_data_begin_, sizeof(image_file_location_oat_data_begin_));
160
161 // Update checksum for variable data size.
162 UpdateChecksum(&key_value_store_size_, sizeof(key_value_store_size_));
163
164 // Update for data, if existing.
165 if (key_value_store_size_ > 0U) {
166 UpdateChecksum(&key_value_store_, key_value_store_size_);
167 }
168
169 UpdateChecksum(&executable_offset_, sizeof(executable_offset_));
170 UpdateChecksum(&interpreter_to_interpreter_bridge_offset_,
171 sizeof(interpreter_to_interpreter_bridge_offset_));
172 UpdateChecksum(&interpreter_to_compiled_code_bridge_offset_,
173 sizeof(interpreter_to_compiled_code_bridge_offset_));
174 UpdateChecksum(&jni_dlsym_lookup_offset_, sizeof(jni_dlsym_lookup_offset_));
175 UpdateChecksum(&quick_generic_jni_trampoline_offset_,
176 sizeof(quick_generic_jni_trampoline_offset_));
177 UpdateChecksum(&quick_imt_conflict_trampoline_offset_,
178 sizeof(quick_imt_conflict_trampoline_offset_));
179 UpdateChecksum(&quick_resolution_trampoline_offset_,
180 sizeof(quick_resolution_trampoline_offset_));
181 UpdateChecksum(&quick_to_interpreter_bridge_offset_,
182 sizeof(quick_to_interpreter_bridge_offset_));
183}
184
Brian Carlstrome24fa612011-09-29 00:53:55 -0700185void OatHeader::UpdateChecksum(const void* data, size_t length) {
186 DCHECK(IsValid());
187 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
188 adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
189}
190
Elliott Hughesa72ec822012-03-05 17:12:22 -0800191InstructionSet OatHeader::GetInstructionSet() const {
192 CHECK(IsValid());
193 return instruction_set_;
194}
195
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700196uint32_t OatHeader::GetInstructionSetFeaturesBitmap() const {
Dave Allison70202782013-10-22 17:52:19 -0700197 CHECK(IsValid());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700198 return instruction_set_features_bitmap_;
Dave Allison70202782013-10-22 17:52:19 -0700199}
200
Brian Carlstrome24fa612011-09-29 00:53:55 -0700201uint32_t OatHeader::GetExecutableOffset() const {
202 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700203 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204 CHECK_GT(executable_offset_, sizeof(OatHeader));
205 return executable_offset_;
206}
207
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700208void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
209 DCHECK_ALIGNED(executable_offset, kPageSize);
210 CHECK_GT(executable_offset, sizeof(OatHeader));
211 DCHECK(IsValid());
212 DCHECK_EQ(executable_offset_, 0U);
213
214 executable_offset_ = executable_offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700215}
216
Ian Rogers468532e2013-08-05 10:56:33 -0700217const void* OatHeader::GetInterpreterToInterpreterBridge() const {
218 return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToInterpreterBridgeOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700219}
220
Ian Rogers468532e2013-08-05 10:56:33 -0700221uint32_t OatHeader::GetInterpreterToInterpreterBridgeOffset() const {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700222 DCHECK(IsValid());
Dave Allisonc6104ae2014-03-12 11:05:39 -0700223 CHECK(interpreter_to_interpreter_bridge_offset_ == 0 ||
224 interpreter_to_interpreter_bridge_offset_ >= executable_offset_);
Ian Rogers468532e2013-08-05 10:56:33 -0700225 return interpreter_to_interpreter_bridge_offset_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700226}
227
Ian Rogers468532e2013-08-05 10:56:33 -0700228void OatHeader::SetInterpreterToInterpreterBridgeOffset(uint32_t offset) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700229 CHECK(offset == 0 || offset >= executable_offset_);
230 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700231 DCHECK_EQ(interpreter_to_interpreter_bridge_offset_, 0U) << offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700232
Ian Rogers468532e2013-08-05 10:56:33 -0700233 interpreter_to_interpreter_bridge_offset_ = offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700234}
235
Ian Rogers468532e2013-08-05 10:56:33 -0700236const void* OatHeader::GetInterpreterToCompiledCodeBridge() const {
237 return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToCompiledCodeBridgeOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700238}
239
Ian Rogers468532e2013-08-05 10:56:33 -0700240uint32_t OatHeader::GetInterpreterToCompiledCodeBridgeOffset() const {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700241 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700242 CHECK_GE(interpreter_to_compiled_code_bridge_offset_, interpreter_to_interpreter_bridge_offset_);
243 return interpreter_to_compiled_code_bridge_offset_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700244}
245
Ian Rogers468532e2013-08-05 10:56:33 -0700246void OatHeader::SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset) {
247 CHECK(offset == 0 || offset >= interpreter_to_interpreter_bridge_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700248 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700249 DCHECK_EQ(interpreter_to_compiled_code_bridge_offset_, 0U) << offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700250
Ian Rogers468532e2013-08-05 10:56:33 -0700251 interpreter_to_compiled_code_bridge_offset_ = offset;
Ian Rogers468532e2013-08-05 10:56:33 -0700252}
253
254const void* OatHeader::GetJniDlsymLookup() const {
255 return reinterpret_cast<const uint8_t*>(this) + GetJniDlsymLookupOffset();
256}
257
258uint32_t OatHeader::GetJniDlsymLookupOffset() const {
259 DCHECK(IsValid());
260 CHECK_GE(jni_dlsym_lookup_offset_, interpreter_to_compiled_code_bridge_offset_);
261 return jni_dlsym_lookup_offset_;
262}
263
264void OatHeader::SetJniDlsymLookupOffset(uint32_t offset) {
265 CHECK(offset == 0 || offset >= interpreter_to_compiled_code_bridge_offset_);
266 DCHECK(IsValid());
267 DCHECK_EQ(jni_dlsym_lookup_offset_, 0U) << offset;
268
269 jni_dlsym_lookup_offset_ = offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700270}
271
Andreas Gampe2da88232014-02-27 12:26:20 -0800272const void* OatHeader::GetQuickGenericJniTrampoline() const {
273 return reinterpret_cast<const uint8_t*>(this) + GetQuickGenericJniTrampolineOffset();
274}
275
276uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const {
277 DCHECK(IsValid());
Elliott Hughes956af0f2014-12-11 14:34:28 -0800278 CHECK_GE(quick_generic_jni_trampoline_offset_, jni_dlsym_lookup_offset_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800279 return quick_generic_jni_trampoline_offset_;
280}
281
282void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800283 CHECK(offset == 0 || offset >= jni_dlsym_lookup_offset_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800284 DCHECK(IsValid());
285 DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset;
286
287 quick_generic_jni_trampoline_offset_ = offset;
Andreas Gampe2da88232014-02-27 12:26:20 -0800288}
289
Jeff Hao88474b42013-10-23 16:24:40 -0700290const void* OatHeader::GetQuickImtConflictTrampoline() const {
291 return reinterpret_cast<const uint8_t*>(this) + GetQuickImtConflictTrampolineOffset();
292}
293
294uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const {
295 DCHECK(IsValid());
Andreas Gampe2da88232014-02-27 12:26:20 -0800296 CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_);
Jeff Hao88474b42013-10-23 16:24:40 -0700297 return quick_imt_conflict_trampoline_offset_;
298}
299
300void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800301 CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_);
Jeff Hao88474b42013-10-23 16:24:40 -0700302 DCHECK(IsValid());
303 DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset;
304
305 quick_imt_conflict_trampoline_offset_ = offset;
Jeff Hao88474b42013-10-23 16:24:40 -0700306}
307
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700308const void* OatHeader::GetQuickResolutionTrampoline() const {
309 return reinterpret_cast<const uint8_t*>(this) + GetQuickResolutionTrampolineOffset();
310}
311
312uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const {
313 DCHECK(IsValid());
Jeff Hao88474b42013-10-23 16:24:40 -0700314 CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700315 return quick_resolution_trampoline_offset_;
316}
317
318void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) {
Jeff Hao88474b42013-10-23 16:24:40 -0700319 CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700320 DCHECK(IsValid());
321 DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset;
322
323 quick_resolution_trampoline_offset_ = offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700324}
325
Ian Rogers468532e2013-08-05 10:56:33 -0700326const void* OatHeader::GetQuickToInterpreterBridge() const {
327 return reinterpret_cast<const uint8_t*>(this) + GetQuickToInterpreterBridgeOffset();
328}
329
330uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const {
331 DCHECK(IsValid());
332 CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_);
333 return quick_to_interpreter_bridge_offset_;
334}
335
336void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) {
337 CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_);
338 DCHECK(IsValid());
339 DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset;
340
341 quick_to_interpreter_bridge_offset_ = offset;
Ian Rogers468532e2013-08-05 10:56:33 -0700342}
343
Alex Lighta59dd802014-07-02 16:28:08 -0700344int32_t OatHeader::GetImagePatchDelta() const {
345 CHECK(IsValid());
346 return image_patch_delta_;
347}
348
349void OatHeader::RelocateOat(off_t delta) {
350 CHECK(IsValid());
351 CHECK_ALIGNED(delta, kPageSize);
352 image_patch_delta_ += delta;
353 if (image_file_location_oat_data_begin_ != 0) {
354 image_file_location_oat_data_begin_ += delta;
355 }
356}
357
358void OatHeader::SetImagePatchDelta(int32_t off) {
359 CHECK(IsValid());
360 CHECK_ALIGNED(off, kPageSize);
361 image_patch_delta_ = off;
362}
363
Brian Carlstrom28db0122012-10-18 16:20:41 -0700364uint32_t OatHeader::GetImageFileLocationOatChecksum() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700365 CHECK(IsValid());
Brian Carlstrom28db0122012-10-18 16:20:41 -0700366 return image_file_location_oat_checksum_;
367}
368
Vladimir Marko49b0f452015-12-10 13:49:19 +0000369void OatHeader::SetImageFileLocationOatChecksum(uint32_t image_file_location_oat_checksum) {
370 CHECK(IsValid());
371 image_file_location_oat_checksum_ = image_file_location_oat_checksum;
372}
373
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800374uint32_t OatHeader::GetImageFileLocationOatDataBegin() const {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700375 CHECK(IsValid());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800376 return image_file_location_oat_data_begin_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700377}
378
Vladimir Marko49b0f452015-12-10 13:49:19 +0000379void OatHeader::SetImageFileLocationOatDataBegin(uint32_t image_file_location_oat_data_begin) {
380 CHECK(IsValid());
381 CHECK_ALIGNED(image_file_location_oat_data_begin, kPageSize);
382 image_file_location_oat_data_begin_ = image_file_location_oat_data_begin;
383}
384
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700385uint32_t OatHeader::GetKeyValueStoreSize() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700386 CHECK(IsValid());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700387 return key_value_store_size_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700388}
389
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700390const uint8_t* OatHeader::GetKeyValueStore() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700391 CHECK(IsValid());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700392 return key_value_store_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700393}
394
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700395// Advance start until it is either end or \0.
396static const char* ParseString(const char* start, const char* end) {
397 while (start < end && *start != 0) {
398 start++;
399 }
400 return start;
401}
402
403const char* OatHeader::GetStoreValueByKey(const char* key) const {
404 const char* ptr = reinterpret_cast<const char*>(&key_value_store_);
405 const char* end = ptr + key_value_store_size_;
406
407 while (ptr < end) {
408 // Scan for a closing zero.
409 const char* str_end = ParseString(ptr, end);
410 if (str_end < end) {
411 if (strcmp(key, ptr) == 0) {
412 // Same as key. Check if value is OK.
413 if (ParseString(str_end + 1, end) < end) {
414 return str_end + 1;
415 }
416 } else {
417 // Different from key. Advance over the value.
418 ptr = ParseString(str_end + 1, end) + 1;
419 }
420 } else {
421 break;
422 }
423 }
424 // Not found.
425 return nullptr;
426}
427
428bool OatHeader::GetStoreKeyValuePairByIndex(size_t index, const char** key,
429 const char** value) const {
430 const char* ptr = reinterpret_cast<const char*>(&key_value_store_);
431 const char* end = ptr + key_value_store_size_;
432 ssize_t counter = static_cast<ssize_t>(index);
433
434 while (ptr < end && counter >= 0) {
435 // Scan for a closing zero.
436 const char* str_end = ParseString(ptr, end);
437 if (str_end < end) {
438 const char* maybe_key = ptr;
439 ptr = ParseString(str_end + 1, end) + 1;
440 if (ptr <= end) {
441 if (counter == 0) {
442 *key = maybe_key;
443 *value = str_end + 1;
444 return true;
445 } else {
446 counter--;
447 }
448 } else {
449 return false;
450 }
451 } else {
452 break;
453 }
454 }
455 // Not found.
456 return false;
457}
458
459size_t OatHeader::GetHeaderSize() const {
460 return sizeof(OatHeader) + key_value_store_size_;
461}
462
Igor Murashkin46774762014-10-22 11:37:02 -0700463bool OatHeader::IsPic() const {
Sebastien Hertz0de11332015-05-13 12:14:05 +0200464 return IsKeyEnabled(OatHeader::kPicKey);
465}
466
467bool OatHeader::IsDebuggable() const {
468 return IsKeyEnabled(OatHeader::kDebuggableKey);
469}
470
David Srbecky5d950762016-03-07 20:47:29 +0000471bool OatHeader::IsNativeDebuggable() const {
472 return IsKeyEnabled(OatHeader::kNativeDebuggableKey);
473}
474
David Brazdilce4b0ba2016-01-28 15:05:49 +0000475bool OatHeader::IsExtractOnly() const {
Calin Juravleb077e152016-02-18 18:47:37 +0000476 return KeyHasValue(kCompilationType,
477 kExtractOnlyValue,
478 sizeof(kExtractOnlyValue));
479}
480
481bool OatHeader::IsProfileGuideCompiled() const {
482 return KeyHasValue(kCompilationType,
483 kProfileGuideCompiledValue,
484 sizeof(kProfileGuideCompiledValue));
485}
486
487bool OatHeader::KeyHasValue(const char* key, const char* value, size_t value_size) const {
488 const char* key_value = GetStoreValueByKey(key);
489 return (key_value != nullptr && strncmp(key_value, value, value_size) == 0);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000490}
491
Sebastien Hertz0de11332015-05-13 12:14:05 +0200492bool OatHeader::IsKeyEnabled(const char* key) const {
Calin Juravleb077e152016-02-18 18:47:37 +0000493 return KeyHasValue(key, kTrueValue, sizeof(kTrueValue));
Igor Murashkin46774762014-10-22 11:37:02 -0700494}
495
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700496void OatHeader::Flatten(const SafeMap<std::string, std::string>* key_value_store) {
497 char* data_ptr = reinterpret_cast<char*>(&key_value_store_);
498 if (key_value_store != nullptr) {
499 SafeMap<std::string, std::string>::const_iterator it = key_value_store->begin();
500 SafeMap<std::string, std::string>::const_iterator end = key_value_store->end();
501 for ( ; it != end; ++it) {
502 strcpy(data_ptr, it->first.c_str());
503 data_ptr += it->first.length() + 1;
504 strcpy(data_ptr, it->second.c_str());
505 data_ptr += it->second.length() + 1;
506 }
507 }
508 key_value_store_size_ = data_ptr - reinterpret_cast<char*>(&key_value_store_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700509}
510
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800511OatMethodOffsets::OatMethodOffsets(uint32_t code_offset) : code_offset_(code_offset) {
512}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700513
514OatMethodOffsets::~OatMethodOffsets() {}
515
Brian Carlstrome24fa612011-09-29 00:53:55 -0700516} // namespace art