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" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "mirror/object_array.h" |
| 21 | #include "mirror/object_array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 22 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 26 | const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' }; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 27 | const uint8_t ImageHeader::kImageVersion[] = { '0', '3', '0', '\0' }; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 28 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | ImageHeader::ImageHeader(uint32_t image_begin, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 30 | uint32_t image_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 31 | ImageSection* sections, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | uint32_t image_roots, |
| 33 | uint32_t oat_checksum, |
| 34 | uint32_t oat_file_begin, |
| 35 | uint32_t oat_data_begin, |
| 36 | uint32_t oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 37 | uint32_t oat_file_end, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 38 | uint32_t boot_image_begin, |
| 39 | uint32_t boot_image_size, |
| 40 | uint32_t boot_oat_begin, |
| 41 | uint32_t boot_oat_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 42 | uint32_t pointer_size, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 43 | bool compile_pic, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 44 | bool is_pic, |
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), |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | oat_checksum_(oat_checksum), |
| 50 | oat_file_begin_(oat_file_begin), |
| 51 | oat_data_begin_(oat_data_begin), |
| 52 | oat_data_end_(oat_data_end), |
| 53 | oat_file_end_(oat_file_end), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 54 | boot_image_begin_(boot_image_begin), |
| 55 | boot_image_size_(boot_image_size), |
| 56 | boot_oat_begin_(boot_oat_begin), |
| 57 | boot_oat_size_(boot_oat_size), |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 58 | patch_delta_(0), |
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 | compile_pic_(compile_pic), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 62 | is_pic_(is_pic), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 63 | storage_mode_(storage_mode), |
| 64 | data_size_(data_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize)); |
| 66 | CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize)); |
| 67 | CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | CHECK_LT(image_roots, oat_file_begin); |
| 69 | CHECK_LE(oat_file_begin, oat_data_begin); |
| 70 | CHECK_LT(oat_data_begin, oat_data_end); |
| 71 | CHECK_LE(oat_data_end, oat_file_end); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 72 | CHECK(ValidPointerSize(pointer_size_)) << pointer_size_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | memcpy(magic_, kImageMagic, sizeof(kImageMagic)); |
| 74 | memcpy(version_, kImageVersion, sizeof(kImageVersion)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 75 | std::copy_n(sections, kSectionCount, sections_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 78 | void ImageHeader::RelocateImage(off_t delta) { |
| 79 | CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 80 | oat_file_begin_ += delta; |
| 81 | oat_data_begin_ += delta; |
| 82 | oat_data_end_ += delta; |
| 83 | oat_file_end_ += delta; |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 84 | patch_delta_ += delta; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 85 | RelocateImageObjects(delta); |
| 86 | RelocateImageMethods(delta); |
| 87 | } |
| 88 | |
| 89 | void ImageHeader::RelocateImageObjects(off_t delta) { |
| 90 | image_begin_ += delta; |
| 91 | image_roots_ += delta; |
| 92 | } |
| 93 | |
| 94 | void ImageHeader::RelocateImageMethods(off_t delta) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 95 | for (size_t i = 0; i < kImageMethodsCount; ++i) { |
| 96 | image_methods_[i] += delta; |
| 97 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 100 | bool ImageHeader::IsValid() const { |
| 101 | if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) { |
| 102 | return false; |
| 103 | } |
| 104 | if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) { |
| 105 | return false; |
| 106 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 107 | // Unsigned so wraparound is well defined. |
| 108 | if (image_begin_ >= image_begin_ + image_size_) { |
| 109 | return false; |
| 110 | } |
| 111 | if (oat_file_begin_ > oat_file_end_) { |
| 112 | return false; |
| 113 | } |
| 114 | if (oat_data_begin_ > oat_data_end_) { |
| 115 | return false; |
| 116 | } |
| 117 | if (oat_file_begin_ >= oat_data_begin_) { |
| 118 | return false; |
| 119 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 120 | if (!IsAligned<kPageSize>(patch_delta_)) { |
| 121 | return false; |
| 122 | } |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
| 126 | const char* ImageHeader::GetMagic() const { |
| 127 | CHECK(IsValid()); |
| 128 | return reinterpret_cast<const char*>(magic_); |
| 129 | } |
| 130 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 131 | ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const { |
| 132 | CHECK_LT(static_cast<size_t>(index), kImageMethodsCount); |
| 133 | return reinterpret_cast<ArtMethod*>(image_methods_[index]); |
| 134 | } |
| 135 | |
| 136 | void ImageHeader::SetImageMethod(ImageMethod index, ArtMethod* method) { |
| 137 | CHECK_LT(static_cast<size_t>(index), kImageMethodsCount); |
| 138 | image_methods_[index] = reinterpret_cast<uint64_t>(method); |
| 139 | } |
| 140 | |
| 141 | const ImageSection& ImageHeader::GetImageSection(ImageSections index) const { |
| 142 | CHECK_LT(static_cast<size_t>(index), kSectionCount); |
| 143 | return sections_[index]; |
| 144 | } |
| 145 | |
| 146 | std::ostream& operator<<(std::ostream& os, const ImageSection& section) { |
| 147 | return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End(); |
| 148 | } |
| 149 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 150 | void ImageHeader::VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const { |
| 151 | const ImageSection& fields = GetFieldsSection(); |
| 152 | for (size_t pos = 0; pos < fields.Size(); ) { |
| 153 | auto* array = reinterpret_cast<LengthPrefixedArray<ArtField>*>(base + fields.Offset() + pos); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 154 | for (size_t i = 0; i < array->size(); ++i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 155 | visitor->Visit(&array->At(i, sizeof(ArtField))); |
| 156 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 157 | pos += array->ComputeSize(array->size()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 161 | void ImageHeader::VisitPackedArtMethods(ArtMethodVisitor* visitor, |
| 162 | uint8_t* base, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 163 | PointerSize pointer_size) const { |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 164 | const size_t method_alignment = ArtMethod::Alignment(pointer_size); |
| 165 | const size_t method_size = ArtMethod::Size(pointer_size); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 166 | const ImageSection& methods = GetMethodsSection(); |
| 167 | for (size_t pos = 0; pos < methods.Size(); ) { |
| 168 | auto* array = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(base + methods.Offset() + pos); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 169 | for (size_t i = 0; i < array->size(); ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 170 | visitor->Visit(&array->At(i, method_size, method_alignment)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 171 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 172 | pos += array->ComputeSize(array->size(), method_size, method_alignment); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 173 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 174 | const ImageSection& runtime_methods = GetRuntimeMethodsSection(); |
| 175 | for (size_t pos = 0; pos < runtime_methods.Size(); ) { |
| 176 | auto* method = reinterpret_cast<ArtMethod*>(base + runtime_methods.Offset() + pos); |
| 177 | visitor->Visit(method); |
| 178 | pos += method_size; |
| 179 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 182 | } // namespace art |