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