blob: 2b1210b5b1c8a2c5e3b250e4c28738a0d367c085 [file] [log] [blame]
Alex Light53cb16b2014-06-12 11:26:29 -07001/*
2 * Copyright (C) 2014 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 */
16
17#ifndef ART_PATCHOAT_PATCHOAT_H_
18#define ART_PATCHOAT_PATCHOAT_H_
19
Ian Rogersd582fa42014-11-05 23:46:43 -080020#include "arch/instruction_set.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Alex Light53cb16b2014-06-12 11:26:29 -070022#include "base/macros.h"
23#include "base/mutex.h"
David Sehr8f4b0562018-03-02 12:01:51 -080024#include "base/os.h"
Alex Light53cb16b2014-06-12 11:26:29 -070025#include "elf_file.h"
26#include "elf_utils.h"
27#include "gc/accounting/space_bitmap.h"
28#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "gc/space/image_space.h"
Jeff Haodcdc85b2015-12-04 14:06:18 -080030#include "runtime.h"
Alex Light53cb16b2014-06-12 11:26:29 -070031
32namespace art {
33
Mathieu Chartiere401d142015-04-22 13:56:20 -070034class ArtMethod;
Alex Light53cb16b2014-06-12 11:26:29 -070035class ImageHeader;
Igor Murashkin46774762014-10-22 11:37:02 -070036class OatHeader;
Alex Light53cb16b2014-06-12 11:26:29 -070037
38namespace mirror {
39class Object;
Mathieu Chartiere401d142015-04-22 13:56:20 -070040class PointerArray;
Alex Light53cb16b2014-06-12 11:26:29 -070041class Reference;
42class Class;
Andreas Gampec8ccf682014-09-29 20:07:43 -070043} // namespace mirror
Alex Light53cb16b2014-06-12 11:26:29 -070044
45class PatchOat {
46 public:
Alex Klyubin3856af02017-10-23 13:53:13 -070047 // Relocates the provided image by the specified offset. If output_image_directory is non-empty,
48 // outputs the relocated image into that directory. If output_image_relocation_directory is
49 // non-empty, outputs image relocation files (see GeneratePatch) into that directory.
Richard Uhler4bc11d02017-02-01 09:53:54 +000050 static bool Patch(const std::string& image_location,
Andreas Gampe6eb6a392016-02-10 20:18:37 -080051 off_t delta,
Alex Klyubin3856af02017-10-23 13:53:13 -070052 const std::string& output_image_directory,
53 const std::string& output_image_relocation_directory,
Andreas Gampe6eb6a392016-02-10 20:18:37 -080054 InstructionSet isa,
55 TimingLogger* timings);
Chris Morin754b7572018-01-19 18:04:46 -080056 static bool Verify(const std::string& image_location,
57 const std::string& output_image_filename,
58 InstructionSet isa,
59 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070060
Alex Klyubin3856af02017-10-23 13:53:13 -070061 // Generates a patch which can be used to efficiently relocate the original file or to check that
62 // a relocated file matches the original. The patch is generated from the difference of the
63 // |original| and the already |relocated| image, and written to |output| in the form of unsigned
64 // LEB128 for each relocation position.
65 static bool GeneratePatch(const MemMap& original,
66 const MemMap& relocated,
67 std::vector<uint8_t>* output,
68 std::string* error_msg);
69
Jeff Haodcdc85b2015-12-04 14:06:18 -080070 ~PatchOat() {}
71 PatchOat(PatchOat&&) = default;
72
Alex Light53cb16b2014-06-12 11:26:29 -070073 private:
Richard Uhler4bc11d02017-02-01 09:53:54 +000074 // All pointers are only borrowed.
75 PatchOat(InstructionSet isa, MemMap* image,
Mathieu Chartier2d721012014-11-10 11:08:06 -080076 gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
Jeff Haodcdc85b2015-12-04 14:06:18 -080077 std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings)
Richard Uhler4bc11d02017-02-01 09:53:54 +000078 : image_(image), bitmap_(bitmap), heap_(heap),
Jeff Haodcdc85b2015-12-04 14:06:18 -080079 delta_(delta), isa_(isa), space_map_(map), timings_(timings) {}
Alex Light53cb16b2014-06-12 11:26:29 -070080
Igor Murashkin46774762014-10-22 11:37:02 -070081 // Was the .art image at image_path made with --compile-pic ?
82 static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
83
84 enum MaybePic {
85 NOT_PIC, // Code not pic. Patch as usual.
86 PIC, // Code was pic. Create symlink; skip OAT patching.
87 ERROR_OAT_FILE, // Failed to symlink oat file
88 ERROR_FIRST = ERROR_OAT_FILE,
89 };
90
91 // Was the .oat image at oat_in made with --compile-pic ?
92 static MaybePic IsOatPic(const ElfFile* oat_in);
93
Chris Morinae6832f2018-02-09 19:12:35 -080094 static bool CreateVdexAndOatSymlinks(const std::string& input_image_filename,
95 const std::string& output_image_filename);
96
Igor Murashkin46774762014-10-22 11:37:02 -070097
Alex Light53cb16b2014-06-12 11:26:29 -070098 void VisitObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100 void FixupMethod(ArtMethod* object, ArtMethod* copy)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700102
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_);
104 void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
105 void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
106 void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700107 void PatchImtConflictTables(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700108 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700109 void PatchInternedStrings(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800111 void PatchClassTable(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700113 void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700115
Alex Light53cb16b2014-06-12 11:26:29 -0700116 bool WriteImage(File* out);
117
Mathieu Chartierc7853442015-03-27 14:35:38 -0700118 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700119 T* RelocatedCopyOf(T* obj) const {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700120 if (obj == nullptr) {
121 return nullptr;
122 }
Jeff Hao0d2af302016-01-04 17:38:06 -0800123 DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
124 DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700125 uintptr_t heap_off =
126 reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
Jeff Hao0d2af302016-01-04 17:38:06 -0800127 DCHECK_LT(heap_off, image_->Size());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700128 return reinterpret_cast<T*>(image_->Begin() + heap_off);
129 }
130
131 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800132 T* RelocatedCopyOfFollowImages(T* obj) const {
133 if (obj == nullptr) {
134 return nullptr;
135 }
136 // Find ImageSpace this belongs to.
137 auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
138 for (gc::space::ImageSpace* image_space : image_spaces) {
139 if (image_space->Contains(obj)) {
140 uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
141 reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
142 return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
143 }
144 }
145 LOG(FATAL) << "Did not find object in boot image space " << obj;
146 UNREACHABLE();
147 }
148
149 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700150 T* RelocatedAddressOfPointer(T* obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 if (obj == nullptr) {
152 return obj;
153 }
154 auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
155 // Trim off high bits in case negative relocation with 64 bit patchoat.
Andreas Gampe542451c2016-07-26 09:02:02 -0700156 if (Is32BitISA()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
158 }
159 return reinterpret_cast<T*>(ret);
160 }
161
Andreas Gampe542451c2016-07-26 09:02:02 -0700162 bool Is32BitISA() const {
163 return InstructionSetPointerSize(isa_) == PointerSize::k32;
164 }
165
Alex Lighteefbe392014-07-08 09:53:18 -0700166 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
167 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700168 class PatchVisitor {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800169 public:
Alex Light53cb16b2014-06-12 11:26:29 -0700170 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
171 ~PatchVisitor() {}
Mathieu Chartier31e88222016-10-14 18:43:19 -0700172 void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700173 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700174 // For reference classes.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700175 void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference> ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700176 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
177 // TODO: Consider using these for updating native class roots?
178 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
179 const {}
180 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
181
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800182 private:
Ian Rogersd4c4d952014-10-16 20:31:53 -0700183 PatchOat* const patcher_;
184 mirror::Object* const copy_;
Alex Light53cb16b2014-06-12 11:26:29 -0700185 };
186
Alex Lighteefbe392014-07-08 09:53:18 -0700187 // A mmap of the image we are patching. This is modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700188 const MemMap* const image_;
189 // The bitmap over the image within the heap we are patching. This is not modified.
190 gc::accounting::ContinuousSpaceBitmap* const bitmap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700191 // The heap we are patching. This is not modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700192 const MemMap* const heap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700193 // The amount we are changing the offset by.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700194 const off_t delta_;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800195 // Active instruction set, used to know the entrypoint size.
196 const InstructionSet isa_;
197
Jeff Haodcdc85b2015-12-04 14:06:18 -0800198 const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
199
Mathieu Chartier2d721012014-11-10 11:08:06 -0800200 TimingLogger* timings_;
Alex Lighteefbe392014-07-08 09:53:18 -0700201
Vladimir Markoad06b982016-11-17 16:38:59 +0000202 class FixupRootVisitor;
203 class RelocatedPointerVisitor;
204 class PatchOatArtFieldVisitor;
205 class PatchOatArtMethodVisitor;
206
Alex Light53cb16b2014-06-12 11:26:29 -0700207 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
208};
209
210} // namespace art
211#endif // ART_PATCHOAT_PATCHOAT_H_