blob: ee02ec43f0e3bf5560456e9a50b99744ceb17e88 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstromdb4d5402011-08-09 12:18:28 -070016
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070017#ifndef ART_SRC_IMAGE_WRITER_H_
18#define ART_SRC_IMAGE_WRITER_H_
Brian Carlstromdb4d5402011-08-09 12:18:28 -070019
Brian Carlstromdb4d5402011-08-09 12:18:28 -070020#include <stdint.h>
21
Elliott Hughes90a33692011-08-30 13:27:07 -070022#include <cstddef>
Brian Carlstromae826982011-11-09 01:33:42 -080023#include <set>
24#include <string>
Elliott Hughes90a33692011-08-30 13:27:07 -070025
Ian Rogers1212a022013-03-04 10:48:41 -080026#include "compiler/driver/compiler_driver.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027#include "mem_map.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070028#include "oat_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070030#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070031#include "safe_map.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070032#include "gc/space.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080033#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070034
35namespace art {
36
Brian Carlstrom4e777d42011-08-15 13:53:52 -070037// Write a Space built during compilation for use during execution.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038class ImageWriter {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039 public:
Elliott Hughesff17f1f2012-01-24 18:12:29 -080040 explicit ImageWriter(const std::set<std::string>* image_classes)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 : oat_file_(NULL), image_end_(0), image_begin_(NULL), image_classes_(image_classes),
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042 oat_data_begin_(NULL) {}
Brian Carlstromae826982011-11-09 01:33:42 -080043
Elliott Hughes362f9bc2011-10-17 18:56:41 -070044 ~ImageWriter() {}
Brian Carlstromdb4d5402011-08-09 12:18:28 -070045
Brian Carlstroma004aa92012-02-08 18:05:09 -080046 bool Write(const std::string& image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080047 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080048 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -070049 const std::string& oat_location,
Ian Rogers1212a022013-03-04 10:48:41 -080050 const CompilerDriver& compiler_driver)
Ian Rogersb726dcb2012-09-05 08:57:23 -070051 LOCKS_EXCLUDED(Locks::mutator_lock_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053 uintptr_t GetOatDataBegin() {
54 return reinterpret_cast<uintptr_t>(oat_data_begin_);
55 }
56
Elliott Hughesa21039c2012-06-21 12:09:25 -070057 private:
Brian Carlstromae826982011-11-09 01:33:42 -080058 bool AllocMemory();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070059
60 // we use the lock word to store the offset of the object in the image
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 void AssignImageOffset(mirror::Object* object)
Ian Rogersb726dcb2012-09-05 08:57:23 -070062 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070063 DCHECK(object != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -080064 SetImageOffset(object, image_end_);
65 image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment
66 DCHECK_LT(image_end_, image_->Size());
Brian Carlstromc74255f2011-09-11 22:47:39 -070067 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080068
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 void SetImageOffset(mirror::Object* object, size_t offset) {
Brian Carlstromc74255f2011-09-11 22:47:39 -070070 DCHECK(object != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -080071 DCHECK_NE(offset, 0U);
72 DCHECK(!IsImageOffsetAssigned(object));
Elliott Hughesa0e18062012-04-13 15:59:59 -070073 offsets_.Put(object, offset);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070074 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080075
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 size_t IsImageOffsetAssigned(const mirror::Object* object) const {
Brian Carlstromc74255f2011-09-11 22:47:39 -070077 DCHECK(object != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -080078 return offsets_.find(object) != offsets_.end();
Brian Carlstromc74255f2011-09-11 22:47:39 -070079 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080080
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 size_t GetImageOffset(const mirror::Object* object) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070082 DCHECK(object != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -080083 DCHECK(IsImageOffsetAssigned(object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080084 return offsets_.find(object)->second;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070085 }
86
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 mirror::Object* GetImageAddress(const mirror::Object* object) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070088 if (object == NULL) {
89 return NULL;
90 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
Brian Carlstromdb4d5402011-08-09 12:18:28 -070092 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080093
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 mirror::Object* GetLocalAddress(const mirror::Object* object) const {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070095 size_t offset = GetImageOffset(object);
Ian Rogers30fab402012-01-23 15:43:46 -080096 byte* dst = image_->Begin() + offset;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 return reinterpret_cast<mirror::Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070098 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070099
Brian Carlstromae826982011-11-09 01:33:42 -0800100 const byte* GetOatAddress(uint32_t offset) const {
Ian Rogers30fab402012-01-23 15:43:46 -0800101 DCHECK_LT(offset, oat_file_->Size());
Brian Carlstromae826982011-11-09 01:33:42 -0800102 if (offset == 0) {
103 return NULL;
104 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800105 return oat_data_begin_ + offset;
Brian Carlstromae826982011-11-09 01:33:42 -0800106 }
107
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 bool IsImageClass(const mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800109 void DumpImageClasses();
Brian Carlstromae826982011-11-09 01:33:42 -0800110
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 void ComputeLazyFieldsForImageClasses()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersd418eda2012-01-30 12:14:28 -0800115
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800116 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution
117 void ComputeEagerResolvedStrings();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800120
Ian Rogersb726dcb2012-09-05 08:57:23 -0700121 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800124
125 void CheckNonImageClassesRemoved();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800128
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800129 void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 static void CalculateNewObjectOffsetsCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700135
136 void CopyAndFixupObjects();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 void FixupClass(const mirror::Class* orig, mirror::Class* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 void FixupMethod(const mirror::AbstractMethod* orig, mirror::AbstractMethod* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 void FixupObject(const mirror::Object* orig, mirror::Object* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700144 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 void FixupObjectArray(const mirror::ObjectArray<mirror::Object>* orig,
146 mirror::ObjectArray<mirror::Object>* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 void FixupInstanceFields(const mirror::Object* orig, mirror::Object* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 void FixupStaticFields(const mirror::Class* orig, mirror::Class* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 void FixupFields(const mirror::Object* orig, mirror::Object* copy, uint32_t ref_offsets,
153 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700155
Ian Rogers1212a022013-03-04 10:48:41 -0800156 void PatchOatCodeAndMethods(const CompilerDriver& compiler)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers1212a022013-03-04 10:48:41 -0800158 void SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700160
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 SafeMap<const mirror::Object*, size_t> offsets_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800162
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163 // oat file with code for this image
Brian Carlstromf5822582012-03-19 22:34:31 -0700164 OatFile* oat_file_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700166 // memory mapped for generating the image
Elliott Hughes90a33692011-08-30 13:27:07 -0700167 UniquePtr<MemMap> image_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700168
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700169 // Offset to the free space in image_
Ian Rogers30fab402012-01-23 15:43:46 -0800170 size_t image_end_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700171
Ian Rogers30fab402012-01-23 15:43:46 -0800172 // Beginning target image address for the output image
173 byte* image_begin_;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700174
Brian Carlstromae826982011-11-09 01:33:42 -0800175 // Set of classes to be include in the image, or NULL for all.
176 const std::set<std::string>* image_classes_;
177
Ian Rogers30fab402012-01-23 15:43:46 -0800178 // Beginning target oat address for the pointers from the output image to its oat file
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800179 const byte* oat_data_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700181 // DexCaches seen while scanning for fixing up CodeAndDirectMethods
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 typedef std::set<mirror::DexCache*> Set;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700183 Set dex_caches_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700184};
185
186} // namespace art
187
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700188#endif // ART_SRC_IMAGE_WRITER_H_