blob: 90ac6780f816f159588c8c81610e42e01034a9a2 [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
Elliott Hughesde69d7f2011-08-18 16:49:37 -070027 ~ClassLinker() {
28 delete classes_lock_;
29 }
Carl Shapiro565f5072011-07-10 13:39:43 -070030
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070031 // Finds a class by its descriptor name.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070032 // If class_loader is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070033 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070034 ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070036 Class* FindPrimitiveClass(char type);
37
Brian Carlstrom6cc18452011-07-18 15:10:33 -070038 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070039 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070040 }
41
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042 bool InitializeClass(Class* klass);
43
Brian Carlstrom4a96b602011-07-26 16:40:23 -070044 void RegisterDexFile(const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
Brian Carlstrom7e93b502011-08-04 14:16:22 -070046 void VisitRoots(Heap::RootVistor* root_visitor, void* arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070047
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070049 ClassLinker()
50 : classes_lock_(Mutex::Create("ClassLinker::Lock")),
51 init_done_(false) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -070052 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070053
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054 void Init(const std::vector<DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070055
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070056 // For early bootstrapping by Init
57 Class* AllocClass(Class* java_lang_Class);
58
59 // Alloc* convenience functions to avoid needing to pass in Class*
60 // values that are known to the ClassLinker such as
61 // kObjectArrayClass and kJavaLangString etc.
62 Class* AllocClass();
63 DexCache* AllocDexCache();
Jesse Wilson35baaab2011-08-10 16:18:03 -040064 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070065 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070066 template <class T>
67 ObjectArray<T>* AllocObjectArray(size_t length) {
68 return ObjectArray<T>::Alloc(class_roots_->Get(kObjectArrayClass), length);
69 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070070 PathClassLoader* AllocPathClassLoader(std::vector<const DexFile*> dex_files);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070071
Brian Carlstroma331b3c2011-07-18 17:47:56 -070072 Class* CreatePrimitiveClass(const StringPiece& descriptor);
73
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070074 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070075 ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070076
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070077 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070078
Brian Carlstromf615a612011-07-23 12:50:34 -070079 DexCache* FindDexCache(const DexFile* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070080
Brian Carlstromf615a612011-07-23 12:50:34 -070081 void AppendToBootClassPath(DexFile* dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070082
Brian Carlstromf615a612011-07-23 12:50:34 -070083 void LoadClass(const DexFile& dex_file,
84 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070085 Class* klass,
86 ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087
Brian Carlstromf615a612011-07-23 12:50:34 -070088 void LoadInterfaces(const DexFile& dex_file,
89 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070090 Class *klass);
91
Brian Carlstromf615a612011-07-23 12:50:34 -070092 void LoadField(const DexFile& dex_file,
93 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070094 Class* klass,
95 Field* dst);
96
Brian Carlstromf615a612011-07-23 12:50:34 -070097 void LoadMethod(const DexFile& dex_file,
98 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070099 Class* klass,
100 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700101
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700102 Class* ResolveClass(const Class* referring,
103 uint32_t class_idx,
104 const DexFile& dex_file);
105
106 String* ResolveString(const Class* referring,
107 uint32_t string_idx,
108 const DexFile& dex_file);
109
110 Class* LookupClass(const StringPiece& descriptor, ClassLoader* class_loader);
111
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700112 // Inserts a class into the class table. Returns true if the class
113 // was inserted.
114 bool InsertClass(Class* klass);
115
116 bool InitializeSuperClass(Class* klass);
117
118 void InitializeStaticFields(Class* klass);
119
120 bool ValidateSuperClassDescriptors(const Class* klass);
121
122 bool HasSameDescriptorClasses(const char* descriptor,
123 const Class* klass1,
124 const Class* klass2);
125
126 bool HasSameMethodDescriptorClasses(const Method* descriptor,
127 const Class* klass1,
128 const Class* klass2);
129
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700130 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700131
132 bool LinkSuperClass(Class* klass);
133
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700134 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700135
136 bool LinkMethods(Class* klass);
137
138 bool LinkVirtualMethods(Class* klass);
139
140 bool LinkInterfaceMethods(Class* klass);
141
142 void LinkAbstractMethods(Class* klass);
143
Jesse Wilson7833bd22011-08-09 18:31:44 -0400144 bool LinkStaticFields(Class* klass);
145
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700146 bool LinkInstanceFields(Class* klass);
147
148 void CreateReferenceOffsets(Class* klass);
149
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700150 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700151
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700152 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700153
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700154 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700155
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700156 // multimap from String::descriptor_ to Class* instances. Results
157 // should be compared for a matching Class::descriptor_ and
158 // Class::class_loader_.
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700159 typedef std::tr1::unordered_multimap<StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700160 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700161 Mutex* classes_lock_;
162
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700163 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700164
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700165 // indexes into class_roots_
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700166 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700167 kJavaLangClass,
168 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700169 kObjectArrayClass,
170 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700171 kJavaLangReflectField,
172 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700173 kJavaLangClassLoader,
174 kDalvikSystemBaseDexClassLoader,
175 kDalvikSystemPathClassLoader,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700176 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700177 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700178 kPrimitiveChar,
179 kPrimitiveDouble,
180 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700181 kPrimitiveInt,
182 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700183 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700184 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700185 kBooleanArrayClass,
186 kByteArrayClass,
187 kCharArrayClass,
188 kDoubleArrayClass,
189 kFloatArrayClass,
190 kIntArrayClass,
191 kLongArrayClass,
192 kShortArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700193 kClassRootsMax,
194 };
195 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700196
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700197 Class* GetClassRoot(ClassRoot class_root) {
198 Class* klass = class_roots_->Get(class_root);
199 DCHECK(klass != NULL);
200 return klass;
201 }
202
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700203 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700204 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700205
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700206 bool init_done_;
207
Brian Carlstromf734cf52011-08-17 16:28:14 -0700208 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700209 FRIEND_TEST(DexCacheTest, Open);
210 friend class ObjectTest;
211 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700212 FRIEND_TEST(ExceptionTest, MyClass_F_G);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700213 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
214};
215
216} // namespace art
217
218#endif // ART_SRC_CLASS_LINKER_H_