blob: b2b4c950d68d3a320af3f534cb231147dfd05363 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// 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 Carlstrom7e49dca2011-07-22 18:07:34 -070010#include "dex_file.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070011#include "heap.h"
12#include "intern_table.h"
13#include "macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "object.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070015#include "thread.h"
16#include "unordered_map.h"
17
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "gtest/gtest.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
20namespace art {
21
22class ClassLinker {
23 public:
Carl Shapiro565f5072011-07-10 13:39:43 -070024 // Initializes the class linker.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025 static ClassLinker* Create(const std::vector<DexFile*>& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070026
27 ~ClassLinker() {}
Carl Shapiro565f5072011-07-10 13:39:43 -070028
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070029 // Finds a class by its descriptor name.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070030 // If class_loader is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070031 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070032 ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070033
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070034 Class* FindPrimitiveClass(char type);
35
Brian Carlstrom6cc18452011-07-18 15:10:33 -070036 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070037 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070038 }
39
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070040 bool InitializeClass(Class* klass);
41
Brian Carlstrom4a96b602011-07-26 16:40:23 -070042 void RegisterDexFile(const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Brian Carlstrom7e93b502011-08-04 14:16:22 -070044 void VisitRoots(Heap::RootVistor* root_visitor, void* arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070045
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070047 ClassLinker()
48 : classes_lock_(Mutex::Create("ClassLinker::Lock")),
49 init_done_(false) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -070050 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070051
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052 void Init(const std::vector<DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070053
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070054 // For early bootstrapping by Init
55 Class* AllocClass(Class* java_lang_Class);
56
57 // Alloc* convenience functions to avoid needing to pass in Class*
58 // values that are known to the ClassLinker such as
59 // kObjectArrayClass and kJavaLangString etc.
60 Class* AllocClass();
61 DexCache* AllocDexCache();
Jesse Wilson35baaab2011-08-10 16:18:03 -040062 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070063 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070064 template <class T>
65 ObjectArray<T>* AllocObjectArray(size_t length) {
66 return ObjectArray<T>::Alloc(class_roots_->Get(kObjectArrayClass), length);
67 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070068 PathClassLoader* AllocPathClassLoader(std::vector<const DexFile*> dex_files);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070069
Brian Carlstroma331b3c2011-07-18 17:47:56 -070070 Class* CreatePrimitiveClass(const StringPiece& descriptor);
71
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070072 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070073 ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070074
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070075 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070076
Brian Carlstromf615a612011-07-23 12:50:34 -070077 DexCache* FindDexCache(const DexFile* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070078
Brian Carlstromf615a612011-07-23 12:50:34 -070079 void AppendToBootClassPath(DexFile* dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070080
Brian Carlstromf615a612011-07-23 12:50:34 -070081 void LoadClass(const DexFile& dex_file,
82 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070083 Class* klass,
84 ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070085
Brian Carlstromf615a612011-07-23 12:50:34 -070086 void LoadInterfaces(const DexFile& dex_file,
87 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070088 Class *klass);
89
Brian Carlstromf615a612011-07-23 12:50:34 -070090 void LoadField(const DexFile& dex_file,
91 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092 Class* klass,
93 Field* dst);
94
Brian Carlstromf615a612011-07-23 12:50:34 -070095 void LoadMethod(const DexFile& dex_file,
96 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070097 Class* klass,
98 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -070099
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700100 Class* ResolveClass(const Class* referring,
101 uint32_t class_idx,
102 const DexFile& dex_file);
103
104 String* ResolveString(const Class* referring,
105 uint32_t string_idx,
106 const DexFile& dex_file);
107
108 Class* LookupClass(const StringPiece& descriptor, ClassLoader* class_loader);
109
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700110 // Inserts a class into the class table. Returns true if the class
111 // was inserted.
112 bool InsertClass(Class* klass);
113
114 bool InitializeSuperClass(Class* klass);
115
116 void InitializeStaticFields(Class* klass);
117
118 bool ValidateSuperClassDescriptors(const Class* klass);
119
120 bool HasSameDescriptorClasses(const char* descriptor,
121 const Class* klass1,
122 const Class* klass2);
123
124 bool HasSameMethodDescriptorClasses(const Method* descriptor,
125 const Class* klass1,
126 const Class* klass2);
127
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700128 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700129
130 bool LinkSuperClass(Class* klass);
131
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700132 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700133
134 bool LinkMethods(Class* klass);
135
136 bool LinkVirtualMethods(Class* klass);
137
138 bool LinkInterfaceMethods(Class* klass);
139
140 void LinkAbstractMethods(Class* klass);
141
Jesse Wilson7833bd22011-08-09 18:31:44 -0400142 bool LinkStaticFields(Class* klass);
143
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700144 bool LinkInstanceFields(Class* klass);
145
146 void CreateReferenceOffsets(Class* klass);
147
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700148 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700149
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700150 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700151
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700152 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700153
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700154 // multimap from String::descriptor_ to Class* instances. Results
155 // should be compared for a matching Class::descriptor_ and
156 // Class::class_loader_.
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700157 typedef std::tr1::unordered_multimap<StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700159 Mutex* classes_lock_;
160
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700161 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700162
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 // indexes into class_roots_
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700164 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700165 kJavaLangClass,
166 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700167 kObjectArrayClass,
168 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700169 kJavaLangReflectField,
170 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700171 kJavaLangClassLoader,
172 kDalvikSystemBaseDexClassLoader,
173 kDalvikSystemPathClassLoader,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700174 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700175 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700176 kPrimitiveChar,
177 kPrimitiveDouble,
178 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700179 kPrimitiveInt,
180 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700181 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700182 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700183 kBooleanArrayClass,
184 kByteArrayClass,
185 kCharArrayClass,
186 kDoubleArrayClass,
187 kFloatArrayClass,
188 kIntArrayClass,
189 kLongArrayClass,
190 kShortArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700191 kClassRootsMax,
192 };
193 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700194
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700195 Class* GetClassRoot(ClassRoot class_root) {
196 Class* klass = class_roots_->Get(class_root);
197 DCHECK(klass != NULL);
198 return klass;
199 }
200
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700201 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700202 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700203
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700204 bool init_done_;
205
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700206 friend class RuntimeTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700207 FRIEND_TEST(DexCacheTest, Open);
208 friend class ObjectTest;
209 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700210 FRIEND_TEST(ExceptionTest, MyClass_F_G);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700211 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
212};
213
214} // namespace art
215
216#endif // ART_SRC_CLASS_LINKER_H_