Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 20 | #include "arch/instruction_set.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 21 | #include "base/macros.h" |
| 22 | #include "base/mutex.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 23 | #include "elf_file.h" |
| 24 | #include "elf_utils.h" |
| 25 | #include "gc/accounting/space_bitmap.h" |
| 26 | #include "gc/heap.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 27 | #include "os.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | class ImageHeader; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 32 | class OatHeader; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 33 | |
| 34 | namespace mirror { |
| 35 | class Object; |
| 36 | class Reference; |
| 37 | class Class; |
| 38 | class ArtMethod; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 39 | } // namespace mirror |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 40 | |
| 41 | class PatchOat { |
| 42 | public: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 43 | // Patch only the oat file |
| 44 | static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings, |
| 45 | bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ? |
| 46 | bool new_oat_out); // Output oat was a new file created by us? |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 47 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 48 | // Patch only the image (art file) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 49 | static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 50 | TimingLogger* timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 51 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 52 | // Patch both the image and the oat file |
| 53 | static bool Patch(File* oat_in, const std::string& art_location, |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 54 | off_t delta, File* oat_out, File* art_out, InstructionSet isa, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 55 | TimingLogger* timings, |
| 56 | bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ? |
| 57 | bool new_oat_out); // Output oat was a new file created by us? |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 58 | |
| 59 | private: |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 60 | // Takes ownership only of the ElfFile. All other pointers are only borrowed. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 61 | PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings) |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 62 | : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 63 | isa_(kNone), timings_(timings) {} |
| 64 | PatchOat(InstructionSet isa, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 65 | MemMap* heap, off_t delta, TimingLogger* timings) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 66 | : image_(image), bitmap_(bitmap), heap_(heap), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 67 | delta_(delta), isa_(isa), timings_(timings) {} |
| 68 | PatchOat(InstructionSet isa, ElfFile* oat_file, MemMap* image, |
| 69 | gc::accounting::ContinuousSpaceBitmap* bitmap, MemMap* heap, off_t delta, |
| 70 | TimingLogger* timings) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 71 | : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 72 | delta_(delta), isa_(isa), timings_(timings) {} |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 73 | ~PatchOat() {} |
| 74 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 75 | // Was the .art image at image_path made with --compile-pic ? |
| 76 | static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path); |
| 77 | |
| 78 | enum MaybePic { |
| 79 | NOT_PIC, // Code not pic. Patch as usual. |
| 80 | PIC, // Code was pic. Create symlink; skip OAT patching. |
| 81 | ERROR_OAT_FILE, // Failed to symlink oat file |
| 82 | ERROR_FIRST = ERROR_OAT_FILE, |
| 83 | }; |
| 84 | |
| 85 | // Was the .oat image at oat_in made with --compile-pic ? |
| 86 | static MaybePic IsOatPic(const ElfFile* oat_in); |
| 87 | |
| 88 | // Attempt to replace the file with a symlink |
| 89 | // Returns false if it fails |
| 90 | static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename, |
| 91 | const std::string& output_oat_filename, |
| 92 | bool output_oat_opened_from_fd, |
| 93 | bool new_oat_out); // Output oat was newly created? |
| 94 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 95 | static void BitmapCallback(mirror::Object* obj, void* arg) |
| 96 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 97 | reinterpret_cast<PatchOat*>(arg)->VisitObject(obj); |
| 98 | } |
| 99 | |
| 100 | void VisitObject(mirror::Object* obj) |
| 101 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 102 | void FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy) |
| 103 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 104 | bool InHeap(mirror::Object*); |
| 105 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 106 | // Patches oat in place, modifying the oat_file given to the constructor. |
| 107 | bool PatchElf(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 108 | template <typename ElfFileImpl> |
| 109 | bool PatchElf(ElfFileImpl* oat_file); |
| 110 | template <typename ElfFileImpl> |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 111 | bool PatchOatHeader(ElfFileImpl* oat_file); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 112 | |
| 113 | bool PatchImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 114 | void PatchArtFields(const ImageHeader* image_header) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 115 | void PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) |
| 116 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 117 | |
| 118 | bool WriteElf(File* out); |
| 119 | bool WriteImage(File* out); |
| 120 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 121 | template <typename T> |
| 122 | T* RelocatedCopyOf(T* obj) { |
| 123 | if (obj == nullptr) { |
| 124 | return nullptr; |
| 125 | } |
| 126 | DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin())); |
| 127 | DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End())); |
| 128 | uintptr_t heap_off = |
| 129 | reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin()); |
| 130 | DCHECK_LT(heap_off, image_->Size()); |
| 131 | return reinterpret_cast<T*>(image_->Begin() + heap_off); |
| 132 | } |
| 133 | |
| 134 | template <typename T> |
| 135 | T* RelocatedAddressOfPointer(T* obj) { |
| 136 | return obj == nullptr ? nullptr : |
| 137 | reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(obj) + delta_); |
| 138 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 139 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 140 | // Look up the oat header from any elf file. |
| 141 | static const OatHeader* GetOatHeader(const ElfFile* elf_file); |
| 142 | |
| 143 | // Templatized version to actually look up the oat header |
| 144 | template <typename ElfFileImpl> |
| 145 | static const OatHeader* GetOatHeader(const ElfFileImpl* elf_file); |
| 146 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 147 | // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not |
| 148 | // change the heap. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 149 | class PatchVisitor { |
| 150 | public: |
| 151 | PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {} |
| 152 | ~PatchVisitor() {} |
| 153 | void operator() (mirror::Object* obj, MemberOffset off, bool b) const |
| 154 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 155 | // For reference classes. |
| 156 | void operator() (mirror::Class* cls, mirror::Reference* ref) const |
| 157 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 158 | private: |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 159 | PatchOat* const patcher_; |
| 160 | mirror::Object* const copy_; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 163 | // The elf file we are patching. |
| 164 | std::unique_ptr<ElfFile> oat_file_; |
| 165 | // A mmap of the image we are patching. This is modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 166 | const MemMap* const image_; |
| 167 | // The bitmap over the image within the heap we are patching. This is not modified. |
| 168 | gc::accounting::ContinuousSpaceBitmap* const bitmap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 169 | // The heap we are patching. This is not modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 170 | const MemMap* const heap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 171 | // The amount we are changing the offset by. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 172 | const off_t delta_; |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 173 | // Active instruction set, used to know the entrypoint size. |
| 174 | const InstructionSet isa_; |
| 175 | |
| 176 | TimingLogger* timings_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 177 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 178 | DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat); |
| 179 | }; |
| 180 | |
| 181 | } // namespace art |
| 182 | #endif // ART_PATCHOAT_PATCHOAT_H_ |