blob: 92b24f60674446adf70776a603b7d083f9c6d8cf [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),
Andreas Gampe2da88232014-02-27 12:26:20 -080044 portable_resolution_trampoline_offset_(0), quick_generic_jni_trampoline_offset_(0),
45 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070046
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 FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148
149 // Patches references in OatFile to expect runtime addresses.
150 void PatchOatCodeAndMethods()
151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
152 void SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value)
153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 const CompilerDriver& compiler_driver_;
156
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 // oat file with code for this image
158 OatFile* oat_file_;
159
160 // Memory mapped for generating the image.
161 UniquePtr<MemMap> image_;
162
163 // Offset to the free space in image_.
164 size_t image_end_;
165
166 // Beginning target image address for the output image.
167 byte* image_begin_;
168
Mathieu Chartier590fee92013-09-13 13:46:47 -0700169 // Saved hashes (objects are inside of the image so that they don't move).
170 std::vector<std::pair<mirror::Object*, uint32_t> > saved_hashes_;
171
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 // Beginning target oat address for the pointers from the output image to its oat file.
173 const byte* oat_data_begin_;
174
Mathieu Chartier31e89252013-08-28 11:29:12 -0700175 // Image bitmap which lets us know where the objects inside of the image reside.
176 UniquePtr<gc::accounting::SpaceBitmap> image_bitmap_;
177
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700179 uint32_t interpreter_to_interpreter_bridge_offset_;
180 uint32_t interpreter_to_compiled_code_bridge_offset_;
181 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700182 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700184 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800185 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700186 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700188 uint32_t quick_to_interpreter_bridge_offset_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700189
190 friend class FixupVisitor;
191 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192};
193
194} // namespace art
195
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700196#endif // ART_COMPILER_IMAGE_WRITER_H_