blob: 1033a2e5e1623fe3622bd162d6da125e451bad98 [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"
Alex Light53cb16b2014-06-12 11:26:29 -070024#include "elf_file.h"
25#include "elf_utils.h"
26#include "gc/accounting/space_bitmap.h"
27#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "gc/space/image_space.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080029#include "os.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);
Alex Light53cb16b2014-06-12 11:26:29 -070056
Alex Klyubin3856af02017-10-23 13:53:13 -070057 // Generates a patch which can be used to efficiently relocate the original file or to check that
58 // a relocated file matches the original. The patch is generated from the difference of the
59 // |original| and the already |relocated| image, and written to |output| in the form of unsigned
60 // LEB128 for each relocation position.
61 static bool GeneratePatch(const MemMap& original,
62 const MemMap& relocated,
63 std::vector<uint8_t>* output,
64 std::string* error_msg);
65
Jeff Haodcdc85b2015-12-04 14:06:18 -080066 ~PatchOat() {}
67 PatchOat(PatchOat&&) = default;
68
Alex Light53cb16b2014-06-12 11:26:29 -070069 private:
Richard Uhler4bc11d02017-02-01 09:53:54 +000070 // All pointers are only borrowed.
71 PatchOat(InstructionSet isa, MemMap* image,
Mathieu Chartier2d721012014-11-10 11:08:06 -080072 gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
Jeff Haodcdc85b2015-12-04 14:06:18 -080073 std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings)
Richard Uhler4bc11d02017-02-01 09:53:54 +000074 : image_(image), bitmap_(bitmap), heap_(heap),
Jeff Haodcdc85b2015-12-04 14:06:18 -080075 delta_(delta), isa_(isa), space_map_(map), timings_(timings) {}
Alex Light53cb16b2014-06-12 11:26:29 -070076
Igor Murashkin46774762014-10-22 11:37:02 -070077 // Was the .art image at image_path made with --compile-pic ?
78 static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
79
80 enum MaybePic {
81 NOT_PIC, // Code not pic. Patch as usual.
82 PIC, // Code was pic. Create symlink; skip OAT patching.
83 ERROR_OAT_FILE, // Failed to symlink oat file
84 ERROR_FIRST = ERROR_OAT_FILE,
85 };
86
87 // Was the .oat image at oat_in made with --compile-pic ?
88 static MaybePic IsOatPic(const ElfFile* oat_in);
89
90 // Attempt to replace the file with a symlink
91 // Returns false if it fails
92 static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
Richard Uhler4bc11d02017-02-01 09:53:54 +000093 const std::string& output_oat_filename);
Igor Murashkin46774762014-10-22 11:37:02 -070094
Alex Light53cb16b2014-06-12 11:26:29 -070095 void VisitObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070097 void FixupMethod(ArtMethod* object, ArtMethod* copy)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -070099
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700100 bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_);
101 void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
102 void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
103 void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700104 void PatchImtConflictTables(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700105 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700106 void PatchInternedStrings(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800108 void PatchClassTable(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700110 void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700111 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700112
Alex Light53cb16b2014-06-12 11:26:29 -0700113 bool WriteImage(File* out);
114
Mathieu Chartierc7853442015-03-27 14:35:38 -0700115 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700116 T* RelocatedCopyOf(T* obj) const {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700117 if (obj == nullptr) {
118 return nullptr;
119 }
Jeff Hao0d2af302016-01-04 17:38:06 -0800120 DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
121 DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700122 uintptr_t heap_off =
123 reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
Jeff Hao0d2af302016-01-04 17:38:06 -0800124 DCHECK_LT(heap_off, image_->Size());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700125 return reinterpret_cast<T*>(image_->Begin() + heap_off);
126 }
127
128 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800129 T* RelocatedCopyOfFollowImages(T* obj) const {
130 if (obj == nullptr) {
131 return nullptr;
132 }
133 // Find ImageSpace this belongs to.
134 auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
135 for (gc::space::ImageSpace* image_space : image_spaces) {
136 if (image_space->Contains(obj)) {
137 uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
138 reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
139 return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
140 }
141 }
142 LOG(FATAL) << "Did not find object in boot image space " << obj;
143 UNREACHABLE();
144 }
145
146 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700147 T* RelocatedAddressOfPointer(T* obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 if (obj == nullptr) {
149 return obj;
150 }
151 auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
152 // Trim off high bits in case negative relocation with 64 bit patchoat.
Andreas Gampe542451c2016-07-26 09:02:02 -0700153 if (Is32BitISA()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
155 }
156 return reinterpret_cast<T*>(ret);
157 }
158
Andreas Gampe542451c2016-07-26 09:02:02 -0700159 bool Is32BitISA() const {
160 return InstructionSetPointerSize(isa_) == PointerSize::k32;
161 }
162
Alex Lighteefbe392014-07-08 09:53:18 -0700163 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
164 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700165 class PatchVisitor {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800166 public:
Alex Light53cb16b2014-06-12 11:26:29 -0700167 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
168 ~PatchVisitor() {}
Mathieu Chartier31e88222016-10-14 18:43:19 -0700169 void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700170 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700171 // For reference classes.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700172 void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference> ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700173 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
174 // TODO: Consider using these for updating native class roots?
175 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
176 const {}
177 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
178
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800179 private:
Ian Rogersd4c4d952014-10-16 20:31:53 -0700180 PatchOat* const patcher_;
181 mirror::Object* const copy_;
Alex Light53cb16b2014-06-12 11:26:29 -0700182 };
183
Alex Lighteefbe392014-07-08 09:53:18 -0700184 // A mmap of the image we are patching. This is modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700185 const MemMap* const image_;
186 // The bitmap over the image within the heap we are patching. This is not modified.
187 gc::accounting::ContinuousSpaceBitmap* const bitmap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700188 // The heap we are patching. This is not modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700189 const MemMap* const heap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700190 // The amount we are changing the offset by.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700191 const off_t delta_;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800192 // Active instruction set, used to know the entrypoint size.
193 const InstructionSet isa_;
194
Jeff Haodcdc85b2015-12-04 14:06:18 -0800195 const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
196
Mathieu Chartier2d721012014-11-10 11:08:06 -0800197 TimingLogger* timings_;
Alex Lighteefbe392014-07-08 09:53:18 -0700198
Vladimir Markoad06b982016-11-17 16:38:59 +0000199 class FixupRootVisitor;
200 class RelocatedPointerVisitor;
201 class PatchOatArtFieldVisitor;
202 class PatchOatArtMethodVisitor;
203
Alex Light53cb16b2014-06-12 11:26:29 -0700204 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
205};
206
207} // namespace art
208#endif // ART_PATCHOAT_PATCHOAT_H_