blob: 99406229a5abcae516605c392e9a5c5b4b8a2a28 [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"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080020#include "base/length_prefixed_array.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include "mirror/object_array-inl.h"
23#include "mirror/object_array.h"
Andreas Gampebda1d602016-08-29 17:43:45 -070024#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070026namespace art {
27
Ian Rogers13735952014-10-08 12:43:28 -070028const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' };
Vladimir Marko305c38b2018-02-14 11:50:07 +000029const uint8_t ImageHeader::kImageVersion[] = { '0', '5', '5', '\0' }; // Bitstring type check off.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070030
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031ImageHeader::ImageHeader(uint32_t image_begin,
Mathieu Chartier31e89252013-08-28 11:29:12 -070032 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -070033 ImageSection* sections,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034 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 Murashkin46774762014-10-22 11:37:02 -070039 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080040 uint32_t boot_image_begin,
41 uint32_t boot_image_size,
42 uint32_t boot_oat_begin,
43 uint32_t boot_oat_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -070044 uint32_t pointer_size,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080045 bool compile_pic,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080046 bool is_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080047 StorageMode storage_mode,
48 size_t data_size)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 : image_begin_(image_begin),
Mathieu Chartier31e89252013-08-28 11:29:12 -070050 image_size_(image_size),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 oat_checksum_(oat_checksum),
52 oat_file_begin_(oat_file_begin),
53 oat_data_begin_(oat_data_begin),
54 oat_data_end_(oat_data_end),
55 oat_file_end_(oat_file_end),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080056 boot_image_begin_(boot_image_begin),
57 boot_image_size_(boot_image_size),
58 boot_oat_begin_(boot_oat_begin),
59 boot_oat_size_(boot_oat_size),
Alex Light53cb16b2014-06-12 11:26:29 -070060 patch_delta_(0),
Igor Murashkin46774762014-10-22 11:37:02 -070061 image_roots_(image_roots),
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 pointer_size_(pointer_size),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080063 compile_pic_(compile_pic),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080064 is_pic_(is_pic),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080065 storage_mode_(storage_mode),
66 data_size_(data_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
68 CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
69 CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 CHECK_LT(image_roots, oat_file_begin);
71 CHECK_LE(oat_file_begin, oat_data_begin);
72 CHECK_LT(oat_data_begin, oat_data_end);
73 CHECK_LE(oat_data_end, oat_file_end);
Mathieu Chartiere401d142015-04-22 13:56:20 -070074 CHECK(ValidPointerSize(pointer_size_)) << pointer_size_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
76 memcpy(version_, kImageVersion, sizeof(kImageVersion));
Mathieu Chartiere401d142015-04-22 13:56:20 -070077 std::copy_n(sections, kSectionCount, sections_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078}
79
Alex Light53cb16b2014-06-12 11:26:29 -070080void ImageHeader::RelocateImage(off_t delta) {
81 CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned";
Alex Light53cb16b2014-06-12 11:26:29 -070082 oat_file_begin_ += delta;
83 oat_data_begin_ += delta;
84 oat_data_end_ += delta;
85 oat_file_end_ += delta;
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +000086 patch_delta_ += delta;
Mathieu Chartierfbc31082016-01-24 11:59:56 -080087 RelocateImageObjects(delta);
88 RelocateImageMethods(delta);
89}
90
91void ImageHeader::RelocateImageObjects(off_t delta) {
92 image_begin_ += delta;
93 image_roots_ += delta;
94}
95
96void ImageHeader::RelocateImageMethods(off_t delta) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070097 for (size_t i = 0; i < kImageMethodsCount; ++i) {
98 image_methods_[i] += delta;
99 }
Alex Light53cb16b2014-06-12 11:26:29 -0700100}
101
Brian Carlstrom68708f52013-09-03 14:15:31 -0700102bool ImageHeader::IsValid() const {
103 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) {
104 return false;
105 }
106 if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) {
107 return false;
108 }
Alex Light53cb16b2014-06-12 11:26:29 -0700109 // Unsigned so wraparound is well defined.
110 if (image_begin_ >= image_begin_ + image_size_) {
111 return false;
112 }
113 if (oat_file_begin_ > oat_file_end_) {
114 return false;
115 }
116 if (oat_data_begin_ > oat_data_end_) {
117 return false;
118 }
119 if (oat_file_begin_ >= oat_data_begin_) {
120 return false;
121 }
Alex Light53cb16b2014-06-12 11:26:29 -0700122 if (!IsAligned<kPageSize>(patch_delta_)) {
123 return false;
124 }
Brian Carlstrom68708f52013-09-03 14:15:31 -0700125 return true;
126}
127
128const char* ImageHeader::GetMagic() const {
129 CHECK(IsValid());
130 return reinterpret_cast<const char*>(magic_);
131}
132
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const {
134 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
135 return reinterpret_cast<ArtMethod*>(image_methods_[index]);
136}
137
138void ImageHeader::SetImageMethod(ImageMethod index, ArtMethod* method) {
139 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
140 image_methods_[index] = reinterpret_cast<uint64_t>(method);
141}
142
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143std::ostream& operator<<(std::ostream& os, const ImageSection& section) {
144 return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End();
145}
146
David Sehra49e0532017-08-25 08:05:29 -0700147void ImageHeader::VisitObjects(ObjectVisitor* visitor,
148 uint8_t* base,
149 PointerSize pointer_size) const {
150 DCHECK_EQ(pointer_size, GetPointerSize());
151 const ImageSection& objects = GetObjectsSection();
152 static const size_t kStartPos = RoundUp(sizeof(ImageHeader), kObjectAlignment);
153 for (size_t pos = kStartPos; pos < objects.Size(); ) {
154 mirror::Object* object = reinterpret_cast<mirror::Object*>(base + objects.Offset() + pos);
155 visitor->Visit(object);
156 pos += RoundUp(object->SizeOf(), kObjectAlignment);
157 }
158}
159
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700160void ImageHeader::VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const {
161 const ImageSection& fields = GetFieldsSection();
162 for (size_t pos = 0; pos < fields.Size(); ) {
163 auto* array = reinterpret_cast<LengthPrefixedArray<ArtField>*>(base + fields.Offset() + pos);
Vladimir Marko35831e82015-09-11 11:59:18 +0100164 for (size_t i = 0; i < array->size(); ++i) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700165 visitor->Visit(&array->At(i, sizeof(ArtField)));
166 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100167 pos += array->ComputeSize(array->size());
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700168 }
169}
170
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700171void ImageHeader::VisitPackedArtMethods(ArtMethodVisitor* visitor,
172 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700173 PointerSize pointer_size) const {
Vladimir Marko14632852015-08-17 12:07:23 +0100174 const size_t method_alignment = ArtMethod::Alignment(pointer_size);
175 const size_t method_size = ArtMethod::Size(pointer_size);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700176 const ImageSection& methods = GetMethodsSection();
177 for (size_t pos = 0; pos < methods.Size(); ) {
178 auto* array = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(base + methods.Offset() + pos);
Vladimir Marko35831e82015-09-11 11:59:18 +0100179 for (size_t i = 0; i < array->size(); ++i) {
Vladimir Markocf36d492015-08-12 19:27:26 +0100180 visitor->Visit(&array->At(i, method_size, method_alignment));
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700181 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100182 pos += array->ComputeSize(array->size(), method_size, method_alignment);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700183 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700184 const ImageSection& runtime_methods = GetRuntimeMethodsSection();
185 for (size_t pos = 0; pos < runtime_methods.Size(); ) {
186 auto* method = reinterpret_cast<ArtMethod*>(base + runtime_methods.Offset() + pos);
187 visitor->Visit(method);
188 pos += method_size;
189 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700190}
191
Andreas Gampebda1d602016-08-29 17:43:45 -0700192PointerSize ImageHeader::GetPointerSize() const {
193 return ConvertToPointerSize(pointer_size_);
194}
195
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700196} // namespace art