oat.cc revision d6ed642458c8820e1beca72f3d7b5f0be4a4b64b
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 */
16
17#include "oat.h"
18#include "utils.h"
19
20#include <zlib.h>
21
22namespace art {
23
24const uint8_t OatHeader::kOatMagic[] = { 'o', 'a', 't', '\n' };
25const uint8_t OatHeader::kOatVersion[] = { '0', '2', '0', '\0' };
26
27OatHeader::OatHeader() {
28  memset(this, 0, sizeof(*this));
29}
30
31OatHeader::OatHeader(InstructionSet instruction_set,
32                     const InstructionSetFeatures& instruction_set_features,
33                     const std::vector<const DexFile*>* dex_files,
34                     uint32_t image_file_location_oat_checksum,
35                     uint32_t image_file_location_oat_data_begin,
36                     const std::string& image_file_location) {
37  memcpy(magic_, kOatMagic, sizeof(kOatMagic));
38  memcpy(version_, kOatVersion, sizeof(kOatVersion));
39
40  adler32_checksum_ = adler32(0L, Z_NULL, 0);
41
42  CHECK_NE(instruction_set, kNone);
43  instruction_set_ = instruction_set;
44  UpdateChecksum(&instruction_set_, sizeof(instruction_set_));
45
46  instruction_set_features_ = instruction_set_features;
47  UpdateChecksum(&instruction_set_features_, sizeof(instruction_set_features_));
48
49  dex_file_count_ = dex_files->size();
50  UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
51
52  image_file_location_oat_checksum_ = image_file_location_oat_checksum;
53  UpdateChecksum(&image_file_location_oat_checksum_, sizeof(image_file_location_oat_checksum_));
54
55  CHECK(IsAligned<kPageSize>(image_file_location_oat_data_begin));
56  image_file_location_oat_data_begin_ = image_file_location_oat_data_begin;
57  UpdateChecksum(&image_file_location_oat_data_begin_, sizeof(image_file_location_oat_data_begin_));
58
59  image_file_location_size_ = image_file_location.size();
60  UpdateChecksum(&image_file_location_size_, sizeof(image_file_location_size_));
61  UpdateChecksum(image_file_location.data(), image_file_location_size_);
62
63  executable_offset_ = 0;
64  interpreter_to_interpreter_bridge_offset_ = 0;
65  interpreter_to_compiled_code_bridge_offset_ = 0;
66  jni_dlsym_lookup_offset_ = 0;
67  portable_imt_conflict_trampoline_offset_ = 0;
68  portable_resolution_trampoline_offset_ = 0;
69  portable_to_interpreter_bridge_offset_ = 0;
70  quick_generic_jni_trampoline_offset_ = 0;
71  quick_imt_conflict_trampoline_offset_ = 0;
72  quick_resolution_trampoline_offset_ = 0;
73  quick_to_interpreter_bridge_offset_ = 0;
74}
75
76bool OatHeader::IsValid() const {
77  if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) {
78    return false;
79  }
80  if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) {
81    return false;
82  }
83  return true;
84}
85
86const char* OatHeader::GetMagic() const {
87  CHECK(IsValid());
88  return reinterpret_cast<const char*>(magic_);
89}
90
91uint32_t OatHeader::GetChecksum() const {
92  CHECK(IsValid());
93  return adler32_checksum_;
94}
95
96void OatHeader::UpdateChecksum(const void* data, size_t length) {
97  DCHECK(IsValid());
98  const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
99  adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
100}
101
102InstructionSet OatHeader::GetInstructionSet() const {
103  CHECK(IsValid());
104  return instruction_set_;
105}
106
107const InstructionSetFeatures& OatHeader::GetInstructionSetFeatures() const {
108  CHECK(IsValid());
109  return instruction_set_features_;
110}
111
112uint32_t OatHeader::GetExecutableOffset() const {
113  DCHECK(IsValid());
114  DCHECK_ALIGNED(executable_offset_, kPageSize);
115  CHECK_GT(executable_offset_, sizeof(OatHeader));
116  return executable_offset_;
117}
118
119void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
120  DCHECK_ALIGNED(executable_offset, kPageSize);
121  CHECK_GT(executable_offset, sizeof(OatHeader));
122  DCHECK(IsValid());
123  DCHECK_EQ(executable_offset_, 0U);
124
125  executable_offset_ = executable_offset;
126  UpdateChecksum(&executable_offset_, sizeof(executable_offset));
127}
128
129const void* OatHeader::GetInterpreterToInterpreterBridge() const {
130  return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToInterpreterBridgeOffset();
131}
132
133uint32_t OatHeader::GetInterpreterToInterpreterBridgeOffset() const {
134  DCHECK(IsValid());
135  CHECK(interpreter_to_interpreter_bridge_offset_ == 0 ||
136        interpreter_to_interpreter_bridge_offset_ >= executable_offset_);
137  return interpreter_to_interpreter_bridge_offset_;
138}
139
140void OatHeader::SetInterpreterToInterpreterBridgeOffset(uint32_t offset) {
141  CHECK(offset == 0 || offset >= executable_offset_);
142  DCHECK(IsValid());
143  DCHECK_EQ(interpreter_to_interpreter_bridge_offset_, 0U) << offset;
144
145  interpreter_to_interpreter_bridge_offset_ = offset;
146  UpdateChecksum(&interpreter_to_interpreter_bridge_offset_, sizeof(offset));
147}
148
149const void* OatHeader::GetInterpreterToCompiledCodeBridge() const {
150  return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToCompiledCodeBridgeOffset();
151}
152
153uint32_t OatHeader::GetInterpreterToCompiledCodeBridgeOffset() const {
154  DCHECK(IsValid());
155  CHECK_GE(interpreter_to_compiled_code_bridge_offset_, interpreter_to_interpreter_bridge_offset_);
156  return interpreter_to_compiled_code_bridge_offset_;
157}
158
159void OatHeader::SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset) {
160  CHECK(offset == 0 || offset >= interpreter_to_interpreter_bridge_offset_);
161  DCHECK(IsValid());
162  DCHECK_EQ(interpreter_to_compiled_code_bridge_offset_, 0U) << offset;
163
164  interpreter_to_compiled_code_bridge_offset_ = offset;
165  UpdateChecksum(&interpreter_to_compiled_code_bridge_offset_, sizeof(offset));
166}
167
168const void* OatHeader::GetJniDlsymLookup() const {
169  return reinterpret_cast<const uint8_t*>(this) + GetJniDlsymLookupOffset();
170}
171
172uint32_t OatHeader::GetJniDlsymLookupOffset() const {
173  DCHECK(IsValid());
174  CHECK_GE(jni_dlsym_lookup_offset_, interpreter_to_compiled_code_bridge_offset_);
175  return jni_dlsym_lookup_offset_;
176}
177
178void OatHeader::SetJniDlsymLookupOffset(uint32_t offset) {
179  CHECK(offset == 0 || offset >= interpreter_to_compiled_code_bridge_offset_);
180  DCHECK(IsValid());
181  DCHECK_EQ(jni_dlsym_lookup_offset_, 0U) << offset;
182
183  jni_dlsym_lookup_offset_ = offset;
184  UpdateChecksum(&jni_dlsym_lookup_offset_, sizeof(offset));
185}
186
187const void* OatHeader::GetPortableImtConflictTrampoline() const {
188  return reinterpret_cast<const uint8_t*>(this) + GetPortableImtConflictTrampolineOffset();
189}
190
191uint32_t OatHeader::GetPortableImtConflictTrampolineOffset() const {
192  DCHECK(IsValid());
193  CHECK_GE(portable_imt_conflict_trampoline_offset_, jni_dlsym_lookup_offset_);
194  return portable_imt_conflict_trampoline_offset_;
195}
196
197void OatHeader::SetPortableImtConflictTrampolineOffset(uint32_t offset) {
198  CHECK(offset == 0 || offset >= jni_dlsym_lookup_offset_);
199  DCHECK(IsValid());
200  DCHECK_EQ(portable_imt_conflict_trampoline_offset_, 0U) << offset;
201
202  portable_imt_conflict_trampoline_offset_ = offset;
203  UpdateChecksum(&portable_imt_conflict_trampoline_offset_, sizeof(offset));
204}
205
206const void* OatHeader::GetPortableResolutionTrampoline() const {
207  return reinterpret_cast<const uint8_t*>(this) + GetPortableResolutionTrampolineOffset();
208}
209
210uint32_t OatHeader::GetPortableResolutionTrampolineOffset() const {
211  DCHECK(IsValid());
212  CHECK_GE(portable_resolution_trampoline_offset_, portable_imt_conflict_trampoline_offset_);
213  return portable_resolution_trampoline_offset_;
214}
215
216void OatHeader::SetPortableResolutionTrampolineOffset(uint32_t offset) {
217  CHECK(offset == 0 || offset >= portable_imt_conflict_trampoline_offset_);
218  DCHECK(IsValid());
219  DCHECK_EQ(portable_resolution_trampoline_offset_, 0U) << offset;
220
221  portable_resolution_trampoline_offset_ = offset;
222  UpdateChecksum(&portable_resolution_trampoline_offset_, sizeof(offset));
223}
224
225const void* OatHeader::GetPortableToInterpreterBridge() const {
226  return reinterpret_cast<const uint8_t*>(this) + GetPortableToInterpreterBridgeOffset();
227}
228
229uint32_t OatHeader::GetPortableToInterpreterBridgeOffset() const {
230  DCHECK(IsValid());
231  CHECK_GE(portable_to_interpreter_bridge_offset_, portable_resolution_trampoline_offset_);
232  return portable_to_interpreter_bridge_offset_;
233}
234
235void OatHeader::SetPortableToInterpreterBridgeOffset(uint32_t offset) {
236  CHECK(offset == 0 || offset >= portable_resolution_trampoline_offset_);
237  DCHECK(IsValid());
238  DCHECK_EQ(portable_to_interpreter_bridge_offset_, 0U) << offset;
239
240  portable_to_interpreter_bridge_offset_ = offset;
241  UpdateChecksum(&portable_to_interpreter_bridge_offset_, sizeof(offset));
242}
243
244const void* OatHeader::GetQuickGenericJniTrampoline() const {
245  return reinterpret_cast<const uint8_t*>(this) + GetQuickGenericJniTrampolineOffset();
246}
247
248uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const {
249  DCHECK(IsValid());
250  CHECK_GE(quick_generic_jni_trampoline_offset_, portable_to_interpreter_bridge_offset_);
251  return quick_generic_jni_trampoline_offset_;
252}
253
254void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) {
255  CHECK(offset == 0 || offset >= portable_to_interpreter_bridge_offset_);
256  DCHECK(IsValid());
257  DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset;
258
259  quick_generic_jni_trampoline_offset_ = offset;
260  UpdateChecksum(&quick_generic_jni_trampoline_offset_, sizeof(offset));
261}
262
263const void* OatHeader::GetQuickImtConflictTrampoline() const {
264  return reinterpret_cast<const uint8_t*>(this) + GetQuickImtConflictTrampolineOffset();
265}
266
267uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const {
268  DCHECK(IsValid());
269  CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_);
270  return quick_imt_conflict_trampoline_offset_;
271}
272
273void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) {
274  CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_);
275  DCHECK(IsValid());
276  DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset;
277
278  quick_imt_conflict_trampoline_offset_ = offset;
279  UpdateChecksum(&quick_imt_conflict_trampoline_offset_, sizeof(offset));
280}
281
282const void* OatHeader::GetQuickResolutionTrampoline() const {
283  return reinterpret_cast<const uint8_t*>(this) + GetQuickResolutionTrampolineOffset();
284}
285
286uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const {
287  DCHECK(IsValid());
288  CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_);
289  return quick_resolution_trampoline_offset_;
290}
291
292void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) {
293  CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_);
294  DCHECK(IsValid());
295  DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset;
296
297  quick_resolution_trampoline_offset_ = offset;
298  UpdateChecksum(&quick_resolution_trampoline_offset_, sizeof(offset));
299}
300
301const void* OatHeader::GetQuickToInterpreterBridge() const {
302  return reinterpret_cast<const uint8_t*>(this) + GetQuickToInterpreterBridgeOffset();
303}
304
305uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const {
306  DCHECK(IsValid());
307  CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_);
308  return quick_to_interpreter_bridge_offset_;
309}
310
311void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) {
312  CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_);
313  DCHECK(IsValid());
314  DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset;
315
316  quick_to_interpreter_bridge_offset_ = offset;
317  UpdateChecksum(&quick_to_interpreter_bridge_offset_, sizeof(offset));
318}
319
320uint32_t OatHeader::GetImageFileLocationOatChecksum() const {
321  CHECK(IsValid());
322  return image_file_location_oat_checksum_;
323}
324
325uint32_t OatHeader::GetImageFileLocationOatDataBegin() const {
326  CHECK(IsValid());
327  return image_file_location_oat_data_begin_;
328}
329
330uint32_t OatHeader::GetImageFileLocationSize() const {
331  CHECK(IsValid());
332  return image_file_location_size_;
333}
334
335const uint8_t* OatHeader::GetImageFileLocationData() const {
336  CHECK(IsValid());
337  return image_file_location_data_;
338}
339
340std::string OatHeader::GetImageFileLocation() const {
341  CHECK(IsValid());
342  return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()),
343                     GetImageFileLocationSize());
344}
345
346OatMethodOffsets::OatMethodOffsets()
347  : code_offset_(0),
348    frame_size_in_bytes_(0),
349    core_spill_mask_(0),
350    fp_spill_mask_(0),
351    mapping_table_offset_(0),
352    vmap_table_offset_(0),
353    gc_map_offset_(0)
354{}
355
356OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
357                                   uint32_t frame_size_in_bytes,
358                                   uint32_t core_spill_mask,
359                                   uint32_t fp_spill_mask,
360                                   uint32_t mapping_table_offset,
361                                   uint32_t vmap_table_offset,
362                                   uint32_t gc_map_offset
363                                   )
364  : code_offset_(code_offset),
365    frame_size_in_bytes_(frame_size_in_bytes),
366    core_spill_mask_(core_spill_mask),
367    fp_spill_mask_(fp_spill_mask),
368    mapping_table_offset_(mapping_table_offset),
369    vmap_table_offset_(vmap_table_offset),
370    gc_map_offset_(gc_map_offset)
371{}
372
373OatMethodOffsets::~OatMethodOffsets() {}
374
375}  // namespace art
376