blob: 9086d58fc735514ce27f1088cba00b9ae5e85268 [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
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
30namespace art {
31
32class ImageHeader;
33
34namespace mirror {
35class Object;
36class Reference;
37class Class;
38class ArtMethod;
Andreas Gampec8ccf682014-09-29 20:07:43 -070039} // namespace mirror
Alex Light53cb16b2014-06-12 11:26:29 -070040
41class PatchOat {
42 public:
Alex Lighteefbe392014-07-08 09:53:18 -070043 static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070044
45 static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
Alex Lighteefbe392014-07-08 09:53:18 -070046 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070047
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 Lighteefbe392014-07-08 09:53:18 -070050 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070051
52 private:
Alex Light53cb16b2014-06-12 11:26:29 -070053 // Takes ownership only of the ElfFile. All other pointers are only borrowed.
Alex Lighteefbe392014-07-08 09:53:18 -070054 PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070055 : oat_file_(oat_file), delta_(delta), timings_(timings) {}
56 PatchOat(MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070057 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070058 : image_(image), bitmap_(bitmap), heap_(heap),
59 delta_(delta), timings_(timings) {}
60 PatchOat(ElfFile* oat_file, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070061 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070062 : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
63 delta_(delta), timings_(timings) {}
64 ~PatchOat() {}
65
66 static void BitmapCallback(mirror::Object* obj, void* arg)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68 reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
69 }
70
71 void VisitObject(mirror::Object* obj)
72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
73 void FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy)
74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
75 bool InHeap(mirror::Object*);
76
Alex Light53cb16b2014-06-12 11:26:29 -070077 // Patches oat in place, modifying the oat_file given to the constructor.
78 bool PatchElf();
79 bool PatchTextSection();
Alex Light4b0d2d92014-08-06 13:37:23 -070080 // Templatized version to actually do the patching with the right sized offsets.
81 template <typename ptr_t> bool PatchTextSection(const Elf32_Shdr& patches_sec);
82 template <typename ptr_t> bool CheckOatFile(const Elf32_Shdr& patches_sec);
Alex Lighta59dd802014-07-02 16:28:08 -070083 bool PatchOatHeader();
Alex Light53cb16b2014-06-12 11:26:29 -070084 bool PatchSymbols(Elf32_Shdr* section);
85
86 bool PatchImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
87
88 bool WriteElf(File* out);
89 bool WriteImage(File* out);
90
91 mirror::Object* RelocatedCopyOf(mirror::Object*);
92 mirror::Object* RelocatedAddressOf(mirror::Object* obj);
93
Alex Lighteefbe392014-07-08 09:53:18 -070094 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
95 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -070096 class PatchVisitor {
97 public:
98 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
99 ~PatchVisitor() {}
100 void operator() (mirror::Object* obj, MemberOffset off, bool b) const
101 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
102 // For reference classes.
103 void operator() (mirror::Class* cls, mirror::Reference* ref) const
104 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
105 private:
106 PatchOat* patcher_;
107 mirror::Object* copy_;
108 };
109
Alex Lighteefbe392014-07-08 09:53:18 -0700110 // The elf file we are patching.
111 std::unique_ptr<ElfFile> oat_file_;
112 // A mmap of the image we are patching. This is modified.
113 const MemMap* image_;
114 // The heap we are patching. This is not modified.
115 gc::accounting::ContinuousSpaceBitmap* bitmap_;
116 // The heap we are patching. This is not modified.
117 const MemMap* heap_;
118 // The amount we are changing the offset by.
119 off_t delta_;
120 TimingLogger* timings_;
121
Alex Light53cb16b2014-06-12 11:26:29 -0700122 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
123};
124
125} // namespace art
126#endif // ART_PATCHOAT_PATCHOAT_H_