blob: a1504eeca859d27a89baa02d7bbad12d06f60477 [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.
Mathieu Chartier590fee92013-09-13 13:46:47 -070066 void AssignImageOffset(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
67 void SetImageOffset(mirror::Object* object, size_t offset)
68 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080069 bool IsImageOffsetAssigned(mirror::Object* object) const;
70 size_t GetImageOffset(mirror::Object* object) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -070071
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 mirror::Object* GetImageAddress(mirror::Object* object) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 if (object == NULL) {
74 return NULL;
75 }
76 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
77 }
78
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 mirror::Object* GetLocalAddress(mirror::Object* object) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 size_t offset = GetImageOffset(object);
81 byte* dst = image_->Begin() + offset;
82 return reinterpret_cast<mirror::Object*>(dst);
83 }
84
85 const byte* GetOatAddress(uint32_t offset) const {
86#if !defined(ART_USE_PORTABLE_COMPILER)
87 // With Quick, code is within the OatFile, as there are all in one
88 // .o ELF object. However with Portable, the code is always in
89 // different .o ELF objects.
90 DCHECK_LT(offset, oat_file_->Size());
91#endif
92 if (offset == 0) {
93 return NULL;
94 }
95 return oat_data_begin_ + offset;
96 }
97
98 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -080099 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100
101 // Debug aid that list of requested image classes.
102 void DumpImageClasses();
103
104 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
105 void ComputeLazyFieldsForImageClasses()
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
109
110 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
111 void ComputeEagerResolvedStrings();
112 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114
115 // Remove unwanted classes from various roots.
116 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
117 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
119
120 // Verify unwanted classes removed.
121 void CheckNonImageClassesRemoved();
122 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
124
125 // Lays out where the image objects will be at runtime.
126 void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
128 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130 void CalculateObjectOffsets(mirror::Object* obj)
131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132
133 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135 void WalkFieldsInOrder(mirror::Object* obj)
136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
137 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
139
140 // Creates the contiguous image in memory and adjusts pointers.
141 void CopyAndFixupObjects();
142 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144 void FixupClass(mirror::Class* orig, mirror::Class* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800150 void FixupObjectArray(mirror::ObjectArray<mirror::Object>* orig,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 mirror::ObjectArray<mirror::Object>* copy)
152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153 void FixupInstanceFields(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155 void FixupStaticFields(mirror::Class* orig, mirror::Class* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 void FixupFields(mirror::Object* orig, mirror::Object* copy, uint32_t ref_offsets,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 bool is_static)
159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
160
161 // Patches references in OatFile to expect runtime addresses.
162 void PatchOatCodeAndMethods()
163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
164 void SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value)
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166
167
168 const CompilerDriver& compiler_driver_;
169
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 // oat file with code for this image
171 OatFile* oat_file_;
172
173 // Memory mapped for generating the image.
174 UniquePtr<MemMap> image_;
175
176 // Offset to the free space in image_.
177 size_t image_end_;
178
179 // Beginning target image address for the output image.
180 byte* image_begin_;
181
Mathieu Chartier590fee92013-09-13 13:46:47 -0700182 // Saved hashes (objects are inside of the image so that they don't move).
183 std::vector<std::pair<mirror::Object*, uint32_t> > saved_hashes_;
184
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 // Beginning target oat address for the pointers from the output image to its oat file.
186 const byte* oat_data_begin_;
187
Mathieu Chartier31e89252013-08-28 11:29:12 -0700188 // Image bitmap which lets us know where the objects inside of the image reside.
189 UniquePtr<gc::accounting::SpaceBitmap> image_bitmap_;
190
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700192 uint32_t interpreter_to_interpreter_bridge_offset_;
193 uint32_t interpreter_to_compiled_code_bridge_offset_;
194 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700195 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700197 uint32_t portable_to_interpreter_bridge_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700198 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700200 uint32_t quick_to_interpreter_bridge_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201};
202
203} // namespace art
204
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700205#endif // ART_COMPILER_IMAGE_WRITER_H_