blob: 2fed4d3bd3b361c39d7b34105f3e93e298df142a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom4a289ed2011-08-16 17:17:49 -070016
17#include "image.h"
18
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/bit_utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/object_array.h"
21#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024namespace art {
25
Ian Rogers13735952014-10-08 12:43:28 -070026const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' };
Mathieu Chartier08707702016-01-22 15:20:23 -080027const uint8_t ImageHeader::kImageVersion[] = { '0', '2', '6', '\0' };
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070028
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029ImageHeader::ImageHeader(uint32_t image_begin,
Mathieu Chartier31e89252013-08-28 11:29:12 -070030 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -070031 ImageSection* sections,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032 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 Murashkin46774762014-10-22 11:37:02 -070037 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080038 uint32_t boot_image_begin,
39 uint32_t boot_image_size,
40 uint32_t boot_oat_begin,
41 uint32_t boot_oat_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -070042 uint32_t pointer_size,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080043 bool compile_pic,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080044 bool is_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080045 StorageMode storage_mode,
46 size_t data_size)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 : image_begin_(image_begin),
Mathieu Chartier31e89252013-08-28 11:29:12 -070048 image_size_(image_size),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 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 Chartierfbc31082016-01-24 11:59:56 -080054 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 Light53cb16b2014-06-12 11:26:29 -070058 patch_delta_(0),
Igor Murashkin46774762014-10-22 11:37:02 -070059 image_roots_(image_roots),
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 pointer_size_(pointer_size),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080061 compile_pic_(compile_pic),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080062 is_pic_(is_pic),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080063 storage_mode_(storage_mode),
64 data_size_(data_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 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 Rogers2dd0e2c2013-01-24 12:42:14 -080068 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 Chartiere401d142015-04-22 13:56:20 -070072 CHECK(ValidPointerSize(pointer_size_)) << pointer_size_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
74 memcpy(version_, kImageVersion, sizeof(kImageVersion));
Mathieu Chartiere401d142015-04-22 13:56:20 -070075 std::copy_n(sections, kSectionCount, sections_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076}
77
Alex Light53cb16b2014-06-12 11:26:29 -070078void ImageHeader::RelocateImage(off_t delta) {
79 CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned";
Alex Light53cb16b2014-06-12 11:26:29 -070080 oat_file_begin_ += delta;
81 oat_data_begin_ += delta;
82 oat_data_end_ += delta;
83 oat_file_end_ += delta;
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +000084 patch_delta_ += delta;
Mathieu Chartierfbc31082016-01-24 11:59:56 -080085 RelocateImageObjects(delta);
86 RelocateImageMethods(delta);
87}
88
89void ImageHeader::RelocateImageObjects(off_t delta) {
90 image_begin_ += delta;
91 image_roots_ += delta;
92}
93
94void ImageHeader::RelocateImageMethods(off_t delta) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070095 for (size_t i = 0; i < kImageMethodsCount; ++i) {
96 image_methods_[i] += delta;
97 }
Alex Light53cb16b2014-06-12 11:26:29 -070098}
99
Brian Carlstrom68708f52013-09-03 14:15:31 -0700100bool 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 Light53cb16b2014-06-12 11:26:29 -0700107 // 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 Light53cb16b2014-06-12 11:26:29 -0700120 if (!IsAligned<kPageSize>(patch_delta_)) {
121 return false;
122 }
Brian Carlstrom68708f52013-09-03 14:15:31 -0700123 return true;
124}
125
126const char* ImageHeader::GetMagic() const {
127 CHECK(IsValid());
128 return reinterpret_cast<const char*>(magic_);
129}
130
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131mirror::Object* ImageHeader::GetImageRoot(ImageRoot image_root) const {
132 return GetImageRoots()->Get(image_root);
133}
134
135mirror::ObjectArray<mirror::Object>* ImageHeader::GetImageRoots() const {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800136 // Need a read barrier as it's not visited during root scan.
137 // Pass in the address of the local variable to the read barrier
138 // rather than image_roots_ because it won't move (asserted below)
139 // and it's a const member.
140 mirror::ObjectArray<mirror::Object>* image_roots =
141 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(image_roots_);
142 mirror::ObjectArray<mirror::Object>* result =
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -0800143 ReadBarrier::BarrierForRoot<mirror::ObjectArray<mirror::Object>, kWithReadBarrier>(
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800144 &image_roots);
145 DCHECK_EQ(image_roots, result);
146 return result;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147}
148
Mathieu Chartiere401d142015-04-22 13:56:20 -0700149ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const {
150 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
151 return reinterpret_cast<ArtMethod*>(image_methods_[index]);
152}
153
154void ImageHeader::SetImageMethod(ImageMethod index, ArtMethod* method) {
155 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
156 image_methods_[index] = reinterpret_cast<uint64_t>(method);
157}
158
159const ImageSection& ImageHeader::GetImageSection(ImageSections index) const {
160 CHECK_LT(static_cast<size_t>(index), kSectionCount);
161 return sections_[index];
162}
163
164std::ostream& operator<<(std::ostream& os, const ImageSection& section) {
165 return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End();
166}
167
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700168void ImageSection::VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const {
169 for (size_t pos = 0; pos < Size(); ) {
170 auto* array = reinterpret_cast<LengthPrefixedArray<ArtField>*>(base + Offset() + pos);
Vladimir Marko35831e82015-09-11 11:59:18 +0100171 for (size_t i = 0; i < array->size(); ++i) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700172 visitor->Visit(&array->At(i, sizeof(ArtField)));
173 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100174 pos += array->ComputeSize(array->size());
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700175 }
176}
177
178void ImageSection::VisitPackedArtMethods(ArtMethodVisitor* visitor,
179 uint8_t* base,
Vladimir Markocf36d492015-08-12 19:27:26 +0100180 size_t pointer_size) const {
Vladimir Marko14632852015-08-17 12:07:23 +0100181 const size_t method_alignment = ArtMethod::Alignment(pointer_size);
182 const size_t method_size = ArtMethod::Size(pointer_size);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700183 for (size_t pos = 0; pos < Size(); ) {
184 auto* array = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(base + Offset() + pos);
Vladimir Marko35831e82015-09-11 11:59:18 +0100185 for (size_t i = 0; i < array->size(); ++i) {
Vladimir Markocf36d492015-08-12 19:27:26 +0100186 visitor->Visit(&array->At(i, method_size, method_alignment));
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700187 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100188 pos += array->ComputeSize(array->size(), method_size, method_alignment);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700189 }
190}
191
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700192} // namespace art