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 | |
| 20 | #include "base/macros.h" |
| 21 | #include "base/mutex.h" |
| 22 | #include "instruction_set.h" |
| 23 | #include "os.h" |
| 24 | #include "elf_file.h" |
| 25 | #include "elf_utils.h" |
| 26 | #include "gc/accounting/space_bitmap.h" |
| 27 | #include "gc/heap.h" |
| 28 | #include "utils.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | class ImageHeader; |
| 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: |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 43 | static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 44 | |
| 45 | 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] | 46 | TimingLogger* timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 47 | |
| 48 | static bool Patch(const File* oat_in, const std::string& art_location, |
| 49 | off_t delta, File* oat_out, 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 | |
| 52 | private: |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 53 | // Takes ownership only of the ElfFile. All other pointers are only borrowed. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 54 | PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings) |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 55 | : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta), |
| 56 | timings_(timings) {} |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 57 | PatchOat(MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 58 | MemMap* heap, off_t delta, TimingLogger* timings) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 59 | : image_(image), bitmap_(bitmap), heap_(heap), |
| 60 | delta_(delta), timings_(timings) {} |
| 61 | PatchOat(ElfFile* oat_file, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 62 | MemMap* heap, off_t delta, TimingLogger* timings) |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 63 | : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap), |
| 64 | delta_(delta), timings_(timings) {} |
| 65 | ~PatchOat() {} |
| 66 | |
| 67 | static void BitmapCallback(mirror::Object* obj, void* arg) |
| 68 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 69 | reinterpret_cast<PatchOat*>(arg)->VisitObject(obj); |
| 70 | } |
| 71 | |
| 72 | void VisitObject(mirror::Object* obj) |
| 73 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 74 | void FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy) |
| 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 76 | bool InHeap(mirror::Object*); |
| 77 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 78 | // Patches oat in place, modifying the oat_file given to the constructor. |
| 79 | bool PatchElf(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 80 | template <typename ElfFileImpl> |
| 81 | bool PatchElf(ElfFileImpl* oat_file); |
| 82 | template <typename ElfFileImpl> |
| 83 | bool PatchTextSection(ElfFileImpl* oat_file); |
Alex Light | 4b0d2d9 | 2014-08-06 13:37:23 -0700 | [diff] [blame] | 84 | // Templatized version to actually do the patching with the right sized offsets. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 85 | template <typename ElfFileImpl, typename patch_loc_t> bool PatchTextSection(ElfFileImpl* oat_file); |
| 86 | template <typename ElfFileImpl, typename patch_loc_t> bool CheckOatFile(ElfFileImpl* oat_filec); |
| 87 | template <typename ElfFileImpl> |
| 88 | bool PatchOatHeader(ElfFileImpl* oat_file); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 89 | |
| 90 | bool PatchImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 91 | |
| 92 | bool WriteElf(File* out); |
| 93 | bool WriteImage(File* out); |
| 94 | |
| 95 | mirror::Object* RelocatedCopyOf(mirror::Object*); |
| 96 | mirror::Object* RelocatedAddressOf(mirror::Object* obj); |
| 97 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 98 | // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not |
| 99 | // change the heap. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 100 | class PatchVisitor { |
| 101 | public: |
| 102 | PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {} |
| 103 | ~PatchVisitor() {} |
| 104 | void operator() (mirror::Object* obj, MemberOffset off, bool b) const |
| 105 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 106 | // For reference classes. |
| 107 | void operator() (mirror::Class* cls, mirror::Reference* ref) const |
| 108 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 109 | private: |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 110 | PatchOat* const patcher_; |
| 111 | mirror::Object* const copy_; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 114 | // The elf file we are patching. |
| 115 | std::unique_ptr<ElfFile> oat_file_; |
| 116 | // A mmap of the image we are patching. This is modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 117 | const MemMap* const image_; |
| 118 | // The bitmap over the image within the heap we are patching. This is not modified. |
| 119 | gc::accounting::ContinuousSpaceBitmap* const bitmap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 120 | // The heap we are patching. This is not modified. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 121 | const MemMap* const heap_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 122 | // The amount we are changing the offset by. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 123 | const off_t delta_; |
| 124 | // Timing splits. |
| 125 | TimingLogger* const timings_; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 126 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 127 | DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat); |
| 128 | }; |
| 129 | |
| 130 | } // namespace art |
| 131 | #endif // ART_PATCHOAT_PATCHOAT_H_ |