blob: 21041fbcaef3da9b0c3929a8b5fbb138fbbfd414 [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;
Igor Murashkin90ca5c02014-10-22 11:37:02 -070033class OatHeader;
Alex Light53cb16b2014-06-12 11:26:29 -070034
35namespace mirror {
36class Object;
37class Reference;
38class Class;
39class ArtMethod;
Alex Lighteefbe392014-07-08 09:53:18 -070040}; // namespace mirror
Alex Light53cb16b2014-06-12 11:26:29 -070041
42class PatchOat {
43 public:
Igor Murashkin90ca5c02014-10-22 11:37:02 -070044 // Patch only the oat file
45 static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings,
46 bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ?
47 bool new_oat_out); // Output oat was a new file created by us?
Alex Light53cb16b2014-06-12 11:26:29 -070048
Igor Murashkin90ca5c02014-10-22 11:37:02 -070049 // Patch only the image (art file)
Alex Light53cb16b2014-06-12 11:26:29 -070050 static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
Alex Lighteefbe392014-07-08 09:53:18 -070051 TimingLogger* timings);
Alex Light53cb16b2014-06-12 11:26:29 -070052
Igor Murashkin90ca5c02014-10-22 11:37:02 -070053 // Patch both the image and the oat file
54 static bool Patch(File* oat_in, const std::string& art_location,
Alex Light53cb16b2014-06-12 11:26:29 -070055 off_t delta, File* oat_out, File* art_out, InstructionSet isa,
Igor Murashkin90ca5c02014-10-22 11:37:02 -070056 TimingLogger* timings,
57 bool output_oat_opened_from_fd, // Was this using --oatput-oat-fd ?
58 bool new_oat_out); // Output oat was a new file created by us?
Alex Light53cb16b2014-06-12 11:26:29 -070059
60 private:
Alex Light53cb16b2014-06-12 11:26:29 -070061 // Takes ownership only of the ElfFile. All other pointers are only borrowed.
Alex Lighteefbe392014-07-08 09:53:18 -070062 PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070063 : oat_file_(oat_file), delta_(delta), timings_(timings) {}
64 PatchOat(MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070065 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070066 : image_(image), bitmap_(bitmap), heap_(heap),
67 delta_(delta), timings_(timings) {}
68 PatchOat(ElfFile* oat_file, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070069 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070070 : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
71 delta_(delta), timings_(timings) {}
72 ~PatchOat() {}
73
Igor Murashkin90ca5c02014-10-22 11:37:02 -070074 // Was the .art image at image_path made with --compile-pic ?
75 static bool IsImagePic(const ImageHeader& image_header, const std::string& image_path);
76
77 enum MaybePic {
78 NOT_PIC, // Code not pic. Patch as usual.
79 PIC, // Code was pic. Create symlink; skip OAT patching.
80 ERROR_OAT_FILE, // Failed to symlink oat file
81 ERROR_FIRST = ERROR_OAT_FILE,
82 };
83
84 // Was the .oat image at oat_in made with --compile-pic ?
85 static MaybePic IsOatPic(const ElfFile* oat_in);
86
87 // Attempt to replace the file with a symlink
88 // Returns false if it fails
89 static bool ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
90 const std::string& output_oat_filename,
91 bool output_oat_opened_from_fd,
92 bool new_oat_out); // Output oat was newly created?
93
Alex Light53cb16b2014-06-12 11:26:29 -070094 static void BitmapCallback(mirror::Object* obj, void* arg)
95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
96 reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
97 }
98
99 void VisitObject(mirror::Object* obj)
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101 void FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103 bool InHeap(mirror::Object*);
104
105 bool CheckOatFile();
106
107 // Patches oat in place, modifying the oat_file given to the constructor.
108 bool PatchElf();
109 bool PatchTextSection();
Alex Lighta59dd802014-07-02 16:28:08 -0700110 bool PatchOatHeader();
Alex Light53cb16b2014-06-12 11:26:29 -0700111 bool PatchSymbols(Elf32_Shdr* section);
112
113 bool PatchImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114
115 bool WriteElf(File* out);
116 bool WriteImage(File* out);
117
118 mirror::Object* RelocatedCopyOf(mirror::Object*);
119 mirror::Object* RelocatedAddressOf(mirror::Object* obj);
120
Igor Murashkin90ca5c02014-10-22 11:37:02 -0700121 // Look up the oat header from any elf file.
122 static const OatHeader* GetOatHeader(const ElfFile* elf_file);
123
Alex Lighteefbe392014-07-08 09:53:18 -0700124 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
125 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700126 class PatchVisitor {
127 public:
128 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
129 ~PatchVisitor() {}
130 void operator() (mirror::Object* obj, MemberOffset off, bool b) const
131 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
132 // For reference classes.
133 void operator() (mirror::Class* cls, mirror::Reference* ref) const
134 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
135 private:
136 PatchOat* patcher_;
137 mirror::Object* copy_;
138 };
139
Alex Lighteefbe392014-07-08 09:53:18 -0700140 // The elf file we are patching.
141 std::unique_ptr<ElfFile> oat_file_;
142 // A mmap of the image we are patching. This is modified.
143 const MemMap* image_;
144 // The heap we are patching. This is not modified.
145 gc::accounting::ContinuousSpaceBitmap* bitmap_;
146 // The heap we are patching. This is not modified.
147 const MemMap* heap_;
148 // The amount we are changing the offset by.
149 off_t delta_;
150 TimingLogger* timings_;
151
Alex Light53cb16b2014-06-12 11:26:29 -0700152 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
153};
154
155} // namespace art
156#endif // ART_PATCHOAT_PATCHOAT_H_