blob: 182ce94a7878b386e239e6da70dec475f0dcd311 [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 void VisitObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 void FixupMethod(ArtMethod* object, ArtMethod* copy)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -070086
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070087 bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_);
88 void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
89 void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
90 void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -070091 void PatchImtConflictTables(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -070093 void PatchInternedStrings(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070094 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -080095 void PatchClassTable(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070097 void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -070099
Alex Light53cb16b2014-06-12 11:26:29 -0700100 bool WriteImage(File* out);
101
Mathieu Chartierc7853442015-03-27 14:35:38 -0700102 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700103 T* RelocatedCopyOf(T* obj) const {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700104 if (obj == nullptr) {
105 return nullptr;
106 }
Jeff Hao0d2af302016-01-04 17:38:06 -0800107 DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
108 DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700109 uintptr_t heap_off =
110 reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
Jeff Hao0d2af302016-01-04 17:38:06 -0800111 DCHECK_LT(heap_off, image_->Size());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700112 return reinterpret_cast<T*>(image_->Begin() + heap_off);
113 }
114
115 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800116 T* RelocatedCopyOfFollowImages(T* obj) const {
117 if (obj == nullptr) {
118 return nullptr;
119 }
120 // Find ImageSpace this belongs to.
121 auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
122 for (gc::space::ImageSpace* image_space : image_spaces) {
123 if (image_space->Contains(obj)) {
124 uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
125 reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
126 return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
127 }
128 }
129 LOG(FATAL) << "Did not find object in boot image space " << obj;
130 UNREACHABLE();
131 }
132
133 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700134 T* RelocatedAddressOfPointer(T* obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 if (obj == nullptr) {
136 return obj;
137 }
138 auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
139 // Trim off high bits in case negative relocation with 64 bit patchoat.
Andreas Gampe542451c2016-07-26 09:02:02 -0700140 if (Is32BitISA()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141 ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
142 }
143 return reinterpret_cast<T*>(ret);
144 }
145
Andreas Gampe542451c2016-07-26 09:02:02 -0700146 bool Is32BitISA() const {
147 return InstructionSetPointerSize(isa_) == PointerSize::k32;
148 }
149
Alex Lighteefbe392014-07-08 09:53:18 -0700150 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
151 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700152 class PatchVisitor {
153 public:
154 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
155 ~PatchVisitor() {}
Mathieu Chartier31e88222016-10-14 18:43:19 -0700156 void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700157 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700158 // For reference classes.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700159 void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference> ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700160 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
161 // TODO: Consider using these for updating native class roots?
162 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
163 const {}
164 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
165
Alex Light53cb16b2014-06-12 11:26:29 -0700166 private:
Ian Rogersd4c4d952014-10-16 20:31:53 -0700167 PatchOat* const patcher_;
168 mirror::Object* const copy_;
Alex Light53cb16b2014-06-12 11:26:29 -0700169 };
170
Alex Lighteefbe392014-07-08 09:53:18 -0700171 // A mmap of the image we are patching. This is modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700172 const MemMap* const image_;
173 // The bitmap over the image within the heap we are patching. This is not modified.
174 gc::accounting::ContinuousSpaceBitmap* const bitmap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700175 // The heap we are patching. This is not modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700176 const MemMap* const heap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700177 // The amount we are changing the offset by.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700178 const off_t delta_;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800179 // Active instruction set, used to know the entrypoint size.
180 const InstructionSet isa_;
181
Jeff Haodcdc85b2015-12-04 14:06:18 -0800182 const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
183
Mathieu Chartier2d721012014-11-10 11:08:06 -0800184 TimingLogger* timings_;
Alex Lighteefbe392014-07-08 09:53:18 -0700185
Vladimir Markoad06b982016-11-17 16:38:59 +0000186 class FixupRootVisitor;
187 class RelocatedPointerVisitor;
188 class PatchOatArtFieldVisitor;
189 class PatchOatArtMethodVisitor;
190
Alex Light53cb16b2014-06-12 11:26:29 -0700191 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
192};
193
194} // namespace art
195#endif // ART_PATCHOAT_PATCHOAT_H_