blob: e606953ed59dd40b23e6612f6de09a5b78ba155f [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' };
Jeff Hao0aba0ba2013-06-03 14:49:28 -070025const uint8_t OatHeader::kOatVersion[] = { '0', '0', '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,
32 const std::vector<const DexFile*>* dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070033 uint32_t image_file_location_oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080034 uint32_t image_file_location_oat_data_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070035 const std::string& image_file_location) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070036 memcpy(magic_, kOatMagic, sizeof(kOatMagic));
37 memcpy(version_, kOatVersion, sizeof(kOatVersion));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070038
Brian Carlstrome24fa612011-09-29 00:53:55 -070039 adler32_checksum_ = adler32(0L, Z_NULL, 0);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070040
Brian Carlstromf852fb22012-10-19 11:01:58 -070041 CHECK_NE(instruction_set, kNone);
Elliott Hughesa72ec822012-03-05 17:12:22 -080042 instruction_set_ = instruction_set;
43 UpdateChecksum(&instruction_set_, sizeof(instruction_set_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070044
Brian Carlstrome24fa612011-09-29 00:53:55 -070045 dex_file_count_ = dex_files->size();
46 UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070047
Brian Carlstrom28db0122012-10-18 16:20:41 -070048 image_file_location_oat_checksum_ = image_file_location_oat_checksum;
49 UpdateChecksum(&image_file_location_oat_checksum_, sizeof(image_file_location_oat_checksum_));
50
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051 CHECK(IsAligned<kPageSize>(image_file_location_oat_data_begin));
52 image_file_location_oat_data_begin_ = image_file_location_oat_data_begin;
53 UpdateChecksum(&image_file_location_oat_data_begin_, sizeof(image_file_location_oat_data_begin_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070054
55 image_file_location_size_ = image_file_location.size();
56 UpdateChecksum(&image_file_location_size_, sizeof(image_file_location_size_));
57 UpdateChecksum(image_file_location.data(), image_file_location_size_);
58
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 executable_offset_ = 0;
Jeff Hao0aba0ba2013-06-03 14:49:28 -070060 interpreter_to_interpreter_entry_offset_ = 0;
61 interpreter_to_quick_entry_offset_ = 0;
62 portable_resolution_trampoline_offset_ = 0;
63 quick_resolution_trampoline_offset_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070064}
65
66bool OatHeader::IsValid() const {
Brian Carlstromf852fb22012-10-19 11:01:58 -070067 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070068 return false;
69 }
Brian Carlstromf852fb22012-10-19 11:01:58 -070070 if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 return false;
72 }
73 return true;
74}
75
76const char* OatHeader::GetMagic() const {
77 CHECK(IsValid());
78 return reinterpret_cast<const char*>(magic_);
79}
80
Brian Carlstrome24fa612011-09-29 00:53:55 -070081uint32_t OatHeader::GetChecksum() const {
82 CHECK(IsValid());
83 return adler32_checksum_;
84}
85
86void OatHeader::UpdateChecksum(const void* data, size_t length) {
87 DCHECK(IsValid());
88 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
89 adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
90}
91
Elliott Hughesa72ec822012-03-05 17:12:22 -080092InstructionSet OatHeader::GetInstructionSet() const {
93 CHECK(IsValid());
94 return instruction_set_;
95}
96
Brian Carlstrome24fa612011-09-29 00:53:55 -070097uint32_t OatHeader::GetExecutableOffset() const {
98 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -070099 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700100 CHECK_GT(executable_offset_, sizeof(OatHeader));
101 return executable_offset_;
102}
103
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700104void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
105 DCHECK_ALIGNED(executable_offset, kPageSize);
106 CHECK_GT(executable_offset, sizeof(OatHeader));
107 DCHECK(IsValid());
108 DCHECK_EQ(executable_offset_, 0U);
109
110 executable_offset_ = executable_offset;
111 UpdateChecksum(&executable_offset_, sizeof(executable_offset));
112}
113
114const void* OatHeader::GetInterpreterToInterpreterEntry() const {
115 return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToInterpreterEntryOffset();
116}
117
118uint32_t OatHeader::GetInterpreterToInterpreterEntryOffset() const {
119 DCHECK(IsValid());
120 CHECK_GE(interpreter_to_interpreter_entry_offset_, executable_offset_);
121 return interpreter_to_interpreter_entry_offset_;
122}
123
124void OatHeader::SetInterpreterToInterpreterEntryOffset(uint32_t offset) {
125 CHECK(offset == 0 || offset >= executable_offset_);
126 DCHECK(IsValid());
127 DCHECK_EQ(interpreter_to_interpreter_entry_offset_, 0U) << offset;
128
129 interpreter_to_interpreter_entry_offset_ = offset;
130 UpdateChecksum(&interpreter_to_interpreter_entry_offset_, sizeof(offset));
131}
132
133const void* OatHeader::GetInterpreterToQuickEntry() const {
134 return reinterpret_cast<const uint8_t*>(this) + GetInterpreterToQuickEntryOffset();
135}
136
137uint32_t OatHeader::GetInterpreterToQuickEntryOffset() const {
138 DCHECK(IsValid());
139 CHECK_GE(interpreter_to_quick_entry_offset_, interpreter_to_interpreter_entry_offset_);
140 return interpreter_to_quick_entry_offset_;
141}
142
143void OatHeader::SetInterpreterToQuickEntryOffset(uint32_t offset) {
144 CHECK(offset == 0 || offset >= interpreter_to_interpreter_entry_offset_);
145 DCHECK(IsValid());
146 DCHECK_EQ(interpreter_to_quick_entry_offset_, 0U) << offset;
147
148 interpreter_to_quick_entry_offset_ = offset;
149 UpdateChecksum(&interpreter_to_quick_entry_offset_, sizeof(offset));
150}
151
152const void* OatHeader::GetPortableResolutionTrampoline() const {
153 return reinterpret_cast<const uint8_t*>(this) + GetPortableResolutionTrampolineOffset();
154}
155
156uint32_t OatHeader::GetPortableResolutionTrampolineOffset() const {
157 DCHECK(IsValid());
158 CHECK_GE(portable_resolution_trampoline_offset_, interpreter_to_quick_entry_offset_);
159 return portable_resolution_trampoline_offset_;
160}
161
162void OatHeader::SetPortableResolutionTrampolineOffset(uint32_t offset) {
163 CHECK(offset == 0 || offset >= interpreter_to_quick_entry_offset_);
164 DCHECK(IsValid());
165 DCHECK_EQ(portable_resolution_trampoline_offset_, 0U) << offset;
166
167 portable_resolution_trampoline_offset_ = offset;
168 UpdateChecksum(&portable_resolution_trampoline_offset_, sizeof(offset));
169}
170
171const void* OatHeader::GetQuickResolutionTrampoline() const {
172 return reinterpret_cast<const uint8_t*>(this) + GetQuickResolutionTrampolineOffset();
173}
174
175uint32_t OatHeader::GetQuickResolutionTrampolineOffset() const {
176 DCHECK(IsValid());
177 CHECK_GE(quick_resolution_trampoline_offset_, portable_resolution_trampoline_offset_);
178 return quick_resolution_trampoline_offset_;
179}
180
181void OatHeader::SetQuickResolutionTrampolineOffset(uint32_t offset) {
182 CHECK(offset == 0 || offset >= portable_resolution_trampoline_offset_);
183 DCHECK(IsValid());
184 DCHECK_EQ(quick_resolution_trampoline_offset_, 0U) << offset;
185
186 quick_resolution_trampoline_offset_ = offset;
187 UpdateChecksum(&quick_resolution_trampoline_offset_, sizeof(offset));
188}
189
Brian Carlstrom28db0122012-10-18 16:20:41 -0700190uint32_t OatHeader::GetImageFileLocationOatChecksum() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700191 CHECK(IsValid());
Brian Carlstrom28db0122012-10-18 16:20:41 -0700192 return image_file_location_oat_checksum_;
193}
194
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800195uint32_t OatHeader::GetImageFileLocationOatDataBegin() const {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700196 CHECK(IsValid());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800197 return image_file_location_oat_data_begin_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700198}
199
200uint32_t OatHeader::GetImageFileLocationSize() const {
201 CHECK(IsValid());
202 return image_file_location_size_;
203}
204
205const uint8_t* OatHeader::GetImageFileLocationData() const {
206 CHECK(IsValid());
207 return image_file_location_data_;
208}
209
210std::string OatHeader::GetImageFileLocation() const {
211 CHECK(IsValid());
212 return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()),
213 GetImageFileLocationSize());
214}
215
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700216OatMethodOffsets::OatMethodOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700217 : code_offset_(0),
218 frame_size_in_bytes_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700219 core_spill_mask_(0),
220 fp_spill_mask_(0),
221 mapping_table_offset_(0),
222 vmap_table_offset_(0),
Jeff Hao74180ca2013-03-27 15:29:11 -0700223 gc_map_offset_(0)
Logan Chienccb7bf12012-03-28 12:52:32 +0800224{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700225
226OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
227 uint32_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700228 uint32_t core_spill_mask,
229 uint32_t fp_spill_mask,
230 uint32_t mapping_table_offset,
231 uint32_t vmap_table_offset,
Jeff Hao74180ca2013-03-27 15:29:11 -0700232 uint32_t gc_map_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800233 )
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700234 : code_offset_(code_offset),
235 frame_size_in_bytes_(frame_size_in_bytes),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700236 core_spill_mask_(core_spill_mask),
237 fp_spill_mask_(fp_spill_mask),
238 mapping_table_offset_(mapping_table_offset),
239 vmap_table_offset_(vmap_table_offset),
Jeff Hao74180ca2013-03-27 15:29:11 -0700240 gc_map_offset_(gc_map_offset)
Logan Chienccb7bf12012-03-28 12:52:32 +0800241{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700242
243OatMethodOffsets::~OatMethodOffsets() {}
244
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245} // namespace art