blob: 2ef60c3e07c56564c519034500ad186bb02438bb [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"
Andreas Gampebda1d602016-08-29 17:43:45 -070023#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070025namespace art {
26
Ian Rogers13735952014-10-08 12:43:28 -070027const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' };
Nicolas Geoffrayf9bf2502016-12-14 14:59:04 +000028const uint8_t ImageHeader::kImageVersion[] = { '0', '3', '4', '\0' }; // mirror::Class update
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070029
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030ImageHeader::ImageHeader(uint32_t image_begin,
Mathieu Chartier31e89252013-08-28 11:29:12 -070031 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -070032 ImageSection* sections,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 uint32_t image_roots,
34 uint32_t oat_checksum,
35 uint32_t oat_file_begin,
36 uint32_t oat_data_begin,
37 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -070038 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080039 uint32_t boot_image_begin,
40 uint32_t boot_image_size,
41 uint32_t boot_oat_begin,
42 uint32_t boot_oat_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 uint32_t pointer_size,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080044 bool compile_pic,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080045 bool is_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080046 StorageMode storage_mode,
47 size_t data_size)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 : image_begin_(image_begin),
Mathieu Chartier31e89252013-08-28 11:29:12 -070049 image_size_(image_size),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 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 Chartierfbc31082016-01-24 11:59:56 -080055 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),
Alex Light53cb16b2014-06-12 11:26:29 -070059 patch_delta_(0),
Igor Murashkin46774762014-10-22 11:37:02 -070060 image_roots_(image_roots),
Mathieu Chartiere401d142015-04-22 13:56:20 -070061 pointer_size_(pointer_size),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080062 compile_pic_(compile_pic),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080063 is_pic_(is_pic),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080064 storage_mode_(storage_mode),
65 data_size_(data_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
67 CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
68 CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 CHECK_LT(image_roots, oat_file_begin);
70 CHECK_LE(oat_file_begin, oat_data_begin);
71 CHECK_LT(oat_data_begin, oat_data_end);
72 CHECK_LE(oat_data_end, oat_file_end);
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 CHECK(ValidPointerSize(pointer_size_)) << pointer_size_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
75 memcpy(version_, kImageVersion, sizeof(kImageVersion));
Mathieu Chartiere401d142015-04-22 13:56:20 -070076 std::copy_n(sections, kSectionCount, sections_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077}
78
Alex Light53cb16b2014-06-12 11:26:29 -070079void ImageHeader::RelocateImage(off_t delta) {
80 CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned";
Alex Light53cb16b2014-06-12 11:26:29 -070081 oat_file_begin_ += delta;
82 oat_data_begin_ += delta;
83 oat_data_end_ += delta;
84 oat_file_end_ += delta;
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +000085 patch_delta_ += delta;
Mathieu Chartierfbc31082016-01-24 11:59:56 -080086 RelocateImageObjects(delta);
87 RelocateImageMethods(delta);
88}
89
90void ImageHeader::RelocateImageObjects(off_t delta) {
91 image_begin_ += delta;
92 image_roots_ += delta;
93}
94
95void ImageHeader::RelocateImageMethods(off_t delta) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070096 for (size_t i = 0; i < kImageMethodsCount; ++i) {
97 image_methods_[i] += delta;
98 }
Alex Light53cb16b2014-06-12 11:26:29 -070099}
100
Brian Carlstrom68708f52013-09-03 14:15:31 -0700101bool ImageHeader::IsValid() const {
102 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) {
103 return false;
104 }
105 if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) {
106 return false;
107 }
Alex Light53cb16b2014-06-12 11:26:29 -0700108 // Unsigned so wraparound is well defined.
109 if (image_begin_ >= image_begin_ + image_size_) {
110 return false;
111 }
112 if (oat_file_begin_ > oat_file_end_) {
113 return false;
114 }
115 if (oat_data_begin_ > oat_data_end_) {
116 return false;
117 }
118 if (oat_file_begin_ >= oat_data_begin_) {
119 return false;
120 }
Alex Light53cb16b2014-06-12 11:26:29 -0700121 if (!IsAligned<kPageSize>(patch_delta_)) {
122 return false;
123 }
Brian Carlstrom68708f52013-09-03 14:15:31 -0700124 return true;
125}
126
127const char* ImageHeader::GetMagic() const {
128 CHECK(IsValid());
129 return reinterpret_cast<const char*>(magic_);
130}
131
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132ArtMethod* ImageHeader::GetImageMethod(ImageMethod index) const {
133 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
134 return reinterpret_cast<ArtMethod*>(image_methods_[index]);
135}
136
137void ImageHeader::SetImageMethod(ImageMethod index, ArtMethod* method) {
138 CHECK_LT(static_cast<size_t>(index), kImageMethodsCount);
139 image_methods_[index] = reinterpret_cast<uint64_t>(method);
140}
141
142const ImageSection& ImageHeader::GetImageSection(ImageSections index) const {
143 CHECK_LT(static_cast<size_t>(index), kSectionCount);
144 return sections_[index];
145}
146
147std::ostream& operator<<(std::ostream& os, const ImageSection& section) {
148 return os << "size=" << section.Size() << " range=" << section.Offset() << "-" << section.End();
149}
150
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700151void ImageHeader::VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const {
152 const ImageSection& fields = GetFieldsSection();
153 for (size_t pos = 0; pos < fields.Size(); ) {
154 auto* array = reinterpret_cast<LengthPrefixedArray<ArtField>*>(base + fields.Offset() + pos);
Vladimir Marko35831e82015-09-11 11:59:18 +0100155 for (size_t i = 0; i < array->size(); ++i) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700156 visitor->Visit(&array->At(i, sizeof(ArtField)));
157 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100158 pos += array->ComputeSize(array->size());
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700159 }
160}
161
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700162void ImageHeader::VisitPackedArtMethods(ArtMethodVisitor* visitor,
163 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700164 PointerSize pointer_size) const {
Vladimir Marko14632852015-08-17 12:07:23 +0100165 const size_t method_alignment = ArtMethod::Alignment(pointer_size);
166 const size_t method_size = ArtMethod::Size(pointer_size);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700167 const ImageSection& methods = GetMethodsSection();
168 for (size_t pos = 0; pos < methods.Size(); ) {
169 auto* array = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(base + methods.Offset() + pos);
Vladimir Marko35831e82015-09-11 11:59:18 +0100170 for (size_t i = 0; i < array->size(); ++i) {
Vladimir Markocf36d492015-08-12 19:27:26 +0100171 visitor->Visit(&array->At(i, method_size, method_alignment));
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700172 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100173 pos += array->ComputeSize(array->size(), method_size, method_alignment);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700174 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700175 const ImageSection& runtime_methods = GetRuntimeMethodsSection();
176 for (size_t pos = 0; pos < runtime_methods.Size(); ) {
177 auto* method = reinterpret_cast<ArtMethod*>(base + runtime_methods.Offset() + pos);
178 visitor->Visit(method);
179 pos += method_size;
180 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700181}
182
Andreas Gampebda1d602016-08-29 17:43:45 -0700183PointerSize ImageHeader::GetPointerSize() const {
184 return ConvertToPointerSize(pointer_size_);
185}
186
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700187} // namespace art