blob: 4df4543f3ff6f7b46f3be857221f7fd73a9c6ad1 [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 Carlstrom341df942012-06-27 12:29:22 -070024const uint8_t OatHeader::kOatVersion[] = { '0', '0', '2', '\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,
32 uint32_t image_file_location_checksum,
33 const std::string& image_file_location) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070034 memcpy(magic_, kOatMagic, sizeof(kOatMagic));
35 memcpy(version_, kOatVersion, sizeof(kOatVersion));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070036
Brian Carlstrome24fa612011-09-29 00:53:55 -070037 adler32_checksum_ = adler32(0L, Z_NULL, 0);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070038
Elliott Hughesa72ec822012-03-05 17:12:22 -080039 instruction_set_ = instruction_set;
40 UpdateChecksum(&instruction_set_, sizeof(instruction_set_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070041
Brian Carlstrome24fa612011-09-29 00:53:55 -070042 dex_file_count_ = dex_files->size();
43 UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070044
45 image_file_location_checksum_ = image_file_location_checksum;
46 UpdateChecksum(&image_file_location_checksum_, sizeof(image_file_location_checksum_));
47
48 image_file_location_size_ = image_file_location.size();
49 UpdateChecksum(&image_file_location_size_, sizeof(image_file_location_size_));
50 UpdateChecksum(image_file_location.data(), image_file_location_size_);
51
Brian Carlstrome24fa612011-09-29 00:53:55 -070052 executable_offset_ = 0;
53}
54
55bool OatHeader::IsValid() const {
56 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic) != 0)) {
57 return false;
58 }
59 if (memcmp(version_, kOatVersion, sizeof(kOatVersion) != 0)) {
60 return false;
61 }
62 return true;
63}
64
65const char* OatHeader::GetMagic() const {
66 CHECK(IsValid());
67 return reinterpret_cast<const char*>(magic_);
68}
69
70uint32_t OatHeader::GetDexFileCount() const {
71 DCHECK(IsValid());
72 return dex_file_count_;
73}
74
75uint32_t OatHeader::GetChecksum() const {
76 CHECK(IsValid());
77 return adler32_checksum_;
78}
79
80void OatHeader::UpdateChecksum(const void* data, size_t length) {
81 DCHECK(IsValid());
82 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
83 adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
84}
85
Elliott Hughesa72ec822012-03-05 17:12:22 -080086InstructionSet OatHeader::GetInstructionSet() const {
87 CHECK(IsValid());
88 return instruction_set_;
89}
90
Brian Carlstrome24fa612011-09-29 00:53:55 -070091uint32_t OatHeader::GetExecutableOffset() const {
92 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -070093 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070094 CHECK_GT(executable_offset_, sizeof(OatHeader));
95 return executable_offset_;
96}
97
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070098uint32_t OatHeader::GetImageFileLocationChecksum() const {
99 CHECK(IsValid());
100 return image_file_location_checksum_;
101}
102
103uint32_t OatHeader::GetImageFileLocationSize() const {
104 CHECK(IsValid());
105 return image_file_location_size_;
106}
107
108const uint8_t* OatHeader::GetImageFileLocationData() const {
109 CHECK(IsValid());
110 return image_file_location_data_;
111}
112
113std::string OatHeader::GetImageFileLocation() const {
114 CHECK(IsValid());
115 return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()),
116 GetImageFileLocationSize());
117}
118
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
Elliott Hughes06b37d92011-10-16 11:51:29 -0700120 DCHECK_ALIGNED(executable_offset, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121 CHECK_GT(executable_offset, sizeof(OatHeader));
122 DCHECK(IsValid());
123 DCHECK_EQ(executable_offset_, 0U);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700124
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125 executable_offset_ = executable_offset;
126 UpdateChecksum(&executable_offset_, sizeof(executable_offset));
127}
128
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700129OatMethodOffsets::OatMethodOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700130 : code_offset_(0),
131 frame_size_in_bytes_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700132 core_spill_mask_(0),
133 fp_spill_mask_(0),
134 mapping_table_offset_(0),
135 vmap_table_offset_(0),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800136 gc_map_offset_(0),
Logan Chienccb7bf12012-03-28 12:52:32 +0800137 invoke_stub_offset_(0)
Logan Chienccb7bf12012-03-28 12:52:32 +0800138{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700139
140OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
141 uint32_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700142 uint32_t core_spill_mask,
143 uint32_t fp_spill_mask,
144 uint32_t mapping_table_offset,
145 uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800146 uint32_t gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800147 uint32_t invoke_stub_offset
148#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800149 , uint32_t proxy_stub_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800150#endif
151 )
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700152 : code_offset_(code_offset),
153 frame_size_in_bytes_(frame_size_in_bytes),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700154 core_spill_mask_(core_spill_mask),
155 fp_spill_mask_(fp_spill_mask),
156 mapping_table_offset_(mapping_table_offset),
157 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800158 gc_map_offset_(gc_map_offset),
Logan Chienccb7bf12012-03-28 12:52:32 +0800159 invoke_stub_offset_(invoke_stub_offset)
160#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800161 , proxy_stub_offset_(proxy_stub_offset)
Logan Chienccb7bf12012-03-28 12:52:32 +0800162#endif
163{}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700164
165OatMethodOffsets::~OatMethodOffsets() {}
166
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167} // namespace art