blob: 12171451ae1c617b99209833a2384b2c362932ba [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 Murashkin46774762014-10-22 11:37:02 -070027#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "driver/compiler_driver.h"
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -080029#include "gc/space/space.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "mem_map.h"
31#include "oat_file.h"
32#include "mirror/dex_cache.h"
33#include "os.h"
34#include "safe_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
36namespace art {
37
38// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070039class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 public:
Igor Murashkin46774762014-10-22 11:37:02 -070041 ImageWriter(const CompilerDriver& compiler_driver, uintptr_t image_begin,
42 bool compile_pic)
Ian Rogers13735952014-10-08 12:43:28 -070043 : compiler_driver_(compiler_driver), image_begin_(reinterpret_cast<uint8_t*>(image_begin)),
Igor Murashkin46774762014-10-22 11:37:02 -070044 image_end_(0), image_roots_address_(0), oat_file_(nullptr),
45 oat_data_begin_(nullptr), interpreter_to_interpreter_bridge_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010046 interpreter_to_compiled_code_bridge_offset_(0), jni_dlsym_lookup_offset_(0),
47 portable_imt_conflict_trampoline_offset_(0), portable_resolution_trampoline_offset_(0),
48 portable_to_interpreter_bridge_offset_(0), quick_generic_jni_trampoline_offset_(0),
49 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
Igor Murashkin46774762014-10-22 11:37:02 -070050 quick_to_interpreter_bridge_offset_(0), compile_pic_(compile_pic) {
Vladimir Markof4da6752014-08-01 19:04:18 +010051 CHECK_NE(image_begin, 0U);
52 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070053
54 ~ImageWriter() {}
55
Vladimir Markof4da6752014-08-01 19:04:18 +010056 bool PrepareImageAddressSpace();
57
58 bool IsImageAddressSpaceReady() const {
59 return image_roots_address_ != 0u;
60 }
61
62 mirror::Object* GetImageAddress(mirror::Object* object) const
63 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Igor Murashkin46774762014-10-22 11:37:02 -070064 if (object == nullptr) {
65 return nullptr;
Vladimir Markof4da6752014-08-01 19:04:18 +010066 }
67 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
68 }
69
Ian Rogers13735952014-10-08 12:43:28 -070070 uint8_t* GetOatFileBegin() const {
Vladimir Markof4da6752014-08-01 19:04:18 +010071 return image_begin_ + RoundUp(image_end_, kPageSize);
72 }
73
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 bool Write(const std::string& image_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 const std::string& oat_filename,
76 const std::string& oat_location)
77 LOCKS_EXCLUDED(Locks::mutator_lock_);
78
79 uintptr_t GetOatDataBegin() {
80 return reinterpret_cast<uintptr_t>(oat_data_begin_);
81 }
82
83 private:
84 bool AllocMemory();
85
Mathieu Chartier31e89252013-08-28 11:29:12 -070086 // Mark the objects defined in this space in the given live bitmap.
87 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88
89 // We use the lock word to store the offset of the object in the image.
Mathieu Chartier590fee92013-09-13 13:46:47 -070090 void AssignImageOffset(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91 void SetImageOffset(mirror::Object* object, size_t offset)
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070093 bool IsImageOffsetAssigned(mirror::Object* object) const
94 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
95 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070096
Alex Lighta59dd802014-07-02 16:28:08 -070097 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
98 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
99 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
100 }
101
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700102 mirror::Object* GetLocalAddress(mirror::Object* object) const
103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700105 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 return reinterpret_cast<mirror::Object*>(dst);
107 }
108
Ian Rogers13735952014-10-08 12:43:28 -0700109 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110#if !defined(ART_USE_PORTABLE_COMPILER)
111 // With Quick, code is within the OatFile, as there are all in one
112 // .o ELF object. However with Portable, the code is always in
113 // different .o ELF objects.
114 DCHECK_LT(offset, oat_file_->Size());
115#endif
Igor Murashkin46774762014-10-22 11:37:02 -0700116 if (offset == 0u) {
117 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 }
119 return oat_data_begin_ + offset;
120 }
121
122 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124
125 // Debug aid that list of requested image classes.
126 void DumpImageClasses();
127
128 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
129 void ComputeLazyFieldsForImageClasses()
130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
131 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
133
134 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700135 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
138
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800139 // Combine string char arrays.
140 void ProcessStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
141
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 // Remove unwanted classes from various roots.
143 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
146
147 // Verify unwanted classes removed.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800148 void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
151
152 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100153 void CalculateNewObjectOffsets()
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700159 void CalculateObjectOffsets(mirror::Object* obj)
160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
161
162 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
164 void WalkFieldsInOrder(mirror::Object* obj)
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
168
169 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800170 void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800173 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800175 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700178 // Get quick code for non-resolution/imt_conflict/abstract method.
Ian Rogers13735952014-10-08 12:43:28 -0700179 const uint8_t* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
181
Ian Rogers13735952014-10-08 12:43:28 -0700182 const uint8_t* GetQuickEntryPoint(mirror::ArtMethod* method)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
184
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100186 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 const CompilerDriver& compiler_driver_;
189
Vladimir Markof4da6752014-08-01 19:04:18 +0100190 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700191 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100192
193 // Offset to the free space in image_.
194 size_t image_end_;
195
196 // The image roots address in the image.
197 uint32_t image_roots_address_;
198
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 // oat file with code for this image
200 OatFile* oat_file_;
201
202 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700203 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204
Mathieu Chartier590fee92013-09-13 13:46:47 -0700205 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700206 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700207
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700209 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210
Mathieu Chartier31e89252013-08-28 11:29:12 -0700211 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700212 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700213
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700215 uint32_t interpreter_to_interpreter_bridge_offset_;
216 uint32_t interpreter_to_compiled_code_bridge_offset_;
217 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700218 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700220 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800221 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700222 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700224 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700225 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700226
227 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700228 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700229 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230};
231
232} // namespace art
233
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700234#endif // ART_COMPILER_IMAGE_WRITER_H_