blob: bdf06148ece523eecf768acc3919036681926ffe [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_IMAGE_WRITER_H_
18#define ART_COMPILER_IMAGE_WRITER_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include <stdint.h>
21
22#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070023#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include <set>
25#include <string>
26
27#include "driver/compiler_driver.h"
28#include "mem_map.h"
29#include "oat_file.h"
30#include "mirror/dex_cache.h"
31#include "os.h"
32#include "safe_map.h"
33#include "gc/space/space.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034
35namespace art {
36
37// Write a Space built during compilation for use during execution.
38class ImageWriter {
39 public:
Vladimir Markof4da6752014-08-01 19:04:18 +010040 ImageWriter(const CompilerDriver& compiler_driver, uintptr_t image_begin)
41 : compiler_driver_(compiler_driver), image_begin_(reinterpret_cast<byte*>(image_begin)),
42 image_end_(0), image_roots_address_(0), oat_file_(NULL),
Ian Rogers848871b2013-08-05 10:56:33 -070043 oat_data_begin_(NULL), interpreter_to_interpreter_bridge_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010044 interpreter_to_compiled_code_bridge_offset_(0), jni_dlsym_lookup_offset_(0),
45 portable_imt_conflict_trampoline_offset_(0), portable_resolution_trampoline_offset_(0),
46 portable_to_interpreter_bridge_offset_(0), quick_generic_jni_trampoline_offset_(0),
47 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
48 quick_to_interpreter_bridge_offset_(0) {
49 CHECK_NE(image_begin, 0U);
50 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070051
52 ~ImageWriter() {}
53
Vladimir Markof4da6752014-08-01 19:04:18 +010054 bool PrepareImageAddressSpace();
55
56 bool IsImageAddressSpaceReady() const {
57 return image_roots_address_ != 0u;
58 }
59
60 mirror::Object* GetImageAddress(mirror::Object* object) const
61 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
62 if (object == NULL) {
63 return NULL;
64 }
65 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
66 }
67
68 byte* GetOatFileBegin() const {
69 return image_begin_ + RoundUp(image_end_, kPageSize);
70 }
71
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 bool Write(const std::string& image_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 const std::string& oat_filename,
74 const std::string& oat_location)
75 LOCKS_EXCLUDED(Locks::mutator_lock_);
76
77 uintptr_t GetOatDataBegin() {
78 return reinterpret_cast<uintptr_t>(oat_data_begin_);
79 }
80
81 private:
82 bool AllocMemory();
83
Mathieu Chartier31e89252013-08-28 11:29:12 -070084 // Mark the objects defined in this space in the given live bitmap.
85 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
87 // We use the lock word to store the offset of the object in the image.
Mathieu Chartier590fee92013-09-13 13:46:47 -070088 void AssignImageOffset(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89 void SetImageOffset(mirror::Object* object, size_t offset)
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070091 bool IsImageOffsetAssigned(mirror::Object* object) const
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070094
Alex Lighta59dd802014-07-02 16:28:08 -070095 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
97 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
98 }
99
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700100 mirror::Object* GetLocalAddress(mirror::Object* object) const
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 size_t offset = GetImageOffset(object);
103 byte* dst = image_->Begin() + offset;
104 return reinterpret_cast<mirror::Object*>(dst);
105 }
106
107 const byte* GetOatAddress(uint32_t offset) const {
108#if !defined(ART_USE_PORTABLE_COMPILER)
109 // With Quick, code is within the OatFile, as there are all in one
110 // .o ELF object. However with Portable, the code is always in
111 // different .o ELF objects.
112 DCHECK_LT(offset, oat_file_->Size());
113#endif
114 if (offset == 0) {
115 return NULL;
116 }
117 return oat_data_begin_ + offset;
118 }
119
120 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122
123 // Debug aid that list of requested image classes.
124 void DumpImageClasses();
125
126 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
127 void ComputeLazyFieldsForImageClasses()
128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
131
132 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700133 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
136
137 // Remove unwanted classes from various roots.
138 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
139 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
141
142 // Verify unwanted classes removed.
143 void CheckNonImageClassesRemoved();
144 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
146
147 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100148 void CalculateNewObjectOffsets()
149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
150 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
152 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700154 void CalculateObjectOffsets(mirror::Object* obj)
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156
157 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
159 void WalkFieldsInOrder(mirror::Object* obj)
160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
161 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
163
164 // Creates the contiguous image in memory and adjusts pointers.
165 void CopyAndFixupObjects();
166 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700173 // Get quick code for non-resolution/imt_conflict/abstract method.
174 const byte* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
176
177 const byte* GetQuickEntryPoint(mirror::ArtMethod* method)
178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100181 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 const CompilerDriver& compiler_driver_;
184
Vladimir Markof4da6752014-08-01 19:04:18 +0100185 // Beginning target image address for the output image.
186 byte* image_begin_;
187
188 // Offset to the free space in image_.
189 size_t image_end_;
190
191 // The image roots address in the image.
192 uint32_t image_roots_address_;
193
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 // oat file with code for this image
195 OatFile* oat_file_;
196
197 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700198 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199
Mathieu Chartier590fee92013-09-13 13:46:47 -0700200 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700201 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700202
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 // Beginning target oat address for the pointers from the output image to its oat file.
204 const byte* oat_data_begin_;
205
Mathieu Chartier31e89252013-08-28 11:29:12 -0700206 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700207 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700208
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700210 uint32_t interpreter_to_interpreter_bridge_offset_;
211 uint32_t interpreter_to_compiled_code_bridge_offset_;
212 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700213 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700215 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800216 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700217 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700219 uint32_t quick_to_interpreter_bridge_offset_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700220
221 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700222 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700223 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224};
225
226} // namespace art
227
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700228#endif // ART_COMPILER_IMAGE_WRITER_H_