blob: 0b408e85cc7649f569fa8858bb177113267f369f [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),
Ian Rogers848871b2013-08-05 10:56:33 -070042 oat_data_begin_(NULL), interpreter_to_interpreter_bridge_offset_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070043 interpreter_to_compiled_code_bridge_offset_(0), portable_imt_conflict_trampoline_offset_(0),
44 portable_resolution_trampoline_offset_(0), quick_imt_conflict_trampoline_offset_(0),
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 quick_resolution_trampoline_offset_(0) {}
46
47 ~ImageWriter() {}
48
49 bool Write(const std::string& image_filename,
50 uintptr_t image_begin,
51 const std::string& oat_filename,
52 const std::string& oat_location)
53 LOCKS_EXCLUDED(Locks::mutator_lock_);
54
55 uintptr_t GetOatDataBegin() {
56 return reinterpret_cast<uintptr_t>(oat_data_begin_);
57 }
58
59 private:
60 bool AllocMemory();
61
Mathieu Chartier31e89252013-08-28 11:29:12 -070062 // Mark the objects defined in this space in the given live bitmap.
63 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
64
65 // We use the lock word to store the offset of the object in the image.
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 void AssignImageOffset(mirror::Object* object)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68 DCHECK(object != NULL);
69 SetImageOffset(object, image_end_);
70 image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment
71 DCHECK_LT(image_end_, image_->Size());
72 }
73
74 void SetImageOffset(mirror::Object* object, size_t offset) {
75 DCHECK(object != NULL);
76 DCHECK_NE(offset, 0U);
77 DCHECK(!IsImageOffsetAssigned(object));
78 offsets_.Put(object, offset);
79 }
80
81 size_t IsImageOffsetAssigned(const mirror::Object* object) const {
82 DCHECK(object != NULL);
83 return offsets_.find(object) != offsets_.end();
84 }
85
86 size_t GetImageOffset(const mirror::Object* object) const {
87 DCHECK(object != NULL);
88 DCHECK(IsImageOffsetAssigned(object));
89 return offsets_.find(object)->second;
90 }
91
92 mirror::Object* GetImageAddress(const mirror::Object* object) const {
93 if (object == NULL) {
94 return NULL;
95 }
96 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
97 }
98
99 mirror::Object* GetLocalAddress(const mirror::Object* object) const {
100 size_t offset = GetImageOffset(object);
101 byte* dst = image_->Begin() + offset;
102 return reinterpret_cast<mirror::Object*>(dst);
103 }
104
105 const byte* GetOatAddress(uint32_t offset) const {
106#if !defined(ART_USE_PORTABLE_COMPILER)
107 // With Quick, code is within the OatFile, as there are all in one
108 // .o ELF object. However with Portable, the code is always in
109 // different .o ELF objects.
110 DCHECK_LT(offset, oat_file_->Size());
111#endif
112 if (offset == 0) {
113 return NULL;
114 }
115 return oat_data_begin_ + offset;
116 }
117
118 // Returns true if the class was in the original requested image classes list.
119 bool IsImageClass(const mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120
121 // Debug aid that list of requested image classes.
122 void DumpImageClasses();
123
124 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
125 void ComputeLazyFieldsForImageClasses()
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
127 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129
130 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
131 void ComputeEagerResolvedStrings();
132 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
134
135 // Remove unwanted classes from various roots.
136 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
137 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
139
140 // Verify unwanted classes removed.
141 void CheckNonImageClassesRemoved();
142 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144
145 // Lays out where the image objects will be at runtime.
146 void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
150 static void CalculateNewObjectOffsetsCallback(mirror::Object* obj, void* arg)
151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
152
153 // Creates the contiguous image in memory and adjusts pointers.
154 void CopyAndFixupObjects();
155 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157 void FixupClass(const mirror::Class* orig, mirror::Class* copy)
158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -0700159 void FixupMethod(const mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
161 void FixupObject(const mirror::Object* orig, mirror::Object* copy)
162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
163 void FixupObjectArray(const mirror::ObjectArray<mirror::Object>* orig,
164 mirror::ObjectArray<mirror::Object>* copy)
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166 void FixupInstanceFields(const mirror::Object* orig, mirror::Object* copy)
167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
168 void FixupStaticFields(const mirror::Class* orig, mirror::Class* copy)
169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
170 void FixupFields(const mirror::Object* orig, mirror::Object* copy, uint32_t ref_offsets,
171 bool is_static)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
173
174 // Patches references in OatFile to expect runtime addresses.
175 void PatchOatCodeAndMethods()
176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
177 void SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value)
178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179
180
181 const CompilerDriver& compiler_driver_;
182
183 // Map of Object to where it will be at runtime.
184 SafeMap<const mirror::Object*, size_t> offsets_;
185
186 // oat file with code for this image
187 OatFile* oat_file_;
188
189 // Memory mapped for generating the image.
190 UniquePtr<MemMap> image_;
191
192 // Offset to the free space in image_.
193 size_t image_end_;
194
195 // Beginning target image address for the output image.
196 byte* image_begin_;
197
198 // Beginning target oat address for the pointers from the output image to its oat file.
199 const byte* oat_data_begin_;
200
Mathieu Chartier31e89252013-08-28 11:29:12 -0700201 // Image bitmap which lets us know where the objects inside of the image reside.
202 UniquePtr<gc::accounting::SpaceBitmap> image_bitmap_;
203
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700205 uint32_t interpreter_to_interpreter_bridge_offset_;
206 uint32_t interpreter_to_compiled_code_bridge_offset_;
207 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700208 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700210 uint32_t portable_to_interpreter_bridge_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700211 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700213 uint32_t quick_to_interpreter_bridge_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214
215 // DexCaches seen while scanning for fixing up CodeAndDirectMethods
Mathieu Chartier02e25112013-08-14 16:14:24 -0700216 std::set<mirror::DexCache*> dex_caches_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217};
218
219} // namespace art
220
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700221#endif // ART_COMPILER_IMAGE_WRITER_H_