blob: 6284f9a4eb64cc0f83aa5c1dbd0d11ad2e571632 [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
19#include <zlib.h>
20
21namespace art {
22
23const uint8_t OatHeader::kOatMagic[] = { 'o', 'a', 't', '\n' };
Brian Carlstrom28db0122012-10-18 16:20:41 -070024const uint8_t OatHeader::kOatVersion[] = { '0', '0', '3', '\0' };
Brian Carlstrome24fa612011-09-29 00:53:55 -070025
Elliott Hughesa72ec822012-03-05 17:12:22 -080026OatHeader::OatHeader() {
27 memset(this, 0, sizeof(*this));
28}
29
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070030OatHeader::OatHeader(InstructionSet instruction_set,
31 const std::vector<const DexFile*>* dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070032 uint32_t image_file_location_oat_checksum,
33 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070034 const std::string& image_file_location) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070035 memcpy(magic_, kOatMagic, sizeof(kOatMagic));
36 memcpy(version_, kOatVersion, sizeof(kOatVersion));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070037
Brian Carlstrome24fa612011-09-29 00:53:55 -070038 adler32_checksum_ = adler32(0L, Z_NULL, 0);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070039
Elliott Hughesa72ec822012-03-05 17:12:22 -080040 instruction_set_ = instruction_set;
41 UpdateChecksum(&instruction_set_, sizeof(instruction_set_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070042
Brian Carlstrome24fa612011-09-29 00:53:55 -070043 dex_file_count_ = dex_files->size();
44 UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070045
Brian Carlstrom28db0122012-10-18 16:20:41 -070046 image_file_location_oat_checksum_ = image_file_location_oat_checksum;
47 UpdateChecksum(&image_file_location_oat_checksum_, sizeof(image_file_location_oat_checksum_));
48
49 CHECK(IsAligned<kPageSize>(image_file_location_oat_begin));
50 image_file_location_oat_begin_ = image_file_location_oat_begin;
51 UpdateChecksum(&image_file_location_oat_begin_, sizeof(image_file_location_oat_begin_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070052
53 image_file_location_size_ = image_file_location.size();
54 UpdateChecksum(&image_file_location_size_, sizeof(image_file_location_size_));
55 UpdateChecksum(image_file_location.data(), image_file_location_size_);
56
Brian Carlstrome24fa612011-09-29 00:53:55 -070057 executable_offset_ = 0;
58}
59
60bool OatHeader::IsValid() const {
61 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic) != 0)) {
62 return false;
63 }
64 if (memcmp(version_, kOatVersion, sizeof(kOatVersion) != 0)) {
65 return false;
66 }
67 return true;
68}
69
70const char* OatHeader::GetMagic() const {
71 CHECK(IsValid());
72 return reinterpret_cast<const char*>(magic_);
73}
74
75uint32_t OatHeader::GetDexFileCount() const {
76 DCHECK(IsValid());
77 return dex_file_count_;
78}
79
80uint32_t OatHeader::GetChecksum() const {
81 CHECK(IsValid());
82 return adler32_checksum_;
83}
84
85void OatHeader::UpdateChecksum(const void* data, size_t length) {
86 DCHECK(IsValid());
87 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
88 adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
89}
90
Elliott Hughesa72ec822012-03-05 17:12:22 -080091InstructionSet OatHeader::GetInstructionSet() const {
92 CHECK(IsValid());
93 return instruction_set_;
94}
95
Brian Carlstrome24fa612011-09-29 00:53:55 -070096uint32_t OatHeader::GetExecutableOffset() const {
97 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -070098 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070099 CHECK_GT(executable_offset_, sizeof(OatHeader));
100 return executable_offset_;
101}
102
Brian Carlstrom28db0122012-10-18 16:20:41 -0700103uint32_t OatHeader::GetImageFileLocationOatChecksum() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700104 CHECK(IsValid());
Brian Carlstrom28db0122012-10-18 16:20:41 -0700105 return image_file_location_oat_checksum_;
106}
107
108uint32_t OatHeader::GetImageFileLocationOatBegin() const {
109 CHECK(IsValid());
110 return image_file_location_oat_begin_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700111}
112
113uint32_t OatHeader::GetImageFileLocationSize() const {
114 CHECK(IsValid());
115 return image_file_location_size_;
116}
117
118const uint8_t* OatHeader::GetImageFileLocationData() const {
119 CHECK(IsValid());
120 return image_file_location_data_;
121}
122
123std::string OatHeader::GetImageFileLocation() const {
124 CHECK(IsValid());
125 return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()),
126 GetImageFileLocationSize());
127}
128
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
Elliott Hughes06b37d92011-10-16 11:51:29 -0700130 DCHECK_ALIGNED(executable_offset, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 CHECK_GT(executable_offset, sizeof(OatHeader));
132 DCHECK(IsValid());
133 DCHECK_EQ(executable_offset_, 0U);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700134
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135 executable_offset_ = executable_offset;
136 UpdateChecksum(&executable_offset_, sizeof(executable_offset));
137}
138
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700139OatMethodOffsets::OatMethodOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700140 : code_offset_(0),
141 frame_size_in_bytes_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700142 core_spill_mask_(0),
143 fp_spill_mask_(0),
144 mapping_table_offset_(0),
145 vmap_table_offset_(0),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800146 gc_map_offset_(0),
Logan Chienccb7bf12012-03-28 12:52:32 +0800147 invoke_stub_offset_(0)
Logan Chienccb7bf12012-03-28 12:52:32 +0800148{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700149
150OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
151 uint32_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700152 uint32_t core_spill_mask,
153 uint32_t fp_spill_mask,
154 uint32_t mapping_table_offset,
155 uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800156 uint32_t gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800157 uint32_t invoke_stub_offset
158#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800159 , uint32_t proxy_stub_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800160#endif
161 )
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700162 : code_offset_(code_offset),
163 frame_size_in_bytes_(frame_size_in_bytes),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700164 core_spill_mask_(core_spill_mask),
165 fp_spill_mask_(fp_spill_mask),
166 mapping_table_offset_(mapping_table_offset),
167 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800168 gc_map_offset_(gc_map_offset),
Logan Chienccb7bf12012-03-28 12:52:32 +0800169 invoke_stub_offset_(invoke_stub_offset)
170#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800171 , proxy_stub_offset_(proxy_stub_offset)
Logan Chienccb7bf12012-03-28 12:52:32 +0800172#endif
173{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700174
175OatMethodOffsets::~OatMethodOffsets() {}
176
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177} // namespace art