blob: 3cb25807c671c74a69468a08d12732bc9608542c [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/object_array.h"
20#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "utils.h"
23
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' };
Andreas Gampef52857f2015-02-18 15:38:57 -080027const uint8_t ImageHeader::kImageVersion[] = { '0', '1', '4', '\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,
31 uint32_t image_bitmap_offset,
32 uint32_t image_bitmap_size,
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,
39 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),
42 image_bitmap_offset_(image_bitmap_offset),
43 image_bitmap_size_(image_bitmap_size),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 oat_checksum_(oat_checksum),
45 oat_file_begin_(oat_file_begin),
46 oat_data_begin_(oat_data_begin),
47 oat_data_end_(oat_data_end),
48 oat_file_end_(oat_file_end),
Alex Light53cb16b2014-06-12 11:26:29 -070049 patch_delta_(0),
Igor Murashkin46774762014-10-22 11:37:02 -070050 image_roots_(image_roots),
51 compile_pic_(compile_pic) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
53 CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
54 CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
55 CHECK_LT(image_begin, image_roots);
56 CHECK_LT(image_roots, oat_file_begin);
57 CHECK_LE(oat_file_begin, oat_data_begin);
58 CHECK_LT(oat_data_begin, oat_data_end);
59 CHECK_LE(oat_data_end, oat_file_end);
60 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
61 memcpy(version_, kImageVersion, sizeof(kImageVersion));
62}
63
Alex Light53cb16b2014-06-12 11:26:29 -070064void ImageHeader::RelocateImage(off_t delta) {
65 CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned";
66 image_begin_ += delta;
67 oat_file_begin_ += delta;
68 oat_data_begin_ += delta;
69 oat_data_end_ += delta;
70 oat_file_end_ += delta;
71 image_roots_ += delta;
72 patch_delta_ += delta;
73}
74
Brian Carlstrom68708f52013-09-03 14:15:31 -070075bool ImageHeader::IsValid() const {
76 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) {
77 return false;
78 }
79 if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) {
80 return false;
81 }
Alex Light53cb16b2014-06-12 11:26:29 -070082 // Unsigned so wraparound is well defined.
83 if (image_begin_ >= image_begin_ + image_size_) {
84 return false;
85 }
86 if (oat_file_begin_ > oat_file_end_) {
87 return false;
88 }
89 if (oat_data_begin_ > oat_data_end_) {
90 return false;
91 }
92 if (oat_file_begin_ >= oat_data_begin_) {
93 return false;
94 }
95 if (image_roots_ <= image_begin_ || oat_file_begin_ <= image_roots_) {
96 return false;
97 }
98 if (!IsAligned<kPageSize>(patch_delta_)) {
99 return false;
100 }
Brian Carlstrom68708f52013-09-03 14:15:31 -0700101 return true;
102}
103
104const char* ImageHeader::GetMagic() const {
105 CHECK(IsValid());
106 return reinterpret_cast<const char*>(magic_);
107}
108
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109mirror::Object* ImageHeader::GetImageRoot(ImageRoot image_root) const {
110 return GetImageRoots()->Get(image_root);
111}
112
113mirror::ObjectArray<mirror::Object>* ImageHeader::GetImageRoots() const {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800114 // Need a read barrier as it's not visited during root scan.
115 // Pass in the address of the local variable to the read barrier
116 // rather than image_roots_ because it won't move (asserted below)
117 // and it's a const member.
118 mirror::ObjectArray<mirror::Object>* image_roots =
119 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(image_roots_);
120 mirror::ObjectArray<mirror::Object>* result =
121 ReadBarrier::BarrierForRoot<mirror::ObjectArray<mirror::Object>, kWithReadBarrier, true>(
122 &image_roots);
123 DCHECK_EQ(image_roots, result);
124 return result;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125}
126
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700127} // namespace art