blob: 2fec0aa82dfd605994453191873318f3fd7b75a7 [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),
Mathieu Chartier2d721012014-11-10 11:08:06 -080050 quick_to_interpreter_bridge_offset_(0), compile_pic_(compile_pic),
51 target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())) {
Vladimir Markof4da6752014-08-01 19:04:18 +010052 CHECK_NE(image_begin, 0U);
53 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070054
55 ~ImageWriter() {}
56
Vladimir Markof4da6752014-08-01 19:04:18 +010057 bool PrepareImageAddressSpace();
58
59 bool IsImageAddressSpaceReady() const {
60 return image_roots_address_ != 0u;
61 }
62
63 mirror::Object* GetImageAddress(mirror::Object* object) const
64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Igor Murashkin46774762014-10-22 11:37:02 -070065 if (object == nullptr) {
66 return nullptr;
Vladimir Markof4da6752014-08-01 19:04:18 +010067 }
68 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
69 }
70
Ian Rogers13735952014-10-08 12:43:28 -070071 uint8_t* GetOatFileBegin() const {
Vladimir Markof4da6752014-08-01 19:04:18 +010072 return image_begin_ + RoundUp(image_end_, kPageSize);
73 }
74
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 bool Write(const std::string& image_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 const std::string& oat_filename,
77 const std::string& oat_location)
78 LOCKS_EXCLUDED(Locks::mutator_lock_);
79
80 uintptr_t GetOatDataBegin() {
81 return reinterpret_cast<uintptr_t>(oat_data_begin_);
82 }
83
84 private:
85 bool AllocMemory();
86
Mathieu Chartier31e89252013-08-28 11:29:12 -070087 // Mark the objects defined in this space in the given live bitmap.
88 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89
90 // We use the lock word to store the offset of the object in the image.
Mathieu Chartier590fee92013-09-13 13:46:47 -070091 void AssignImageOffset(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
92 void SetImageOffset(mirror::Object* object, size_t offset)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070094 bool IsImageOffsetAssigned(mirror::Object* object) const
95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070097
Alex Lighta59dd802014-07-02 16:28:08 -070098 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
101 }
102
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700103 mirror::Object* GetLocalAddress(mirror::Object* object) const
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700106 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 return reinterpret_cast<mirror::Object*>(dst);
108 }
109
Ian Rogers13735952014-10-08 12:43:28 -0700110 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111#if !defined(ART_USE_PORTABLE_COMPILER)
112 // With Quick, code is within the OatFile, as there are all in one
113 // .o ELF object. However with Portable, the code is always in
114 // different .o ELF objects.
115 DCHECK_LT(offset, oat_file_->Size());
116#endif
Igor Murashkin46774762014-10-22 11:37:02 -0700117 if (offset == 0u) {
118 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 }
120 return oat_data_begin_ + offset;
121 }
122
123 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125
126 // Debug aid that list of requested image classes.
127 void DumpImageClasses();
128
129 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
130 void ComputeLazyFieldsForImageClasses()
131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
134
135 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700136 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
139
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800140 // Combine string char arrays.
141 void ProcessStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
142
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 // Remove unwanted classes from various roots.
144 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
145 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147
148 // Verify unwanted classes removed.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800149 void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
152
153 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100154 void CalculateNewObjectOffsets()
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
158 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700160 void CalculateObjectOffsets(mirror::Object* obj)
161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
162
163 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
165 void WalkFieldsInOrder(mirror::Object* obj)
166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
167 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
169
170 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800171 void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800174 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800176 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700179 // Get quick code for non-resolution/imt_conflict/abstract method.
Ian Rogers13735952014-10-08 12:43:28 -0700180 const uint8_t* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
182
Ian Rogers13735952014-10-08 12:43:28 -0700183 const uint8_t* GetQuickEntryPoint(mirror::ArtMethod* method)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
185
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100187 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 const CompilerDriver& compiler_driver_;
190
Vladimir Markof4da6752014-08-01 19:04:18 +0100191 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700192 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100193
194 // Offset to the free space in image_.
195 size_t image_end_;
196
197 // The image roots address in the image.
198 uint32_t image_roots_address_;
199
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 // oat file with code for this image
201 OatFile* oat_file_;
202
203 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700204 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205
Mathieu Chartier590fee92013-09-13 13:46:47 -0700206 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700207 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700208
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700210 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211
Mathieu Chartier31e89252013-08-28 11:29:12 -0700212 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700213 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700214
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700216 uint32_t interpreter_to_interpreter_bridge_offset_;
217 uint32_t interpreter_to_compiled_code_bridge_offset_;
218 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700219 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700221 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800222 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700223 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700225 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700226 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700227
Mathieu Chartier2d721012014-11-10 11:08:06 -0800228 // Size of pointers on the target architecture.
229 size_t target_ptr_size_;
230
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700231 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700232 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700233 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234};
235
236} // namespace art
237
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700238#endif // ART_COMPILER_IMAGE_WRITER_H_