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 | |
| 26 | OatHeader::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 | |
| 35 | bool 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 | |
| 45 | const char* OatHeader::GetMagic() const { |
| 46 | CHECK(IsValid()); |
| 47 | return reinterpret_cast<const char*>(magic_); |
| 48 | } |
| 49 | |
| 50 | uint32_t OatHeader::GetDexFileCount() const { |
| 51 | DCHECK(IsValid()); |
| 52 | return dex_file_count_; |
| 53 | } |
| 54 | |
| 55 | uint32_t OatHeader::GetChecksum() const { |
| 56 | CHECK(IsValid()); |
| 57 | return adler32_checksum_; |
| 58 | } |
| 59 | |
| 60 | void 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 | |
| 66 | uint32_t OatHeader::GetExecutableOffset() const { |
| 67 | DCHECK(IsValid()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 68 | DCHECK_ALIGNED(executable_offset_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 69 | CHECK_GT(executable_offset_, sizeof(OatHeader)); |
| 70 | return executable_offset_; |
| 71 | } |
| 72 | |
| 73 | void OatHeader::SetExecutableOffset(uint32_t executable_offset) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 74 | DCHECK_ALIGNED(executable_offset, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 75 | 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 Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 82 | OatMethodOffsets::OatMethodOffsets() |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 83 | : code_offset_(0), |
| 84 | frame_size_in_bytes_(0), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 85 | core_spill_mask_(0), |
| 86 | fp_spill_mask_(0), |
| 87 | mapping_table_offset_(0), |
| 88 | vmap_table_offset_(0), |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 89 | gc_map_offset_(0), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 90 | invoke_stub_offset_(0) {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 91 | |
| 92 | OatMethodOffsets::OatMethodOffsets(uint32_t code_offset, |
| 93 | uint32_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 94 | uint32_t core_spill_mask, |
| 95 | uint32_t fp_spill_mask, |
| 96 | uint32_t mapping_table_offset, |
| 97 | uint32_t vmap_table_offset, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 98 | uint32_t gc_map_offset, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 99 | uint32_t invoke_stub_offset) |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 100 | : code_offset_(code_offset), |
| 101 | frame_size_in_bytes_(frame_size_in_bytes), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 102 | 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 Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 106 | gc_map_offset_(gc_map_offset), |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 107 | invoke_stub_offset_(invoke_stub_offset) {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 108 | |
| 109 | OatMethodOffsets::~OatMethodOffsets() {} |
| 110 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 111 | } // namespace art |