blob: 03c8341d3abb6daf3bfb2fd517540b9726196373 [file] [log] [blame]
Elliott Hughes418d20f2011-09-22 14:00:39 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
17#ifndef ART_SRC_CLASS_LINKER_H_
18#define ART_SRC_CLASS_LINKER_H_
19
20#include <map>
21#include <utility>
22#include <vector>
23
Ian Rogerscaab8c42011-10-12 12:11:18 -070024#include "dex_cache.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025#include "dex_file.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070026#include "heap.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027#include "macros.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070028#include "mutex.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070029#include "oat_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "object.h"
Brian Carlstrom40381fb2011-10-19 14:13:40 -070031#include "stack_indirect_reference_table.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070032#include "unordered_map.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070033#include "unordered_set.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070034
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035#include "gtest/gtest.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070036
37namespace art {
38
Elliott Hughescf4c6c42011-09-01 15:16:42 -070039class ClassLoader;
40class InternTable;
Brian Carlstromd1422f82011-09-28 11:37:09 -070041class ObjectLock;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070042
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043class ClassLinker {
44 public:
Brian Carlstrom58ae9412011-10-04 00:56:06 -070045 // Creates the class linker by boot strapping from dex files.
46 static ClassLinker* Create(const std::string& boot_class_path, InternTable* intern_table);
47
48 // Creates the class linker from one or more images.
49 static ClassLinker* Create(InternTable* intern_table);
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070051 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070052
Elliott Hughes64bf5a32011-09-20 14:43:12 -070053 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070054 // If class_loader is null, searches boot_class_path_.
Brian Carlstromaded5f72011-10-07 17:15:04 -070055 Class* FindClass(const std::string& descriptor, const ClassLoader* class_loader);
56
57 Class* FindSystemClass(const std::string& descriptor) {
58 return FindClass(descriptor, NULL);
59 }
60
61 // Define a new a class based on a ClassDef from a DexFile
62 Class* DefineClass(const std::string& descriptor, const ClassLoader* class_loader,
63 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070064
65 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
66 // by the given 'class_loader'.
Brian Carlstromaded5f72011-10-07 17:15:04 -070067 Class* LookupClass(const std::string& descriptor, const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070069 Class* FindPrimitiveClass(char type);
70
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070071 void DumpAllClasses(int flags) const;
72
Elliott Hughescac6cc72011-11-03 20:31:21 -070073 void DumpForSigQuit(std::ostream& os) const;
74
Elliott Hughese27955c2011-08-26 15:21:24 -070075 size_t NumLoadedClasses() const;
76
Brian Carlstromb63ec392011-08-27 17:38:27 -070077 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070078 // result in the DexCache. The referrer is used to identify the
79 // target DexCache and ClassLoader to use for resolution.
80 String* ResolveString(uint32_t string_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -070081 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
82 if (UNLIKELY(resolved_string == NULL)) {
83 Class* declaring_class = referrer->GetDeclaringClass();
84 DexCache* dex_cache = declaring_class->GetDexCache();
85 const DexFile& dex_file = FindDexFile(dex_cache);
86 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
87 }
88 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -070089 }
90
91 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070092 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070093 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070094
Brian Carlstromb63ec392011-08-27 17:38:27 -070095 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070096 // result in the DexCache. The referrer is used to identity the
97 // target DexCache and ClassLoader to use for resolution.
98 Class* ResolveType(const DexFile& dex_file,
99 uint32_t type_idx,
100 const Class* referrer) {
101 return ResolveType(dex_file,
102 type_idx,
103 referrer->GetDexCache(),
104 referrer->GetClassLoader());
105 }
106
Brian Carlstromb63ec392011-08-27 17:38:27 -0700107 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700108 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700109 // target DexCache and ClassLoader to use for resolution.
Brian Carlstromb9edb842011-08-28 16:31:06 -0700110 Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700111 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
112 if (UNLIKELY(resolved_type == NULL)) {
113 Class* declaring_class = referrer->GetDeclaringClass();
114 DexCache* dex_cache = declaring_class->GetDexCache();
115 const ClassLoader* class_loader = declaring_class->GetClassLoader();
116 const DexFile& dex_file = FindDexFile(dex_cache);
117 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
118 }
119 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700120 }
121
122 Class* ResolveType(uint32_t type_idx, const Field* referrer) {
123 Class* declaring_class = referrer->GetDeclaringClass();
124 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700125 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
126 if (UNLIKELY(resolved_type == NULL)) {
127 const ClassLoader* class_loader = declaring_class->GetClassLoader();
128 const DexFile& dex_file = FindDexFile(dex_cache);
129 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
130 }
131 return resolved_type;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700132 }
133
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700134 // Resolve a type with the given ID from the DexFile, storing the
135 // result in DexCache. The ClassLoader is used to search for the
136 // type, since it may be referenced from but not contained within
137 // the given DexFile.
138 Class* ResolveType(const DexFile& dex_file,
139 uint32_t type_idx,
140 DexCache* dex_cache,
141 const ClassLoader* class_loader);
142
143 // Resolve a method with a given ID from the DexFile, storing the
144 // result in DexCache. The ClassLinker and ClassLoader are used as
145 // in ResolveType. What is unique is the method type argument which
146 // is used to determine if this method is a direct, static, or
147 // virtual method.
148 Method* ResolveMethod(const DexFile& dex_file,
149 uint32_t method_idx,
150 DexCache* dex_cache,
151 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700152 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700153
Brian Carlstrom16192862011-09-12 17:50:06 -0700154 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700155 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
156 if (UNLIKELY(resolved_method == NULL)) {
157 Class* declaring_class = referrer->GetDeclaringClass();
158 DexCache* dex_cache = declaring_class->GetDexCache();
159 const ClassLoader* class_loader = declaring_class->GetClassLoader();
160 const DexFile& dex_file = FindDexFile(dex_cache);
161 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
162 }
163 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700164 }
165
Brian Carlstrom845490b2011-09-19 15:56:53 -0700166 Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700167 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
168 if (UNLIKELY(resolved_field == NULL)) {
169 Class* declaring_class = referrer->GetDeclaringClass();
170 DexCache* dex_cache = declaring_class->GetDexCache();
171 const ClassLoader* class_loader = declaring_class->GetClassLoader();
172 const DexFile& dex_file = FindDexFile(dex_cache);
173 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
174 }
175 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700176 }
177
Brian Carlstrom16192862011-09-12 17:50:06 -0700178 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700179 // result in DexCache. The ClassLinker and ClassLoader are used as
180 // in ResolveType. What is unique is the is_static argument which is
181 // used to determine if we are resolving a static or non-static
182 // field.
183 Field* ResolveField(const DexFile& dex_file,
184 uint32_t field_idx,
185 DexCache* dex_cache,
186 const ClassLoader* class_loader,
187 bool is_static);
188
Ian Rogersad25ac52011-10-04 19:13:33 -0700189 // Get shorty from method index without resolution. Used to do handlerization.
190 const char* MethodShorty(uint32_t method_idx, Method* referrer);
191
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700192 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700193 // can_run_clinit=false allows the compiler to attempt to init a class,
194 // given the restriction that no <clinit> execution is possible.
195 bool EnsureInitialized(Class* c, bool can_run_clinit);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700196
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700197 // Initializes classes that have instances in the image but that have
198 // <clinit> methods so they could not be initialized by the compiler.
199 void RunRootClinits();
200
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700201 void RegisterDexFile(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700202 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700203
Brian Carlstrom8a487412011-08-29 20:08:52 -0700204 const std::vector<const DexFile*>& GetBootClassPath() {
205 return boot_class_path_;
206 }
207
Elliott Hughes410c0c82011-09-01 17:58:25 -0700208 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700209
buzbeec143c552011-08-20 17:38:58 -0700210 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700211 DexCache* FindDexCache(const DexFile& dex_file) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700212 bool IsDexFileRegistered(const DexFile& dex_file) const;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700213
jeffhao262bf462011-10-20 18:36:32 -0700214 // Generate an oat file from a dex file
215 const OatFile* GenerateOatFile(const std::string& filename);
216
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700217 // Find, possibily opening, an OatFile corresponding to a DexFile
218 const OatFile* FindOatFile(const DexFile& dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700219 const OatFile* FindOatFile(const std::string& location);
buzbeec143c552011-08-20 17:38:58 -0700220
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700222 template <class T>
223 ObjectArray<T>* AllocObjectArray(size_t length) {
224 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
225 }
226
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227 ObjectArray<Class>* AllocClassArray(size_t length) {
228 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
229 }
230
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700231 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
232
jeffhao98eacac2011-09-14 16:11:53 -0700233 void VerifyClass(Class* klass);
234
Jesse Wilson95caa792011-10-12 18:14:17 -0400235 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Ian Rogers466bb252011-10-14 03:29:56 -0700236 ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
Jesse Wilson95caa792011-10-12 18:14:17 -0400237
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700238 pid_t GetClassesLockOwner(); // For SignalCatcher.
239 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700240
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700241 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700242 explicit ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700243
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700244 // Initialize class linker by bootstraping from dex files
245 void Init(const std::string& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700246
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700247 // Initialize class linker from one or more images.
248 void InitFromImage();
249 OatFile* OpenOat(const Space* space);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700250 static void InitFromImageCallback(Object* obj, void* arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700251 struct InitFromImageCallbackState;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700252
253 void FinishInit();
254
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700255 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700256 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700257
258 // Alloc* convenience functions to avoid needing to pass in Class*
259 // values that are known to the ClassLinker such as
260 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700261 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700262 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400263 Field* AllocField();
Ian Rogersbdb03912011-09-14 00:55:44 -0700264
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700265 Method* AllocMethod();
Ian Rogersbdb03912011-09-14 00:55:44 -0700266
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700267 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700268 InterfaceEntry* AllocInterfaceEntry(Class* interface);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700269
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700270 Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700271 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
272 }
273 Class* InitializePrimitiveClass(Class* primitive_class,
274 const char* descriptor,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700275 Primitive::Type type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700276
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700277
Brian Carlstromaded5f72011-10-07 17:15:04 -0700278 Class* CreateArrayClass(const std::string& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700279 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700280
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700281 void AppendToBootClassPath(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700282 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700283
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700284 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
285 Class* c, std::map<int, Field*>& field_map);
286
Brian Carlstrom4873d462011-08-21 15:23:39 -0700287 size_t SizeOfClass(const DexFile& dex_file,
288 const DexFile::ClassDef& dex_class_def);
289
Brian Carlstromf615a612011-07-23 12:50:34 -0700290 void LoadClass(const DexFile& dex_file,
291 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700292 SirtRef<Class>& klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700293 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700294
Brian Carlstromf615a612011-07-23 12:50:34 -0700295 void LoadInterfaces(const DexFile& dex_file,
296 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700297 SirtRef<Class>& klass);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700298
Brian Carlstromf615a612011-07-23 12:50:34 -0700299 void LoadField(const DexFile& dex_file,
300 const DexFile::Field& dex_field,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700301 SirtRef<Class>& klass,
302 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700303
Brian Carlstromf615a612011-07-23 12:50:34 -0700304 void LoadMethod(const DexFile& dex_file,
305 const DexFile::Method& dex_method,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700306 SirtRef<Class>& klass,
307 SirtRef<Method>& dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700308
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700309 // Inserts a class into the class table. Returns true if the class
310 // was inserted.
Ian Rogers5d76c432011-10-31 21:42:49 -0700311 bool InsertClass(const std::string& descriptor, Class* klass, bool image_class);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700312
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700313 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700314 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
315
Brian Carlstromd1422f82011-09-28 11:37:09 -0700316 bool InitializeClass(Class* klass, bool can_run_clinit);
317 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700318 bool ValidateSuperClassDescriptors(const Class* klass);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700319 bool InitializeSuperClass(Class* klass, bool can_run_clinit);
320 void InitializeStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700321
322 bool HasSameDescriptorClasses(const char* descriptor,
323 const Class* klass1,
324 const Class* klass2);
325
326 bool HasSameMethodDescriptorClasses(const Method* descriptor,
327 const Class* klass1,
328 const Class* klass2);
329
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700330 bool LinkClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700331
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700332 bool LinkSuperClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700333
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700334 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700335
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700336 bool LinkMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700337
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700338 bool LinkVirtualMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700339
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700340 bool LinkInterfaceMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700341
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700342 bool LinkStaticFields(SirtRef<Class>& klass);
343 bool LinkInstanceFields(SirtRef<Class>& klass);
344 bool LinkFields(SirtRef<Class>& klass, bool is_static);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700346
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700347 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
348 void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
349 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700351
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700352 // For use by ImageWriter to find DexCaches for its roots
353 const std::vector<DexCache*>& GetDexCaches() {
354 return dex_caches_;
355 }
356
Brian Carlstromfad71432011-10-16 20:25:10 -0700357 const OatFile* FindOpenedOatFile(const std::string& location);
358
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700359 Method* CreateProxyConstructor(SirtRef<Class>& klass);
360 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype, ObjectArray<Class>* throws);
Jesse Wilson95caa792011-10-12 18:14:17 -0400361
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700362 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700363
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700364 std::vector<const DexFile*> dex_files_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700365 std::vector<DexCache*> dex_caches_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700366 std::vector<const OatFile*> oat_files_;
367 // lock to protect concurrent access to dex_files_, dex_caches_, and oat_files_
368 mutable Mutex dex_lock_;
369
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700370
Brian Carlstromaded5f72011-10-07 17:15:04 -0700371 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700372 // Class* instances. Results should be compared for a matching
373 // Class::descriptor_ and Class::class_loader_.
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700374 // Protected by classes_lock_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700375 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Ian Rogers5d76c432011-10-31 21:42:49 -0700376 Table image_classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700377 Table classes_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700378 mutable Mutex classes_lock_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700379
Brian Carlstroma663ea52011-08-19 23:33:41 -0700380 // indexes into class_roots_.
381 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700382 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700383 kJavaLangClass,
384 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700385 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700386 kObjectArrayClass,
387 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700388 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700389 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700390 kJavaLangReflectField,
391 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700392 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700393 kJavaLangClassLoader,
394 kDalvikSystemBaseDexClassLoader,
395 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700396 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700397 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700398 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700399 kPrimitiveChar,
400 kPrimitiveDouble,
401 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700402 kPrimitiveInt,
403 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700404 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700405 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700406 kBooleanArrayClass,
407 kByteArrayClass,
408 kCharArrayClass,
409 kDoubleArrayClass,
410 kFloatArrayClass,
411 kIntArrayClass,
412 kLongArrayClass,
413 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700414 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700415 kClassRootsMax,
416 };
417 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700418
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700419 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700420 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700421 Class* klass = class_roots_->Get(class_root);
422 DCHECK(klass != NULL);
423 return klass;
424 }
425
Brian Carlstroma663ea52011-08-19 23:33:41 -0700426 void SetClassRoot(ClassRoot class_root, Class* klass) {
427 DCHECK(!init_done_);
428
429 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430 DCHECK(klass->GetClassLoader() == NULL);
431 DCHECK(klass->GetDescriptor() != NULL);
432 DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700433
434 DCHECK(class_roots_ != NULL);
435 DCHECK(class_roots_->Get(class_root) == NULL);
436 class_roots_->Set(class_root, klass);
437 }
438
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700439 ObjectArray<Class>* GetClassRoots() {
440 DCHECK(class_roots_ != NULL);
441 return class_roots_;
442 }
443
Elliott Hughes418d20f2011-09-22 14:00:39 -0700444 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700445
446 const char* GetClassRootDescriptor(ClassRoot class_root) {
447 const char* descriptor = class_roots_descriptors_[class_root];
448 CHECK(descriptor != NULL);
449 return descriptor;
450 }
451
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700452 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700453 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700454
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700455 bool init_done_;
456
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700457 InternTable* intern_table_;
458
Brian Carlstromf734cf52011-08-17 16:28:14 -0700459 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700460 FRIEND_TEST(DexCacheTest, Open);
461 friend class ObjectTest;
462 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700463 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700464 friend class ImageWriter; // for GetClassRoots
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700465 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
466};
467
468} // namespace art
469
470#endif // ART_SRC_CLASS_LINKER_H_