blob: db3093679780abdedd3322747b03495ef81ba329 [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>
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include <string>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022#include <utility>
23#include <vector>
24
Ian Rogerscaab8c42011-10-12 12:11:18 -070025#include "dex_cache.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026#include "dex_file.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027#include "heap.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070028#include "macros.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070029#include "mutex.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070030#include "oat_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "object.h"
Brian Carlstrom40381fb2011-10-19 14:13:40 -070032#include "stack_indirect_reference_table.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070033#include "unordered_map.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070034#include "unordered_set.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070035
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070036#include "gtest/gtest.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
38namespace art {
39
Elliott Hughescf4c6c42011-09-01 15:16:42 -070040class ClassLoader;
41class InternTable;
Brian Carlstromd1422f82011-09-28 11:37:09 -070042class ObjectLock;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070043
Elliott Hughesa2155262011-11-16 16:26:58 -080044typedef bool (ClassVisitor)(Class* c, void* arg);
45
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046class ClassLinker {
47 public:
Brian Carlstrom58ae9412011-10-04 00:56:06 -070048 // Creates the class linker by boot strapping from dex files.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080049 static ClassLinker* Create(const std::string& boot_class_path, InternTable* intern_table);
Brian Carlstrom58ae9412011-10-04 00:56:06 -070050
51 // Creates the class linker from one or more images.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080052 static ClassLinker* Create(InternTable* intern_table);
Carl Shapiro61e019d2011-07-14 16:53:09 -070053
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070054 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070055
Elliott Hughes64bf5a32011-09-20 14:43:12 -070056 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070057 // If class_loader is null, searches boot_class_path_.
Brian Carlstromaded5f72011-10-07 17:15:04 -070058 Class* FindClass(const std::string& descriptor, const ClassLoader* class_loader);
59
60 Class* FindSystemClass(const std::string& descriptor) {
61 return FindClass(descriptor, NULL);
62 }
63
64 // Define a new a class based on a ClassDef from a DexFile
65 Class* DefineClass(const std::string& descriptor, const ClassLoader* class_loader,
66 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070067
68 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
69 // by the given 'class_loader'.
Brian Carlstromaded5f72011-10-07 17:15:04 -070070 Class* LookupClass(const std::string& descriptor, const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070071
Elliott Hughes6fa602d2011-12-02 17:54:25 -080072 // Finds all the classes with the given descriptor, regardless of ClassLoader.
73 void LookupClasses(const std::string& descriptor, std::vector<Class*>& classes);
74
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070075 Class* FindPrimitiveClass(char type);
76
Brian Carlstromae826982011-11-09 01:33:42 -080077 // General class unloading is not supported, this is used to prune
78 // unwanted classes during image writing.
79 bool RemoveClass(const std::string& descriptor, const ClassLoader* class_loader);
80
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070081 void DumpAllClasses(int flags) const;
82
Elliott Hughescac6cc72011-11-03 20:31:21 -070083 void DumpForSigQuit(std::ostream& os) const;
84
Elliott Hughese27955c2011-08-26 15:21:24 -070085 size_t NumLoadedClasses() const;
86
Brian Carlstromb63ec392011-08-27 17:38:27 -070087 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070088 // result in the DexCache. The referrer is used to identify the
89 // target DexCache and ClassLoader to use for resolution.
90 String* ResolveString(uint32_t string_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -070091 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
92 if (UNLIKELY(resolved_string == NULL)) {
93 Class* declaring_class = referrer->GetDeclaringClass();
94 DexCache* dex_cache = declaring_class->GetDexCache();
95 const DexFile& dex_file = FindDexFile(dex_cache);
96 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
97 }
98 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -070099 }
100
101 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700102 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700103 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700104
Brian Carlstromb63ec392011-08-27 17:38:27 -0700105 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700106 // result in the DexCache. The referrer is used to identity the
107 // target DexCache and ClassLoader to use for resolution.
108 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800109 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700110 const Class* referrer) {
111 return ResolveType(dex_file,
112 type_idx,
113 referrer->GetDexCache(),
114 referrer->GetClassLoader());
115 }
116
Brian Carlstromb63ec392011-08-27 17:38:27 -0700117 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700118 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700119 // target DexCache and ClassLoader to use for resolution.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800120 Class* ResolveType(uint16_t type_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700121 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
122 if (UNLIKELY(resolved_type == NULL)) {
123 Class* declaring_class = referrer->GetDeclaringClass();
124 DexCache* dex_cache = declaring_class->GetDexCache();
125 const ClassLoader* class_loader = declaring_class->GetClassLoader();
126 const DexFile& dex_file = FindDexFile(dex_cache);
127 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
128 }
129 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130 }
131
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800132 Class* ResolveType(uint16_t type_idx, const Field* referrer) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700133 Class* declaring_class = referrer->GetDeclaringClass();
134 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700135 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
136 if (UNLIKELY(resolved_type == NULL)) {
137 const ClassLoader* class_loader = declaring_class->GetClassLoader();
138 const DexFile& dex_file = FindDexFile(dex_cache);
139 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
140 }
141 return resolved_type;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700142 }
143
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700144 // Resolve a type with the given ID from the DexFile, storing the
145 // result in DexCache. The ClassLoader is used to search for the
146 // type, since it may be referenced from but not contained within
147 // the given DexFile.
148 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800149 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700150 DexCache* dex_cache,
151 const ClassLoader* class_loader);
152
153 // Resolve a method with a given ID from the DexFile, storing the
154 // result in DexCache. The ClassLinker and ClassLoader are used as
155 // in ResolveType. What is unique is the method type argument which
156 // is used to determine if this method is a direct, static, or
157 // virtual method.
158 Method* ResolveMethod(const DexFile& dex_file,
159 uint32_t method_idx,
160 DexCache* dex_cache,
161 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700162 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700163
Brian Carlstrom16192862011-09-12 17:50:06 -0700164 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700165 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
166 if (UNLIKELY(resolved_method == NULL)) {
167 Class* declaring_class = referrer->GetDeclaringClass();
168 DexCache* dex_cache = declaring_class->GetDexCache();
169 const ClassLoader* class_loader = declaring_class->GetClassLoader();
170 const DexFile& dex_file = FindDexFile(dex_cache);
171 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
172 }
173 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700174 }
175
Brian Carlstrom845490b2011-09-19 15:56:53 -0700176 Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700177 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
178 if (UNLIKELY(resolved_field == NULL)) {
179 Class* declaring_class = referrer->GetDeclaringClass();
180 DexCache* dex_cache = declaring_class->GetDexCache();
181 const ClassLoader* class_loader = declaring_class->GetClassLoader();
182 const DexFile& dex_file = FindDexFile(dex_cache);
183 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
184 }
185 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700186 }
187
Brian Carlstrom16192862011-09-12 17:50:06 -0700188 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700189 // result in DexCache. The ClassLinker and ClassLoader are used as
190 // in ResolveType. What is unique is the is_static argument which is
191 // used to determine if we are resolving a static or non-static
192 // field.
193 Field* ResolveField(const DexFile& dex_file,
194 uint32_t field_idx,
195 DexCache* dex_cache,
196 const ClassLoader* class_loader,
197 bool is_static);
198
Ian Rogersb067ac22011-12-13 18:05:09 -0800199 Field* ResolveFieldJLS(uint32_t field_idx, const Method* referrer) {
200 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
201 if (UNLIKELY(resolved_field == NULL)) {
202 Class* declaring_class = referrer->GetDeclaringClass();
203 DexCache* dex_cache = declaring_class->GetDexCache();
204 const ClassLoader* class_loader = declaring_class->GetClassLoader();
205 const DexFile& dex_file = FindDexFile(dex_cache);
206 resolved_field = ResolveFieldJLS(dex_file, field_idx, dex_cache, class_loader);
207 }
208 return resolved_field;
209 }
210
211 // Resolve a field with a given ID from the DexFile, storing the
212 // result in DexCache. The ClassLinker and ClassLoader are used as
213 // in ResolveType. No is_static argument is provided so that Java
214 // field resolution semantics are followed.
215 Field* ResolveFieldJLS(const DexFile& dex_file,
216 uint32_t field_idx,
217 DexCache* dex_cache,
218 const ClassLoader* class_loader);
219
Ian Rogersad25ac52011-10-04 19:13:33 -0700220 // Get shorty from method index without resolution. Used to do handlerization.
221 const char* MethodShorty(uint32_t method_idx, Method* referrer);
222
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700223 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700224 // can_run_clinit=false allows the compiler to attempt to init a class,
225 // given the restriction that no <clinit> execution is possible.
226 bool EnsureInitialized(Class* c, bool can_run_clinit);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700228 // Initializes classes that have instances in the image but that have
229 // <clinit> methods so they could not be initialized by the compiler.
230 void RunRootClinits();
231
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700232 void RegisterDexFile(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700233 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234
Brian Carlstrom8a487412011-08-29 20:08:52 -0700235 const std::vector<const DexFile*>& GetBootClassPath() {
236 return boot_class_path_;
237 }
238
Elliott Hughesa2155262011-11-16 16:26:58 -0800239 void VisitClasses(ClassVisitor* visitor, void* arg) const;
240
Elliott Hughes410c0c82011-09-01 17:58:25 -0700241 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700242
buzbeec143c552011-08-20 17:38:58 -0700243 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700244 DexCache* FindDexCache(const DexFile& dex_file) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700245 bool IsDexFileRegistered(const DexFile& dex_file) const;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700246
jeffhao262bf462011-10-20 18:36:32 -0700247 // Generate an oat file from a dex file
248 const OatFile* GenerateOatFile(const std::string& filename);
249
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700250 // Find, possibily opening, an OatFile corresponding to a DexFile
Brian Carlstromae826982011-11-09 01:33:42 -0800251 const OatFile* FindOatFileForDexFile(const DexFile& dex_file);
252 const OatFile* FindOatFileFromOatLocation(const std::string& location);
buzbeec143c552011-08-20 17:38:58 -0700253
Elliott Hughes418d20f2011-09-22 14:00:39 -0700254 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700255 template <class T>
256 ObjectArray<T>* AllocObjectArray(size_t length) {
257 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
258 }
259
Elliott Hughes418d20f2011-09-22 14:00:39 -0700260 ObjectArray<Class>* AllocClassArray(size_t length) {
261 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
262 }
263
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700264 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
265
jeffhao98eacac2011-09-14 16:11:53 -0700266 void VerifyClass(Class* klass);
267
Jesse Wilson95caa792011-10-12 18:14:17 -0400268 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800269 ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
270 std::string GetDescriptorForProxy(const Class* proxy_class);
Jesse Wilson95caa792011-10-12 18:14:17 -0400271
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700272 pid_t GetClassesLockOwner(); // For SignalCatcher.
273 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700274
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700275 private:
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800276 explicit ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700277
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700278 // Initialize class linker by bootstraping from dex files
279 void Init(const std::string& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700280
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700281 // Initialize class linker from one or more images.
282 void InitFromImage();
283 OatFile* OpenOat(const Space* space);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700284 static void InitFromImageCallback(Object* obj, void* arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700285 struct InitFromImageCallbackState;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700286
287 void FinishInit();
288
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700289 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700290 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700291
292 // Alloc* convenience functions to avoid needing to pass in Class*
293 // values that are known to the ClassLinker such as
294 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700295 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700296 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400297 Field* AllocField();
Ian Rogersbdb03912011-09-14 00:55:44 -0700298
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700299 Method* AllocMethod();
Ian Rogersbdb03912011-09-14 00:55:44 -0700300
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700301 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700302 InterfaceEntry* AllocInterfaceEntry(Class* interface);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700303
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700304 Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700305 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
306 }
307 Class* InitializePrimitiveClass(Class* primitive_class,
308 const char* descriptor,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700309 Primitive::Type type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700310
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700311
Brian Carlstromaded5f72011-10-07 17:15:04 -0700312 Class* CreateArrayClass(const std::string& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700313 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700314
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700315 void AppendToBootClassPath(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700316 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700317
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700318 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers0571d352011-11-03 19:51:38 -0700319 Class* c, std::map<uint32_t, Field*>& field_map);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700320
Brian Carlstrom4873d462011-08-21 15:23:39 -0700321 size_t SizeOfClass(const DexFile& dex_file,
322 const DexFile::ClassDef& dex_class_def);
323
Brian Carlstromf615a612011-07-23 12:50:34 -0700324 void LoadClass(const DexFile& dex_file,
325 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700326 SirtRef<Class>& klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700327 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700328
Ian Rogers0571d352011-11-03 19:51:38 -0700329 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700330 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700331
Ian Rogers0571d352011-11-03 19:51:38 -0700332 void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
333 SirtRef<Class>& klass, SirtRef<Method>& dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700334
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700335 // Inserts a class into the class table. Returns true if the class
336 // was inserted.
Ian Rogers5d76c432011-10-31 21:42:49 -0700337 bool InsertClass(const std::string& descriptor, Class* klass, bool image_class);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700338
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700339 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700340 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
341
Brian Carlstromd1422f82011-09-28 11:37:09 -0700342 bool InitializeClass(Class* klass, bool can_run_clinit);
343 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700344 bool ValidateSuperClassDescriptors(const Class* klass);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700345 bool InitializeSuperClass(Class* klass, bool can_run_clinit);
346 void InitializeStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700347
348 bool HasSameDescriptorClasses(const char* descriptor,
349 const Class* klass1,
350 const Class* klass2);
351
352 bool HasSameMethodDescriptorClasses(const Method* descriptor,
353 const Class* klass1,
354 const Class* klass2);
355
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700356 bool LinkClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700357
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700358 bool LinkSuperClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700359
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700360 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700361
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800362 bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700363
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700364 bool LinkVirtualMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700365
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800366 bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700367
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700368 bool LinkStaticFields(SirtRef<Class>& klass);
369 bool LinkInstanceFields(SirtRef<Class>& klass);
370 bool LinkFields(SirtRef<Class>& klass, bool is_static);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700372
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700373 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
374 void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
375 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700377
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700378 // For use by ImageWriter to find DexCaches for its roots
379 const std::vector<DexCache*>& GetDexCaches() {
380 return dex_caches_;
381 }
382
Brian Carlstromae826982011-11-09 01:33:42 -0800383 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file);
384 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location);
Brian Carlstromfad71432011-10-16 20:25:10 -0700385
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800386 Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class);
387 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype);
Jesse Wilson95caa792011-10-12 18:14:17 -0400388
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700389 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700390
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700391 std::vector<const DexFile*> dex_files_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700392 std::vector<DexCache*> dex_caches_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700393 std::vector<const OatFile*> oat_files_;
394 // lock to protect concurrent access to dex_files_, dex_caches_, and oat_files_
395 mutable Mutex dex_lock_;
396
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700397
Brian Carlstromaded5f72011-10-07 17:15:04 -0700398 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700399 // Class* instances. Results should be compared for a matching
400 // Class::descriptor_ and Class::class_loader_.
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700401 // Protected by classes_lock_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700402 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Ian Rogers5d76c432011-10-31 21:42:49 -0700403 Table image_classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404 Table classes_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700405 mutable Mutex classes_lock_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700406
Brian Carlstroma663ea52011-08-19 23:33:41 -0700407 // indexes into class_roots_.
408 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700409 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700410 kJavaLangClass,
411 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700412 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700413 kObjectArrayClass,
414 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700415 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700416 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700417 kJavaLangReflectField,
418 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700419 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700420 kJavaLangClassLoader,
421 kDalvikSystemBaseDexClassLoader,
422 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700423 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700424 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700425 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700426 kPrimitiveChar,
427 kPrimitiveDouble,
428 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700429 kPrimitiveInt,
430 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700431 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700432 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700433 kBooleanArrayClass,
434 kByteArrayClass,
435 kCharArrayClass,
436 kDoubleArrayClass,
437 kFloatArrayClass,
438 kIntArrayClass,
439 kLongArrayClass,
440 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700441 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700442 kClassRootsMax,
443 };
444 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700445
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700446 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700447 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700448 Class* klass = class_roots_->Get(class_root);
449 DCHECK(klass != NULL);
450 return klass;
451 }
452
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800453 void SetClassRoot(ClassRoot class_root, Class* klass);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700454
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700455 ObjectArray<Class>* GetClassRoots() {
456 DCHECK(class_roots_ != NULL);
457 return class_roots_;
458 }
459
Elliott Hughes418d20f2011-09-22 14:00:39 -0700460 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700461
462 const char* GetClassRootDescriptor(ClassRoot class_root) {
463 const char* descriptor = class_roots_descriptors_[class_root];
464 CHECK(descriptor != NULL);
465 return descriptor;
466 }
467
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700468 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700469
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700470 bool init_done_;
471
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700472 InternTable* intern_table_;
473
Brian Carlstromf734cf52011-08-17 16:28:14 -0700474 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700475 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800476 friend class ObjectTest;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800477 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Brian Carlstromae826982011-11-09 01:33:42 -0800478 FRIEND_TEST(DexCacheTest, Open);
479 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
480 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700481 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
482};
483
484} // namespace art
485
486#endif // ART_SRC_CLASS_LINKER_H_