blob: e43ec6338f892f34f563dcc6e683558b338a6f1d [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>
23#include <set>
24#include <string>
25
26#include "driver/compiler_driver.h"
27#include "mem_map.h"
28#include "oat_file.h"
29#include "mirror/dex_cache.h"
30#include "os.h"
31#include "safe_map.h"
32#include "gc/space/space.h"
33#include "UniquePtr.h"
34
35namespace art {
36
37// Write a Space built during compilation for use during execution.
38class ImageWriter {
39 public:
40 explicit ImageWriter(const CompilerDriver& compiler_driver)
41 : compiler_driver_(compiler_driver), oat_file_(NULL), image_end_(0), image_begin_(NULL),
42 oat_data_begin_(NULL), interpreter_to_interpreter_entry_offset_(0),
43 interpreter_to_quick_entry_offset_(0), portable_resolution_trampoline_offset_(0),
44 quick_resolution_trampoline_offset_(0) {}
45
46 ~ImageWriter() {}
47
48 bool Write(const std::string& image_filename,
49 uintptr_t image_begin,
50 const std::string& oat_filename,
51 const std::string& oat_location)
52 LOCKS_EXCLUDED(Locks::mutator_lock_);
53
54 uintptr_t GetOatDataBegin() {
55 return reinterpret_cast<uintptr_t>(oat_data_begin_);
56 }
57
58 private:
59 bool AllocMemory();
60
61 // we use the lock word to store the offset of the object in the image
62 void AssignImageOffset(mirror::Object* object)
63 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
64 DCHECK(object != NULL);
65 SetImageOffset(object, image_end_);
66 image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment
67 DCHECK_LT(image_end_, image_->Size());
68 }
69
70 void SetImageOffset(mirror::Object* object, size_t offset) {
71 DCHECK(object != NULL);
72 DCHECK_NE(offset, 0U);
73 DCHECK(!IsImageOffsetAssigned(object));
74 offsets_.Put(object, offset);
75 }
76
77 size_t IsImageOffsetAssigned(const mirror::Object* object) const {
78 DCHECK(object != NULL);
79 return offsets_.find(object) != offsets_.end();
80 }
81
82 size_t GetImageOffset(const mirror::Object* object) const {
83 DCHECK(object != NULL);
84 DCHECK(IsImageOffsetAssigned(object));
85 return offsets_.find(object)->second;
86 }
87
88 mirror::Object* GetImageAddress(const mirror::Object* object) const {
89 if (object == NULL) {
90 return NULL;
91 }
92 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
93 }
94
95 mirror::Object* GetLocalAddress(const mirror::Object* object) const {
96 size_t offset = GetImageOffset(object);
97 byte* dst = image_->Begin() + offset;
98 return reinterpret_cast<mirror::Object*>(dst);
99 }
100
101 const byte* GetOatAddress(uint32_t offset) const {
102#if !defined(ART_USE_PORTABLE_COMPILER)
103 // With Quick, code is within the OatFile, as there are all in one
104 // .o ELF object. However with Portable, the code is always in
105 // different .o ELF objects.
106 DCHECK_LT(offset, oat_file_->Size());
107#endif
108 if (offset == 0) {
109 return NULL;
110 }
111 return oat_data_begin_ + offset;
112 }
113
114 // Returns true if the class was in the original requested image classes list.
115 bool IsImageClass(const mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
117 // Debug aid that list of requested image classes.
118 void DumpImageClasses();
119
120 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
121 void ComputeLazyFieldsForImageClasses()
122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
123 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
125
126 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
127 void ComputeEagerResolvedStrings();
128 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
130
131 // Remove unwanted classes from various roots.
132 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
133 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135
136 // Verify unwanted classes removed.
137 void CheckNonImageClassesRemoved();
138 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
140
141 // Lays out where the image objects will be at runtime.
142 void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
146 static void CalculateNewObjectOffsetsCallback(mirror::Object* obj, void* arg)
147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148
149 // Creates the contiguous image in memory and adjusts pointers.
150 void CopyAndFixupObjects();
151 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
153 void FixupClass(const mirror::Class* orig, mirror::Class* copy)
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155 void FixupMethod(const mirror::AbstractMethod* orig, mirror::AbstractMethod* copy)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157 void FixupObject(const mirror::Object* orig, mirror::Object* copy)
158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
159 void FixupObjectArray(const mirror::ObjectArray<mirror::Object>* orig,
160 mirror::ObjectArray<mirror::Object>* copy)
161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
162 void FixupInstanceFields(const mirror::Object* orig, mirror::Object* copy)
163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
164 void FixupStaticFields(const mirror::Class* orig, mirror::Class* copy)
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166 void FixupFields(const mirror::Object* orig, mirror::Object* copy, uint32_t ref_offsets,
167 bool is_static)
168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
169
170 // Patches references in OatFile to expect runtime addresses.
171 void PatchOatCodeAndMethods()
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
173 void SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value)
174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
175
176
177 const CompilerDriver& compiler_driver_;
178
179 // Map of Object to where it will be at runtime.
180 SafeMap<const mirror::Object*, size_t> offsets_;
181
182 // oat file with code for this image
183 OatFile* oat_file_;
184
185 // Memory mapped for generating the image.
186 UniquePtr<MemMap> image_;
187
188 // Offset to the free space in image_.
189 size_t image_end_;
190
191 // Beginning target image address for the output image.
192 byte* image_begin_;
193
194 // Beginning target oat address for the pointers from the output image to its oat file.
195 const byte* oat_data_begin_;
196
197 // Offset from oat_data_begin_ to the stubs.
198 uint32_t interpreter_to_interpreter_entry_offset_;
199 uint32_t interpreter_to_quick_entry_offset_;
200 uint32_t portable_resolution_trampoline_offset_;
201 uint32_t quick_resolution_trampoline_offset_;
202
203 // DexCaches seen while scanning for fixing up CodeAndDirectMethods
204 typedef std::set<mirror::DexCache*> Set;
205 Set dex_caches_;
206};
207
208} // namespace art
209
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700210#endif // ART_COMPILER_IMAGE_WRITER_H_