blob: 5a3545bb7b510accccee99f3fe68141f67fee4ee [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
Ian Rogersd582fa42014-11-05 23:46:43 -080020#include "arch/instruction_set.h"
Alex Light53cb16b2014-06-12 11:26:29 -070021#include "base/macros.h"
22#include "base/mutex.h"
Alex Light53cb16b2014-06-12 11:26:29 -070023#include "elf_file.h"
24#include "elf_utils.h"
25#include "gc/accounting/space_bitmap.h"
26#include "gc/heap.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080027#include "os.h"
Alex Light53cb16b2014-06-12 11:26:29 -070028#include "utils.h"
29
30namespace art {
31
32class ImageHeader;
Igor Murashkin46774762014-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;
Andreas Gampec8ccf682014-09-29 20:07:43 -070040} // namespace mirror
Alex Light53cb16b2014-06-12 11:26:29 -070041
42class PatchOat {
43 public:
Igor Murashkin46774762014-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 Murashkin46774762014-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 Murashkin46774762014-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 Murashkin46774762014-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)
Ian Rogersd4c4d952014-10-16 20:31:53 -070063 : oat_file_(oat_file), image_(nullptr), bitmap_(nullptr), heap_(nullptr), delta_(delta),
64 timings_(timings) {}
Alex Light53cb16b2014-06-12 11:26:29 -070065 PatchOat(MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070066 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070067 : image_(image), bitmap_(bitmap), heap_(heap),
68 delta_(delta), timings_(timings) {}
69 PatchOat(ElfFile* oat_file, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
Alex Lighteefbe392014-07-08 09:53:18 -070070 MemMap* heap, off_t delta, TimingLogger* timings)
Alex Light53cb16b2014-06-12 11:26:29 -070071 : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
72 delta_(delta), timings_(timings) {}
73 ~PatchOat() {}
74
Igor Murashkin46774762014-10-22 11:37:02 -070075 // 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 Light53cb16b2014-06-12 11:26:29 -070095 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 Light53cb16b2014-06-12 11:26:29 -0700106 // Patches oat in place, modifying the oat_file given to the constructor.
107 bool PatchElf();
Tong Shen62d1ca32014-09-03 17:24:56 -0700108 template <typename ElfFileImpl>
109 bool PatchElf(ElfFileImpl* oat_file);
110 template <typename ElfFileImpl>
111 bool PatchTextSection(ElfFileImpl* oat_file);
Alex Light4b0d2d92014-08-06 13:37:23 -0700112 // Templatized version to actually do the patching with the right sized offsets.
Tong Shen62d1ca32014-09-03 17:24:56 -0700113 template <typename ElfFileImpl, typename patch_loc_t> bool PatchTextSection(ElfFileImpl* oat_file);
114 template <typename ElfFileImpl, typename patch_loc_t> bool CheckOatFile(ElfFileImpl* oat_filec);
115 template <typename ElfFileImpl>
116 bool PatchOatHeader(ElfFileImpl* oat_file);
Alex Light53cb16b2014-06-12 11:26:29 -0700117
118 bool PatchImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
119
120 bool WriteElf(File* out);
121 bool WriteImage(File* out);
122
123 mirror::Object* RelocatedCopyOf(mirror::Object*);
124 mirror::Object* RelocatedAddressOf(mirror::Object* obj);
125
Igor Murashkin46774762014-10-22 11:37:02 -0700126 // Look up the oat header from any elf file.
127 static const OatHeader* GetOatHeader(const ElfFile* elf_file);
128
129 // Templatized version to actually look up the oat header
130 template <typename ElfFileImpl>
131 static const OatHeader* GetOatHeader(const ElfFileImpl* elf_file);
132
Alex Lighteefbe392014-07-08 09:53:18 -0700133 // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
134 // change the heap.
Alex Light53cb16b2014-06-12 11:26:29 -0700135 class PatchVisitor {
136 public:
137 PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
138 ~PatchVisitor() {}
139 void operator() (mirror::Object* obj, MemberOffset off, bool b) const
140 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
141 // For reference classes.
142 void operator() (mirror::Class* cls, mirror::Reference* ref) const
143 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
144 private:
Ian Rogersd4c4d952014-10-16 20:31:53 -0700145 PatchOat* const patcher_;
146 mirror::Object* const copy_;
Alex Light53cb16b2014-06-12 11:26:29 -0700147 };
148
Alex Lighteefbe392014-07-08 09:53:18 -0700149 // The elf file we are patching.
150 std::unique_ptr<ElfFile> oat_file_;
151 // A mmap of the image we are patching. This is modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700152 const MemMap* const image_;
153 // The bitmap over the image within the heap we are patching. This is not modified.
154 gc::accounting::ContinuousSpaceBitmap* const bitmap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700155 // The heap we are patching. This is not modified.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700156 const MemMap* const heap_;
Alex Lighteefbe392014-07-08 09:53:18 -0700157 // The amount we are changing the offset by.
Ian Rogersd4c4d952014-10-16 20:31:53 -0700158 const off_t delta_;
159 // Timing splits.
160 TimingLogger* const timings_;
Alex Lighteefbe392014-07-08 09:53:18 -0700161
Alex Light53cb16b2014-06-12 11:26:29 -0700162 DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
163};
164
165} // namespace art
166#endif // ART_PATCHOAT_PATCHOAT_H_