blob: 638add7905eaa8fdf0fab351bca2158f2b123162 [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
Brian Carlstromf5822582012-03-19 22:34:31 -070026#include "compiler.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070027#include "dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028#include "mem_map.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070029#include "oat_file.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070030#include "object.h"
31#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070032#include "safe_map.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070033#include "gc/space.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080034#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070035
36namespace art {
37
Brian Carlstrom4e777d42011-08-15 13:53:52 -070038// Write a Space built during compilation for use during execution.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039class ImageWriter {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040 public:
Elliott Hughesff17f1f2012-01-24 18:12:29 -080041 explicit ImageWriter(const std::set<std::string>* image_classes)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042 : oat_file_(NULL), image_end_(0), image_begin_(NULL), image_classes_(image_classes),
43 oat_begin_(NULL) {}
Brian Carlstromae826982011-11-09 01:33:42 -080044
Elliott Hughes362f9bc2011-10-17 18:56:41 -070045 ~ImageWriter() {}
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046
Brian Carlstroma004aa92012-02-08 18:05:09 -080047 bool Write(const std::string& image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080048 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080049 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -070050 const std::string& oat_location,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051 const Compiler& compiler)
Ian Rogersb726dcb2012-09-05 08:57:23 -070052 LOCKS_EXCLUDED(Locks::mutator_lock_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070053
Elliott Hughesa21039c2012-06-21 12:09:25 -070054 private:
Brian Carlstromae826982011-11-09 01:33:42 -080055 bool AllocMemory();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070056
57 // we use the lock word to store the offset of the object in the image
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 void AssignImageOffset(Object* object)
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070060 DCHECK(object != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -080061 SetImageOffset(object, image_end_);
62 image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment
63 DCHECK_LT(image_end_, image_->Size());
Brian Carlstromc74255f2011-09-11 22:47:39 -070064 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080065
66 void SetImageOffset(Object* object, size_t offset) {
Brian Carlstromc74255f2011-09-11 22:47:39 -070067 DCHECK(object != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -080068 DCHECK_NE(offset, 0U);
69 DCHECK(!IsImageOffsetAssigned(object));
Elliott Hughesa0e18062012-04-13 15:59:59 -070070 offsets_.Put(object, offset);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070071 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080072
73 size_t IsImageOffsetAssigned(const Object* object) const {
Brian Carlstromc74255f2011-09-11 22:47:39 -070074 DCHECK(object != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -080075 return offsets_.find(object) != offsets_.end();
Brian Carlstromc74255f2011-09-11 22:47:39 -070076 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080077
78 size_t GetImageOffset(const Object* object) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070079 DCHECK(object != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -080080 DCHECK(IsImageOffsetAssigned(object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080081 return offsets_.find(object)->second;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070082 }
83
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070084 bool InSourceSpace(const Object* object) const;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080085
Brian Carlstrom58ae9412011-10-04 00:56:06 -070086 Object* GetImageAddress(const Object* object) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070087 if (object == NULL) {
88 return NULL;
89 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070090 // if object outside the relocating source_space_, assume unchanged
91 if (!InSourceSpace(object)) {
92 return const_cast<Object*>(object);
93 }
Ian Rogers30fab402012-01-23 15:43:46 -080094 return reinterpret_cast<Object*>(image_begin_ + GetImageOffset(object));
Brian Carlstromdb4d5402011-08-09 12:18:28 -070095 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080096
Brian Carlstrom58ae9412011-10-04 00:56:06 -070097 Object* GetLocalAddress(const Object* object) const {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070098 size_t offset = GetImageOffset(object);
Ian Rogers30fab402012-01-23 15:43:46 -080099 byte* dst = image_->Begin() + offset;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700100 return reinterpret_cast<Object*>(dst);
101 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700102
Brian Carlstromae826982011-11-09 01:33:42 -0800103 const byte* GetOatAddress(uint32_t offset) const {
Ian Rogers30fab402012-01-23 15:43:46 -0800104 DCHECK_LT(offset, oat_file_->Size());
Brian Carlstromae826982011-11-09 01:33:42 -0800105 if (offset == 0) {
106 return NULL;
107 }
Ian Rogers30fab402012-01-23 15:43:46 -0800108 return oat_begin_ + offset;
Brian Carlstromae826982011-11-09 01:33:42 -0800109 }
110
Ian Rogersb726dcb2012-09-05 08:57:23 -0700111 bool IsImageClass(const Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800112 void DumpImageClasses();
Brian Carlstromae826982011-11-09 01:33:42 -0800113
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 void ComputeLazyFieldsForImageClasses()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 static bool ComputeLazyFieldsForClassesVisitor(Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersd418eda2012-01-30 12:14:28 -0800118
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800119 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution
120 void ComputeEagerResolvedStrings();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 static void ComputeEagerResolvedStringsCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800123
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 static bool NonImageClassesVisitor(Class* c, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800127
128 void CheckNonImageClassesRemoved();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 static void CheckNonImageClassesRemovedCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800131
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 void CalculateNewObjectOffsets() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133 ObjectArray<Object>* CreateImageRoots() const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 static void CalculateNewObjectOffsetsCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700137
138 void CopyAndFixupObjects();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 static void CopyAndFixupObjectsCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 void FixupClass(const Class* orig, Class* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700143 void FixupMethod(const AbstractMethod* orig, AbstractMethod* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700144 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 void FixupObject(const Object* orig, Object* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 void FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 void FixupInstanceFields(const Object* orig, Object* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 void FixupStaticFields(const Class* orig, Class* copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153 void FixupFields(const Object* orig, Object* copy, uint32_t ref_offsets, 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 Rogers00f7d0e2012-07-19 15:28:27 -0700156 void PatchOatCodeAndMethods(const Compiler& compiler)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 void SetPatchLocation(const Compiler::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
Elliott Hughesa0e18062012-04-13 15:59:59 -0700161 SafeMap<const 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
179 const byte* oat_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
Elliott Hughese5448b52012-01-18 16:44:06 -0800182 typedef std::set<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_