blob: e7a3e91d8107d9b7ba15754be07d1f9f5f96fe34 [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:
Igor Murashkin46774762014-10-22 11:37:02 -070047 // Patch only the oat file
48 static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings,
49 bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ?
50 bool new_oat_out); // Output oat was a new file created by us?
Alex Light53cb16b2014-06-12 11:26:29 -070051
Igor Murashkin46774762014-10-22 11:37:02 -070052 // Patch only the image (art file)
Alex Light53cb16b2014-06-12 11:26:29 -070053 static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
Alex Lighteefbe392014-07-08 09:53:18 -070054 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070055
Igor Murashkin46774762014-10-22 11:37:02 -070056 // Patch both the image and the oat file
Andreas Gampe6eb6a392016-02-10 20:18:37 -080057 static bool Patch(const std::string& art_location,
58 off_t delta,
59 const std::string& output_directory,
60 InstructionSet isa,
61 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070062
Jeff Haodcdc85b2015-12-04 14:06:18 -080063 ~PatchOat() {}
64 PatchOat(PatchOat&&) = default;
65
Alex Light53cb16b2014-06-12 11:26:29 -070066 private:
Alex Light53cb16b2014-06-12 11:26:29 -070067 // Takes ownership only of the ElfFile. All other pointers are only borrowed.
Alex Lighteefbe392014-07-08 09:53:18 -070068 PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings)
Ian Rogersd4c4d952014-10-16 20:31:53 -070069 : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta),
Jeff Haodcdc85b2015-12-04 14:06:18 -080070 isa_(kNone), space_map_(nullptr), timings_(timings) {}
Mathieu Chartier2d721012014-11-10 11:08:06 -080071 PatchOat(InstructionSet isa, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070072 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070073 : image_(image), bitmap_(bitmap), heap_(heap),
Jeff Haodcdc85b2015-12-04 14:06:18 -080074 delta_(delta), isa_(isa), space_map_(nullptr), timings_(timings) {}
Mathieu Chartier2d721012014-11-10 11:08:06 -080075 PatchOat(InstructionSet isa, ElfFile* oat_file, MemMap* image,
76 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)
Alex Light53cb16b2014-06-12 11:26:29 -070078 : oat_file_(oat_file), 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
94 // Attempt to replace the file with a symlink
95 // Returns false if it fails
96 static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
97 const std::string& output_oat_filename,
98 bool output_oat_opened_from_fd,
99 bool new_oat_out); // Output oat was newly created?
100
Alex Light53cb16b2014-06-12 11:26:29 -0700101 static void BitmapCallback(mirror::Object* obj, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light53cb16b2014-06-12 11:26:29 -0700103 reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
104 }
105
106 void VisitObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108 void FixupMethod(ArtMethod* object, ArtMethod* copy)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700110
Alex Light53cb16b2014-06-12 11:26:29 -0700111 // Patches oat in place, modifying the oat_file given to the constructor.
112 bool PatchElf();
Tong Shen62d1ca32014-09-03 17:24:56 -0700113 template <typename ElfFileImpl>
114 bool PatchElf(ElfFileImpl* oat_file);
115 template <typename ElfFileImpl>
Tong Shen62d1ca32014-09-03 17:24:56 -0700116 bool PatchOatHeader(ElfFileImpl* oat_file);
Alex Light53cb16b2014-06-12 11:26:29 -0700117
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 bool PatchImage(bool primary_image) REQUIRES_SHARED(Locks::mutator_lock_);
119 void PatchArtFields(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
120 void PatchArtMethods(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
121 void PatchImTables(const ImageHeader* image_header) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700122 void PatchImtConflictTables(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700123 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700124 void PatchInternedStrings(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800126 void PatchClassTable(const ImageHeader* image_header)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700127 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700128 void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700130
131 bool WriteElf(File* out);
132 bool WriteImage(File* out);
133
Mathieu Chartierc7853442015-03-27 14:35:38 -0700134 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700135 T* RelocatedCopyOf(T* obj) const {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700136 if (obj == nullptr) {
137 return nullptr;
138 }
Jeff Hao0d2af302016-01-04 17:38:06 -0800139 DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
140 DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700141 uintptr_t heap_off =
142 reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
Jeff Hao0d2af302016-01-04 17:38:06 -0800143 DCHECK_LT(heap_off, image_->Size());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700144 return reinterpret_cast<T*>(image_->Begin() + heap_off);
145 }
146
147 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800148 T* RelocatedCopyOfFollowImages(T* obj) const {
149 if (obj == nullptr) {
150 return nullptr;
151 }
152 // Find ImageSpace this belongs to.
153 auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
154 for (gc::space::ImageSpace* image_space : image_spaces) {
155 if (image_space->Contains(obj)) {
156 uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
157 reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
158 return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
159 }
160 }
161 LOG(FATAL) << "Did not find object in boot image space " << obj;
162 UNREACHABLE();
163 }
164
165 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700166 T* RelocatedAddressOfPointer(T* obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 if (obj == nullptr) {
168 return obj;
169 }
170 auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
171 // Trim off high bits in case negative relocation with 64 bit patchoat.
Andreas Gampe542451c2016-07-26 09:02:02 -0700172 if (Is32BitISA()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700173 ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
174 }
175 return reinterpret_cast<T*>(ret);
176 }
177
178 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700179 T RelocatedAddressOfIntPointer(T obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700180 if (obj == 0) {
181 return obj;
182 }
183 T ret = obj + delta_;
184 // Trim off high bits in case negative relocation with 64 bit patchoat.
Andreas Gampe542451c2016-07-26 09:02:02 -0700185 if (Is32BitISA()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 ret = static_cast<T>(static_cast<uint32_t>(ret));
187 }
188 return ret;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700189 }
Alex Light53cb16b2014-06-12 11:26:29 -0700190
Andreas Gampe542451c2016-07-26 09:02:02 -0700191 bool Is32BitISA() const {
192 return InstructionSetPointerSize(isa_) == PointerSize::k32;
193 }
194
Alex Lighteefbe392014-07-08 09:53:18 -0700195 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
196 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700197 class PatchVisitor {
198 public:
199 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
200 ~PatchVisitor() {}
Mathieu Chartier31e88222016-10-14 18:43:19 -0700201 void operator() (ObjPtr<mirror::Object> obj, MemberOffset off, bool b) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700202 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700203 // For reference classes.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700204 void operator() (ObjPtr<mirror::Class> cls, ObjPtr<mirror::Reference> ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700205 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
206 // TODO: Consider using these for updating native class roots?
207 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
208 const {}
209 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
210
Alex Light53cb16b2014-06-12 11:26:29 -0700211 private:
Ian Rogersd4c4d952014-10-16 20:31:53 -0700212 PatchOat* const patcher_;
213 mirror::Object* const copy_;
Alex Light53cb16b2014-06-12 11:26:29 -0700214 };
215
Alex Lighteefbe392014-07-08 09:53:18 -0700216 // The elf file we are patching.
217 std::unique_ptr<ElfFile> oat_file_;
218 // A mmap of the image we are patching. This is modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700219 const MemMap* const image_;
220 // The bitmap over the image within the heap we are patching. This is not modified.
221 gc::accounting::ContinuousSpaceBitmap* const bitmap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700222 // The heap we are patching. This is not modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700223 const MemMap* const heap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700224 // The amount we are changing the offset by.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700225 const off_t delta_;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800226 // Active instruction set, used to know the entrypoint size.
227 const InstructionSet isa_;
228
Jeff Haodcdc85b2015-12-04 14:06:18 -0800229 const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
230
Mathieu Chartier2d721012014-11-10 11:08:06 -0800231 TimingLogger* timings_;
Alex Lighteefbe392014-07-08 09:53:18 -0700232
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700233 friend class FixupRootVisitor;
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800234 friend class RelocatedPointerVisitor;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700235 friend class PatchOatArtFieldVisitor;
236 friend class PatchOatArtMethodVisitor;
Alex Light53cb16b2014-06-12 11:26:29 -0700237 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
238};
239
240} // namespace art
241#endif // ART_PATCHOAT_PATCHOAT_H_