blob: d8d01df6a6c1af4877bf1e6497f172530b6a3274 [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' };
24const uint8_t OatHeader::kOatVersion[] = { '0', '0', '1', '\0' };
25
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),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700137 invoke_stub_offset_(0) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700138
139OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
140 uint32_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141 uint32_t core_spill_mask,
142 uint32_t fp_spill_mask,
143 uint32_t mapping_table_offset,
144 uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800145 uint32_t gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700146 uint32_t invoke_stub_offset)
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700147 : code_offset_(code_offset),
148 frame_size_in_bytes_(frame_size_in_bytes),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700149 core_spill_mask_(core_spill_mask),
150 fp_spill_mask_(fp_spill_mask),
151 mapping_table_offset_(mapping_table_offset),
152 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800153 gc_map_offset_(gc_map_offset),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700154 invoke_stub_offset_(invoke_stub_offset) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700155
156OatMethodOffsets::~OatMethodOffsets() {}
157
Brian Carlstrome24fa612011-09-29 00:53:55 -0700158} // namespace art