Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
| 17 | #include "oat.h" |
| 18 | |
| 19 | #include <zlib.h> |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | const uint8_t OatHeader::kOatMagic[] = { 'o', 'a', 't', '\n' }; |
| 24 | const uint8_t OatHeader::kOatVersion[] = { '0', '0', '1', '\0' }; |
| 25 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame^] | 26 | OatHeader::OatHeader() { |
| 27 | memset(this, 0, sizeof(*this)); |
| 28 | } |
| 29 | |
| 30 | OatHeader::OatHeader(InstructionSet instruction_set, const std::vector<const DexFile*>* dex_files) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 31 | memcpy(magic_, kOatMagic, sizeof(kOatMagic)); |
| 32 | memcpy(version_, kOatVersion, sizeof(kOatVersion)); |
| 33 | adler32_checksum_ = adler32(0L, Z_NULL, 0); |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame^] | 34 | instruction_set_ = instruction_set; |
| 35 | UpdateChecksum(&instruction_set_, sizeof(instruction_set_)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 36 | dex_file_count_ = dex_files->size(); |
| 37 | UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_)); |
| 38 | executable_offset_ = 0; |
| 39 | } |
| 40 | |
| 41 | bool 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 | |
| 51 | const char* OatHeader::GetMagic() const { |
| 52 | CHECK(IsValid()); |
| 53 | return reinterpret_cast<const char*>(magic_); |
| 54 | } |
| 55 | |
| 56 | uint32_t OatHeader::GetDexFileCount() const { |
| 57 | DCHECK(IsValid()); |
| 58 | return dex_file_count_; |
| 59 | } |
| 60 | |
| 61 | uint32_t OatHeader::GetChecksum() const { |
| 62 | CHECK(IsValid()); |
| 63 | return adler32_checksum_; |
| 64 | } |
| 65 | |
| 66 | void 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 Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame^] | 72 | InstructionSet OatHeader::GetInstructionSet() const { |
| 73 | CHECK(IsValid()); |
| 74 | return instruction_set_; |
| 75 | } |
| 76 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 77 | uint32_t OatHeader::GetExecutableOffset() const { |
| 78 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 79 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 80 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 81 | return executable_offset_; |
| 82 | } |
| 83 | |
| 84 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 85 | DCHECK_ALIGNED(executable_offset, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 86 | 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 Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 93 | OatMethodOffsets::OatMethodOffsets() |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 94 | : code_offset_(0), |
| 95 | frame_size_in_bytes_(0), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 96 | core_spill_mask_(0), |
| 97 | fp_spill_mask_(0), |
| 98 | mapping_table_offset_(0), |
| 99 | vmap_table_offset_(0), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 100 | gc_map_offset_(0), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 101 | invoke_stub_offset_(0) {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 102 | |
| 103 | OatMethodOffsets::OatMethodOffsets(uint32_t code_offset, |
| 104 | uint32_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 105 | uint32_t core_spill_mask, |
| 106 | uint32_t fp_spill_mask, |
| 107 | uint32_t mapping_table_offset, |
| 108 | uint32_t vmap_table_offset, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 109 | uint32_t gc_map_offset, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 110 | uint32_t invoke_stub_offset) |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 111 | : code_offset_(code_offset), |
| 112 | frame_size_in_bytes_(frame_size_in_bytes), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 113 | 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 Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 117 | gc_map_offset_(gc_map_offset), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 118 | invoke_stub_offset_(invoke_stub_offset) {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 119 | |
| 120 | OatMethodOffsets::~OatMethodOffsets() {} |
| 121 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 122 | } // namespace art |