blob: 15bd054287aa547f9a41cab1538b03dd62783e58 [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();
59 StaticField* AllocStaticField();
60 InstanceField* AllocInstanceField();
61 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070062 template <class T>
63 ObjectArray<T>* AllocObjectArray(size_t length) {
64 return ObjectArray<T>::Alloc(class_roots_->Get(kObjectArrayClass), length);
65 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070066 PathClassLoader* AllocPathClassLoader(std::vector<const DexFile*> dex_files);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070067
Brian Carlstroma331b3c2011-07-18 17:47:56 -070068 Class* CreatePrimitiveClass(const StringPiece& descriptor);
69
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070070 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070071 ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070072
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070073 Class* FindPrimitiveClass(char type);
Carl Shapiro565f5072011-07-10 13:39:43 -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
142 bool LinkInstanceFields(Class* klass);
143
144 void CreateReferenceOffsets(Class* klass);
145
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700146 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700147
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700148 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700149
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700150 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700151
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700152 // multimap from String::descriptor_ to Class* instances. Results
153 // should be compared for a matching Class::descriptor_ and
154 // Class::class_loader_.
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700155 typedef std::tr1::unordered_multimap<StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157 Mutex* classes_lock_;
158
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700159 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700160
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700161 // indexes into class_roots_
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700162 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 kJavaLangClass,
164 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 kObjectArrayClass,
166 kJavaLangString,
167 kCharArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700168 kJavaLangReflectField,
169 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700170 kJavaLangClassLoader,
171 kDalvikSystemBaseDexClassLoader,
172 kDalvikSystemPathClassLoader,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700173 kPrimitiveBoolean,
174 kPrimitiveChar,
175 kPrimitiveFloat,
176 kPrimitiveDouble,
177 kPrimitiveByte,
178 kPrimitiveShort,
179 kPrimitiveInt,
180 kPrimitiveLong,
181 kPrimitiveVoid,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700182 kClassRootsMax,
183 };
184 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700185
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700186 Class* GetClassRoot(ClassRoot class_root) {
187 Class* klass = class_roots_->Get(class_root);
188 DCHECK(klass != NULL);
189 return klass;
190 }
191
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700192 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700193 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700194
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700195 bool init_done_;
196
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700197 friend class RuntimeTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700198 FRIEND_TEST(DexCacheTest, Open);
199 friend class ObjectTest;
200 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700201 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
202};
203
204} // namespace art
205
206#endif // ART_SRC_CLASS_LINKER_H_