blob: b9f36d4f8dc3ce60d5d2004e04f6d3534c7b2cde [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;
39};
40
41int patchoat(int argc, char** argv);
42
43class PatchOat {
44 public:
45 static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger& timings);
46
47 static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
48 TimingLogger& timings);
49
50 static bool Patch(const File* oat_in, const std::string& art_location,
51 off_t delta, File* oat_out, File* art_out, InstructionSet isa,
52 TimingLogger& timings);
53
54 private:
55 std::unique_ptr<ElfFile> oat_file_;
56 MemMap* image_;
57 gc::accounting::ContinuousSpaceBitmap* bitmap_;
58 MemMap* heap_;
59 off_t delta_;
60 TimingLogger& timings_;
61
62 // Takes ownership only of the ElfFile. All other pointers are only borrowed.
63 PatchOat(ElfFile* oat_file, off_t delta, TimingLogger& timings)
64 : oat_file_(oat_file), delta_(delta), timings_(timings) {}
65 PatchOat(MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
66 MemMap* heap, off_t delta, TimingLogger& timings)
67 : image_(image), bitmap_(bitmap), heap_(heap),
68 delta_(delta), timings_(timings) {}
69 PatchOat(ElfFile* oat_file, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
70 MemMap* heap, off_t delta, TimingLogger& timings)
71 : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
72 delta_(delta), timings_(timings) {}
73 ~PatchOat() {}
74
75 static void BitmapCallback(mirror::Object* obj, void* arg)
76 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
77 reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
78 }
79
80 void VisitObject(mirror::Object* obj)
81 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82 void FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
84 bool InHeap(mirror::Object*);
85
86 bool CheckOatFile();
87
88 // Patches oat in place, modifying the oat_file given to the constructor.
89 bool PatchElf();
90 bool PatchTextSection();
91 bool PatchSymbols(Elf32_Shdr* section);
92
93 bool PatchImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94
95 bool WriteElf(File* out);
96 bool WriteImage(File* out);
97
98 mirror::Object* RelocatedCopyOf(mirror::Object*);
99 mirror::Object* RelocatedAddressOf(mirror::Object* obj);
100
101 class PatchVisitor {
102 public:
103 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
104 ~PatchVisitor() {}
105 void operator() (mirror::Object* obj, MemberOffset off, bool b) const
106 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
107 // For reference classes.
108 void operator() (mirror::Class* cls, mirror::Reference* ref) const
109 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
110 private:
111 PatchOat* patcher_;
112 mirror::Object* copy_;
113 };
114
115 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
116};
117
118} // namespace art
119#endif // ART_PATCHOAT_PATCHOAT_H_