blob: e235bc45530401c6861de1f54df816b7f67814ff [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 Markocf36d492015-08-12 19:27:26 +010057 bin_slot_sizes_(), bin_slot_offsets_(), 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 Marko05792b92015-08-03 11:56:49 +010081 template <typename PtrType>
82 PtrType GetDexCacheArrayElementImageAddress(const DexFile* dex_file, uint32_t offset)
83 const SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko20f85592015-03-19 10:07:02 +000084 auto it = dex_cache_array_starts_.find(dex_file);
85 DCHECK(it != dex_cache_array_starts_.end());
Vladimir Marko05792b92015-08-03 11:56:49 +010086 return reinterpret_cast<PtrType>(
87 image_begin_ + bin_slot_offsets_[kBinDexCacheArray] + it->second + offset);
Vladimir Marko20f85592015-03-19 10:07:02 +000088 }
89
Mathieu Chartierd39645e2015-06-09 17:50:29 -070090 uint8_t* GetOatFileBegin() const;
Vladimir Markof4da6752014-08-01 19:04:18 +010091
Mathieu Chartiere401d142015-04-22 13:56:20 -070092 bool Write(const std::string& image_filename, const std::string& oat_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 const std::string& oat_location)
Mathieu Chartier90443472015-07-16 20:32:27 -070094 REQUIRES(!Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070095
96 uintptr_t GetOatDataBegin() {
97 return reinterpret_cast<uintptr_t>(oat_data_begin_);
98 }
99
100 private:
101 bool AllocMemory();
102
Mathieu Chartier31e89252013-08-28 11:29:12 -0700103 // Mark the objects defined in this space in the given live bitmap.
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 void RecordImageAllocations() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700105
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800106 // Classify different kinds of bins that objects end up getting packed into during image writing.
107 enum Bin {
108 // Likely-clean:
109 kBinString, // [String] Almost always immutable (except for obj header).
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800110 // Unknown mix of clean/dirty:
111 kBinRegular,
112 // Likely-dirty:
113 // All classes get their own bins since their fields often dirty
114 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
115 kBinClassInitialized, // Class initializers have been run
116 kBinClassVerified, // Class verified, but initializers haven't been run
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800117 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 // Non mirror fields must be below.
119 // ArtFields should be always clean.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700120 kBinArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 // If the class is initialized, then the ArtMethods are probably clean.
122 kBinArtMethodClean,
123 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
124 // initialized.
125 kBinArtMethodDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100126 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
127 // huge, and as such their dirtiness is not important for the clean/dirty separation,
128 // we arbitrarily keep them at the end of the native data.
129 kBinDexCacheArray, // Arrays belonging to dex cache.
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,
Vladimir Marko05792b92015-08-03 11:56:49 +0100143 kNativeObjectRelocationTypeDexCacheArray,
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700144 };
145 friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type);
146
Vladimir Marko80afd022015-05-19 18:08:00 +0100147 static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800148 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700149 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
150 // failures due to invalid read barrier bits during object field reads.
151 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits -
152 LockWord::kReadBarrierStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800153 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800155
156 // We use the lock word to store the bin # and bin index of the object in the image.
157 //
158 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
159 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
160 struct BinSlot {
161 explicit BinSlot(uint32_t lockword);
162 BinSlot(Bin bin, uint32_t index);
163
164 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
165 Bin GetBin() const;
166 // The offset in bytes from the beginning of the bin. Aligned to object size.
167 uint32_t GetIndex() const;
168 // Pack into a single uint32_t, for storing into a lock word.
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700169 uint32_t Uint32Value() const { return lockword_; }
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800170 // Comparison operator for map support
171 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
172
173 private:
174 // Must be the same size as LockWord, any larger and we would truncate the data.
175 const uint32_t lockword_;
176 };
177
Mathieu Chartier31e89252013-08-28 11:29:12 -0700178 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800179 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700180 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700181 void SetImageOffset(mirror::Object* object, size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700182 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700183 bool IsImageOffsetAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700184 SHARED_REQUIRES(Locks::mutator_lock_);
185 size_t GetImageOffset(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
Mathieu Chartier90443472015-07-16 20:32:27 -0700189 void PrepareDexCacheArraySlots() SHARED_REQUIRES(Locks::mutator_lock_);
190 void AssignImageBinSlot(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800191 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700192 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800193 bool IsImageBinSlotAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700194 SHARED_REQUIRES(Locks::mutator_lock_);
195 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800196
Vladimir Marko05792b92015-08-03 11:56:49 +0100197 void AddDexCacheArrayRelocation(void* array, size_t offset) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199
Alex Lighta59dd802014-07-02 16:28:08 -0700200 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700201 SHARED_REQUIRES(Locks::mutator_lock_) {
Alex Lighta59dd802014-07-02 16:28:08 -0700202 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
203 }
204
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700205 mirror::Object* GetLocalAddress(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700206 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700208 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 return reinterpret_cast<mirror::Object*>(dst);
210 }
211
Ian Rogers13735952014-10-08 12:43:28 -0700212 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 // With Quick, code is within the OatFile, as there are all in one
Elliott Hughes956af0f2014-12-11 14:34:28 -0800214 // .o ELF object.
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100215 DCHECK_LE(offset, oat_file_->Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 DCHECK(oat_data_begin_ != nullptr);
217 return offset == 0u ? nullptr : oat_data_begin_ + offset;
218 }
219
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 // Returns true if the class was in the original requested image classes list.
Mathieu Chartier90443472015-07-16 20:32:27 -0700221 bool IsImageClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222
223 // Debug aid that list of requested image classes.
224 void DumpImageClasses();
225
226 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
227 void ComputeLazyFieldsForImageClasses()
Mathieu Chartier90443472015-07-16 20:32:27 -0700228 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 // Remove unwanted classes from various roots.
Mathieu Chartier90443472015-07-16 20:32:27 -0700231 void PruneNonImageClasses() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232
233 // Verify unwanted classes removed.
Mathieu Chartier90443472015-07-16 20:32:27 -0700234 void CheckNonImageClassesRemoved() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700236 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237
238 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100239 void CalculateNewObjectOffsets()
Mathieu Chartier90443472015-07-16 20:32:27 -0700240 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100241 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700242 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700244 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800245 void CalculateObjectBinSlots(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700246 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800247 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700248 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700249
250 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700251 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700252 void WalkFieldsInOrder(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700253 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700254 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700255 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800256 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258
259 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartier90443472015-07-16 20:32:27 -0700260 void CopyAndFixupNativeData() SHARED_REQUIRES(Locks::mutator_lock_);
261 void CopyAndFixupObjects() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700263 SHARED_REQUIRES(Locks::mutator_lock_);
264 void CopyAndFixupObject(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700266 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700267 void FixupClass(mirror::Class* orig, mirror::Class* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700268 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700270 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100271 void FixupDexCache(mirror::DexCache* orig_dex_cache, mirror::DexCache* copy_dex_cache)
272 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273 void FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, mirror::Class* klass,
Mathieu Chartier90443472015-07-16 20:32:27 -0700274 Bin array_type) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700276 // Get quick code for non-resolution/imt_conflict/abstract method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 const uint8_t* GetQuickCode(ArtMethod* method, bool* quick_is_interpreted)
Mathieu Chartier90443472015-07-16 20:32:27 -0700278 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700279
Mathieu Chartiere401d142015-04-22 13:56:20 -0700280 const uint8_t* GetQuickEntryPoint(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700281 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700282
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100284 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800286 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
287 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
288
Mathieu Chartiere401d142015-04-22 13:56:20 -0700289 // Return true if a method is likely to be dirtied at runtime.
Mathieu Chartier90443472015-07-16 20:32:27 -0700290 bool WillMethodBeDirty(ArtMethod* m) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700291
292 // Assign the offset for an ArtMethod.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700293 void AssignMethodOffset(ArtMethod* method, NativeObjectRelocationType type)
294 SHARED_REQUIRES(Locks::mutator_lock_);
295
296 static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
297
Vladimir Marko05792b92015-08-03 11:56:49 +0100298 uintptr_t NativeOffsetInImage(void* obj);
299
300 template <typename T>
301 T* NativeLocationInImage(T* obj);
Andreas Gampe245ee002014-12-04 21:25:04 -0800302
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 const CompilerDriver& compiler_driver_;
304
Vladimir Markof4da6752014-08-01 19:04:18 +0100305 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700306 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100307
308 // Offset to the free space in image_.
309 size_t image_end_;
310
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800311 // Offset from image_begin_ to where the first object is in image_.
312 size_t image_objects_offset_begin_;
313
Vladimir Markof4da6752014-08-01 19:04:18 +0100314 // The image roots address in the image.
315 uint32_t image_roots_address_;
316
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 // oat file with code for this image
318 OatFile* oat_file_;
319
320 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700321 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
324 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
325 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
326
Vladimir Marko20f85592015-03-19 10:07:02 +0000327 // The start offsets of the dex cache arrays.
328 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
329
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700330 // Saved hash codes. We use these to restore lockwords which were temporarily used to have
331 // forwarding addresses as well as copying over hash codes.
332 std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800333
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700335 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336
Mathieu Chartier31e89252013-08-28 11:29:12 -0700337 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700338 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700339
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700341 uint32_t interpreter_to_interpreter_bridge_offset_;
342 uint32_t interpreter_to_compiled_code_bridge_offset_;
343 uint32_t jni_dlsym_lookup_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800344 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700345 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700347 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700348 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700349
Mathieu Chartier2d721012014-11-10 11:08:06 -0800350 // Size of pointers on the target architecture.
351 size_t target_ptr_size_;
352
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800353 // Bin slot tracking for dirty object packing
354 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
Vladimir Markocf36d492015-08-12 19:27:26 +0100355 size_t bin_slot_offsets_[kBinSize]; // Number of bytes in previous bins.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800356 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
357
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700358 // Cached size of the intern table for when we allocate memory.
359 size_t intern_table_bytes_;
360
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
362 // have one entry per art field for convenience. ArtFields are placed right after the end of the
363 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700364 struct NativeObjectRelocation {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365 uintptr_t offset;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700366 NativeObjectRelocationType type;
367
368 bool IsArtMethodRelocation() const {
369 return type == kNativeObjectRelocationTypeArtMethodClean ||
370 type == kNativeObjectRelocationTypeArtMethodDirty;
371 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 };
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700373 std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700374
Mathieu Chartiere401d142015-04-22 13:56:20 -0700375 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
376 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -0700377 // Fake length prefixed array for image methods. This array does not contain the actual
378 // ArtMethods. We only use it for the header and relocation addresses.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700379 LengthPrefixedArray<ArtMethod> image_method_array_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700380
381 // Counters for measurements, used for logging only.
382 uint64_t dirty_methods_;
383 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800384
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700385 friend class FixupClassVisitor;
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700386 friend class FixupRootVisitor;
387 friend class FixupVisitor;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700388 friend class NonImageClassesVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700389 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390};
391
392} // namespace art
393
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700394#endif // ART_COMPILER_IMAGE_WRITER_H_