blob: e747a8bb185820cabced7d025a67495b44111660 [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
30OatHeader::OatHeader(InstructionSet instruction_set, const std::vector<const DexFile*>* dex_files) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070031 memcpy(magic_, kOatMagic, sizeof(kOatMagic));
32 memcpy(version_, kOatVersion, sizeof(kOatVersion));
33 adler32_checksum_ = adler32(0L, Z_NULL, 0);
Elliott Hughesa72ec822012-03-05 17:12:22 -080034 instruction_set_ = instruction_set;
35 UpdateChecksum(&instruction_set_, sizeof(instruction_set_));
Brian Carlstrome24fa612011-09-29 00:53:55 -070036 dex_file_count_ = dex_files->size();
37 UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
38 executable_offset_ = 0;
39}
40
41bool OatHeader::IsValid() const {
42 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic) != 0)) {
43 return false;
44 }
45 if (memcmp(version_, kOatVersion, sizeof(kOatVersion) != 0)) {
46 return false;
47 }
48 return true;
49}
50
51const char* OatHeader::GetMagic() const {
52 CHECK(IsValid());
53 return reinterpret_cast<const char*>(magic_);
54}
55
56uint32_t OatHeader::GetDexFileCount() const {
57 DCHECK(IsValid());
58 return dex_file_count_;
59}
60
61uint32_t OatHeader::GetChecksum() const {
62 CHECK(IsValid());
63 return adler32_checksum_;
64}
65
66void OatHeader::UpdateChecksum(const void* data, size_t length) {
67 DCHECK(IsValid());
68 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
69 adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
70}
71
Elliott Hughesa72ec822012-03-05 17:12:22 -080072InstructionSet OatHeader::GetInstructionSet() const {
73 CHECK(IsValid());
74 return instruction_set_;
75}
76
Brian Carlstrome24fa612011-09-29 00:53:55 -070077uint32_t OatHeader::GetExecutableOffset() const {
78 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -070079 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 CHECK_GT(executable_offset_, sizeof(OatHeader));
81 return executable_offset_;
82}
83
84void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
Elliott Hughes06b37d92011-10-16 11:51:29 -070085 DCHECK_ALIGNED(executable_offset, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070086 CHECK_GT(executable_offset, sizeof(OatHeader));
87 DCHECK(IsValid());
88 DCHECK_EQ(executable_offset_, 0U);
89 executable_offset_ = executable_offset;
90 UpdateChecksum(&executable_offset_, sizeof(executable_offset));
91}
92
Brian Carlstrom3320cf42011-10-04 14:58:28 -070093OatMethodOffsets::OatMethodOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -070094 : code_offset_(0),
95 frame_size_in_bytes_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -070096 core_spill_mask_(0),
97 fp_spill_mask_(0),
98 mapping_table_offset_(0),
99 vmap_table_offset_(0),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800100 gc_map_offset_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700101 invoke_stub_offset_(0) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700102
103OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
104 uint32_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700105 uint32_t core_spill_mask,
106 uint32_t fp_spill_mask,
107 uint32_t mapping_table_offset,
108 uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800109 uint32_t gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700110 uint32_t invoke_stub_offset)
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700111 : code_offset_(code_offset),
112 frame_size_in_bytes_(frame_size_in_bytes),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700113 core_spill_mask_(core_spill_mask),
114 fp_spill_mask_(fp_spill_mask),
115 mapping_table_offset_(mapping_table_offset),
116 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800117 gc_map_offset_(gc_map_offset),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700118 invoke_stub_offset_(invoke_stub_offset) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700119
120OatMethodOffsets::~OatMethodOffsets() {}
121
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122} // namespace art