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 | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 24 | const uint8_t OatHeader::kOatVersion[] = { '0', '0', '3', '\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, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 32 | uint32_t image_file_location_oat_checksum, |
| 33 | uint32_t image_file_location_oat_begin, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 34 | const std::string& image_file_location) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 35 | memcpy(magic_, kOatMagic, sizeof(kOatMagic)); |
| 36 | memcpy(version_, kOatVersion, sizeof(kOatVersion)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 37 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 38 | adler32_checksum_ = adler32(0L, Z_NULL, 0); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 39 | |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame^] | 40 | CHECK_NE(instruction_set, kNone); |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 41 | instruction_set_ = instruction_set; |
| 42 | UpdateChecksum(&instruction_set_, sizeof(instruction_set_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 43 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 44 | dex_file_count_ = dex_files->size(); |
| 45 | UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 46 | |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 47 | image_file_location_oat_checksum_ = image_file_location_oat_checksum; |
| 48 | UpdateChecksum(&image_file_location_oat_checksum_, sizeof(image_file_location_oat_checksum_)); |
| 49 | |
| 50 | CHECK(IsAligned<kPageSize>(image_file_location_oat_begin)); |
| 51 | image_file_location_oat_begin_ = image_file_location_oat_begin; |
| 52 | UpdateChecksum(&image_file_location_oat_begin_, sizeof(image_file_location_oat_begin_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 53 | |
| 54 | image_file_location_size_ = image_file_location.size(); |
| 55 | UpdateChecksum(&image_file_location_size_, sizeof(image_file_location_size_)); |
| 56 | UpdateChecksum(image_file_location.data(), image_file_location_size_); |
| 57 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 58 | executable_offset_ = 0; |
| 59 | } |
| 60 | |
| 61 | bool OatHeader::IsValid() const { |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame^] | 62 | if (memcmp(magic_, kOatMagic, sizeof(kOatMagic)) != 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 63 | return false; |
| 64 | } |
Brian Carlstrom | f852fb2 | 2012-10-19 11:01:58 -0700 | [diff] [blame^] | 65 | if (memcmp(version_, kOatVersion, sizeof(kOatVersion)) != 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | const char* OatHeader::GetMagic() const { |
| 72 | CHECK(IsValid()); |
| 73 | return reinterpret_cast<const char*>(magic_); |
| 74 | } |
| 75 | |
| 76 | uint32_t OatHeader::GetDexFileCount() const { |
| 77 | DCHECK(IsValid()); |
| 78 | return dex_file_count_; |
| 79 | } |
| 80 | |
| 81 | uint32_t OatHeader::GetChecksum() const { |
| 82 | CHECK(IsValid()); |
| 83 | return adler32_checksum_; |
| 84 | } |
| 85 | |
| 86 | void OatHeader::UpdateChecksum(const void* data, size_t length) { |
| 87 | DCHECK(IsValid()); |
| 88 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 89 | adler32_checksum_ = adler32(adler32_checksum_, bytes, length); |
| 90 | } |
| 91 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 92 | InstructionSet OatHeader::GetInstructionSet() const { |
| 93 | CHECK(IsValid()); |
| 94 | return instruction_set_; |
| 95 | } |
| 96 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 97 | uint32_t OatHeader::GetExecutableOffset() const { |
| 98 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 99 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 100 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 101 | return executable_offset_; |
| 102 | } |
| 103 | |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 104 | uint32_t OatHeader::GetImageFileLocationOatChecksum() const { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 105 | CHECK(IsValid()); |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 106 | return image_file_location_oat_checksum_; |
| 107 | } |
| 108 | |
| 109 | uint32_t OatHeader::GetImageFileLocationOatBegin() const { |
| 110 | CHECK(IsValid()); |
| 111 | return image_file_location_oat_begin_; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | uint32_t OatHeader::GetImageFileLocationSize() const { |
| 115 | CHECK(IsValid()); |
| 116 | return image_file_location_size_; |
| 117 | } |
| 118 | |
| 119 | const uint8_t* OatHeader::GetImageFileLocationData() const { |
| 120 | CHECK(IsValid()); |
| 121 | return image_file_location_data_; |
| 122 | } |
| 123 | |
| 124 | std::string OatHeader::GetImageFileLocation() const { |
| 125 | CHECK(IsValid()); |
| 126 | return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()), |
| 127 | GetImageFileLocationSize()); |
| 128 | } |
| 129 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 130 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 131 | DCHECK_ALIGNED(executable_offset, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 132 | CHECK_GT(executable_offset, sizeof(OatHeader)); |
| 133 | DCHECK(IsValid()); |
| 134 | DCHECK_EQ(executable_offset_, 0U); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 135 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 136 | executable_offset_ = executable_offset; |
| 137 | UpdateChecksum(&executable_offset_, sizeof(executable_offset)); |
| 138 | } |
| 139 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 140 | OatMethodOffsets::OatMethodOffsets() |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 141 | : code_offset_(0), |
| 142 | frame_size_in_bytes_(0), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 143 | core_spill_mask_(0), |
| 144 | fp_spill_mask_(0), |
| 145 | mapping_table_offset_(0), |
| 146 | vmap_table_offset_(0), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 147 | gc_map_offset_(0), |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 148 | invoke_stub_offset_(0) |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 149 | {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 150 | |
| 151 | OatMethodOffsets::OatMethodOffsets(uint32_t code_offset, |
| 152 | uint32_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 153 | uint32_t core_spill_mask, |
| 154 | uint32_t fp_spill_mask, |
| 155 | uint32_t mapping_table_offset, |
| 156 | uint32_t vmap_table_offset, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 157 | uint32_t gc_map_offset, |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 158 | uint32_t invoke_stub_offset |
| 159 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 160 | , uint32_t proxy_stub_offset |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 161 | #endif |
| 162 | ) |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 163 | : code_offset_(code_offset), |
| 164 | frame_size_in_bytes_(frame_size_in_bytes), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 165 | core_spill_mask_(core_spill_mask), |
| 166 | fp_spill_mask_(fp_spill_mask), |
| 167 | mapping_table_offset_(mapping_table_offset), |
| 168 | vmap_table_offset_(vmap_table_offset), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 169 | gc_map_offset_(gc_map_offset), |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 170 | invoke_stub_offset_(invoke_stub_offset) |
| 171 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 172 | , proxy_stub_offset_(proxy_stub_offset) |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 173 | #endif |
| 174 | {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 175 | |
| 176 | OatMethodOffsets::~OatMethodOffsets() {} |
| 177 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 178 | } // namespace art |