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 | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 16 | |
| 17 | #include "image.h" |
| 18 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 19 | #include "base/bit_utils.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 20 | #include "base/length_prefixed_array.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 21 | #include "base/utils.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 22 | #include "mirror/object-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include "mirror/object_array-inl.h" |
| 24 | #include "mirror/object_array.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 26 | namespace art { |
| 27 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 28 | const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' }; |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame^] | 29 | const uint8_t ImageHeader::kImageVersion[] = { '0', '6', '8', '\0' }; // Image checksums. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 30 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | ImageHeader::ImageHeader(uint32_t image_begin, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 32 | uint32_t image_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | ImageSection* sections, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | uint32_t image_roots, |
| 35 | uint32_t oat_checksum, |
| 36 | uint32_t oat_file_begin, |
| 37 | uint32_t oat_data_begin, |
| 38 | uint32_t oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 39 | uint32_t oat_file_end, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 40 | uint32_t boot_image_begin, |
| 41 | uint32_t boot_image_size, |
| 42 | uint32_t boot_oat_begin, |
| 43 | uint32_t boot_oat_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 44 | uint32_t pointer_size, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 45 | StorageMode storage_mode, |
| 46 | size_t data_size) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | : image_begin_(image_begin), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 48 | image_size_(image_size), |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame^] | 49 | image_checksum_(0u), |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | oat_checksum_(oat_checksum), |
| 51 | oat_file_begin_(oat_file_begin), |
| 52 | oat_data_begin_(oat_data_begin), |
| 53 | oat_data_end_(oat_data_end), |
| 54 | oat_file_end_(oat_file_end), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 55 | boot_image_begin_(boot_image_begin), |
| 56 | boot_image_size_(boot_image_size), |
| 57 | boot_oat_begin_(boot_oat_begin), |
| 58 | boot_oat_size_(boot_oat_size), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 59 | image_roots_(image_roots), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 60 | pointer_size_(pointer_size), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 61 | storage_mode_(storage_mode), |
| 62 | data_size_(data_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize)); |
| 64 | CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize)); |
| 65 | CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | CHECK_LT(image_roots, oat_file_begin); |
| 67 | CHECK_LE(oat_file_begin, oat_data_begin); |
| 68 | CHECK_LT(oat_data_begin, oat_data_end); |
| 69 | CHECK_LE(oat_data_end, oat_file_end); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 70 | CHECK(ValidPointerSize(pointer_size_)) << pointer_size_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | memcpy(magic_, kImageMagic, sizeof(kImageMagic)); |
| 72 | memcpy(version_, kImageVersion, sizeof(kImageVersion)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 73 | std::copy_n(sections, kSectionCount, sections_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 76 | void ImageHeader::RelocateImage(int64_t delta) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 77 | CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 78 | oat_file_begin_ += delta; |
| 79 | oat_data_begin_ += delta; |
| 80 | oat_data_end_ += delta; |
| 81 | oat_file_end_ += delta; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 82 | RelocateImageObjects(delta); |
| 83 | RelocateImageMethods(delta); |
| 84 | } |
| 85 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 86 | void ImageHeader::RelocateImageObjects(int64_t delta) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 87 | image_begin_ += delta; |
| 88 | image_roots_ += delta; |
| 89 | } |
| 90 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 91 | void ImageHeader::RelocateImageMethods(int64_t delta) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 92 | for (size_t i = 0; i < kImageMethodsCount; ++i) { |
| 93 | image_methods_[i] += delta; |
| 94 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 97 | bool ImageHeader::IsValid() const { |
| 98 | if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) { |
| 99 | return false; |
| 100 | } |
| 101 | if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) { |
| 102 | return false; |
| 103 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 104 | // Unsigned so wraparound is well defined. |
| 105 | if (image_begin_ >= image_begin_ + image_size_) { |
| 106 | return false; |
| 107 | } |
| 108 | if (oat_file_begin_ > oat_file_end_) { |
| 109 | return false; |
| 110 | } |
| 111 | if (oat_data_begin_ > oat_data_end_) { |
| 112 | return false; |
| 113 | } |
| 114 | if (oat_file_begin_ >= oat_data_begin_) { |
| 115 | return false; |
| 116 | } |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
| 120 | const char* ImageHeader::GetMagic() const { |
| 121 | CHECK(IsValid()); |
| 122 | return reinterpret_cast<const char*>(magic_); |
| 123 | } |
| 124 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 125 | ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const { |
| 126 | CHECK_LT(static_cast<size_t>(index), kImageMethodsCount); |
| 127 | return reinterpret_cast<ArtMethod*>(image_methods_[index]); |
| 128 | } |
| 129 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 130 | std::ostream& operator<<(std::ostream& os, const ImageSection& section) { |
| 131 | return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End(); |
| 132 | } |
| 133 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 134 | void ImageHeader::VisitObjects(ObjectVisitor* visitor, |
| 135 | uint8_t* base, |
| 136 | PointerSize pointer_size) const { |
| 137 | DCHECK_EQ(pointer_size, GetPointerSize()); |
| 138 | const ImageSection& objects = GetObjectsSection(); |
| 139 | static const size_t kStartPos = RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 140 | for (size_t pos = kStartPos; pos < objects.Size(); ) { |
| 141 | mirror::Object* object = reinterpret_cast<mirror::Object*>(base + objects.Offset() + pos); |
| 142 | visitor->Visit(object); |
| 143 | pos += RoundUp(object->SizeOf(), kObjectAlignment); |
| 144 | } |
| 145 | } |
| 146 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 147 | PointerSize ImageHeader::GetPointerSize() const { |
| 148 | return ConvertToPointerSize(pointer_size_); |
| 149 | } |
| 150 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 151 | } // namespace art |