blob: 09150144ec12243fcc9a4ddc572c47026998f66e [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"
Alex Light53cb16b2014-06-12 11:26:29 -070021#include "base/macros.h"
22#include "base/mutex.h"
Alex Light53cb16b2014-06-12 11:26:29 -070023#include "elf_file.h"
24#include "elf_utils.h"
25#include "gc/accounting/space_bitmap.h"
26#include "gc/heap.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080027#include "os.h"
Alex Light53cb16b2014-06-12 11:26:29 -070028
29namespace art {
30
Mathieu Chartiere401d142015-04-22 13:56:20 -070031class ArtMethod;
Alex Light53cb16b2014-06-12 11:26:29 -070032class ImageHeader;
Igor Murashkin46774762014-10-22 11:37:02 -070033class OatHeader;
Alex Light53cb16b2014-06-12 11:26:29 -070034
35namespace mirror {
36class Object;
Mathieu Chartiere401d142015-04-22 13:56:20 -070037class PointerArray;
Alex Light53cb16b2014-06-12 11:26:29 -070038class Reference;
39class Class;
Andreas Gampec8ccf682014-09-29 20:07:43 -070040} // namespace mirror
Alex Light53cb16b2014-06-12 11:26:29 -070041
42class PatchOat {
43 public:
Igor Murashkin46774762014-10-22 11:37:02 -070044 // Patch only the oat file
45 static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings,
46 bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ?
47 bool new_oat_out); // Output oat was a new file created by us?
Alex Light53cb16b2014-06-12 11:26:29 -070048
Igor Murashkin46774762014-10-22 11:37:02 -070049 // Patch only the image (art file)
Alex Light53cb16b2014-06-12 11:26:29 -070050 static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
Alex Lighteefbe392014-07-08 09:53:18 -070051 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070052
Igor Murashkin46774762014-10-22 11:37:02 -070053 // Patch both the image and the oat file
54 static bool Patch(File* oat_in, const std::string& art_location,
Alex Light53cb16b2014-06-12 11:26:29 -070055 off_t delta, File* oat_out, File* art_out, InstructionSet isa,
Igor Murashkin46774762014-10-22 11:37:02 -070056 TimingLogger* timings,
57 bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ?
58 bool new_oat_out); // Output oat was a new file created by us?
Alex Light53cb16b2014-06-12 11:26:29 -070059
60 private:
Alex Light53cb16b2014-06-12 11:26:29 -070061 // Takes ownership only of the ElfFile. All other pointers are only borrowed.
Alex Lighteefbe392014-07-08 09:53:18 -070062 PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings)
Ian Rogersd4c4d952014-10-16 20:31:53 -070063 : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta),
Mathieu Chartier2d721012014-11-10 11:08:06 -080064 isa_(kNone), timings_(timings) {}
65 PatchOat(InstructionSet isa, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070066 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070067 : image_(image), bitmap_(bitmap), heap_(heap),
Mathieu Chartier2d721012014-11-10 11:08:06 -080068 delta_(delta), isa_(isa), timings_(timings) {}
69 PatchOat(InstructionSet isa, ElfFile* oat_file, MemMap* image,
70 gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
71 TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070072 : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
Mathieu Chartier2d721012014-11-10 11:08:06 -080073 delta_(delta), isa_(isa), timings_(timings) {}
Alex Light53cb16b2014-06-12 11:26:29 -070074 ~PatchOat() {}
75
Igor Murashkin46774762014-10-22 11:37:02 -070076 // Was the .art image at image_path made with --compile-pic ?
77 static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
78
79 enum MaybePic {
80 NOT_PIC, // Code not pic. Patch as usual.
81 PIC, // Code was pic. Create symlink; skip OAT patching.
82 ERROR_OAT_FILE, // Failed to symlink oat file
83 ERROR_FIRST = ERROR_OAT_FILE,
84 };
85
86 // Was the .oat image at oat_in made with --compile-pic ?
87 static MaybePic IsOatPic(const ElfFile* oat_in);
88
89 // Attempt to replace the file with a symlink
90 // Returns false if it fails
91 static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
92 const std::string& output_oat_filename,
93 bool output_oat_opened_from_fd,
94 bool new_oat_out); // Output oat was newly created?
95
Alex Light53cb16b2014-06-12 11:26:29 -070096 static void BitmapCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -070097 SHARED_REQUIRES(Locks::mutator_lock_) {
Alex Light53cb16b2014-06-12 11:26:29 -070098 reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
99 }
100
101 void VisitObject(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700102 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103 void FixupMethod(ArtMethod* object, ArtMethod* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 SHARED_REQUIRES(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700105 bool InHeap(mirror::Object*);
106
Alex Light53cb16b2014-06-12 11:26:29 -0700107 // Patches oat in place, modifying the oat_file given to the constructor.
108 bool PatchElf();
Tong Shen62d1ca32014-09-03 17:24:56 -0700109 template <typename ElfFileImpl>
110 bool PatchElf(ElfFileImpl* oat_file);
111 template <typename ElfFileImpl>
Tong Shen62d1ca32014-09-03 17:24:56 -0700112 bool PatchOatHeader(ElfFileImpl* oat_file);
Alex Light53cb16b2014-06-12 11:26:29 -0700113
Mathieu Chartier90443472015-07-16 20:32:27 -0700114 bool PatchImage() SHARED_REQUIRES(Locks::mutator_lock_);
115 void PatchArtFields(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_);
116 void PatchArtMethods(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700117 void PatchInternedStrings(const ImageHeader* image_header)
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700119 void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
Mathieu Chartier90443472015-07-16 20:32:27 -0700120 SHARED_REQUIRES(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700121
122 bool WriteElf(File* out);
123 bool WriteImage(File* out);
124
Mathieu Chartierc7853442015-03-27 14:35:38 -0700125 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700126 T* RelocatedCopyOf(T* obj) const {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700127 if (obj == nullptr) {
128 return nullptr;
129 }
130 DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
131 DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
132 uintptr_t heap_off =
133 reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
134 DCHECK_LT(heap_off, image_->Size());
135 return reinterpret_cast<T*>(image_->Begin() + heap_off);
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.
145 if (InstructionSetPointerSize(isa_) == sizeof(uint32_t)) {
146 ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
147 }
148 return reinterpret_cast<T*>(ret);
149 }
150
151 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700152 T RelocatedAddressOfIntPointer(T obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 if (obj == 0) {
154 return obj;
155 }
156 T ret = obj + delta_;
157 // Trim off high bits in case negative relocation with 64 bit patchoat.
158 if (InstructionSetPointerSize(isa_) == 4) {
159 ret = static_cast<T>(static_cast<uint32_t>(ret));
160 }
161 return ret;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700162 }
Alex Light53cb16b2014-06-12 11:26:29 -0700163
Alex Lighteefbe392014-07-08 09:53:18 -0700164 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
165 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700166 class PatchVisitor {
167 public:
168 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
169 ~PatchVisitor() {}
170 void operator() (mirror::Object* obj, MemberOffset off, bool b) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700171 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700172 // For reference classes.
173 void operator() (mirror::Class* cls, mirror::Reference* ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700174 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
175 // TODO: Consider using these for updating native class roots?
176 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
177 const {}
178 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
179
Alex Light53cb16b2014-06-12 11:26:29 -0700180 private:
Ian Rogersd4c4d952014-10-16 20:31:53 -0700181 PatchOat* const patcher_;
182 mirror::Object* const copy_;
Alex Light53cb16b2014-06-12 11:26:29 -0700183 };
184
Alex Lighteefbe392014-07-08 09:53:18 -0700185 // The elf file we are patching.
186 std::unique_ptr<ElfFile> oat_file_;
187 // 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
198 TimingLogger* timings_;
Alex Lighteefbe392014-07-08 09:53:18 -0700199
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700200 friend class FixupRootVisitor;
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800201 friend class RelocatedPointerVisitor;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700202 friend class PatchOatArtFieldVisitor;
203 friend class PatchOatArtMethodVisitor;
Alex Light53cb16b2014-06-12 11:26:29 -0700204 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
205};
206
207} // namespace art
208#endif // ART_PATCHOAT_PATCHOAT_H_