blob: 1a884e3ad7e4ef1c998cd385aaa6dd0d08ac5ab6 [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
26OatHeader::OatHeader(const std::vector<const DexFile*>* dex_files) {
27 memcpy(magic_, kOatMagic, sizeof(kOatMagic));
28 memcpy(version_, kOatVersion, sizeof(kOatVersion));
29 adler32_checksum_ = adler32(0L, Z_NULL, 0);
30 dex_file_count_ = dex_files->size();
31 UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_));
32 executable_offset_ = 0;
33}
34
35bool OatHeader::IsValid() const {
36 if (memcmp(magic_, kOatMagic, sizeof(kOatMagic) != 0)) {
37 return false;
38 }
39 if (memcmp(version_, kOatVersion, sizeof(kOatVersion) != 0)) {
40 return false;
41 }
42 return true;
43}
44
45const char* OatHeader::GetMagic() const {
46 CHECK(IsValid());
47 return reinterpret_cast<const char*>(magic_);
48}
49
50uint32_t OatHeader::GetDexFileCount() const {
51 DCHECK(IsValid());
52 return dex_file_count_;
53}
54
55uint32_t OatHeader::GetChecksum() const {
56 CHECK(IsValid());
57 return adler32_checksum_;
58}
59
60void OatHeader::UpdateChecksum(const void* data, size_t length) {
61 DCHECK(IsValid());
62 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
63 adler32_checksum_ = adler32(adler32_checksum_, bytes, length);
64}
65
66uint32_t OatHeader::GetExecutableOffset() const {
67 DCHECK(IsValid());
Elliott Hughes06b37d92011-10-16 11:51:29 -070068 DCHECK_ALIGNED(executable_offset_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 CHECK_GT(executable_offset_, sizeof(OatHeader));
70 return executable_offset_;
71}
72
73void OatHeader::SetExecutableOffset(uint32_t executable_offset) {
Elliott Hughes06b37d92011-10-16 11:51:29 -070074 DCHECK_ALIGNED(executable_offset, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 CHECK_GT(executable_offset, sizeof(OatHeader));
76 DCHECK(IsValid());
77 DCHECK_EQ(executable_offset_, 0U);
78 executable_offset_ = executable_offset;
79 UpdateChecksum(&executable_offset_, sizeof(executable_offset));
80}
81
Brian Carlstrom3320cf42011-10-04 14:58:28 -070082OatMethodOffsets::OatMethodOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -070083 : code_offset_(0),
84 frame_size_in_bytes_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -070085 core_spill_mask_(0),
86 fp_spill_mask_(0),
87 mapping_table_offset_(0),
88 vmap_table_offset_(0),
Brian Carlstrome7d856b2012-01-11 18:10:55 -080089 gc_map_offset_(0),
Elliott Hughes362f9bc2011-10-17 18:56:41 -070090 invoke_stub_offset_(0) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -070091
92OatMethodOffsets::OatMethodOffsets(uint32_t code_offset,
93 uint32_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070094 uint32_t core_spill_mask,
95 uint32_t fp_spill_mask,
96 uint32_t mapping_table_offset,
97 uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080098 uint32_t gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070099 uint32_t invoke_stub_offset)
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700100 : code_offset_(code_offset),
101 frame_size_in_bytes_(frame_size_in_bytes),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700102 core_spill_mask_(core_spill_mask),
103 fp_spill_mask_(fp_spill_mask),
104 mapping_table_offset_(mapping_table_offset),
105 vmap_table_offset_(vmap_table_offset),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800106 gc_map_offset_(gc_map_offset),
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700107 invoke_stub_offset_(invoke_stub_offset) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700108
109OatMethodOffsets::~OatMethodOffsets() {}
110
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111} // namespace art