blob: edd48eb4db994a84284534e4877579f2a63fd647 [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
Brian Carlstrom6cc18452011-07-18 15:10:33 -070034 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070035 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070036 }
37
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070038 bool InitializeClass(Class* klass);
39
Brian Carlstrom4a96b602011-07-26 16:40:23 -070040 void RegisterDexFile(const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041
Brian Carlstrom7e93b502011-08-04 14:16:22 -070042 void VisitRoots(Heap::RootVistor* root_visitor, void* arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070043
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044 private:
Brian Carlstrom7e93b502011-08-04 14:16:22 -070045 ClassLinker() {
46 classes_lock_ = Mutex::Create("ClassLinker::Lock");
47 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070048
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049 void Init(const std::vector<DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070051 // For early bootstrapping by Init
52 Class* AllocClass(Class* java_lang_Class);
53
54 // Alloc* convenience functions to avoid needing to pass in Class*
55 // values that are known to the ClassLinker such as
56 // kObjectArrayClass and kJavaLangString etc.
57 Class* AllocClass();
58 DexCache* AllocDexCache();
Jesse Wilson35baaab2011-08-10 16:18:03 -040059 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070060 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070061 template <class T>
62 ObjectArray<T>* AllocObjectArray(size_t length) {
63 return ObjectArray<T>::Alloc(class_roots_->Get(kObjectArrayClass), length);
64 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070065 PathClassLoader* AllocPathClassLoader(std::vector<const DexFile*> dex_files);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070066
Brian Carlstroma331b3c2011-07-18 17:47:56 -070067 Class* CreatePrimitiveClass(const StringPiece& descriptor);
68
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070069 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070070 ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070071
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070072 Class* FindPrimitiveClass(char type);
Carl Shapiro565f5072011-07-10 13:39:43 -070073
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070074 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070075
Brian Carlstromf615a612011-07-23 12:50:34 -070076 DexCache* FindDexCache(const DexFile* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070077
Brian Carlstromf615a612011-07-23 12:50:34 -070078 void AppendToBootClassPath(DexFile* dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070079
Brian Carlstromf615a612011-07-23 12:50:34 -070080 void LoadClass(const DexFile& dex_file,
81 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070082 Class* klass,
83 ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070084
Brian Carlstromf615a612011-07-23 12:50:34 -070085 void LoadInterfaces(const DexFile& dex_file,
86 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087 Class *klass);
88
Brian Carlstromf615a612011-07-23 12:50:34 -070089 void LoadField(const DexFile& dex_file,
90 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070091 Class* klass,
92 Field* dst);
93
Brian Carlstromf615a612011-07-23 12:50:34 -070094 void LoadMethod(const DexFile& dex_file,
95 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070096 Class* klass,
97 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -070098
Brian Carlstrom7e93b502011-08-04 14:16:22 -070099 Class* ResolveClass(const Class* referring,
100 uint32_t class_idx,
101 const DexFile& dex_file);
102
103 String* ResolveString(const Class* referring,
104 uint32_t string_idx,
105 const DexFile& dex_file);
106
107 Class* LookupClass(const StringPiece& descriptor, ClassLoader* class_loader);
108
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700109 // Inserts a class into the class table. Returns true if the class
110 // was inserted.
111 bool InsertClass(Class* klass);
112
113 bool InitializeSuperClass(Class* klass);
114
115 void InitializeStaticFields(Class* klass);
116
117 bool ValidateSuperClassDescriptors(const Class* klass);
118
119 bool HasSameDescriptorClasses(const char* descriptor,
120 const Class* klass1,
121 const Class* klass2);
122
123 bool HasSameMethodDescriptorClasses(const Method* descriptor,
124 const Class* klass1,
125 const Class* klass2);
126
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700127 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700128
129 bool LinkSuperClass(Class* klass);
130
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700131 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700132
133 bool LinkMethods(Class* klass);
134
135 bool LinkVirtualMethods(Class* klass);
136
137 bool LinkInterfaceMethods(Class* klass);
138
139 void LinkAbstractMethods(Class* klass);
140
Jesse Wilson7833bd22011-08-09 18:31:44 -0400141 bool LinkStaticFields(Class* klass);
142
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700143 bool LinkInstanceFields(Class* klass);
144
145 void CreateReferenceOffsets(Class* klass);
146
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700147 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700148
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700149 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700150
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700151 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700152
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700153 // multimap from String::descriptor_ to Class* instances. Results
154 // should be compared for a matching Class::descriptor_ and
155 // Class::class_loader_.
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700156 typedef std::tr1::unordered_multimap<StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158 Mutex* classes_lock_;
159
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700160 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700161
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700162 // indexes into class_roots_
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700163 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700164 kJavaLangClass,
165 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700166 kObjectArrayClass,
167 kJavaLangString,
168 kCharArrayClass,
Jesse Wilson7833bd22011-08-09 18:31:44 -0400169 kIntArrayClass,
170 kLongArrayClass,
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,
177 kPrimitiveChar,
178 kPrimitiveFloat,
179 kPrimitiveDouble,
180 kPrimitiveByte,
181 kPrimitiveShort,
182 kPrimitiveInt,
183 kPrimitiveLong,
184 kPrimitiveVoid,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700185 kClassRootsMax,
186 };
187 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700188
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700189 Class* GetClassRoot(ClassRoot class_root) {
190 Class* klass = class_roots_->Get(class_root);
191 DCHECK(klass != NULL);
192 return klass;
193 }
194
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700195 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700196 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700197
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700198 bool init_done_;
199
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700200 friend class RuntimeTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700201 FRIEND_TEST(DexCacheTest, Open);
202 friend class ObjectTest;
203 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
205};
206
207} // namespace art
208
209#endif // ART_SRC_CLASS_LINKER_H_