blob: d212c59d65cc7d3afa4421d6dac7af6c585e4ae2 [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
Elliott Hughesa2155262011-11-16 16:26:58 -080043typedef bool (ClassVisitor)(Class* c, void* arg);
44
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045class ClassLinker {
46 public:
Brian Carlstrom58ae9412011-10-04 00:56:06 -070047 // Creates the class linker by boot strapping from dex files.
Brian Carlstromae826982011-11-09 01:33:42 -080048 static ClassLinker* Create(bool verbose, const std::string& boot_class_path, InternTable* intern_table);
Brian Carlstrom58ae9412011-10-04 00:56:06 -070049
50 // Creates the class linker from one or more images.
Brian Carlstromae826982011-11-09 01:33:42 -080051 static ClassLinker* Create(bool verbose, InternTable* intern_table);
Carl Shapiro61e019d2011-07-14 16:53:09 -070052
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070053 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070054
Elliott Hughes64bf5a32011-09-20 14:43:12 -070055 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070056 // If class_loader is null, searches boot_class_path_.
Brian Carlstromaded5f72011-10-07 17:15:04 -070057 Class* FindClass(const std::string& descriptor, const ClassLoader* class_loader);
58
59 Class* FindSystemClass(const std::string& descriptor) {
60 return FindClass(descriptor, NULL);
61 }
62
63 // Define a new a class based on a ClassDef from a DexFile
64 Class* DefineClass(const std::string& descriptor, const ClassLoader* class_loader,
65 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070066
67 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
68 // by the given 'class_loader'.
Brian Carlstromaded5f72011-10-07 17:15:04 -070069 Class* LookupClass(const std::string& descriptor, const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070070
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070071 Class* FindPrimitiveClass(char type);
72
Brian Carlstromae826982011-11-09 01:33:42 -080073 // General class unloading is not supported, this is used to prune
74 // unwanted classes during image writing.
75 bool RemoveClass(const std::string& descriptor, const ClassLoader* class_loader);
76
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070077 void DumpAllClasses(int flags) const;
78
Elliott Hughescac6cc72011-11-03 20:31:21 -070079 void DumpForSigQuit(std::ostream& os) const;
80
Elliott Hughese27955c2011-08-26 15:21:24 -070081 size_t NumLoadedClasses() const;
82
Brian Carlstromb63ec392011-08-27 17:38:27 -070083 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070084 // result in the DexCache. The referrer is used to identify the
85 // target DexCache and ClassLoader to use for resolution.
86 String* ResolveString(uint32_t string_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -070087 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
88 if (UNLIKELY(resolved_string == NULL)) {
89 Class* declaring_class = referrer->GetDeclaringClass();
90 DexCache* dex_cache = declaring_class->GetDexCache();
91 const DexFile& dex_file = FindDexFile(dex_cache);
92 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
93 }
94 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -070095 }
96
97 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070098 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070099 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700100
Brian Carlstromb63ec392011-08-27 17:38:27 -0700101 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700102 // result in the DexCache. The referrer is used to identity the
103 // target DexCache and ClassLoader to use for resolution.
104 Class* ResolveType(const DexFile& dex_file,
105 uint32_t type_idx,
106 const Class* referrer) {
107 return ResolveType(dex_file,
108 type_idx,
109 referrer->GetDexCache(),
110 referrer->GetClassLoader());
111 }
112
Brian Carlstromb63ec392011-08-27 17:38:27 -0700113 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700114 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700115 // target DexCache and ClassLoader to use for resolution.
Brian Carlstromb9edb842011-08-28 16:31:06 -0700116 Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700117 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
118 if (UNLIKELY(resolved_type == NULL)) {
119 Class* declaring_class = referrer->GetDeclaringClass();
120 DexCache* dex_cache = declaring_class->GetDexCache();
121 const ClassLoader* class_loader = declaring_class->GetClassLoader();
122 const DexFile& dex_file = FindDexFile(dex_cache);
123 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
124 }
125 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700126 }
127
128 Class* ResolveType(uint32_t type_idx, const Field* referrer) {
129 Class* declaring_class = referrer->GetDeclaringClass();
130 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700131 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
132 if (UNLIKELY(resolved_type == NULL)) {
133 const ClassLoader* class_loader = declaring_class->GetClassLoader();
134 const DexFile& dex_file = FindDexFile(dex_cache);
135 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
136 }
137 return resolved_type;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700138 }
139
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700140 // Resolve a type with the given ID from the DexFile, storing the
141 // result in DexCache. The ClassLoader is used to search for the
142 // type, since it may be referenced from but not contained within
143 // the given DexFile.
144 Class* ResolveType(const DexFile& dex_file,
145 uint32_t type_idx,
146 DexCache* dex_cache,
147 const ClassLoader* class_loader);
148
149 // Resolve a method with a given ID from the DexFile, storing the
150 // result in DexCache. The ClassLinker and ClassLoader are used as
151 // in ResolveType. What is unique is the method type argument which
152 // is used to determine if this method is a direct, static, or
153 // virtual method.
154 Method* ResolveMethod(const DexFile& dex_file,
155 uint32_t method_idx,
156 DexCache* dex_cache,
157 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700158 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700159
Brian Carlstrom16192862011-09-12 17:50:06 -0700160 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700161 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
162 if (UNLIKELY(resolved_method == NULL)) {
163 Class* declaring_class = referrer->GetDeclaringClass();
164 DexCache* dex_cache = declaring_class->GetDexCache();
165 const ClassLoader* class_loader = declaring_class->GetClassLoader();
166 const DexFile& dex_file = FindDexFile(dex_cache);
167 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
168 }
169 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700170 }
171
Brian Carlstrom845490b2011-09-19 15:56:53 -0700172 Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700173 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
174 if (UNLIKELY(resolved_field == NULL)) {
175 Class* declaring_class = referrer->GetDeclaringClass();
176 DexCache* dex_cache = declaring_class->GetDexCache();
177 const ClassLoader* class_loader = declaring_class->GetClassLoader();
178 const DexFile& dex_file = FindDexFile(dex_cache);
179 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
180 }
181 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700182 }
183
Brian Carlstrom16192862011-09-12 17:50:06 -0700184 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700185 // result in DexCache. The ClassLinker and ClassLoader are used as
186 // in ResolveType. What is unique is the is_static argument which is
187 // used to determine if we are resolving a static or non-static
188 // field.
189 Field* ResolveField(const DexFile& dex_file,
190 uint32_t field_idx,
191 DexCache* dex_cache,
192 const ClassLoader* class_loader,
193 bool is_static);
194
Ian Rogersad25ac52011-10-04 19:13:33 -0700195 // Get shorty from method index without resolution. Used to do handlerization.
196 const char* MethodShorty(uint32_t method_idx, Method* referrer);
197
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700198 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700199 // can_run_clinit=false allows the compiler to attempt to init a class,
200 // given the restriction that no <clinit> execution is possible.
201 bool EnsureInitialized(Class* c, bool can_run_clinit);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700202
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700203 // Initializes classes that have instances in the image but that have
204 // <clinit> methods so they could not be initialized by the compiler.
205 void RunRootClinits();
206
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700207 void RegisterDexFile(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700208 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209
Brian Carlstrom8a487412011-08-29 20:08:52 -0700210 const std::vector<const DexFile*>& GetBootClassPath() {
211 return boot_class_path_;
212 }
213
Elliott Hughesa2155262011-11-16 16:26:58 -0800214 void VisitClasses(ClassVisitor* visitor, void* arg) const;
215
Elliott Hughes410c0c82011-09-01 17:58:25 -0700216 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700217
buzbeec143c552011-08-20 17:38:58 -0700218 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700219 DexCache* FindDexCache(const DexFile& dex_file) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 bool IsDexFileRegistered(const DexFile& dex_file) const;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700221
jeffhao262bf462011-10-20 18:36:32 -0700222 // Generate an oat file from a dex file
223 const OatFile* GenerateOatFile(const std::string& filename);
224
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700225 // Find, possibily opening, an OatFile corresponding to a DexFile
Brian Carlstromae826982011-11-09 01:33:42 -0800226 const OatFile* FindOatFileForDexFile(const DexFile& dex_file);
227 const OatFile* FindOatFileFromOatLocation(const std::string& location);
buzbeec143c552011-08-20 17:38:58 -0700228
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700230 template <class T>
231 ObjectArray<T>* AllocObjectArray(size_t length) {
232 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
233 }
234
Elliott Hughes418d20f2011-09-22 14:00:39 -0700235 ObjectArray<Class>* AllocClassArray(size_t length) {
236 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
237 }
238
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700239 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
240
jeffhao98eacac2011-09-14 16:11:53 -0700241 void VerifyClass(Class* klass);
242
Jesse Wilson95caa792011-10-12 18:14:17 -0400243 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Ian Rogers466bb252011-10-14 03:29:56 -0700244 ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
Jesse Wilson95caa792011-10-12 18:14:17 -0400245
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700246 pid_t GetClassesLockOwner(); // For SignalCatcher.
247 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700248
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249 private:
Brian Carlstromae826982011-11-09 01:33:42 -0800250 explicit ClassLinker(bool verbose, InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700251
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700252 // Initialize class linker by bootstraping from dex files
253 void Init(const std::string& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700254
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700255 // Initialize class linker from one or more images.
256 void InitFromImage();
257 OatFile* OpenOat(const Space* space);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700258 static void InitFromImageCallback(Object* obj, void* arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700259 struct InitFromImageCallbackState;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700260
261 void FinishInit();
262
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700263 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700264 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700265
266 // Alloc* convenience functions to avoid needing to pass in Class*
267 // values that are known to the ClassLinker such as
268 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700269 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700270 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400271 Field* AllocField();
Ian Rogersbdb03912011-09-14 00:55:44 -0700272
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700273 Method* AllocMethod();
Ian Rogersbdb03912011-09-14 00:55:44 -0700274
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700275 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700276 InterfaceEntry* AllocInterfaceEntry(Class* interface);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700277
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700278 Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700279 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
280 }
281 Class* InitializePrimitiveClass(Class* primitive_class,
282 const char* descriptor,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700283 Primitive::Type type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700284
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700285
Brian Carlstromaded5f72011-10-07 17:15:04 -0700286 Class* CreateArrayClass(const std::string& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700287 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700288
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700289 void AppendToBootClassPath(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700290 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700291
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700292 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers0571d352011-11-03 19:51:38 -0700293 Class* c, std::map<uint32_t, Field*>& field_map);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700294
Brian Carlstrom4873d462011-08-21 15:23:39 -0700295 size_t SizeOfClass(const DexFile& dex_file,
296 const DexFile::ClassDef& dex_class_def);
297
Brian Carlstromf615a612011-07-23 12:50:34 -0700298 void LoadClass(const DexFile& dex_file,
299 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700300 SirtRef<Class>& klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700301 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700302
Brian Carlstromf615a612011-07-23 12:50:34 -0700303 void LoadInterfaces(const DexFile& dex_file,
304 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700305 SirtRef<Class>& klass);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700306
Ian Rogers0571d352011-11-03 19:51:38 -0700307 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700308 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700309
Ian Rogers0571d352011-11-03 19:51:38 -0700310 void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
311 SirtRef<Class>& klass, SirtRef<Method>& dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700312
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700313 // Inserts a class into the class table. Returns true if the class
314 // was inserted.
Ian Rogers5d76c432011-10-31 21:42:49 -0700315 bool InsertClass(const std::string& descriptor, Class* klass, bool image_class);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700316
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700317 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700318 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
319
Brian Carlstromd1422f82011-09-28 11:37:09 -0700320 bool InitializeClass(Class* klass, bool can_run_clinit);
321 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700322 bool ValidateSuperClassDescriptors(const Class* klass);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700323 bool InitializeSuperClass(Class* klass, bool can_run_clinit);
324 void InitializeStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700325
326 bool HasSameDescriptorClasses(const char* descriptor,
327 const Class* klass1,
328 const Class* klass2);
329
330 bool HasSameMethodDescriptorClasses(const Method* descriptor,
331 const Class* klass1,
332 const Class* klass2);
333
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700334 bool LinkClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700335
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700336 bool LinkSuperClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700337
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700338 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700339
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700340 bool LinkMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700341
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700342 bool LinkVirtualMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700343
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700344 bool LinkInterfaceMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700345
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700346 bool LinkStaticFields(SirtRef<Class>& klass);
347 bool LinkInstanceFields(SirtRef<Class>& klass);
348 bool LinkFields(SirtRef<Class>& klass, bool is_static);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700350
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700351 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
352 void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
353 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700354 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700355
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700356 // For use by ImageWriter to find DexCaches for its roots
357 const std::vector<DexCache*>& GetDexCaches() {
358 return dex_caches_;
359 }
360
Brian Carlstromae826982011-11-09 01:33:42 -0800361 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file);
362 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location);
Brian Carlstromfad71432011-10-16 20:25:10 -0700363
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700364 Method* CreateProxyConstructor(SirtRef<Class>& klass);
365 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype, ObjectArray<Class>* throws);
Jesse Wilson95caa792011-10-12 18:14:17 -0400366
Brian Carlstromae826982011-11-09 01:33:42 -0800367 const bool verbose_;
368
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700369 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700370
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700371 std::vector<const DexFile*> dex_files_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700372 std::vector<DexCache*> dex_caches_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700373 std::vector<const OatFile*> oat_files_;
374 // lock to protect concurrent access to dex_files_, dex_caches_, and oat_files_
375 mutable Mutex dex_lock_;
376
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700377
Brian Carlstromaded5f72011-10-07 17:15:04 -0700378 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700379 // Class* instances. Results should be compared for a matching
380 // Class::descriptor_ and Class::class_loader_.
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700381 // Protected by classes_lock_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700382 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Ian Rogers5d76c432011-10-31 21:42:49 -0700383 Table image_classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700384 Table classes_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700385 mutable Mutex classes_lock_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700386
Brian Carlstroma663ea52011-08-19 23:33:41 -0700387 // indexes into class_roots_.
388 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700389 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700390 kJavaLangClass,
391 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700392 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700393 kObjectArrayClass,
394 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700395 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700396 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700397 kJavaLangReflectField,
398 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700399 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700400 kJavaLangClassLoader,
401 kDalvikSystemBaseDexClassLoader,
402 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700403 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700404 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700405 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700406 kPrimitiveChar,
407 kPrimitiveDouble,
408 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700409 kPrimitiveInt,
410 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700411 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700412 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700413 kBooleanArrayClass,
414 kByteArrayClass,
415 kCharArrayClass,
416 kDoubleArrayClass,
417 kFloatArrayClass,
418 kIntArrayClass,
419 kLongArrayClass,
420 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700421 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700422 kClassRootsMax,
423 };
424 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700425
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700426 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700427 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700428 Class* klass = class_roots_->Get(class_root);
429 DCHECK(klass != NULL);
430 return klass;
431 }
432
Brian Carlstroma663ea52011-08-19 23:33:41 -0700433 void SetClassRoot(ClassRoot class_root, Class* klass) {
434 DCHECK(!init_done_);
435
436 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 DCHECK(klass->GetClassLoader() == NULL);
438 DCHECK(klass->GetDescriptor() != NULL);
439 DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700440
441 DCHECK(class_roots_ != NULL);
442 DCHECK(class_roots_->Get(class_root) == NULL);
443 class_roots_->Set(class_root, klass);
444 }
445
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700446 ObjectArray<Class>* GetClassRoots() {
447 DCHECK(class_roots_ != NULL);
448 return class_roots_;
449 }
450
Elliott Hughes418d20f2011-09-22 14:00:39 -0700451 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700452
453 const char* GetClassRootDescriptor(ClassRoot class_root) {
454 const char* descriptor = class_roots_descriptors_[class_root];
455 CHECK(descriptor != NULL);
456 return descriptor;
457 }
458
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700459 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700460 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700461
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700462 bool init_done_;
463
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700464 InternTable* intern_table_;
465
Brian Carlstromf734cf52011-08-17 16:28:14 -0700466 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700467 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800468 friend class ObjectTest;
469 FRIEND_TEST(DexCacheTest, Open);
470 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
471 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700472 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
473};
474
475} // namespace art
476
477#endif // ART_SRC_CLASS_LINKER_H_