blob: 8df17c6929d1d4d16c0a6e915f10f288d4526192 [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 Chartier52a7f5c2015-08-18 18:35:52 -070027const uint8_t ImageHeader::kImageVersion[] = { '0', '2', '0', '\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 Chartiere401d142015-04-22 13:56:20 -070038 uint32_t pointer_size,
Igor Murashkin46774762014-10-22 11:37:02 -070039 bool compile_pic)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 : image_begin_(image_begin),
Mathieu Chartier31e89252013-08-28 11:29:12 -070041 image_size_(image_size),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 oat_checksum_(oat_checksum),
43 oat_file_begin_(oat_file_begin),
44 oat_data_begin_(oat_data_begin),
45 oat_data_end_(oat_data_end),
46 oat_file_end_(oat_file_end),
Alex Light53cb16b2014-06-12 11:26:29 -070047 patch_delta_(0),
Igor Murashkin46774762014-10-22 11:37:02 -070048 image_roots_(image_roots),
Mathieu Chartiere401d142015-04-22 13:56:20 -070049 pointer_size_(pointer_size),
Igor Murashkin46774762014-10-22 11:37:02 -070050 compile_pic_(compile_pic) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
52 CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
53 CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
54 CHECK_LT(image_begin, image_roots);
55 CHECK_LT(image_roots, oat_file_begin);
56 CHECK_LE(oat_file_begin, oat_data_begin);
57 CHECK_LT(oat_data_begin, oat_data_end);
58 CHECK_LE(oat_data_end, oat_file_end);
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 CHECK(ValidPointerSize(pointer_size_)) << pointer_size_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
61 memcpy(version_, kImageVersion, sizeof(kImageVersion));
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 std::copy_n(sections, kSectionCount, sections_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063}
64
Alex Light53cb16b2014-06-12 11:26:29 -070065void ImageHeader::RelocateImage(off_t delta) {
66 CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned";
67 image_begin_ += delta;
68 oat_file_begin_ += delta;
69 oat_data_begin_ += delta;
70 oat_data_end_ += delta;
71 oat_file_end_ += delta;
72 image_roots_ += delta;
73 patch_delta_ += delta;
Mathieu Chartiere401d142015-04-22 13:56:20 -070074 for (size_t i = 0; i < kImageMethodsCount; ++i) {
75 image_methods_[i] += delta;
76 }
Alex Light53cb16b2014-06-12 11:26:29 -070077}
78
Brian Carlstrom68708f52013-09-03 14:15:31 -070079bool ImageHeader::IsValid() const {
80 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) {
81 return false;
82 }
83 if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) {
84 return false;
85 }
Alex Light53cb16b2014-06-12 11:26:29 -070086 // Unsigned so wraparound is well defined.
87 if (image_begin_ >= image_begin_ + image_size_) {
88 return false;
89 }
90 if (oat_file_begin_ > oat_file_end_) {
91 return false;
92 }
93 if (oat_data_begin_ > oat_data_end_) {
94 return false;
95 }
96 if (oat_file_begin_ >= oat_data_begin_) {
97 return false;
98 }
99 if (image_roots_ <= image_begin_ || oat_file_begin_ <= image_roots_) {
100 return false;
101 }
102 if (!IsAligned<kPageSize>(patch_delta_)) {
103 return false;
104 }
Brian Carlstrom68708f52013-09-03 14:15:31 -0700105 return true;
106}
107
108const char* ImageHeader::GetMagic() const {
109 CHECK(IsValid());
110 return reinterpret_cast<const char*>(magic_);
111}
112
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113mirror::Object* ImageHeader::GetImageRoot(ImageRoot image_root) const {
114 return GetImageRoots()->Get(image_root);
115}
116
117mirror::ObjectArray<mirror::Object>* ImageHeader::GetImageRoots() const {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800118 // Need a read barrier as it's not visited during root scan.
119 // Pass in the address of the local variable to the read barrier
120 // rather than image_roots_ because it won't move (asserted below)
121 // and it's a const member.
122 mirror::ObjectArray<mirror::Object>* image_roots =
123 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(image_roots_);
124 mirror::ObjectArray<mirror::Object>* result =
125 ReadBarrier::BarrierForRoot<mirror::ObjectArray<mirror::Object>, kWithReadBarrier, true>(
126 &image_roots);
127 DCHECK_EQ(image_roots, result);
128 return result;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129}
130
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const {
132 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
133 return reinterpret_cast<ArtMethod*>(image_methods_[index]);
134}
135
136void 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
141const ImageSection& ImageHeader::GetImageSection(ImageSections index) const {
142 CHECK_LT(static_cast<size_t>(index), kSectionCount);
143 return sections_[index];
144}
145
146std::ostream& operator<<(std::ostream& os, const ImageSection& section) {
147 return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End();
148}
149
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700150void ImageSection::VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const {
151 for (size_t pos = 0; pos < Size(); ) {
152 auto* array = reinterpret_cast<LengthPrefixedArray<ArtField>*>(base + Offset() + pos);
153 for (size_t i = 0; i < array->Length(); ++i) {
154 visitor->Visit(&array->At(i, sizeof(ArtField)));
155 }
Vladimir Markocf36d492015-08-12 19:27:26 +0100156 pos += array->ComputeSize(array->Length());
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700157 }
158}
159
160void ImageSection::VisitPackedArtMethods(ArtMethodVisitor* visitor,
161 uint8_t* base,
Vladimir Markocf36d492015-08-12 19:27:26 +0100162 size_t pointer_size) const {
Vladimir Marko14632852015-08-17 12:07:23 +0100163 const size_t method_alignment = ArtMethod::Alignment(pointer_size);
164 const size_t method_size = ArtMethod::Size(pointer_size);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700165 for (size_t pos = 0; pos < Size(); ) {
166 auto* array = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(base + Offset() + pos);
167 for (size_t i = 0; i < array->Length(); ++i) {
Vladimir Markocf36d492015-08-12 19:27:26 +0100168 visitor->Visit(&array->At(i, method_size, method_alignment));
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700169 }
Vladimir Markocf36d492015-08-12 19:27:26 +0100170 pos += array->ComputeSize(array->Length(), method_size, method_alignment);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700171 }
172}
173
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700174} // namespace art