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 | |
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, |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 32 | uint32_t elf_image_count, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 33 | uint32_t image_file_location_checksum, |
| 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 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 40 | instruction_set_ = instruction_set; |
| 41 | UpdateChecksum(&instruction_set_, sizeof(instruction_set_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 42 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 43 | dex_file_count_ = dex_files->size(); |
| 44 | UpdateChecksum(&dex_file_count_, sizeof(dex_file_count_)); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 45 | |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 46 | elf_image_count_ = elf_image_count; |
| 47 | UpdateChecksum(&elf_image_count_, sizeof(elf_image_count_)); |
| 48 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 49 | image_file_location_checksum_ = image_file_location_checksum; |
| 50 | UpdateChecksum(&image_file_location_checksum_, sizeof(image_file_location_checksum_)); |
| 51 | |
| 52 | image_file_location_size_ = image_file_location.size(); |
| 53 | UpdateChecksum(&image_file_location_size_, sizeof(image_file_location_size_)); |
| 54 | UpdateChecksum(image_file_location.data(), image_file_location_size_); |
| 55 | |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 56 | elf_image_table_offset_ = 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 57 | executable_offset_ = 0; |
| 58 | } |
| 59 | |
| 60 | bool OatHeader::IsValid() const { |
| 61 | if (memcmp(magic_, kOatMagic, sizeof(kOatMagic) != 0)) { |
| 62 | return false; |
| 63 | } |
| 64 | if (memcmp(version_, kOatVersion, sizeof(kOatVersion) != 0)) { |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | const char* OatHeader::GetMagic() const { |
| 71 | CHECK(IsValid()); |
| 72 | return reinterpret_cast<const char*>(magic_); |
| 73 | } |
| 74 | |
| 75 | uint32_t OatHeader::GetDexFileCount() const { |
| 76 | DCHECK(IsValid()); |
| 77 | return dex_file_count_; |
| 78 | } |
| 79 | |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 80 | uint32_t OatHeader::GetElfImageCount() const { |
| 81 | DCHECK(IsValid()); |
| 82 | return elf_image_count_; |
| 83 | } |
| 84 | |
| 85 | uint32_t OatHeader::GetElfImageTableOffset() const { |
| 86 | DCHECK(IsValid()); |
| 87 | return elf_image_table_offset_; |
| 88 | } |
| 89 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 90 | uint32_t OatHeader::GetChecksum() const { |
| 91 | CHECK(IsValid()); |
| 92 | return adler32_checksum_; |
| 93 | } |
| 94 | |
| 95 | void OatHeader::UpdateChecksum(const void* data, size_t length) { |
| 96 | DCHECK(IsValid()); |
| 97 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 98 | adler32_checksum_ = adler32(adler32_checksum_, bytes, length); |
| 99 | } |
| 100 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 101 | InstructionSet OatHeader::GetInstructionSet() const { |
| 102 | CHECK(IsValid()); |
| 103 | return instruction_set_; |
| 104 | } |
| 105 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 106 | uint32_t OatHeader::GetExecutableOffset() const { |
| 107 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 108 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 109 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 110 | return executable_offset_; |
| 111 | } |
| 112 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 113 | uint32_t OatHeader::GetImageFileLocationChecksum() const { |
| 114 | CHECK(IsValid()); |
| 115 | return image_file_location_checksum_; |
| 116 | } |
| 117 | |
| 118 | uint32_t OatHeader::GetImageFileLocationSize() const { |
| 119 | CHECK(IsValid()); |
| 120 | return image_file_location_size_; |
| 121 | } |
| 122 | |
| 123 | const uint8_t* OatHeader::GetImageFileLocationData() const { |
| 124 | CHECK(IsValid()); |
| 125 | return image_file_location_data_; |
| 126 | } |
| 127 | |
| 128 | std::string OatHeader::GetImageFileLocation() const { |
| 129 | CHECK(IsValid()); |
| 130 | return std::string(reinterpret_cast<const char*>(GetImageFileLocationData()), |
| 131 | GetImageFileLocationSize()); |
| 132 | } |
| 133 | |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 134 | void OatHeader::SetElfImageTableOffset(uint32_t elf_image_table_offset) { |
| 135 | DCHECK(IsValid()); |
| 136 | elf_image_table_offset_ = elf_image_table_offset; |
| 137 | UpdateChecksum(&elf_image_table_offset_, sizeof(elf_image_table_offset_)); |
| 138 | } |
| 139 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 140 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 141 | DCHECK_ALIGNED(executable_offset, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 142 | CHECK_GT(executable_offset, sizeof(OatHeader)); |
| 143 | DCHECK(IsValid()); |
| 144 | DCHECK_EQ(executable_offset_, 0U); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 145 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 146 | executable_offset_ = executable_offset; |
| 147 | UpdateChecksum(&executable_offset_, sizeof(executable_offset)); |
| 148 | } |
| 149 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 150 | OatMethodOffsets::OatMethodOffsets() |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 151 | : code_offset_(0), |
| 152 | frame_size_in_bytes_(0), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 153 | core_spill_mask_(0), |
| 154 | fp_spill_mask_(0), |
| 155 | mapping_table_offset_(0), |
| 156 | vmap_table_offset_(0), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 157 | gc_map_offset_(0), |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 158 | invoke_stub_offset_(0) |
| 159 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 160 | , code_elf_idx_(static_cast<uint16_t>(-1u)), |
| 161 | code_elf_func_idx_(static_cast<uint16_t>(-1u)), |
| 162 | invoke_stub_elf_idx_(static_cast<uint16_t>(-1u)), |
| 163 | invoke_stub_elf_func_idx_(static_cast<uint16_t>(-1u)) |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 164 | #endif |
| 165 | {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 166 | |
| 167 | OatMethodOffsets::OatMethodOffsets(uint32_t code_offset, |
| 168 | uint32_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 169 | uint32_t core_spill_mask, |
| 170 | uint32_t fp_spill_mask, |
| 171 | uint32_t mapping_table_offset, |
| 172 | uint32_t vmap_table_offset, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 173 | uint32_t gc_map_offset, |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 174 | uint32_t invoke_stub_offset |
| 175 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 176 | , uint16_t code_elf_idx, |
| 177 | uint16_t code_elf_func_idx, |
| 178 | uint16_t invoke_stub_elf_idx, |
| 179 | uint16_t invoke_stub_elf_func_idx |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 180 | #endif |
| 181 | ) |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 182 | : code_offset_(code_offset), |
| 183 | frame_size_in_bytes_(frame_size_in_bytes), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 184 | core_spill_mask_(core_spill_mask), |
| 185 | fp_spill_mask_(fp_spill_mask), |
| 186 | mapping_table_offset_(mapping_table_offset), |
| 187 | vmap_table_offset_(vmap_table_offset), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 188 | gc_map_offset_(gc_map_offset), |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 189 | invoke_stub_offset_(invoke_stub_offset) |
| 190 | #if defined(ART_USE_LLVM_COMPILER) |
| 191 | , code_elf_idx_(code_elf_idx), |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 192 | code_elf_func_idx_(code_elf_func_idx), |
| 193 | invoke_stub_elf_idx_(invoke_stub_elf_idx), |
| 194 | invoke_stub_elf_func_idx_(invoke_stub_elf_func_idx) |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 195 | #endif |
| 196 | {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 197 | |
| 198 | OatMethodOffsets::~OatMethodOffsets() {} |
| 199 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 200 | } // namespace art |