blob: 37802ae67f4be94bec8a5478d2ec518b9f8d63c8 [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 Carlstrom7e93b502011-08-04 14:16:22 -070031#include "unordered_map.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070032#include "unordered_set.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070033
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "gtest/gtest.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035
36namespace art {
37
Elliott Hughescf4c6c42011-09-01 15:16:42 -070038class ClassLoader;
39class InternTable;
Brian Carlstromd1422f82011-09-28 11:37:09 -070040class ObjectLock;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070041
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042class ClassLinker {
43 public:
Brian Carlstrom58ae9412011-10-04 00:56:06 -070044 // Creates the class linker by boot strapping from dex files.
45 static ClassLinker* Create(const std::string& boot_class_path, InternTable* intern_table);
46
47 // Creates the class linker from one or more images.
48 static ClassLinker* Create(InternTable* intern_table);
Carl Shapiro61e019d2011-07-14 16:53:09 -070049
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070050 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070051
Elliott Hughes64bf5a32011-09-20 14:43:12 -070052 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070053 // If class_loader is null, searches boot_class_path_.
Brian Carlstromaded5f72011-10-07 17:15:04 -070054 Class* FindClass(const std::string& descriptor, const ClassLoader* class_loader);
55
56 Class* FindSystemClass(const std::string& descriptor) {
57 return FindClass(descriptor, NULL);
58 }
59
60 // Define a new a class based on a ClassDef from a DexFile
61 Class* DefineClass(const std::string& descriptor, const ClassLoader* class_loader,
62 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070063
64 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
65 // by the given 'class_loader'.
Brian Carlstromaded5f72011-10-07 17:15:04 -070066 Class* LookupClass(const std::string& descriptor, const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070067
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070068 Class* FindPrimitiveClass(char type);
69
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070070 void DumpAllClasses(int flags) const;
71
Elliott Hughese27955c2011-08-26 15:21:24 -070072 size_t NumLoadedClasses() const;
73
Brian Carlstromb63ec392011-08-27 17:38:27 -070074 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070075 // result in the DexCache. The referrer is used to identify the
76 // target DexCache and ClassLoader to use for resolution.
77 String* ResolveString(uint32_t string_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -070078 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
79 if (UNLIKELY(resolved_string == NULL)) {
80 Class* declaring_class = referrer->GetDeclaringClass();
81 DexCache* dex_cache = declaring_class->GetDexCache();
82 const DexFile& dex_file = FindDexFile(dex_cache);
83 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
84 }
85 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -070086 }
87
88 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070089 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070090 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070091
Brian Carlstromb63ec392011-08-27 17:38:27 -070092 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070093 // result in the DexCache. The referrer is used to identity the
94 // target DexCache and ClassLoader to use for resolution.
95 Class* ResolveType(const DexFile& dex_file,
96 uint32_t type_idx,
97 const Class* referrer) {
98 return ResolveType(dex_file,
99 type_idx,
100 referrer->GetDexCache(),
101 referrer->GetClassLoader());
102 }
103
Brian Carlstromb63ec392011-08-27 17:38:27 -0700104 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700106 // target DexCache and ClassLoader to use for resolution.
Brian Carlstromb9edb842011-08-28 16:31:06 -0700107 Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700108 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
109 if (UNLIKELY(resolved_type == NULL)) {
110 Class* declaring_class = referrer->GetDeclaringClass();
111 DexCache* dex_cache = declaring_class->GetDexCache();
112 const ClassLoader* class_loader = declaring_class->GetClassLoader();
113 const DexFile& dex_file = FindDexFile(dex_cache);
114 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
115 }
116 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117 }
118
119 Class* ResolveType(uint32_t type_idx, const Field* referrer) {
120 Class* declaring_class = referrer->GetDeclaringClass();
121 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700122 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
123 if (UNLIKELY(resolved_type == NULL)) {
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;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700129 }
130
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700131 // Resolve a type with the given ID from the DexFile, storing the
132 // result in DexCache. The ClassLoader is used to search for the
133 // type, since it may be referenced from but not contained within
134 // the given DexFile.
135 Class* ResolveType(const DexFile& dex_file,
136 uint32_t type_idx,
137 DexCache* dex_cache,
138 const ClassLoader* class_loader);
139
140 // Resolve a method with a given ID from the DexFile, storing the
141 // result in DexCache. The ClassLinker and ClassLoader are used as
142 // in ResolveType. What is unique is the method type argument which
143 // is used to determine if this method is a direct, static, or
144 // virtual method.
145 Method* ResolveMethod(const DexFile& dex_file,
146 uint32_t method_idx,
147 DexCache* dex_cache,
148 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700149 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700150
Brian Carlstrom16192862011-09-12 17:50:06 -0700151 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700152 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
153 if (UNLIKELY(resolved_method == NULL)) {
154 Class* declaring_class = referrer->GetDeclaringClass();
155 DexCache* dex_cache = declaring_class->GetDexCache();
156 const ClassLoader* class_loader = declaring_class->GetClassLoader();
157 const DexFile& dex_file = FindDexFile(dex_cache);
158 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
159 }
160 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700161 }
162
Brian Carlstrom845490b2011-09-19 15:56:53 -0700163 Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700164 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
165 if (UNLIKELY(resolved_field == 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_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
171 }
172 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700173 }
174
Brian Carlstrom16192862011-09-12 17:50:06 -0700175 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700176 // result in DexCache. The ClassLinker and ClassLoader are used as
177 // in ResolveType. What is unique is the is_static argument which is
178 // used to determine if we are resolving a static or non-static
179 // field.
180 Field* ResolveField(const DexFile& dex_file,
181 uint32_t field_idx,
182 DexCache* dex_cache,
183 const ClassLoader* class_loader,
184 bool is_static);
185
Ian Rogersad25ac52011-10-04 19:13:33 -0700186 // Get shorty from method index without resolution. Used to do handlerization.
187 const char* MethodShorty(uint32_t method_idx, Method* referrer);
188
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700189 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700190 // can_run_clinit=false allows the compiler to attempt to init a class,
191 // given the restriction that no <clinit> execution is possible.
192 bool EnsureInitialized(Class* c, bool can_run_clinit);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700193
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700194 // Initializes classes that have instances in the image but that have
195 // <clinit> methods so they could not be initialized by the compiler.
196 void RunRootClinits();
197
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700198 void RegisterDexFile(const DexFile& dex_file);
199 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200
Brian Carlstrom8a487412011-08-29 20:08:52 -0700201 const std::vector<const DexFile*>& GetBootClassPath() {
202 return boot_class_path_;
203 }
204
Elliott Hughes410c0c82011-09-01 17:58:25 -0700205 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700206
buzbeec143c552011-08-20 17:38:58 -0700207 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700208 DexCache* FindDexCache(const DexFile& dex_file) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700209 bool IsDexFileRegistered(const DexFile& dex_file) const;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700210
211 // Find, possibily opening, an OatFile corresponding to a DexFile
212 const OatFile* FindOatFile(const DexFile& dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213 const OatFile* FindOatFile(const std::string& location);
buzbeec143c552011-08-20 17:38:58 -0700214
Elliott Hughes418d20f2011-09-22 14:00:39 -0700215 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700216 template <class T>
217 ObjectArray<T>* AllocObjectArray(size_t length) {
218 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
219 }
220
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 ObjectArray<Class>* AllocClassArray(size_t length) {
222 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
223 }
224
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700225 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
226
jeffhao98eacac2011-09-14 16:11:53 -0700227 void VerifyClass(Class* klass);
228
Jesse Wilson95caa792011-10-12 18:14:17 -0400229 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
230 ObjectArray<Method>* methods, ObjectArray<Object>* throws);
231
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700233 ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700234
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700235 // Initialize class linker by bootstraping from dex files
236 void Init(const std::string& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700237
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700238 // Initialize class linker from one or more images.
239 void InitFromImage();
240 OatFile* OpenOat(const Space* space);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700241 static void InitFromImageCallback(Object* obj, void* arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700242 struct InitFromImageCallbackState;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700243
244 void FinishInit();
245
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700246 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700247 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700248
249 // Alloc* convenience functions to avoid needing to pass in Class*
250 // values that are known to the ClassLinker such as
251 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700252 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700253 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400254 Field* AllocField();
Ian Rogersbdb03912011-09-14 00:55:44 -0700255
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700256 Method* AllocMethod();
Ian Rogersbdb03912011-09-14 00:55:44 -0700257
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700258 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700259 InterfaceEntry* AllocInterfaceEntry(Class* interface);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700260
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 Class* CreatePrimitiveClass(const char* descriptor,
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700262 Class::PrimitiveType type) {
263 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
264 }
265 Class* InitializePrimitiveClass(Class* primitive_class,
266 const char* descriptor,
267 Class::PrimitiveType type);
268
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700269
Brian Carlstromaded5f72011-10-07 17:15:04 -0700270 Class* CreateArrayClass(const std::string& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700271 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700272
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700273 void AppendToBootClassPath(const DexFile& dex_file);
274 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700275
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700276 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
277 Class* c, std::map<int, Field*>& field_map);
278
Brian Carlstrom4873d462011-08-21 15:23:39 -0700279 size_t SizeOfClass(const DexFile& dex_file,
280 const DexFile::ClassDef& dex_class_def);
281
Brian Carlstromf615a612011-07-23 12:50:34 -0700282 void LoadClass(const DexFile& dex_file,
283 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700284 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700285 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700286
Brian Carlstromf615a612011-07-23 12:50:34 -0700287 void LoadInterfaces(const DexFile& dex_file,
288 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700289 Class *klass);
290
Brian Carlstromf615a612011-07-23 12:50:34 -0700291 void LoadField(const DexFile& dex_file,
292 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700293 Class* klass,
294 Field* dst);
295
Brian Carlstromf615a612011-07-23 12:50:34 -0700296 void LoadMethod(const DexFile& dex_file,
297 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700298 Class* klass,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700299 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700300
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700301 // Inserts a class into the class table. Returns true if the class
302 // was inserted.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700303 bool InsertClass(const std::string& descriptor, Class* klass);
304
305 void RegisterDexFileLocked(const DexFile& dex_file, DexCache* dex_cache);
306 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
307
Brian Carlstromd1422f82011-09-28 11:37:09 -0700308 bool InitializeClass(Class* klass, bool can_run_clinit);
309 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700310 bool ValidateSuperClassDescriptors(const Class* klass);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700311 bool InitializeSuperClass(Class* klass, bool can_run_clinit);
312 void InitializeStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700313
314 bool HasSameDescriptorClasses(const char* descriptor,
315 const Class* klass1,
316 const Class* klass2);
317
318 bool HasSameMethodDescriptorClasses(const Method* descriptor,
319 const Class* klass1,
320 const Class* klass2);
321
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 bool LinkClass(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700323
324 bool LinkSuperClass(Class* klass);
325
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700326 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700327
328 bool LinkMethods(Class* klass);
329
330 bool LinkVirtualMethods(Class* klass);
331
332 bool LinkInterfaceMethods(Class* klass);
333
Jesse Wilson7833bd22011-08-09 18:31:44 -0400334 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700335 bool LinkInstanceFields(Class* klass);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700336 bool LinkFields(Class *klass, bool is_static);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700338
Brian Carlstrom4873d462011-08-21 15:23:39 -0700339 void CreateReferenceInstanceOffsets(Class* klass);
340 void CreateReferenceStaticOffsets(Class* klass);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700341 void CreateReferenceOffsets(Class *klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700343
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700344 // For use by ImageWriter to find DexCaches for its roots
345 const std::vector<DexCache*>& GetDexCaches() {
346 return dex_caches_;
347 }
348
Jesse Wilson95caa792011-10-12 18:14:17 -0400349 Method* CreateProxyConstructor(Class* klass);
350 Method* CreateProxyMethod(Class* klass, Method* prototype, Object* throws);
351
Brian Carlstrom16192862011-09-12 17:50:06 -0700352 // lock to protect ClassLinker state
353 mutable Mutex lock_;
354
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700355 std::vector<const OatFile*> oat_files_;
356
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700357 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700358
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700359 std::vector<const DexFile*> dex_files_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700360 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700361
Brian Carlstromaded5f72011-10-07 17:15:04 -0700362 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700363 // Class* instances. Results should be compared for a matching
364 // Class::descriptor_ and Class::class_loader_.
365 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700366 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700367
Brian Carlstroma663ea52011-08-19 23:33:41 -0700368 // indexes into class_roots_.
369 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700370 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700371 kJavaLangClass,
372 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700373 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700374 kObjectArrayClass,
375 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700376 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700377 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700378 kJavaLangReflectField,
379 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700380 kJavaLangClassLoader,
381 kDalvikSystemBaseDexClassLoader,
382 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700383 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700384 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700385 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700386 kPrimitiveChar,
387 kPrimitiveDouble,
388 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700389 kPrimitiveInt,
390 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700391 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700392 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700393 kBooleanArrayClass,
394 kByteArrayClass,
395 kCharArrayClass,
396 kDoubleArrayClass,
397 kFloatArrayClass,
398 kIntArrayClass,
399 kLongArrayClass,
400 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700401 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700402 kClassRootsMax,
403 };
404 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700405
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700406 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700407 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700408 Class* klass = class_roots_->Get(class_root);
409 DCHECK(klass != NULL);
410 return klass;
411 }
412
Brian Carlstroma663ea52011-08-19 23:33:41 -0700413 void SetClassRoot(ClassRoot class_root, Class* klass) {
414 DCHECK(!init_done_);
415
416 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417 DCHECK(klass->GetClassLoader() == NULL);
418 DCHECK(klass->GetDescriptor() != NULL);
419 DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700420
421 DCHECK(class_roots_ != NULL);
422 DCHECK(class_roots_->Get(class_root) == NULL);
423 class_roots_->Set(class_root, klass);
424 }
425
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700426 ObjectArray<Class>* GetClassRoots() {
427 DCHECK(class_roots_ != NULL);
428 return class_roots_;
429 }
430
Elliott Hughes418d20f2011-09-22 14:00:39 -0700431 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700432
433 const char* GetClassRootDescriptor(ClassRoot class_root) {
434 const char* descriptor = class_roots_descriptors_[class_root];
435 CHECK(descriptor != NULL);
436 return descriptor;
437 }
438
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700439 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700440 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700441
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700442 bool init_done_;
443
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700444 InternTable* intern_table_;
445
Brian Carlstromf734cf52011-08-17 16:28:14 -0700446 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700447 FRIEND_TEST(DexCacheTest, Open);
448 friend class ObjectTest;
449 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700450 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700451 friend class ImageWriter; // for GetClassRoots
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700452 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
453};
454
455} // namespace art
456
457#endif // ART_SRC_CLASS_LINKER_H_