blob: 16c970528d79baca6675a38ef082fa6710029267 [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 Hao74180ca2013-03-27 15:29:11 -070025const uint8_t OatHeader::kOatVersion[] = { '0', '0', '4', '\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;
60}
61
62bool OatHeader::IsValid() const {
Brian Carlstromf852fb22012-10-19 11:01:58 -070063 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070064 return false;
65 }
Brian Carlstromf852fb22012-10-19 11:01:58 -070066 if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070067 return false;
68 }
69 return true;
70}
71
72const char* OatHeader::GetMagic() const {
73 CHECK(IsValid());
74 return reinterpret_cast<const char*>(magic_);
75}
76
Brian Carlstrome24fa612011-09-29 00:53:55 -070077uint32_t OatHeader::GetChecksum() const {
78 CHECK(IsValid());
79 return adler32_checksum_;
80}
81
82void OatHeader::UpdateChecksum(const void* data, size_t length) {
83 DCHECK(IsValid());
84 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
85 adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
86}
87
Elliott Hughesa72ec822012-03-05 17:12:22 -080088InstructionSet OatHeader::GetInstructionSet() const {
89 CHECK(IsValid());
90 return instruction_set_;
91}
92
Brian Carlstrome24fa612011-09-29 00:53:55 -070093uint32_t OatHeader::GetExecutableOffset() const {
94 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -070095 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070096 CHECK_GT(executable_offset_, sizeof(OatHeader));
97 return executable_offset_;
98}
99
Brian Carlstrom28db0122012-10-18 16:20:41 -0700100uint32_t OatHeader::GetImageFileLocationOatChecksum() const {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700101 CHECK(IsValid());
Brian Carlstrom28db0122012-10-18 16:20:41 -0700102 return image_file_location_oat_checksum_;
103}
104
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800105uint32_t OatHeader::GetImageFileLocationOatDataBegin() const {
Brian Carlstrom28db0122012-10-18 16:20:41 -0700106 CHECK(IsValid());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800107 return image_file_location_oat_data_begin_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700108}
109
110uint32_t OatHeader::GetImageFileLocationSize() const {
111 CHECK(IsValid());
112 return image_file_location_size_;
113}
114
115const uint8_t* OatHeader::GetImageFileLocationData() const {
116 CHECK(IsValid());
117 return image_file_location_data_;
118}
119
120std::string OatHeader::GetImageFileLocation() const {
121 CHECK(IsValid());
122 return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()),
123 GetImageFileLocationSize());
124}
125
Brian Carlstrome24fa612011-09-29 00:53:55 -0700126void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
Elliott Hughes06b37d92011-10-16 11:51:29 -0700127 DCHECK_ALIGNED(executable_offset, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 CHECK_GT(executable_offset, sizeof(OatHeader));
129 DCHECK(IsValid());
130 DCHECK_EQ(executable_offset_, 0U);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700131
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132 executable_offset_ = executable_offset;
133 UpdateChecksum(&executable_offset_, sizeof(executable_offset));
134}
135
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700136OatMethodOffsets::OatMethodOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700137 : code_offset_(0),
138 frame_size_in_bytes_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700139 core_spill_mask_(0),
140 fp_spill_mask_(0),
141 mapping_table_offset_(0),
142 vmap_table_offset_(0),
Jeff Hao74180ca2013-03-27 15:29:11 -0700143 gc_map_offset_(0)
Ian Rogersc928de92013-02-27 14:30:44 -0800144#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien75bc8962013-01-25 16:28:35 +0800145 , proxy_stub_offset_(0)
146#endif
Logan Chienccb7bf12012-03-28 12:52:32 +0800147{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148
149OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
150 uint32_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700151 uint32_t core_spill_mask,
152 uint32_t fp_spill_mask,
153 uint32_t mapping_table_offset,
154 uint32_t vmap_table_offset,
Jeff Hao74180ca2013-03-27 15:29:11 -0700155 uint32_t gc_map_offset
Ian Rogersc928de92013-02-27 14:30:44 -0800156#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800157 , uint32_t proxy_stub_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800158#endif
159 )
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700160 : code_offset_(code_offset),
161 frame_size_in_bytes_(frame_size_in_bytes),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700162 core_spill_mask_(core_spill_mask),
163 fp_spill_mask_(fp_spill_mask),
164 mapping_table_offset_(mapping_table_offset),
165 vmap_table_offset_(vmap_table_offset),
Jeff Hao74180ca2013-03-27 15:29:11 -0700166 gc_map_offset_(gc_map_offset)
Ian Rogersc928de92013-02-27 14:30:44 -0800167#if defined(ART_USE_PORTABLE_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800168 , proxy_stub_offset_(proxy_stub_offset)
Logan Chienccb7bf12012-03-28 12:52:32 +0800169#endif
170{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700171
172OatMethodOffsets::~OatMethodOffsets() {}
173
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174} // namespace art