blob: 61365fe230cc6182203474b3a2c63b98e411dd22 [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
Igor Murashkin90ca5c02014-10-22 11:37:02 -070027#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "driver/compiler_driver.h"
29#include "mem_map.h"
30#include "oat_file.h"
31#include "mirror/dex_cache.h"
32#include "os.h"
33#include "safe_map.h"
34#include "gc/space/space.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
36namespace art {
37
38// Write a Space built during compilation for use during execution.
Igor Murashkin90ca5c02014-10-22 11:37:02 -070039class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 public:
41 explicit ImageWriter(const CompilerDriver& compiler_driver)
42 : compiler_driver_(compiler_driver), oat_file_(NULL), image_end_(0), image_begin_(NULL),
Ian Rogers848871b2013-08-05 10:56:33 -070043 oat_data_begin_(NULL), interpreter_to_interpreter_bridge_offset_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070044 interpreter_to_compiled_code_bridge_offset_(0), portable_imt_conflict_trampoline_offset_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080045 portable_resolution_trampoline_offset_(0), quick_generic_jni_trampoline_offset_(0),
Igor Murashkin90ca5c02014-10-22 11:37:02 -070046 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
47 compile_pic_(false) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070048
49 ~ImageWriter() {}
50
51 bool Write(const std::string& image_filename,
52 uintptr_t image_begin,
53 const std::string& oat_filename,
Igor Murashkin90ca5c02014-10-22 11:37:02 -070054 const std::string& oat_location,
55 bool compile_pic)
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 LOCKS_EXCLUDED(Locks::mutator_lock_);
57
58 uintptr_t GetOatDataBegin() {
59 return reinterpret_cast<uintptr_t>(oat_data_begin_);
60 }
61
62 private:
63 bool AllocMemory();
64
Mathieu Chartier31e89252013-08-28 11:29:12 -070065 // Mark the objects defined in this space in the given live bitmap.
66 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
67
68 // We use the lock word to store the offset of the object in the image.
Mathieu Chartier590fee92013-09-13 13:46:47 -070069 void AssignImageOffset(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
70 void SetImageOffset(mirror::Object* object, size_t offset)
71 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070072 bool IsImageOffsetAssigned(mirror::Object* object) const
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
74 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070075
Alex Lighta59dd802014-07-02 16:28:08 -070076 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
78 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
79 }
80
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070081 mirror::Object* GetImageAddress(mirror::Object* object) const
82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 if (object == NULL) {
84 return NULL;
85 }
86 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
87 }
88
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070089 mirror::Object* GetLocalAddress(mirror::Object* object) const
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 size_t offset = GetImageOffset(object);
92 byte* dst = image_->Begin() + offset;
93 return reinterpret_cast<mirror::Object*>(dst);
94 }
95
96 const byte* GetOatAddress(uint32_t offset) const {
97#if !defined(ART_USE_PORTABLE_COMPILER)
98 // With Quick, code is within the OatFile, as there are all in one
99 // .o ELF object. However with Portable, the code is always in
100 // different .o ELF objects.
101 DCHECK_LT(offset, oat_file_->Size());
102#endif
Igor Murashkin90ca5c02014-10-22 11:37:02 -0700103 if (offset == 0u) {
104 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 }
106 return oat_data_begin_ + offset;
107 }
108
109 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111
112 // Debug aid that list of requested image classes.
113 void DumpImageClasses();
114
115 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
116 void ComputeLazyFieldsForImageClasses()
117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
118 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120
121 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700122 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
125
126 // Remove unwanted classes from various roots.
127 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
128 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
130
131 // Verify unwanted classes removed.
132 void CheckNonImageClassesRemoved();
133 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135
136 // Lays out where the image objects will be at runtime.
137 void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
139 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700141 void CalculateObjectOffsets(mirror::Object* obj)
142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
143
144 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
146 void WalkFieldsInOrder(mirror::Object* obj)
147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
150
151 // Creates the contiguous image in memory and adjusts pointers.
152 void CopyAndFixupObjects();
153 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700160 // Get quick code for non-resolution/imt_conflict/abstract method.
161 const byte* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
163
164 const byte* GetQuickEntryPoint(mirror::ArtMethod* method)
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 // Patches references in OatFile to expect runtime addresses.
Alex Light53cb16b2014-06-12 11:26:29 -0700168 void PatchOatCodeAndMethods(File* elf_file)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 const CompilerDriver& compiler_driver_;
172
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 // oat file with code for this image
174 OatFile* oat_file_;
175
176 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700177 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178
179 // Offset to the free space in image_.
180 size_t image_end_;
181
182 // Beginning target image address for the output image.
183 byte* image_begin_;
184
Mathieu Chartier590fee92013-09-13 13:46:47 -0700185 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700186 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700187
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 // Beginning target oat address for the pointers from the output image to its oat file.
189 const byte* oat_data_begin_;
190
Mathieu Chartier31e89252013-08-28 11:29:12 -0700191 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700192 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700193
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700195 uint32_t interpreter_to_interpreter_bridge_offset_;
196 uint32_t interpreter_to_compiled_code_bridge_offset_;
197 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700198 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700200 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800201 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700202 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700204 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin90ca5c02014-10-22 11:37:02 -0700205 bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700206
207 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700208 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700209 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210};
211
212} // namespace art
213
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700214#endif // ART_COMPILER_IMAGE_WRITER_H_