Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_CLASS_LINKER_H_ |
| 4 | #define ART_SRC_CLASS_LINKER_H_ |
| 5 | |
| 6 | #include <map> |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 10 | #include "dex_file.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 11 | #include "heap.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 12 | #include "macros.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 13 | #include "mutex.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 14 | #include "object.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 15 | #include "unordered_map.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 16 | #include "unordered_set.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 17 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 22 | class ClassLoader; |
| 23 | class InternTable; |
| 24 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | class ClassLinker { |
| 26 | public: |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 27 | // Initializes the class linker using DexFiles and an optional an image. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 28 | static ClassLinker* Create(const std::vector<const DexFile*>& boot_class_path, |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 29 | const std::vector<const DexFile*>& class_path, |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 30 | InternTable* intern_table, bool image); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 31 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 32 | ~ClassLinker(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 33 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 34 | // Finds a class by its descriptor name. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 35 | // If class_loader is null, searches boot_class_path_. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 36 | Class* FindClass(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 37 | const ClassLoader* class_loader); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 39 | Class* FindPrimitiveClass(char type); |
| 40 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 41 | Class* FindSystemClass(const StringPiece& descriptor) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 42 | return FindClass(descriptor, NULL); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 45 | void DumpAllClasses(int flags) const; |
| 46 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 47 | size_t NumLoadedClasses() const; |
| 48 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 49 | // Resolve a String with the given index from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 50 | // result in the DexCache. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 51 | String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 52 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 53 | // Resolve a Type with the given index from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 54 | // result in the DexCache. The referrer is used to identity the |
| 55 | // target DexCache and ClassLoader to use for resolution. |
| 56 | Class* ResolveType(const DexFile& dex_file, |
| 57 | uint32_t type_idx, |
| 58 | const Class* referrer) { |
| 59 | return ResolveType(dex_file, |
| 60 | type_idx, |
| 61 | referrer->GetDexCache(), |
| 62 | referrer->GetClassLoader()); |
| 63 | } |
| 64 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 65 | // Resolve a Type with the given index from the DexFile, storing the |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 66 | // result in the DexCache. The referrer is used to identify the |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 67 | // target DexCache and ClassLoader to use for resolution. |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 68 | Class* ResolveType(uint32_t type_idx, const Method* referrer) { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 69 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 70 | DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 71 | // TODO: we could check for a dex cache hit here |
| 72 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 73 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 74 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 75 | } |
| 76 | |
| 77 | Class* ResolveType(uint32_t type_idx, const Field* referrer) { |
| 78 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 79 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 80 | // TODO: we could check for a dex cache hit here |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 81 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 82 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 83 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 84 | } |
| 85 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 86 | // Resolve a type with the given ID from the DexFile, storing the |
| 87 | // result in DexCache. The ClassLoader is used to search for the |
| 88 | // type, since it may be referenced from but not contained within |
| 89 | // the given DexFile. |
| 90 | Class* ResolveType(const DexFile& dex_file, |
| 91 | uint32_t type_idx, |
| 92 | DexCache* dex_cache, |
| 93 | const ClassLoader* class_loader); |
| 94 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 95 | static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx, |
| 96 | const Method* referrer); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 97 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 98 | // Resolve a method with a given ID from the DexFile, storing the |
| 99 | // result in DexCache. The ClassLinker and ClassLoader are used as |
| 100 | // in ResolveType. What is unique is the method type argument which |
| 101 | // is used to determine if this method is a direct, static, or |
| 102 | // virtual method. |
| 103 | Method* ResolveMethod(const DexFile& dex_file, |
| 104 | uint32_t method_idx, |
| 105 | DexCache* dex_cache, |
| 106 | const ClassLoader* class_loader, |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 107 | bool is_direct); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 108 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 109 | Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) { |
| 110 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 111 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 112 | // TODO: we could check for a dex cache hit here |
| 113 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 114 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 115 | return ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct); |
| 116 | } |
| 117 | |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame^] | 118 | Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) { |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 119 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 120 | DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 121 | // TODO: we could check for a dex cache hit here |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 122 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 123 | const DexFile& dex_file = FindDexFile(dex_cache); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame^] | 124 | return ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 127 | // Resolve a field with a given ID from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 128 | // result in DexCache. The ClassLinker and ClassLoader are used as |
| 129 | // in ResolveType. What is unique is the is_static argument which is |
| 130 | // used to determine if we are resolving a static or non-static |
| 131 | // field. |
| 132 | Field* ResolveField(const DexFile& dex_file, |
| 133 | uint32_t field_idx, |
| 134 | DexCache* dex_cache, |
| 135 | const ClassLoader* class_loader, |
| 136 | bool is_static); |
| 137 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 138 | // Returns true on success, false if there's an exception pending. |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 139 | // can_run_clinit=false allows the compiler to attempt to init a class, |
| 140 | // given the restriction that no <clinit> execution is possible. |
| 141 | bool EnsureInitialized(Class* c, bool can_run_clinit); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 142 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 143 | void RegisterDexFile(const DexFile& dex_file); |
| 144 | void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 145 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 146 | const std::vector<const DexFile*>& GetBootClassPath() { |
| 147 | return boot_class_path_; |
| 148 | } |
| 149 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 150 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 151 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 152 | const DexFile& FindDexFile(const DexCache* dex_cache) const; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 153 | DexCache* FindDexCache(const DexFile& dex_file) const; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 154 | |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 155 | template <class T> |
| 156 | ObjectArray<T>* AllocObjectArray(size_t length) { |
| 157 | return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length); |
| 158 | } |
| 159 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 160 | ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length); |
| 161 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 162 | void VerifyClass(Class* klass); |
| 163 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 164 | private: |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 165 | ClassLinker(InternTable*); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 166 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 167 | // Initialize class linker from DexFile instances. |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 168 | void Init(const std::vector<const DexFile*>& boot_class_path_, |
| 169 | const std::vector<const DexFile*>& class_path_); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 170 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 171 | // Initialize class linker from pre-initialized image. |
| 172 | void InitFromImage(const std::vector<const DexFile*>& boot_class_path_, |
| 173 | const std::vector<const DexFile*>& class_path_); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 174 | static void InitFromImageCallback(Object* obj, void* arg); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 175 | struct InitFromImageCallbackState; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 176 | |
| 177 | void FinishInit(); |
| 178 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 179 | bool InitializeClass(Class* klass, bool can_run_clinit); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 180 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 181 | // For early bootstrapping by Init |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 182 | Class* AllocClass(Class* java_lang_Class, size_t class_size); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 183 | |
| 184 | // Alloc* convenience functions to avoid needing to pass in Class* |
| 185 | // values that are known to the ClassLinker such as |
| 186 | // kObjectArrayClass and kJavaLangString etc. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 187 | Class* AllocClass(size_t class_size); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 188 | DexCache* AllocDexCache(const DexFile& dex_file); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 189 | Field* AllocField(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 190 | |
| 191 | // TODO: have no friends, we need this currently to create a special method |
| 192 | // to describe callee save registers for throwing exceptions |
| 193 | friend class Thread; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 194 | Method* AllocMethod(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 195 | |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 196 | CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 197 | InterfaceEntry* AllocInterfaceEntry(Class* interface); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 198 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 199 | Class* CreatePrimitiveClass(const char* descriptor, |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 200 | Class::PrimitiveType type) { |
| 201 | return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type); |
| 202 | } |
| 203 | Class* InitializePrimitiveClass(Class* primitive_class, |
| 204 | const char* descriptor, |
| 205 | Class::PrimitiveType type); |
| 206 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 207 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 208 | Class* CreateArrayClass(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 209 | const ClassLoader* class_loader); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 210 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 211 | void AppendToBootClassPath(const DexFile& dex_file); |
| 212 | void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 213 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 214 | void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 215 | Class* c, std::map<int, Field*>& field_map); |
| 216 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 217 | size_t SizeOfClass(const DexFile& dex_file, |
| 218 | const DexFile::ClassDef& dex_class_def); |
| 219 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 220 | void LoadClass(const DexFile& dex_file, |
| 221 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 222 | Class* klass, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 223 | const ClassLoader* class_loader); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 224 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 225 | void LoadInterfaces(const DexFile& dex_file, |
| 226 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 227 | Class *klass); |
| 228 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 229 | void LoadField(const DexFile& dex_file, |
| 230 | const DexFile::Field& dex_field, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 231 | Class* klass, |
| 232 | Field* dst); |
| 233 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 234 | void LoadMethod(const DexFile& dex_file, |
| 235 | const DexFile::Method& dex_method, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 236 | Class* klass, |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 237 | Method* dst); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 238 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 239 | Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 240 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 241 | // Inserts a class into the class table. Returns true if the class |
| 242 | // was inserted. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 243 | bool InsertClass(const StringPiece& descriptor, Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 244 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 245 | bool InitializeSuperClass(Class* klass, bool can_run_clinit); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 246 | |
| 247 | void InitializeStaticFields(Class* klass); |
| 248 | |
| 249 | bool ValidateSuperClassDescriptors(const Class* klass); |
| 250 | |
| 251 | bool HasSameDescriptorClasses(const char* descriptor, |
| 252 | const Class* klass1, |
| 253 | const Class* klass2); |
| 254 | |
| 255 | bool HasSameMethodDescriptorClasses(const Method* descriptor, |
| 256 | const Class* klass1, |
| 257 | const Class* klass2); |
| 258 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 259 | bool LinkClass(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 260 | |
| 261 | bool LinkSuperClass(Class* klass); |
| 262 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 263 | bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 264 | |
| 265 | bool LinkMethods(Class* klass); |
| 266 | |
| 267 | bool LinkVirtualMethods(Class* klass); |
| 268 | |
| 269 | bool LinkInterfaceMethods(Class* klass); |
| 270 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 271 | bool LinkStaticFields(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 272 | bool LinkInstanceFields(Class* klass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 273 | bool LinkFields(Class *klass, bool instance); |
| 274 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 275 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 276 | void CreateReferenceInstanceOffsets(Class* klass); |
| 277 | void CreateReferenceStaticOffsets(Class* klass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 278 | void CreateReferenceOffsets(Class *klass, bool instance, |
| 279 | uint32_t reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 280 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 281 | // lock to protect ClassLinker state |
| 282 | mutable Mutex lock_; |
| 283 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 284 | std::vector<const DexFile*> boot_class_path_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 285 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 286 | std::vector<const DexFile*> dex_files_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 287 | std::vector<DexCache*> dex_caches_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 288 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 289 | // multimap from a StringPiece hash code of a class descriptor to |
| 290 | // Class* instances. Results should be compared for a matching |
| 291 | // Class::descriptor_ and Class::class_loader_. |
| 292 | typedef std::tr1::unordered_multimap<size_t, Class*> Table; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 293 | Table classes_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 294 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 295 | // indexes into class_roots_. |
| 296 | // needs to be kept in sync with class_roots_descriptors_. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 297 | enum ClassRoot { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 298 | kJavaLangClass, |
| 299 | kJavaLangObject, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 300 | kObjectArrayClass, |
| 301 | kJavaLangString, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 302 | kJavaLangReflectField, |
| 303 | kJavaLangReflectMethod, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 304 | kJavaLangClassLoader, |
| 305 | kDalvikSystemBaseDexClassLoader, |
| 306 | kDalvikSystemPathClassLoader, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 307 | kJavaLangStackTraceElement, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 308 | kPrimitiveBoolean, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 309 | kPrimitiveByte, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 310 | kPrimitiveChar, |
| 311 | kPrimitiveDouble, |
| 312 | kPrimitiveFloat, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 313 | kPrimitiveInt, |
| 314 | kPrimitiveLong, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 315 | kPrimitiveShort, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 316 | kPrimitiveVoid, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 317 | kBooleanArrayClass, |
| 318 | kByteArrayClass, |
| 319 | kCharArrayClass, |
| 320 | kDoubleArrayClass, |
| 321 | kFloatArrayClass, |
| 322 | kIntArrayClass, |
| 323 | kLongArrayClass, |
| 324 | kShortArrayClass, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 325 | kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 326 | kClassRootsMax, |
| 327 | }; |
| 328 | ObjectArray<Class>* class_roots_; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 329 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 330 | Class* GetClassRoot(ClassRoot class_root) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 331 | DCHECK(class_roots_ != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 332 | Class* klass = class_roots_->Get(class_root); |
| 333 | DCHECK(klass != NULL); |
| 334 | return klass; |
| 335 | } |
| 336 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 337 | void SetClassRoot(ClassRoot class_root, Class* klass) { |
| 338 | DCHECK(!init_done_); |
| 339 | |
| 340 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 341 | DCHECK(klass->GetClassLoader() == NULL); |
| 342 | DCHECK(klass->GetDescriptor() != NULL); |
| 343 | DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root))); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 344 | |
| 345 | DCHECK(class_roots_ != NULL); |
| 346 | DCHECK(class_roots_->Get(class_root) == NULL); |
| 347 | class_roots_->Set(class_root, klass); |
| 348 | } |
| 349 | |
| 350 | static const char* class_roots_descriptors_[kClassRootsMax]; |
| 351 | |
| 352 | const char* GetClassRootDescriptor(ClassRoot class_root) { |
| 353 | const char* descriptor = class_roots_descriptors_[class_root]; |
| 354 | CHECK(descriptor != NULL); |
| 355 | return descriptor; |
| 356 | } |
| 357 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 358 | ObjectArray<Class>* array_interfaces_; |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 359 | ObjectArray<InterfaceEntry>* array_iftable_; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 360 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 361 | bool init_done_; |
| 362 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 363 | InternTable* intern_table_; |
| 364 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 365 | friend class CommonTest; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 366 | FRIEND_TEST(DexCacheTest, Open); |
| 367 | friend class ObjectTest; |
| 368 | FRIEND_TEST(ObjectTest, AllocObjectArray); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 369 | FRIEND_TEST(ExceptionTest, FindExceptionHandler); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 370 | DISALLOW_COPY_AND_ASSIGN(ClassLinker); |
| 371 | }; |
| 372 | |
| 373 | } // namespace art |
| 374 | |
| 375 | #endif // ART_SRC_CLASS_LINKER_H_ |