blob: c8eb3e27ae77e9a533d2673b77f13a1b49cb2cb0 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018#include "utils.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070019
20#include <zlib.h>
21
22namespace art {
23
24const uint8_t OatHeader::kOatMagic[] = { 'o', 'a', 't', '\n' };
Andreas Gampe2da88232014-02-27 12:26:20 -080025const uint8_t OatHeader::kOatVersion[] = { '0', '1', '6', '\0' };
Brian Carlstrome24fa612011-09-29 00:53:55 -070026
Elliott Hughesa72ec822012-03-05 17:12:22 -080027OatHeader::OatHeader() {
28 memset(this, 0, sizeof(*this));
29}
30
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070031OatHeader::OatHeader(InstructionSet instruction_set,
Dave Allison70202782013-10-22 17:52:19 -070032 const InstructionSetFeatures& instruction_set_features,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070033 const std::vector<const DexFile*>* dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070034 uint32_t image_file_location_oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080035 uint32_t image_file_location_oat_data_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070036 const std::string& image_file_location) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070037 memcpy(magic_, kOatMagic, sizeof(kOatMagic));
38 memcpy(version_, kOatVersion, sizeof(kOatVersion));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070039
Brian Carlstrome24fa612011-09-29 00:53:55 -070040 adler32_checksum_ = adler32(0L, Z_NULL, 0);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070041
Brian Carlstromf852fb22012-10-19 11:01:58 -070042 CHECK_NE(instruction_set, kNone);
Elliott Hughesa72ec822012-03-05 17:12:22 -080043 instruction_set_ = instruction_set;
44 UpdateChecksum(&instruction_set_, sizeof(instruction_set_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070045
Dave Allison70202782013-10-22 17:52:19 -070046 instruction_set_features_ = instruction_set_features;
47 UpdateChecksum(&instruction_set_features_, sizeof(instruction_set_features_));
48
Brian Carlstrome24fa612011-09-29 00:53:55 -070049 dex_file_count_ = dex_files->size();
50 UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070051
Brian Carlstrom28db0122012-10-18 16:20:41 -070052 image_file_location_oat_checksum_ = image_file_location_oat_checksum;
53 UpdateChecksum(&image_file_location_oat_checksum_, sizeof(image_file_location_oat_checksum_));
54
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 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_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070058
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
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 executable_offset_ = 0;
Ian Rogers468532e2013-08-05 10:56:33 -070064 interpreter_to_interpreter_bridge_offset_ = 0;
65 interpreter_to_compiled_code_bridge_offset_ = 0;
66 jni_dlsym_lookup_offset_ = 0;
Jeff Hao88474b42013-10-23 16:24:40 -070067 portable_imt_conflict_trampoline_offset_ = 0;
Jeff Hao0aba0ba2013-06-03 14:49:28 -070068 portable_resolution_trampoline_offset_ = 0;
Ian Rogers468532e2013-08-05 10:56:33 -070069 portable_to_interpreter_bridge_offset_ = 0;
Andreas Gampe2da88232014-02-27 12:26:20 -080070 quick_generic_jni_trampoline_offset_ = 0;
Jeff Hao88474b42013-10-23 16:24:40 -070071 quick_imt_conflict_trampoline_offset_ = 0;
Jeff Hao0aba0ba2013-06-03 14:49:28 -070072 quick_resolution_trampoline_offset_ = 0;
Ian Rogers468532e2013-08-05 10:56:33 -070073 quick_to_interpreter_bridge_offset_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070074}
75
76bool OatHeader::IsValid() const {
Brian Carlstromf852fb22012-10-19 11:01:58 -070077 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 return false;
79 }
Brian Carlstromf852fb22012-10-19 11:01:58 -070080 if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 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
Brian Carlstrome24fa612011-09-29 00:53:55 -070091uint32_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
Elliott Hughesa72ec822012-03-05 17:12:22 -0800102InstructionSet OatHeader::GetInstructionSet() const {
103 CHECK(IsValid());
104 return instruction_set_;
105}
106
Dave Allison70202782013-10-22 17:52:19 -0700107const InstructionSetFeatures& OatHeader::GetInstructionSetFeatures() const {
108 CHECK(IsValid());
109 return instruction_set_features_;
110}
111
Brian Carlstrome24fa612011-09-29 00:53:55 -0700112uint32_t OatHeader::GetExecutableOffset() const {
113 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700114 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115 CHECK_GT(executable_offset_, sizeof(OatHeader));
116 return executable_offset_;
117}
118
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700119void 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
Ian Rogers468532e2013-08-05 10:56:33 -0700129const void* OatHeader::GetInterpreterToInterpreterBridge() const {
130 return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToInterpreterBridgeOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700131}
132
Ian Rogers468532e2013-08-05 10:56:33 -0700133uint32_t OatHeader::GetInterpreterToInterpreterBridgeOffset() const {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700134 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700135 CHECK_GE(interpreter_to_interpreter_bridge_offset_, executable_offset_);
136 return interpreter_to_interpreter_bridge_offset_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700137}
138
Ian Rogers468532e2013-08-05 10:56:33 -0700139void OatHeader::SetInterpreterToInterpreterBridgeOffset(uint32_t offset) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700140 CHECK(offset == 0 || offset >= executable_offset_);
141 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700142 DCHECK_EQ(interpreter_to_interpreter_bridge_offset_, 0U) << offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700143
Ian Rogers468532e2013-08-05 10:56:33 -0700144 interpreter_to_interpreter_bridge_offset_ = offset;
145 UpdateChecksum(&interpreter_to_interpreter_bridge_offset_, sizeof(offset));
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700146}
147
Ian Rogers468532e2013-08-05 10:56:33 -0700148const void* OatHeader::GetInterpreterToCompiledCodeBridge() const {
149 return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToCompiledCodeBridgeOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700150}
151
Ian Rogers468532e2013-08-05 10:56:33 -0700152uint32_t OatHeader::GetInterpreterToCompiledCodeBridgeOffset() const {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700153 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700154 CHECK_GE(interpreter_to_compiled_code_bridge_offset_, interpreter_to_interpreter_bridge_offset_);
155 return interpreter_to_compiled_code_bridge_offset_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700156}
157
Ian Rogers468532e2013-08-05 10:56:33 -0700158void OatHeader::SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset) {
159 CHECK(offset == 0 || offset >= interpreter_to_interpreter_bridge_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700160 DCHECK(IsValid());
Ian Rogers468532e2013-08-05 10:56:33 -0700161 DCHECK_EQ(interpreter_to_compiled_code_bridge_offset_, 0U) << offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700162
Ian Rogers468532e2013-08-05 10:56:33 -0700163 interpreter_to_compiled_code_bridge_offset_ = offset;
164 UpdateChecksum(&interpreter_to_compiled_code_bridge_offset_, sizeof(offset));
165}
166
167const void* OatHeader::GetJniDlsymLookup() const {
168 return reinterpret_cast<const uint8_t*>(this) + GetJniDlsymLookupOffset();
169}
170
171uint32_t OatHeader::GetJniDlsymLookupOffset() const {
172 DCHECK(IsValid());
173 CHECK_GE(jni_dlsym_lookup_offset_, interpreter_to_compiled_code_bridge_offset_);
174 return jni_dlsym_lookup_offset_;
175}
176
177void OatHeader::SetJniDlsymLookupOffset(uint32_t offset) {
178 CHECK(offset == 0 || offset >= interpreter_to_compiled_code_bridge_offset_);
179 DCHECK(IsValid());
180 DCHECK_EQ(jni_dlsym_lookup_offset_, 0U) << offset;
181
182 jni_dlsym_lookup_offset_ = offset;
183 UpdateChecksum(&jni_dlsym_lookup_offset_, sizeof(offset));
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700184}
185
Jeff Hao88474b42013-10-23 16:24:40 -0700186const void* OatHeader::GetPortableImtConflictTrampoline() const {
187 return reinterpret_cast<const uint8_t*>(this) + GetPortableImtConflictTrampolineOffset();
188}
189
190uint32_t OatHeader::GetPortableImtConflictTrampolineOffset() const {
191 DCHECK(IsValid());
192 CHECK_GE(portable_imt_conflict_trampoline_offset_, jni_dlsym_lookup_offset_);
193 return portable_imt_conflict_trampoline_offset_;
194}
195
196void OatHeader::SetPortableImtConflictTrampolineOffset(uint32_t offset) {
197 CHECK(offset == 0 || offset >= jni_dlsym_lookup_offset_);
198 DCHECK(IsValid());
199 DCHECK_EQ(portable_imt_conflict_trampoline_offset_, 0U) << offset;
200
201 portable_imt_conflict_trampoline_offset_ = offset;
202 UpdateChecksum(&portable_imt_conflict_trampoline_offset_, sizeof(offset));
203}
204
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700205const void* OatHeader::GetPortableResolutionTrampoline() const {
206 return reinterpret_cast<const uint8_t*>(this) + GetPortableResolutionTrampolineOffset();
207}
208
209uint32_t OatHeader::GetPortableResolutionTrampolineOffset() const {
210 DCHECK(IsValid());
Jeff Hao88474b42013-10-23 16:24:40 -0700211 CHECK_GE(portable_resolution_trampoline_offset_, portable_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700212 return portable_resolution_trampoline_offset_;
213}
214
215void OatHeader::SetPortableResolutionTrampolineOffset(uint32_t offset) {
Jeff Hao88474b42013-10-23 16:24:40 -0700216 CHECK(offset == 0 || offset >= portable_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700217 DCHECK(IsValid());
218 DCHECK_EQ(portable_resolution_trampoline_offset_, 0U) << offset;
219
220 portable_resolution_trampoline_offset_ = offset;
221 UpdateChecksum(&portable_resolution_trampoline_offset_, sizeof(offset));
222}
223
Ian Rogers468532e2013-08-05 10:56:33 -0700224const void* OatHeader::GetPortableToInterpreterBridge() const {
225 return reinterpret_cast<const uint8_t*>(this) + GetPortableToInterpreterBridgeOffset();
226}
227
228uint32_t OatHeader::GetPortableToInterpreterBridgeOffset() const {
229 DCHECK(IsValid());
230 CHECK_GE(portable_to_interpreter_bridge_offset_, portable_resolution_trampoline_offset_);
231 return portable_to_interpreter_bridge_offset_;
232}
233
234void OatHeader::SetPortableToInterpreterBridgeOffset(uint32_t offset) {
235 CHECK(offset == 0 || offset >= portable_resolution_trampoline_offset_);
236 DCHECK(IsValid());
237 DCHECK_EQ(portable_to_interpreter_bridge_offset_, 0U) << offset;
238
239 portable_to_interpreter_bridge_offset_ = offset;
240 UpdateChecksum(&portable_to_interpreter_bridge_offset_, sizeof(offset));
241}
242
Andreas Gampe2da88232014-02-27 12:26:20 -0800243const void* OatHeader::GetQuickGenericJniTrampoline() const {
244 return reinterpret_cast<const uint8_t*>(this) + GetQuickGenericJniTrampolineOffset();
245}
246
247uint32_t OatHeader::GetQuickGenericJniTrampolineOffset() const {
248 DCHECK(IsValid());
249 CHECK_GE(quick_generic_jni_trampoline_offset_, portable_to_interpreter_bridge_offset_);
250 return quick_generic_jni_trampoline_offset_;
251}
252
253void OatHeader::SetQuickGenericJniTrampolineOffset(uint32_t offset) {
254 CHECK(offset == 0 || offset >= portable_to_interpreter_bridge_offset_);
255 DCHECK(IsValid());
256 DCHECK_EQ(quick_generic_jni_trampoline_offset_, 0U) << offset;
257
258 quick_generic_jni_trampoline_offset_ = offset;
259 UpdateChecksum(&quick_generic_jni_trampoline_offset_, sizeof(offset));
260}
261
Jeff Hao88474b42013-10-23 16:24:40 -0700262const void* OatHeader::GetQuickImtConflictTrampoline() const {
263 return reinterpret_cast<const uint8_t*>(this) + GetQuickImtConflictTrampolineOffset();
264}
265
266uint32_t OatHeader::GetQuickImtConflictTrampolineOffset() const {
267 DCHECK(IsValid());
Andreas Gampe2da88232014-02-27 12:26:20 -0800268 CHECK_GE(quick_imt_conflict_trampoline_offset_, quick_generic_jni_trampoline_offset_);
Jeff Hao88474b42013-10-23 16:24:40 -0700269 return quick_imt_conflict_trampoline_offset_;
270}
271
272void OatHeader::SetQuickImtConflictTrampolineOffset(uint32_t offset) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800273 CHECK(offset == 0 || offset >= quick_generic_jni_trampoline_offset_);
Jeff Hao88474b42013-10-23 16:24:40 -0700274 DCHECK(IsValid());
275 DCHECK_EQ(quick_imt_conflict_trampoline_offset_, 0U) << offset;
276
277 quick_imt_conflict_trampoline_offset_ = offset;
278 UpdateChecksum(&quick_imt_conflict_trampoline_offset_, sizeof(offset));
279}
280
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700281const void* OatHeader::GetQuickResolutionTrampoline() const {
282 return reinterpret_cast<const uint8_t*>(this) + GetQuickResolutionTrampolineOffset();
283}
284
285uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const {
286 DCHECK(IsValid());
Jeff Hao88474b42013-10-23 16:24:40 -0700287 CHECK_GE(quick_resolution_trampoline_offset_, quick_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700288 return quick_resolution_trampoline_offset_;
289}
290
291void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) {
Jeff Hao88474b42013-10-23 16:24:40 -0700292 CHECK(offset == 0 || offset >= quick_imt_conflict_trampoline_offset_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700293 DCHECK(IsValid());
294 DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset;
295
296 quick_resolution_trampoline_offset_ = offset;
297 UpdateChecksum(&quick_resolution_trampoline_offset_, sizeof(offset));
298}
299
Ian Rogers468532e2013-08-05 10:56:33 -0700300const void* OatHeader::GetQuickToInterpreterBridge() const {
301 return reinterpret_cast<const uint8_t*>(this) + GetQuickToInterpreterBridgeOffset();
302}
303
304uint32_t OatHeader::GetQuickToInterpreterBridgeOffset() const {
305 DCHECK(IsValid());
306 CHECK_GE(quick_to_interpreter_bridge_offset_, quick_resolution_trampoline_offset_);
307 return quick_to_interpreter_bridge_offset_;
308}
309
310void OatHeader::SetQuickToInterpreterBridgeOffset(uint32_t offset) {
311 CHECK(offset == 0 || offset >= quick_resolution_trampoline_offset_);
312 DCHECK(IsValid());
313 DCHECK_EQ(quick_to_interpreter_bridge_offset_, 0U) << offset;
314
315 quick_to_interpreter_bridge_offset_ = offset;
316 UpdateChecksum(&quick_to_interpreter_bridge_offset_, sizeof(offset));
317}
318
Brian Carlstrom28db0122012-10-18 16:20:41 -0700319uint32_t OatHeader::GetImageFileLocationOatChecksum() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700320 CHECK(IsValid());
Brian Carlstrom28db0122012-10-18 16:20:41 -0700321 return image_file_location_oat_checksum_;
322}
323
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800324uint32_t OatHeader::GetImageFileLocationOatDataBegin() const {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700325 CHECK(IsValid());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800326 return image_file_location_oat_data_begin_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700327}
328
329uint32_t OatHeader::GetImageFileLocationSize() const {
330 CHECK(IsValid());
331 return image_file_location_size_;
332}
333
334const uint8_t* OatHeader::GetImageFileLocationData() const {
335 CHECK(IsValid());
336 return image_file_location_data_;
337}
338
339std::string OatHeader::GetImageFileLocation() const {
340 CHECK(IsValid());
341 return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()),
342 GetImageFileLocationSize());
343}
344
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700345OatMethodOffsets::OatMethodOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700346 : code_offset_(0),
347 frame_size_in_bytes_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700348 core_spill_mask_(0),
349 fp_spill_mask_(0),
350 mapping_table_offset_(0),
351 vmap_table_offset_(0),
Jeff Hao74180ca2013-03-27 15:29:11 -0700352 gc_map_offset_(0)
Logan Chienccb7bf12012-03-28 12:52:32 +0800353{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354
355OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
356 uint32_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700357 uint32_t core_spill_mask,
358 uint32_t fp_spill_mask,
359 uint32_t mapping_table_offset,
360 uint32_t vmap_table_offset,
Jeff Hao74180ca2013-03-27 15:29:11 -0700361 uint32_t gc_map_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800362 )
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700363 : code_offset_(code_offset),
364 frame_size_in_bytes_(frame_size_in_bytes),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700365 core_spill_mask_(core_spill_mask),
366 fp_spill_mask_(fp_spill_mask),
367 mapping_table_offset_(mapping_table_offset),
368 vmap_table_offset_(vmap_table_offset),
Jeff Hao74180ca2013-03-27 15:29:11 -0700369 gc_map_offset_(gc_map_offset)
Logan Chienccb7bf12012-03-28 12:52:32 +0800370{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700371
372OatMethodOffsets::~OatMethodOffsets() {}
373
Brian Carlstrome24fa612011-09-29 00:53:55 -0700374} // namespace art