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' }; |
Brian Carlstrom | 341df94 | 2012-06-27 12:29:22 -0700 | [diff] [blame] | 24 | const uint8_t OatHeader::kOatVersion[] = { '0', '0', '2', '\0' }; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 26 | OatHeader::OatHeader() { |
| 27 | memset(this, 0, sizeof(*this)); |
| 28 | } |
| 29 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 30 | OatHeader::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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 34 | memcpy(magic_, kOatMagic, sizeof(kOatMagic)); |
| 35 | memcpy(version_, kOatVersion, sizeof(kOatVersion)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 36 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 37 | adler32_checksum_ = adler32(0L, Z_NULL, 0); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 39 | instruction_set_ = instruction_set; |
| 40 | UpdateChecksum(&instruction_set_, sizeof(instruction_set_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 41 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 42 | dex_file_count_ = dex_files->size(); |
| 43 | UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 44 | |
| 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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 52 | executable_offset_ = 0; |
| 53 | } |
| 54 | |
| 55 | bool 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 | |
| 65 | const char* OatHeader::GetMagic() const { |
| 66 | CHECK(IsValid()); |
| 67 | return reinterpret_cast<const char*>(magic_); |
| 68 | } |
| 69 | |
| 70 | uint32_t OatHeader::GetDexFileCount() const { |
| 71 | DCHECK(IsValid()); |
| 72 | return dex_file_count_; |
| 73 | } |
| 74 | |
| 75 | uint32_t OatHeader::GetChecksum() const { |
| 76 | CHECK(IsValid()); |
| 77 | return adler32_checksum_; |
| 78 | } |
| 79 | |
| 80 | void 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 Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 86 | InstructionSet OatHeader::GetInstructionSet() const { |
| 87 | CHECK(IsValid()); |
| 88 | return instruction_set_; |
| 89 | } |
| 90 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 91 | uint32_t OatHeader::GetExecutableOffset() const { |
| 92 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 93 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 94 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 95 | return executable_offset_; |
| 96 | } |
| 97 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 98 | uint32_t OatHeader::GetImageFileLocationChecksum() const { |
| 99 | CHECK(IsValid()); |
| 100 | return image_file_location_checksum_; |
| 101 | } |
| 102 | |
| 103 | uint32_t OatHeader::GetImageFileLocationSize() const { |
| 104 | CHECK(IsValid()); |
| 105 | return image_file_location_size_; |
| 106 | } |
| 107 | |
| 108 | const uint8_t* OatHeader::GetImageFileLocationData() const { |
| 109 | CHECK(IsValid()); |
| 110 | return image_file_location_data_; |
| 111 | } |
| 112 | |
| 113 | std::string OatHeader::GetImageFileLocation() const { |
| 114 | CHECK(IsValid()); |
| 115 | return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()), |
| 116 | GetImageFileLocationSize()); |
| 117 | } |
| 118 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 119 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 120 | DCHECK_ALIGNED(executable_offset, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 121 | CHECK_GT(executable_offset, sizeof(OatHeader)); |
| 122 | DCHECK(IsValid()); |
| 123 | DCHECK_EQ(executable_offset_, 0U); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 124 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 125 | executable_offset_ = executable_offset; |
| 126 | UpdateChecksum(&executable_offset_, sizeof(executable_offset)); |
| 127 | } |
| 128 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 129 | OatMethodOffsets::OatMethodOffsets() |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 130 | : code_offset_(0), |
| 131 | frame_size_in_bytes_(0), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 132 | core_spill_mask_(0), |
| 133 | fp_spill_mask_(0), |
| 134 | mapping_table_offset_(0), |
| 135 | vmap_table_offset_(0), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 136 | gc_map_offset_(0), |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 137 | invoke_stub_offset_(0) |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 138 | {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 139 | |
| 140 | OatMethodOffsets::OatMethodOffsets(uint32_t code_offset, |
| 141 | uint32_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 142 | uint32_t core_spill_mask, |
| 143 | uint32_t fp_spill_mask, |
| 144 | uint32_t mapping_table_offset, |
| 145 | uint32_t vmap_table_offset, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 146 | uint32_t gc_map_offset, |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 147 | uint32_t invoke_stub_offset |
| 148 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 149 | , uint32_t proxy_stub_offset |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 150 | #endif |
| 151 | ) |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 152 | : code_offset_(code_offset), |
| 153 | frame_size_in_bytes_(frame_size_in_bytes), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 154 | 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 Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 158 | gc_map_offset_(gc_map_offset), |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 159 | invoke_stub_offset_(invoke_stub_offset) |
| 160 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 161 | , proxy_stub_offset_(proxy_stub_offset) |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 162 | #endif |
| 163 | {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 164 | |
| 165 | OatMethodOffsets::~OatMethodOffsets() {} |
| 166 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 167 | } // namespace art |