blob: eb6aa6f3461e6c7dfeea24678eda4ec0a69b0d0e [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>
Evgenii Stepanov1e133742015-05-20 12:30:59 -070021#include "base/memory_tool.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022
23#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070024#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include <set>
26#include <string>
Igor Murashkinf5b4c502014-11-14 15:01:59 -080027#include <ostream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
Vladimir Marko80afd022015-05-19 18:08:00 +010029#include "base/bit_utils.h"
Igor Murashkin46774762014-10-22 11:37:02 -070030#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "driver/compiler_driver.h"
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -080032#include "gc/space/space.h"
Mathieu Chartier54d220e2015-07-30 16:20:06 -070033#include "length_prefixed_array.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "lock_word.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "mem_map.h"
36#include "oat_file.h"
37#include "mirror/dex_cache.h"
38#include "os.h"
39#include "safe_map.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070040#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070041
42namespace art {
43
44// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070045class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 public:
Igor Murashkin46774762014-10-22 11:37:02 -070047 ImageWriter(const CompilerDriver& compiler_driver, uintptr_t image_begin,
48 bool compile_pic)
Ian Rogers13735952014-10-08 12:43:28 -070049 : compiler_driver_(compiler_driver), image_begin_(reinterpret_cast<uint8_t*>(image_begin)),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080050 image_end_(0), image_objects_offset_begin_(0), image_roots_address_(0), oat_file_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -070051 oat_data_begin_(nullptr), interpreter_to_interpreter_bridge_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010052 interpreter_to_compiled_code_bridge_offset_(0), jni_dlsym_lookup_offset_(0),
Elliott Hughes956af0f2014-12-11 14:34:28 -080053 quick_generic_jni_trampoline_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010054 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
Mathieu Chartier2d721012014-11-10 11:08:06 -080055 quick_to_interpreter_bridge_offset_(0), compile_pic_(compile_pic),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080056 target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())),
Vladimir Marko20f85592015-03-19 10:07:02 +000057 bin_slot_sizes_(), bin_slot_previous_sizes_(), bin_slot_count_(),
Mathieu Chartier54d220e2015-07-30 16:20:06 -070058 intern_table_bytes_(0u), image_method_array_(ImageHeader::kImageMethodsCount),
59 dirty_methods_(0u), clean_methods_(0u) {
Vladimir Markof4da6752014-08-01 19:04:18 +010060 CHECK_NE(image_begin, 0U);
Mathieu Chartiere401d142015-04-22 13:56:20 -070061 std::fill(image_methods_, image_methods_ + arraysize(image_methods_), nullptr);
Vladimir Markof4da6752014-08-01 19:04:18 +010062 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070063
Andreas Gampe245ee002014-12-04 21:25:04 -080064 ~ImageWriter() {
Andreas Gampe245ee002014-12-04 21:25:04 -080065 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070066
Vladimir Markof4da6752014-08-01 19:04:18 +010067 bool PrepareImageAddressSpace();
68
69 bool IsImageAddressSpaceReady() const {
70 return image_roots_address_ != 0u;
71 }
72
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 template <typename T>
Mathieu Chartier90443472015-07-16 20:32:27 -070074 T* GetImageAddress(T* object) const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070075 return object == nullptr ? nullptr :
76 reinterpret_cast<T*>(image_begin_ + GetImageOffset(object));
Vladimir Markof4da6752014-08-01 19:04:18 +010077 }
78
Mathieu Chartier90443472015-07-16 20:32:27 -070079 ArtMethod* GetImageMethodAddress(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070080
Vladimir Marko20f85592015-03-19 10:07:02 +000081 mirror::HeapReference<mirror::Object>* GetDexCacheArrayElementImageAddress(
Mathieu Chartier90443472015-07-16 20:32:27 -070082 const DexFile* dex_file, uint32_t offset) const SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko20f85592015-03-19 10:07:02 +000083 auto it = dex_cache_array_starts_.find(dex_file);
84 DCHECK(it != dex_cache_array_starts_.end());
85 return reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
86 image_begin_ + RoundUp(sizeof(ImageHeader), kObjectAlignment) + it->second + offset);
87 }
88
Mathieu Chartierd39645e2015-06-09 17:50:29 -070089 uint8_t* GetOatFileBegin() const;
Vladimir Markof4da6752014-08-01 19:04:18 +010090
Mathieu Chartiere401d142015-04-22 13:56:20 -070091 bool Write(const std::string& image_filename, const std::string& oat_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 const std::string& oat_location)
Mathieu Chartier90443472015-07-16 20:32:27 -070093 REQUIRES(!Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070094
95 uintptr_t GetOatDataBegin() {
96 return reinterpret_cast<uintptr_t>(oat_data_begin_);
97 }
98
99 private:
100 bool AllocMemory();
101
Mathieu Chartier31e89252013-08-28 11:29:12 -0700102 // Mark the objects defined in this space in the given live bitmap.
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 void RecordImageAllocations() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700104
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800105 // Classify different kinds of bins that objects end up getting packed into during image writing.
106 enum Bin {
Vladimir Marko20f85592015-03-19 10:07:02 +0000107 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
108 // huge, and as such their dirtiness is not important for the clean/dirty separation,
109 // we arbitrarily keep them at the beginning.
110 kBinDexCacheArray, // Object arrays belonging to dex cache.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800111 // Likely-clean:
112 kBinString, // [String] Almost always immutable (except for obj header).
113 kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty
114 // Unknown mix of clean/dirty:
115 kBinRegular,
116 // Likely-dirty:
117 // All classes get their own bins since their fields often dirty
118 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
119 kBinClassInitialized, // Class initializers have been run
120 kBinClassVerified, // Class verified, but initializers haven't been run
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800121 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 // Non mirror fields must be below.
123 // ArtFields should be always clean.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700124 kBinArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 // If the class is initialized, then the ArtMethods are probably clean.
126 kBinArtMethodClean,
127 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
128 // initialized.
129 kBinArtMethodDirty,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800130 kBinSize,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700131 // Number of bins which are for mirror objects.
132 kBinMirrorCount = kBinArtField,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800133 };
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800134 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
135
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700136 enum NativeObjectRelocationType {
137 kNativeObjectRelocationTypeArtField,
138 kNativeObjectRelocationTypeArtFieldArray,
139 kNativeObjectRelocationTypeArtMethodClean,
140 kNativeObjectRelocationTypeArtMethodArrayClean,
141 kNativeObjectRelocationTypeArtMethodDirty,
142 kNativeObjectRelocationTypeArtMethodArrayDirty,
143 };
144 friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type);
145
Vladimir Marko80afd022015-05-19 18:08:00 +0100146 static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800147 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
149 // failures due to invalid read barrier bits during object field reads.
150 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits -
151 LockWord::kReadBarrierStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800152 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800154
155 // We use the lock word to store the bin # and bin index of the object in the image.
156 //
157 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
158 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
159 struct BinSlot {
160 explicit BinSlot(uint32_t lockword);
161 BinSlot(Bin bin, uint32_t index);
162
163 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
164 Bin GetBin() const;
165 // The offset in bytes from the beginning of the bin. Aligned to object size.
166 uint32_t GetIndex() const;
167 // Pack into a single uint32_t, for storing into a lock word.
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700168 uint32_t Uint32Value() const { return lockword_; }
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800169 // Comparison operator for map support
170 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
171
172 private:
173 // Must be the same size as LockWord, any larger and we would truncate the data.
174 const uint32_t lockword_;
175 };
176
Mathieu Chartier31e89252013-08-28 11:29:12 -0700177 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800178 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700179 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700180 void SetImageOffset(mirror::Object* object, size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700182 bool IsImageOffsetAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700183 SHARED_REQUIRES(Locks::mutator_lock_);
184 size_t GetImageOffset(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700185 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700186 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187
Mathieu Chartier90443472015-07-16 20:32:27 -0700188 void PrepareDexCacheArraySlots() SHARED_REQUIRES(Locks::mutator_lock_);
189 void AssignImageBinSlot(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800190 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800192 bool IsImageBinSlotAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 SHARED_REQUIRES(Locks::mutator_lock_);
194 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800195
Mathieu Chartier90443472015-07-16 20:32:27 -0700196 void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197
Alex Lighta59dd802014-07-02 16:28:08 -0700198 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700199 SHARED_REQUIRES(Locks::mutator_lock_) {
Alex Lighta59dd802014-07-02 16:28:08 -0700200 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
201 }
202
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700203 mirror::Object* GetLocalAddress(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700204 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700206 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 return reinterpret_cast<mirror::Object*>(dst);
208 }
209
Ian Rogers13735952014-10-08 12:43:28 -0700210 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 // With Quick, code is within the OatFile, as there are all in one
Elliott Hughes956af0f2014-12-11 14:34:28 -0800212 // .o ELF object.
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100213 DCHECK_LE(offset, oat_file_->Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 DCHECK(oat_data_begin_ != nullptr);
215 return offset == 0u ? nullptr : oat_data_begin_ + offset;
216 }
217
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 // Returns true if the class was in the original requested image classes list.
Mathieu Chartier90443472015-07-16 20:32:27 -0700219 bool IsImageClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220
221 // Debug aid that list of requested image classes.
222 void DumpImageClasses();
223
224 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
225 void ComputeLazyFieldsForImageClasses()
Mathieu Chartier90443472015-07-16 20:32:27 -0700226 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227
228 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Mathieu Chartier90443472015-07-16 20:32:27 -0700229 void ComputeEagerResolvedStrings() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700231 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232
233 // Remove unwanted classes from various roots.
Mathieu Chartier90443472015-07-16 20:32:27 -0700234 void PruneNonImageClasses() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235
236 // Verify unwanted classes removed.
Mathieu Chartier90443472015-07-16 20:32:27 -0700237 void CheckNonImageClassesRemoved() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700239 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240
241 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100242 void CalculateNewObjectOffsets()
Mathieu Chartier90443472015-07-16 20:32:27 -0700243 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100244 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700245 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700247 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800248 void CalculateObjectBinSlots(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700249 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800250 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700251 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700252
253 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700254 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700255 void WalkFieldsInOrder(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700256 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700257 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700258 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800259 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700260 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261
262 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartier90443472015-07-16 20:32:27 -0700263 void CopyAndFixupNativeData() SHARED_REQUIRES(Locks::mutator_lock_);
264 void CopyAndFixupObjects() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700266 SHARED_REQUIRES(Locks::mutator_lock_);
267 void CopyAndFixupObject(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700269 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700270 void FixupClass(mirror::Class* orig, mirror::Class* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700271 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800272 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700273 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 void FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, mirror::Class* klass,
Mathieu Chartier90443472015-07-16 20:32:27 -0700275 Bin array_type) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277 // Get quick code for non-resolution/imt_conflict/abstract method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 const uint8_t* GetQuickCode(ArtMethod* method, bool* quick_is_interpreted)
Mathieu Chartier90443472015-07-16 20:32:27 -0700279 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700280
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 const uint8_t* GetQuickEntryPoint(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700283
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100285 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800287 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
288 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
289
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 // Return true if a method is likely to be dirtied at runtime.
Mathieu Chartier90443472015-07-16 20:32:27 -0700291 bool WillMethodBeDirty(ArtMethod* m) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292
293 // Assign the offset for an ArtMethod.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700294 void AssignMethodOffset(ArtMethod* method, NativeObjectRelocationType type)
295 SHARED_REQUIRES(Locks::mutator_lock_);
296
297 static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
298
299 void* NativeLocationInImage(void* obj);
Andreas Gampe245ee002014-12-04 21:25:04 -0800300
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 const CompilerDriver& compiler_driver_;
302
Vladimir Markof4da6752014-08-01 19:04:18 +0100303 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700304 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100305
306 // Offset to the free space in image_.
307 size_t image_end_;
308
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800309 // Offset from image_begin_ to where the first object is in image_.
310 size_t image_objects_offset_begin_;
311
Vladimir Markof4da6752014-08-01 19:04:18 +0100312 // The image roots address in the image.
313 uint32_t image_roots_address_;
314
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 // oat file with code for this image
316 OatFile* oat_file_;
317
318 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700319 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320
Mathieu Chartierc7853442015-03-27 14:35:38 -0700321 // Indexes, lengths for dex cache arrays (objects are inside of the image so that they don't
322 // move).
323 struct DexCacheArrayLocation {
324 size_t offset_;
325 size_t length_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 Bin bin_type_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700327 };
328 SafeMap<mirror::Object*, DexCacheArrayLocation> dex_cache_array_indexes_;
Vladimir Marko20f85592015-03-19 10:07:02 +0000329
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
331 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
332 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
333
Vladimir Marko20f85592015-03-19 10:07:02 +0000334 // The start offsets of the dex cache arrays.
335 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
336
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700337 // Saved hash codes. We use these to restore lockwords which were temporarily used to have
338 // forwarding addresses as well as copying over hash codes.
339 std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800340
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700342 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343
Mathieu Chartier31e89252013-08-28 11:29:12 -0700344 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700345 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700346
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700348 uint32_t interpreter_to_interpreter_bridge_offset_;
349 uint32_t interpreter_to_compiled_code_bridge_offset_;
350 uint32_t jni_dlsym_lookup_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800351 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700352 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700354 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700355 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700356
Mathieu Chartier2d721012014-11-10 11:08:06 -0800357 // Size of pointers on the target architecture.
358 size_t target_ptr_size_;
359
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800360 // Bin slot tracking for dirty object packing
361 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
Vladimir Marko20f85592015-03-19 10:07:02 +0000362 size_t bin_slot_previous_sizes_[kBinSize]; // Number of bytes in previous bins.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800363 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
364
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700365 // Cached size of the intern table for when we allocate memory.
366 size_t intern_table_bytes_;
367
Mathieu Chartiere401d142015-04-22 13:56:20 -0700368 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
369 // have one entry per art field for convenience. ArtFields are placed right after the end of the
370 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700371 struct NativeObjectRelocation {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 uintptr_t offset;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700373 NativeObjectRelocationType type;
374
375 bool IsArtMethodRelocation() const {
376 return type == kNativeObjectRelocationTypeArtMethodClean ||
377 type == kNativeObjectRelocationTypeArtMethodDirty;
378 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700379 };
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700380 std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700381
Mathieu Chartiere401d142015-04-22 13:56:20 -0700382 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
383 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700384 // Fake length prefixed array for image methods.
385 LengthPrefixedArray<ArtMethod> image_method_array_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386
387 // Counters for measurements, used for logging only.
388 uint64_t dirty_methods_;
389 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800390
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700391 friend class FixupClassVisitor;
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700392 friend class FixupRootVisitor;
393 friend class FixupVisitor;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700394 friend class NonImageClassesVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700395 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396};
397
398} // namespace art
399
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700400#endif // ART_COMPILER_IMAGE_WRITER_H_