blob: e15a6bc69593536040e85908058826f9334c3ded [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"
Jeff Haodcdc85b2015-12-04 14:06:18 -080027#include "gc/space/image_space.h"
Alex Light53cb16b2014-06-12 11:26:29 -070028#include "gc/heap.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:
Richard Uhler4bc11d02017-02-01 09:53:54 +000047 static bool Patch(const std::string& image_location,
Andreas Gampe6eb6a392016-02-10 20:18:37 -080048 off_t delta,
49 const std::string& output_directory,
50 InstructionSet isa,
51 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070052
Jeff Haodcdc85b2015-12-04 14:06:18 -080053 ~PatchOat() {}
54 PatchOat(PatchOat&&) = default;
55
Alex Light53cb16b2014-06-12 11:26:29 -070056 private:
Richard Uhler4bc11d02017-02-01 09:53:54 +000057 // All pointers are only borrowed.
58 PatchOat(InstructionSet isa, MemMap* image,
Mathieu Chartier2d721012014-11-10 11:08:06 -080059 gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
Jeff Haodcdc85b2015-12-04 14:06:18 -080060 std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings)
Richard Uhler4bc11d02017-02-01 09:53:54 +000061 : image_(image), bitmap_(bitmap), heap_(heap),
Jeff Haodcdc85b2015-12-04 14:06:18 -080062 delta_(delta), isa_(isa), space_map_(map), timings_(timings) {}
Alex Light53cb16b2014-06-12 11:26:29 -070063
Igor Murashkin46774762014-10-22 11:37:02 -070064 // Was the .art image at image_path made with --compile-pic ?
65 static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
66
67 enum MaybePic {
68 NOT_PIC, // Code not pic. Patch as usual.
69 PIC, // Code was pic. Create symlink; skip OAT patching.
70 ERROR_OAT_FILE, // Failed to symlink oat file
71 ERROR_FIRST = ERROR_OAT_FILE,
72 };
73
74 // Was the .oat image at oat_in made with --compile-pic ?
75 static MaybePic IsOatPic(const ElfFile* oat_in);
76
77 // Attempt to replace the file with a symlink
78 // Returns false if it fails
79 static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
Richard Uhler4bc11d02017-02-01 09:53:54 +000080 const std::string& output_oat_filename);
Igor Murashkin46774762014-10-22 11:37:02 -070081
Alex Light53cb16b2014-06-12 11:26:29 -070082 static void BitmapCallback(mirror::Object* obj, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light53cb16b2014-06-12 11:26:29 -070084 reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
85 }
86
87 void VisitObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070088 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 void FixupMethod(ArtMethod* object, ArtMethod* copy)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -070091
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_);
93 void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
94 void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
95 void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -070096 void PatchImtConflictTables(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -070098 void PatchInternedStrings(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800100 void PatchClassTable(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700102 void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700104
Alex Light53cb16b2014-06-12 11:26:29 -0700105 bool WriteImage(File* out);
106
Mathieu Chartierc7853442015-03-27 14:35:38 -0700107 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700108 T* RelocatedCopyOf(T* obj) const {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700109 if (obj == nullptr) {
110 return nullptr;
111 }
Jeff Hao0d2af302016-01-04 17:38:06 -0800112 DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
113 DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700114 uintptr_t heap_off =
115 reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
Jeff Hao0d2af302016-01-04 17:38:06 -0800116 DCHECK_LT(heap_off, image_->Size());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700117 return reinterpret_cast<T*>(image_->Begin() + heap_off);
118 }
119
120 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800121 T* RelocatedCopyOfFollowImages(T* obj) const {
122 if (obj == nullptr) {
123 return nullptr;
124 }
125 // Find ImageSpace this belongs to.
126 auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
127 for (gc::space::ImageSpace* image_space : image_spaces) {
128 if (image_space->Contains(obj)) {
129 uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
130 reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
131 return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
132 }
133 }
134 LOG(FATAL) << "Did not find object in boot image space " << obj;
135 UNREACHABLE();
136 }
137
138 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700139 T* RelocatedAddressOfPointer(T* obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 if (obj == nullptr) {
141 return obj;
142 }
143 auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
144 // Trim off high bits in case negative relocation with 64 bit patchoat.
Andreas Gampe542451c2016-07-26 09:02:02 -0700145 if (Is32BitISA()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
147 }
148 return reinterpret_cast<T*>(ret);
149 }
150
Andreas Gampe542451c2016-07-26 09:02:02 -0700151 bool Is32BitISA() const {
152 return InstructionSetPointerSize(isa_) == PointerSize::k32;
153 }
154
Alex Lighteefbe392014-07-08 09:53:18 -0700155 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
156 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700157 class PatchVisitor {
158 public:
159 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
160 ~PatchVisitor() {}
Mathieu Chartier31e88222016-10-14 18:43:19 -0700161 void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700162 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700163 // For reference classes.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700164 void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference> ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700165 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
166 // TODO: Consider using these for updating native class roots?
167 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
168 const {}
169 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
170
Alex Light53cb16b2014-06-12 11:26:29 -0700171 private:
Ian Rogersd4c4d952014-10-16 20:31:53 -0700172 PatchOat* const patcher_;
173 mirror::Object* const copy_;
Alex Light53cb16b2014-06-12 11:26:29 -0700174 };
175
Alex Lighteefbe392014-07-08 09:53:18 -0700176 // A mmap of the image we are patching. This is modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700177 const MemMap* const image_;
178 // The bitmap over the image within the heap we are patching. This is not modified.
179 gc::accounting::ContinuousSpaceBitmap* const bitmap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700180 // The heap we are patching. This is not modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700181 const MemMap* const heap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700182 // The amount we are changing the offset by.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700183 const off_t delta_;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800184 // Active instruction set, used to know the entrypoint size.
185 const InstructionSet isa_;
186
Jeff Haodcdc85b2015-12-04 14:06:18 -0800187 const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
188
Mathieu Chartier2d721012014-11-10 11:08:06 -0800189 TimingLogger* timings_;
Alex Lighteefbe392014-07-08 09:53:18 -0700190
Vladimir Markoad06b982016-11-17 16:38:59 +0000191 class FixupRootVisitor;
192 class RelocatedPointerVisitor;
193 class PatchOatArtFieldVisitor;
194 class PatchOatArtMethodVisitor;
195
Alex Light53cb16b2014-06-12 11:26:29 -0700196 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
197};
198
199} // namespace art
200#endif // ART_PATCHOAT_PATCHOAT_H_