blob: aff155affccdaeafbe62c9df84de9384d4aa09cd [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:
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 Rogersb0fa5dc2014-04-28 16:47:08 -070069 bool IsImageOffsetAssigned(mirror::Object* object) const
70 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
71 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070072
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070073 mirror::Object* GetImageAddress(mirror::Object* object) const
74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 if (object == NULL) {
76 return NULL;
77 }
78 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
79 }
80
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070081 mirror::Object* GetLocalAddress(mirror::Object* object) const
82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 size_t offset = GetImageOffset(object);
84 byte* dst = image_->Begin() + offset;
85 return reinterpret_cast<mirror::Object*>(dst);
86 }
87
88 const byte* GetOatAddress(uint32_t offset) const {
89#if !defined(ART_USE_PORTABLE_COMPILER)
90 // With Quick, code is within the OatFile, as there are all in one
91 // .o ELF object. However with Portable, the code is always in
92 // different .o ELF objects.
93 DCHECK_LT(offset, oat_file_->Size());
94#endif
95 if (offset == 0) {
96 return NULL;
97 }
98 return oat_data_begin_ + offset;
99 }
100
101 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103
104 // Debug aid that list of requested image classes.
105 void DumpImageClasses();
106
107 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
108 void ComputeLazyFieldsForImageClasses()
109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112
113 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700114 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
117
118 // Remove unwanted classes from various roots.
119 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
123 // Verify unwanted classes removed.
124 void CheckNonImageClassesRemoved();
125 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
127
128 // Lays out where the image objects will be at runtime.
129 void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
131 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133 void CalculateObjectOffsets(mirror::Object* obj)
134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135
136 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
138 void WalkFieldsInOrder(mirror::Object* obj)
139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
140 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
142
143 // Creates the contiguous image in memory and adjusts pointers.
144 void CopyAndFixupObjects();
145 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151
152 // Patches references in OatFile to expect runtime addresses.
153 void PatchOatCodeAndMethods()
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155 void SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 const CompilerDriver& compiler_driver_;
159
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 // oat file with code for this image
161 OatFile* oat_file_;
162
163 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700164 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165
166 // Offset to the free space in image_.
167 size_t image_end_;
168
169 // Beginning target image address for the output image.
170 byte* image_begin_;
171
Mathieu Chartier590fee92013-09-13 13:46:47 -0700172 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700173 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700174
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 // Beginning target oat address for the pointers from the output image to its oat file.
176 const byte* oat_data_begin_;
177
Mathieu Chartier31e89252013-08-28 11:29:12 -0700178 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700179 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700180
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700182 uint32_t interpreter_to_interpreter_bridge_offset_;
183 uint32_t interpreter_to_compiled_code_bridge_offset_;
184 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700185 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700187 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800188 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700189 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700191 uint32_t quick_to_interpreter_bridge_offset_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700192
193 friend class FixupVisitor;
194 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195};
196
197} // namespace art
198
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700199#endif // ART_COMPILER_IMAGE_WRITER_H_