blob: 61ec695d83b3ba1c47e7a5cb49e4d6af1a80243b [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"
Jeff Haodcdc85b2015-12-04 14:06:18 -080026#include "gc/space/image_space.h"
Alex Light53cb16b2014-06-12 11:26:29 -070027#include "gc/heap.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "os.h"
Jeff Haodcdc85b2015-12-04 14:06:18 -080029#include "runtime.h"
Alex Light53cb16b2014-06-12 11:26:29 -070030
31namespace art {
32
Mathieu Chartiere401d142015-04-22 13:56:20 -070033class ArtMethod;
Alex Light53cb16b2014-06-12 11:26:29 -070034class ImageHeader;
Igor Murashkin46774762014-10-22 11:37:02 -070035class OatHeader;
Alex Light53cb16b2014-06-12 11:26:29 -070036
37namespace mirror {
38class Object;
Mathieu Chartiere401d142015-04-22 13:56:20 -070039class PointerArray;
Alex Light53cb16b2014-06-12 11:26:29 -070040class Reference;
41class Class;
Andreas Gampec8ccf682014-09-29 20:07:43 -070042} // namespace mirror
Alex Light53cb16b2014-06-12 11:26:29 -070043
44class PatchOat {
45 public:
Igor Murashkin46774762014-10-22 11:37:02 -070046 // Patch only the oat file
47 static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings,
48 bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ?
49 bool new_oat_out); // Output oat was a new file created by us?
Alex Light53cb16b2014-06-12 11:26:29 -070050
Igor Murashkin46774762014-10-22 11:37:02 -070051 // Patch only the image (art file)
Alex Light53cb16b2014-06-12 11:26:29 -070052 static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
Alex Lighteefbe392014-07-08 09:53:18 -070053 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070054
Igor Murashkin46774762014-10-22 11:37:02 -070055 // Patch both the image and the oat file
Andreas Gampe6eb6a392016-02-10 20:18:37 -080056 static bool Patch(const std::string& art_location,
57 off_t delta,
58 const std::string& output_directory,
59 InstructionSet isa,
60 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070061
Jeff Haodcdc85b2015-12-04 14:06:18 -080062 ~PatchOat() {}
63 PatchOat(PatchOat&&) = default;
64
Alex Light53cb16b2014-06-12 11:26:29 -070065 private:
Alex Light53cb16b2014-06-12 11:26:29 -070066 // Takes ownership only of the ElfFile. All other pointers are only borrowed.
Alex Lighteefbe392014-07-08 09:53:18 -070067 PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings)
Ian Rogersd4c4d952014-10-16 20:31:53 -070068 : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta),
Jeff Haodcdc85b2015-12-04 14:06:18 -080069 isa_(kNone), space_map_(nullptr), timings_(timings) {}
Mathieu Chartier2d721012014-11-10 11:08:06 -080070 PatchOat(InstructionSet isa, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070071 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070072 : image_(image), bitmap_(bitmap), heap_(heap),
Jeff Haodcdc85b2015-12-04 14:06:18 -080073 delta_(delta), isa_(isa), space_map_(nullptr), timings_(timings) {}
Mathieu Chartier2d721012014-11-10 11:08:06 -080074 PatchOat(InstructionSet isa, ElfFile* oat_file, MemMap* image,
75 gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta,
Jeff Haodcdc85b2015-12-04 14:06:18 -080076 std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* map, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070077 : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
Jeff Haodcdc85b2015-12-04 14:06:18 -080078 delta_(delta), isa_(isa), space_map_(map), timings_(timings) {}
Alex Light53cb16b2014-06-12 11:26:29 -070079
Igor Murashkin46774762014-10-22 11:37:02 -070080 // Was the .art image at image_path made with --compile-pic ?
81 static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
82
83 enum MaybePic {
84 NOT_PIC, // Code not pic. Patch as usual.
85 PIC, // Code was pic. Create symlink; skip OAT patching.
86 ERROR_OAT_FILE, // Failed to symlink oat file
87 ERROR_FIRST = ERROR_OAT_FILE,
88 };
89
90 // Was the .oat image at oat_in made with --compile-pic ?
91 static MaybePic IsOatPic(const ElfFile* oat_in);
92
93 // Attempt to replace the file with a symlink
94 // Returns false if it fails
95 static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
96 const std::string& output_oat_filename,
97 bool output_oat_opened_from_fd,
98 bool new_oat_out); // Output oat was newly created?
99
Alex Light53cb16b2014-06-12 11:26:29 -0700100 static void BitmapCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700101 SHARED_REQUIRES(Locks::mutator_lock_) {
Alex Light53cb16b2014-06-12 11:26:29 -0700102 reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
103 }
104
105 void VisitObject(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700106 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700107 void FixupMethod(ArtMethod* object, ArtMethod* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700108 SHARED_REQUIRES(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700109
Alex Light53cb16b2014-06-12 11:26:29 -0700110 // Patches oat in place, modifying the oat_file given to the constructor.
111 bool PatchElf();
Tong Shen62d1ca32014-09-03 17:24:56 -0700112 template <typename ElfFileImpl>
113 bool PatchElf(ElfFileImpl* oat_file);
114 template <typename ElfFileImpl>
Tong Shen62d1ca32014-09-03 17:24:56 -0700115 bool PatchOatHeader(ElfFileImpl* oat_file);
Alex Light53cb16b2014-06-12 11:26:29 -0700116
Jeff Haodcdc85b2015-12-04 14:06:18 -0800117 bool PatchImage(bool primary_image) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 void PatchArtFields(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_);
119 void PatchArtMethods(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000120 void PatchImTables(const ImageHeader* image_header) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700121 void PatchImtConflictTables(const ImageHeader* image_header)
122 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700123 void PatchInternedStrings(const ImageHeader* image_header)
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800125 void PatchClassTable(const ImageHeader* image_header)
126 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700127 void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots)
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 SHARED_REQUIRES(Locks::mutator_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700129
130 bool WriteElf(File* out);
131 bool WriteImage(File* out);
132
Mathieu Chartierc7853442015-03-27 14:35:38 -0700133 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700134 T* RelocatedCopyOf(T* obj) const {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700135 if (obj == nullptr) {
136 return nullptr;
137 }
Jeff Hao0d2af302016-01-04 17:38:06 -0800138 DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
139 DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700140 uintptr_t heap_off =
141 reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
Jeff Hao0d2af302016-01-04 17:38:06 -0800142 DCHECK_LT(heap_off, image_->Size());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700143 return reinterpret_cast<T*>(image_->Begin() + heap_off);
144 }
145
146 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800147 T* RelocatedCopyOfFollowImages(T* obj) const {
148 if (obj == nullptr) {
149 return nullptr;
150 }
151 // Find ImageSpace this belongs to.
152 auto image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
153 for (gc::space::ImageSpace* image_space : image_spaces) {
154 if (image_space->Contains(obj)) {
155 uintptr_t heap_off = reinterpret_cast<uintptr_t>(obj) -
156 reinterpret_cast<uintptr_t>(image_space->GetMemMap()->Begin());
157 return reinterpret_cast<T*>(space_map_->find(image_space)->second->Begin() + heap_off);
158 }
159 }
160 LOG(FATAL) << "Did not find object in boot image space " << obj;
161 UNREACHABLE();
162 }
163
164 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700165 T* RelocatedAddressOfPointer(T* obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 if (obj == nullptr) {
167 return obj;
168 }
169 auto ret = reinterpret_cast<uintptr_t>(obj) + delta_;
170 // Trim off high bits in case negative relocation with 64 bit patchoat.
171 if (InstructionSetPointerSize(isa_) == sizeof(uint32_t)) {
172 ret = static_cast<uintptr_t>(static_cast<uint32_t>(ret));
173 }
174 return reinterpret_cast<T*>(ret);
175 }
176
177 template <typename T>
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700178 T RelocatedAddressOfIntPointer(T obj) const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700179 if (obj == 0) {
180 return obj;
181 }
182 T ret = obj + delta_;
183 // Trim off high bits in case negative relocation with 64 bit patchoat.
184 if (InstructionSetPointerSize(isa_) == 4) {
185 ret = static_cast<T>(static_cast<uint32_t>(ret));
186 }
187 return ret;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700188 }
Alex Light53cb16b2014-06-12 11:26:29 -0700189
Alex Lighteefbe392014-07-08 09:53:18 -0700190 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
191 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700192 class PatchVisitor {
193 public:
194 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
195 ~PatchVisitor() {}
196 void operator() (mirror::Object* obj, MemberOffset off, bool b) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700197 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Alex Light53cb16b2014-06-12 11:26:29 -0700198 // For reference classes.
199 void operator() (mirror::Class* cls, mirror::Reference* ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700200 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
201 // TODO: Consider using these for updating native class roots?
202 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
203 const {}
204 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
205
Alex Light53cb16b2014-06-12 11:26:29 -0700206 private:
Ian Rogersd4c4d952014-10-16 20:31:53 -0700207 PatchOat* const patcher_;
208 mirror::Object* const copy_;
Alex Light53cb16b2014-06-12 11:26:29 -0700209 };
210
Alex Lighteefbe392014-07-08 09:53:18 -0700211 // The elf file we are patching.
212 std::unique_ptr<ElfFile> oat_file_;
213 // A mmap of the image we are patching. This is modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700214 const MemMap* const image_;
215 // The bitmap over the image within the heap we are patching. This is not modified.
216 gc::accounting::ContinuousSpaceBitmap* const bitmap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700217 // The heap we are patching. This is not modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700218 const MemMap* const heap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700219 // The amount we are changing the offset by.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700220 const off_t delta_;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800221 // Active instruction set, used to know the entrypoint size.
222 const InstructionSet isa_;
223
Jeff Haodcdc85b2015-12-04 14:06:18 -0800224 const std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>>* space_map_;
225
Mathieu Chartier2d721012014-11-10 11:08:06 -0800226 TimingLogger* timings_;
Alex Lighteefbe392014-07-08 09:53:18 -0700227
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700228 friend class FixupRootVisitor;
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800229 friend class RelocatedPointerVisitor;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700230 friend class PatchOatArtFieldVisitor;
231 friend class PatchOatArtMethodVisitor;
Alex Light53cb16b2014-06-12 11:26:29 -0700232 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
233};
234
235} // namespace art
236#endif // ART_PATCHOAT_PATCHOAT_H_