blob: e408dbb4c30821db7075158dde6217dadd4de157 [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 Hughes6fa602d2011-12-02 17:54:25 -080071 // Finds all the classes with the given descriptor, regardless of ClassLoader.
72 void LookupClasses(const std::string& descriptor, std::vector<Class*>& classes);
73
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070074 Class* FindPrimitiveClass(char type);
75
Brian Carlstromae826982011-11-09 01:33:42 -080076 // General class unloading is not supported, this is used to prune
77 // unwanted classes during image writing.
78 bool RemoveClass(const std::string& descriptor, const ClassLoader* class_loader);
79
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070080 void DumpAllClasses(int flags) const;
81
Elliott Hughescac6cc72011-11-03 20:31:21 -070082 void DumpForSigQuit(std::ostream& os) const;
83
Elliott Hughese27955c2011-08-26 15:21:24 -070084 size_t NumLoadedClasses() const;
85
Brian Carlstromb63ec392011-08-27 17:38:27 -070086 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070087 // result in the DexCache. The referrer is used to identify the
88 // target DexCache and ClassLoader to use for resolution.
89 String* ResolveString(uint32_t string_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -070090 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
91 if (UNLIKELY(resolved_string == NULL)) {
92 Class* declaring_class = referrer->GetDeclaringClass();
93 DexCache* dex_cache = declaring_class->GetDexCache();
94 const DexFile& dex_file = FindDexFile(dex_cache);
95 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
96 }
97 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -070098 }
99
100 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700101 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700102 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103
Brian Carlstromb63ec392011-08-27 17:38:27 -0700104 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700105 // result in the DexCache. The referrer is used to identity the
106 // target DexCache and ClassLoader to use for resolution.
107 Class* ResolveType(const DexFile& dex_file,
108 uint32_t type_idx,
109 const Class* referrer) {
110 return ResolveType(dex_file,
111 type_idx,
112 referrer->GetDexCache(),
113 referrer->GetClassLoader());
114 }
115
Brian Carlstromb63ec392011-08-27 17:38:27 -0700116 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700118 // target DexCache and ClassLoader to use for resolution.
Brian Carlstromb9edb842011-08-28 16:31:06 -0700119 Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700120 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
121 if (UNLIKELY(resolved_type == NULL)) {
122 Class* declaring_class = referrer->GetDeclaringClass();
123 DexCache* dex_cache = declaring_class->GetDexCache();
124 const ClassLoader* class_loader = declaring_class->GetClassLoader();
125 const DexFile& dex_file = FindDexFile(dex_cache);
126 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
127 }
128 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700129 }
130
131 Class* ResolveType(uint32_t type_idx, const Field* referrer) {
132 Class* declaring_class = referrer->GetDeclaringClass();
133 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700134 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
135 if (UNLIKELY(resolved_type == NULL)) {
136 const ClassLoader* class_loader = declaring_class->GetClassLoader();
137 const DexFile& dex_file = FindDexFile(dex_cache);
138 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
139 }
140 return resolved_type;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700141 }
142
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700143 // Resolve a type with the given ID from the DexFile, storing the
144 // result in DexCache. The ClassLoader is used to search for the
145 // type, since it may be referenced from but not contained within
146 // the given DexFile.
147 Class* ResolveType(const DexFile& dex_file,
148 uint32_t type_idx,
149 DexCache* dex_cache,
150 const ClassLoader* class_loader);
151
152 // Resolve a method with a given ID from the DexFile, storing the
153 // result in DexCache. The ClassLinker and ClassLoader are used as
154 // in ResolveType. What is unique is the method type argument which
155 // is used to determine if this method is a direct, static, or
156 // virtual method.
157 Method* ResolveMethod(const DexFile& dex_file,
158 uint32_t method_idx,
159 DexCache* dex_cache,
160 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700161 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700162
Brian Carlstrom16192862011-09-12 17:50:06 -0700163 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700164 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
165 if (UNLIKELY(resolved_method == NULL)) {
166 Class* declaring_class = referrer->GetDeclaringClass();
167 DexCache* dex_cache = declaring_class->GetDexCache();
168 const ClassLoader* class_loader = declaring_class->GetClassLoader();
169 const DexFile& dex_file = FindDexFile(dex_cache);
170 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
171 }
172 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700173 }
174
Brian Carlstrom845490b2011-09-19 15:56:53 -0700175 Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700176 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
177 if (UNLIKELY(resolved_field == NULL)) {
178 Class* declaring_class = referrer->GetDeclaringClass();
179 DexCache* dex_cache = declaring_class->GetDexCache();
180 const ClassLoader* class_loader = declaring_class->GetClassLoader();
181 const DexFile& dex_file = FindDexFile(dex_cache);
182 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
183 }
184 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700185 }
186
Brian Carlstrom16192862011-09-12 17:50:06 -0700187 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700188 // result in DexCache. The ClassLinker and ClassLoader are used as
189 // in ResolveType. What is unique is the is_static argument which is
190 // used to determine if we are resolving a static or non-static
191 // field.
192 Field* ResolveField(const DexFile& dex_file,
193 uint32_t field_idx,
194 DexCache* dex_cache,
195 const ClassLoader* class_loader,
196 bool is_static);
197
Ian Rogersad25ac52011-10-04 19:13:33 -0700198 // Get shorty from method index without resolution. Used to do handlerization.
199 const char* MethodShorty(uint32_t method_idx, Method* referrer);
200
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700201 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700202 // can_run_clinit=false allows the compiler to attempt to init a class,
203 // given the restriction that no <clinit> execution is possible.
204 bool EnsureInitialized(Class* c, bool can_run_clinit);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700205
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700206 // Initializes classes that have instances in the image but that have
207 // <clinit> methods so they could not be initialized by the compiler.
208 void RunRootClinits();
209
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700210 void RegisterDexFile(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700211 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700212
Brian Carlstrom8a487412011-08-29 20:08:52 -0700213 const std::vector<const DexFile*>& GetBootClassPath() {
214 return boot_class_path_;
215 }
216
Elliott Hughesa2155262011-11-16 16:26:58 -0800217 void VisitClasses(ClassVisitor* visitor, void* arg) const;
218
Elliott Hughes410c0c82011-09-01 17:58:25 -0700219 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700220
buzbeec143c552011-08-20 17:38:58 -0700221 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700222 DexCache* FindDexCache(const DexFile& dex_file) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223 bool IsDexFileRegistered(const DexFile& dex_file) const;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700224
jeffhao262bf462011-10-20 18:36:32 -0700225 // Generate an oat file from a dex file
226 const OatFile* GenerateOatFile(const std::string& filename);
227
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700228 // Find, possibily opening, an OatFile corresponding to a DexFile
Brian Carlstromae826982011-11-09 01:33:42 -0800229 const OatFile* FindOatFileForDexFile(const DexFile& dex_file);
230 const OatFile* FindOatFileFromOatLocation(const std::string& location);
buzbeec143c552011-08-20 17:38:58 -0700231
Elliott Hughes418d20f2011-09-22 14:00:39 -0700232 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700233 template <class T>
234 ObjectArray<T>* AllocObjectArray(size_t length) {
235 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
236 }
237
Elliott Hughes418d20f2011-09-22 14:00:39 -0700238 ObjectArray<Class>* AllocClassArray(size_t length) {
239 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
240 }
241
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700242 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
243
jeffhao98eacac2011-09-14 16:11:53 -0700244 void VerifyClass(Class* klass);
245
Jesse Wilson95caa792011-10-12 18:14:17 -0400246 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Ian Rogers466bb252011-10-14 03:29:56 -0700247 ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
Jesse Wilson95caa792011-10-12 18:14:17 -0400248
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700249 pid_t GetClassesLockOwner(); // For SignalCatcher.
250 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700251
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700252 private:
Brian Carlstromae826982011-11-09 01:33:42 -0800253 explicit ClassLinker(bool verbose, InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700254
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700255 // Initialize class linker by bootstraping from dex files
256 void Init(const std::string& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700257
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700258 // Initialize class linker from one or more images.
259 void InitFromImage();
260 OatFile* OpenOat(const Space* space);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700261 static void InitFromImageCallback(Object* obj, void* arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700262 struct InitFromImageCallbackState;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700263
264 void FinishInit();
265
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700266 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700267 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700268
269 // Alloc* convenience functions to avoid needing to pass in Class*
270 // values that are known to the ClassLinker such as
271 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700272 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700273 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400274 Field* AllocField();
Ian Rogersbdb03912011-09-14 00:55:44 -0700275
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700276 Method* AllocMethod();
Ian Rogersbdb03912011-09-14 00:55:44 -0700277
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700278 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700279 InterfaceEntry* AllocInterfaceEntry(Class* interface);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700280
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700281 Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700282 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
283 }
284 Class* InitializePrimitiveClass(Class* primitive_class,
285 const char* descriptor,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700286 Primitive::Type type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700287
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700288
Brian Carlstromaded5f72011-10-07 17:15:04 -0700289 Class* CreateArrayClass(const std::string& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700290 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700291
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700292 void AppendToBootClassPath(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700293 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700294
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700295 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers0571d352011-11-03 19:51:38 -0700296 Class* c, std::map<uint32_t, Field*>& field_map);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700297
Brian Carlstrom4873d462011-08-21 15:23:39 -0700298 size_t SizeOfClass(const DexFile& dex_file,
299 const DexFile::ClassDef& dex_class_def);
300
Brian Carlstromf615a612011-07-23 12:50:34 -0700301 void LoadClass(const DexFile& dex_file,
302 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700303 SirtRef<Class>& klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700304 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700305
Brian Carlstromf615a612011-07-23 12:50:34 -0700306 void LoadInterfaces(const DexFile& dex_file,
307 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700308 SirtRef<Class>& klass);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700309
Ian Rogers0571d352011-11-03 19:51:38 -0700310 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700311 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700312
Ian Rogers0571d352011-11-03 19:51:38 -0700313 void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
314 SirtRef<Class>& klass, SirtRef<Method>& dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700315
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700316 // Inserts a class into the class table. Returns true if the class
317 // was inserted.
Ian Rogers5d76c432011-10-31 21:42:49 -0700318 bool InsertClass(const std::string& descriptor, Class* klass, bool image_class);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700319
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700320 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700321 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
322
Brian Carlstromd1422f82011-09-28 11:37:09 -0700323 bool InitializeClass(Class* klass, bool can_run_clinit);
324 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700325 bool ValidateSuperClassDescriptors(const Class* klass);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700326 bool InitializeSuperClass(Class* klass, bool can_run_clinit);
327 void InitializeStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700328
329 bool HasSameDescriptorClasses(const char* descriptor,
330 const Class* klass1,
331 const Class* klass2);
332
333 bool HasSameMethodDescriptorClasses(const Method* descriptor,
334 const Class* klass1,
335 const Class* klass2);
336
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700337 bool LinkClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700338
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700339 bool LinkSuperClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700340
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700341 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700342
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700343 bool LinkMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700344
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700345 bool LinkVirtualMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700346
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700347 bool LinkInterfaceMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700348
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700349 bool LinkStaticFields(SirtRef<Class>& klass);
350 bool LinkInstanceFields(SirtRef<Class>& klass);
351 bool LinkFields(SirtRef<Class>& klass, bool is_static);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700353
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700354 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
355 void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
356 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700358
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700359 // For use by ImageWriter to find DexCaches for its roots
360 const std::vector<DexCache*>& GetDexCaches() {
361 return dex_caches_;
362 }
363
Brian Carlstromae826982011-11-09 01:33:42 -0800364 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file);
365 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location);
Brian Carlstromfad71432011-10-16 20:25:10 -0700366
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700367 Method* CreateProxyConstructor(SirtRef<Class>& klass);
368 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype, ObjectArray<Class>* throws);
Jesse Wilson95caa792011-10-12 18:14:17 -0400369
Brian Carlstromae826982011-11-09 01:33:42 -0800370 const bool verbose_;
371
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700372 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700373
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700374 std::vector<const DexFile*> dex_files_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700375 std::vector<DexCache*> dex_caches_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700376 std::vector<const OatFile*> oat_files_;
377 // lock to protect concurrent access to dex_files_, dex_caches_, and oat_files_
378 mutable Mutex dex_lock_;
379
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380
Brian Carlstromaded5f72011-10-07 17:15:04 -0700381 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700382 // Class* instances. Results should be compared for a matching
383 // Class::descriptor_ and Class::class_loader_.
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700384 // Protected by classes_lock_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700385 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Ian Rogers5d76c432011-10-31 21:42:49 -0700386 Table image_classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700387 Table classes_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700388 mutable Mutex classes_lock_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700389
Brian Carlstroma663ea52011-08-19 23:33:41 -0700390 // indexes into class_roots_.
391 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700392 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700393 kJavaLangClass,
394 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700395 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700396 kObjectArrayClass,
397 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700398 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700399 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700400 kJavaLangReflectField,
401 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700402 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700403 kJavaLangClassLoader,
404 kDalvikSystemBaseDexClassLoader,
405 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700406 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700407 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700408 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700409 kPrimitiveChar,
410 kPrimitiveDouble,
411 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700412 kPrimitiveInt,
413 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700414 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700415 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700416 kBooleanArrayClass,
417 kByteArrayClass,
418 kCharArrayClass,
419 kDoubleArrayClass,
420 kFloatArrayClass,
421 kIntArrayClass,
422 kLongArrayClass,
423 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700424 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700425 kClassRootsMax,
426 };
427 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700428
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700429 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700430 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700431 Class* klass = class_roots_->Get(class_root);
432 DCHECK(klass != NULL);
433 return klass;
434 }
435
Brian Carlstroma663ea52011-08-19 23:33:41 -0700436 void SetClassRoot(ClassRoot class_root, Class* klass) {
437 DCHECK(!init_done_);
438
439 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700440 DCHECK(klass->GetClassLoader() == NULL);
441 DCHECK(klass->GetDescriptor() != NULL);
442 DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700443
444 DCHECK(class_roots_ != NULL);
445 DCHECK(class_roots_->Get(class_root) == NULL);
446 class_roots_->Set(class_root, klass);
447 }
448
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700449 ObjectArray<Class>* GetClassRoots() {
450 DCHECK(class_roots_ != NULL);
451 return class_roots_;
452 }
453
Elliott Hughes418d20f2011-09-22 14:00:39 -0700454 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700455
456 const char* GetClassRootDescriptor(ClassRoot class_root) {
457 const char* descriptor = class_roots_descriptors_[class_root];
458 CHECK(descriptor != NULL);
459 return descriptor;
460 }
461
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700462 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700463 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700464
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700465 bool init_done_;
466
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700467 InternTable* intern_table_;
468
Brian Carlstromf734cf52011-08-17 16:28:14 -0700469 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700470 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800471 friend class ObjectTest;
472 FRIEND_TEST(DexCacheTest, Open);
473 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
474 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700475 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
476};
477
478} // namespace art
479
480#endif // ART_SRC_CLASS_LINKER_H_