blob: 1c5de63610249b5561c7580b07ba742a79b29cae [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"
Elliott Hughese5448b52012-01-18 16:44:06 -080027#include "gtest/gtest.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070028#include "heap.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070029#include "macros.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070030#include "mutex.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070031#include "oat_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "object.h"
Brian Carlstrom40381fb2011-10-19 14:13:40 -070033#include "stack_indirect_reference_table.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
35namespace art {
36
Elliott Hughescf4c6c42011-09-01 15:16:42 -070037class ClassLoader;
38class InternTable;
Brian Carlstromd1422f82011-09-28 11:37:09 -070039class ObjectLock;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070040
Elliott Hughesa2155262011-11-16 16:26:58 -080041typedef bool (ClassVisitor)(Class* c, void* arg);
42
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043class ClassLinker {
44 public:
Brian Carlstrom58ae9412011-10-04 00:56:06 -070045 // Creates the class linker by boot strapping from dex files.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080046 static ClassLinker* Create(const std::string& boot_class_path, InternTable* intern_table);
Brian Carlstrom58ae9412011-10-04 00:56:06 -070047
48 // Creates the class linker from one or more images.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080049 static ClassLinker* Create(InternTable* intern_table);
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070051 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070052
Elliott Hughes64bf5a32011-09-20 14:43:12 -070053 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070054 // If class_loader is null, searches boot_class_path_.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080055 Class* FindClass(const char* descriptor, const ClassLoader* class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -070056
Elliott Hughesdb7d5e92011-12-16 18:47:37 -080057 Class* FindSystemClass(const char* descriptor);
Brian Carlstromaded5f72011-10-07 17:15:04 -070058
59 // Define a new a class based on a ClassDef from a DexFile
Elliott Hughesc3b77c72011-12-15 20:56:48 -080060 Class* DefineClass(const StringPiece& descriptor, const ClassLoader* class_loader,
Brian Carlstromaded5f72011-10-07 17:15:04 -070061 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070062
63 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
64 // by the given 'class_loader'.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080065 Class* LookupClass(const char* descriptor, const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070066
Elliott Hughes6fa602d2011-12-02 17:54:25 -080067 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080068 void LookupClasses(const char* descriptor, std::vector<Class*>& classes);
Elliott Hughes6fa602d2011-12-02 17:54:25 -080069
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070070 Class* FindPrimitiveClass(char type);
71
Brian Carlstromae826982011-11-09 01:33:42 -080072 // General class unloading is not supported, this is used to prune
73 // unwanted classes during image writing.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080074 bool RemoveClass(const char* descriptor, const ClassLoader* class_loader);
Brian Carlstromae826982011-11-09 01:33:42 -080075
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070076 void DumpAllClasses(int flags) const;
77
Elliott Hughescac6cc72011-11-03 20:31:21 -070078 void DumpForSigQuit(std::ostream& os) const;
79
Elliott Hughese27955c2011-08-26 15:21:24 -070080 size_t NumLoadedClasses() const;
81
Brian Carlstromb63ec392011-08-27 17:38:27 -070082 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070083 // result in the DexCache. The referrer is used to identify the
84 // target DexCache and ClassLoader to use for resolution.
85 String* ResolveString(uint32_t string_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -070086 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
87 if (UNLIKELY(resolved_string == NULL)) {
88 Class* declaring_class = referrer->GetDeclaringClass();
89 DexCache* dex_cache = declaring_class->GetDexCache();
90 const DexFile& dex_file = FindDexFile(dex_cache);
91 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
92 }
93 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -070094 }
95
96 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070097 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070098 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070099
Brian Carlstromb63ec392011-08-27 17:38:27 -0700100 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700101 // result in the DexCache. The referrer is used to identity the
102 // target DexCache and ClassLoader to use for resolution.
103 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800104 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700105 const Class* referrer) {
106 return ResolveType(dex_file,
107 type_idx,
108 referrer->GetDexCache(),
109 referrer->GetClassLoader());
110 }
111
Brian Carlstromb63ec392011-08-27 17:38:27 -0700112 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700113 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700114 // target DexCache and ClassLoader to use for resolution.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800115 Class* ResolveType(uint16_t type_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700116 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
117 if (UNLIKELY(resolved_type == NULL)) {
118 Class* declaring_class = referrer->GetDeclaringClass();
119 DexCache* dex_cache = declaring_class->GetDexCache();
120 const ClassLoader* class_loader = declaring_class->GetClassLoader();
121 const DexFile& dex_file = FindDexFile(dex_cache);
122 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
123 }
124 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700125 }
126
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800127 Class* ResolveType(uint16_t type_idx, const Field* referrer) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700128 Class* declaring_class = referrer->GetDeclaringClass();
129 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700130 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
131 if (UNLIKELY(resolved_type == NULL)) {
132 const ClassLoader* class_loader = declaring_class->GetClassLoader();
133 const DexFile& dex_file = FindDexFile(dex_cache);
134 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
135 }
136 return resolved_type;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700137 }
138
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700139 // Resolve a type with the given ID from the DexFile, storing the
140 // result in DexCache. The ClassLoader is used to search for the
141 // type, since it may be referenced from but not contained within
142 // the given DexFile.
143 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800144 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700145 DexCache* dex_cache,
146 const ClassLoader* class_loader);
147
148 // Resolve a method with a given ID from the DexFile, storing the
149 // result in DexCache. The ClassLinker and ClassLoader are used as
150 // in ResolveType. What is unique is the method type argument which
151 // is used to determine if this method is a direct, static, or
152 // virtual method.
153 Method* ResolveMethod(const DexFile& dex_file,
154 uint32_t method_idx,
155 DexCache* dex_cache,
156 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700157 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700158
Brian Carlstrom16192862011-09-12 17:50:06 -0700159 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700160 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
161 if (UNLIKELY(resolved_method == NULL)) {
162 Class* declaring_class = referrer->GetDeclaringClass();
163 DexCache* dex_cache = declaring_class->GetDexCache();
164 const ClassLoader* class_loader = declaring_class->GetClassLoader();
165 const DexFile& dex_file = FindDexFile(dex_cache);
166 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
167 }
168 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700169 }
170
Brian Carlstrom845490b2011-09-19 15:56:53 -0700171 Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700172 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
173 if (UNLIKELY(resolved_field == NULL)) {
174 Class* declaring_class = referrer->GetDeclaringClass();
175 DexCache* dex_cache = declaring_class->GetDexCache();
176 const ClassLoader* class_loader = declaring_class->GetClassLoader();
177 const DexFile& dex_file = FindDexFile(dex_cache);
178 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
179 }
180 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700181 }
182
Brian Carlstrom16192862011-09-12 17:50:06 -0700183 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700184 // result in DexCache. The ClassLinker and ClassLoader are used as
185 // in ResolveType. What is unique is the is_static argument which is
186 // used to determine if we are resolving a static or non-static
187 // field.
188 Field* ResolveField(const DexFile& dex_file,
189 uint32_t field_idx,
190 DexCache* dex_cache,
191 const ClassLoader* class_loader,
192 bool is_static);
193
Ian Rogersb067ac22011-12-13 18:05:09 -0800194 Field* ResolveFieldJLS(uint32_t field_idx, const Method* referrer) {
195 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
196 if (UNLIKELY(resolved_field == NULL)) {
197 Class* declaring_class = referrer->GetDeclaringClass();
198 DexCache* dex_cache = declaring_class->GetDexCache();
199 const ClassLoader* class_loader = declaring_class->GetClassLoader();
200 const DexFile& dex_file = FindDexFile(dex_cache);
201 resolved_field = ResolveFieldJLS(dex_file, field_idx, dex_cache, class_loader);
202 }
203 return resolved_field;
204 }
205
206 // Resolve a field with a given ID from the DexFile, storing the
207 // result in DexCache. The ClassLinker and ClassLoader are used as
208 // in ResolveType. No is_static argument is provided so that Java
209 // field resolution semantics are followed.
210 Field* ResolveFieldJLS(const DexFile& dex_file,
211 uint32_t field_idx,
212 DexCache* dex_cache,
213 const ClassLoader* class_loader);
214
Ian Rogersad25ac52011-10-04 19:13:33 -0700215 // Get shorty from method index without resolution. Used to do handlerization.
216 const char* MethodShorty(uint32_t method_idx, Method* referrer);
217
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700218 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700219 // can_run_clinit=false allows the compiler to attempt to init a class,
220 // given the restriction that no <clinit> execution is possible.
221 bool EnsureInitialized(Class* c, bool can_run_clinit);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700222
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700223 // Initializes classes that have instances in the image but that have
224 // <clinit> methods so they could not be initialized by the compiler.
225 void RunRootClinits();
226
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700227 void RegisterDexFile(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700228 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229
Brian Carlstrom866c8622012-01-06 16:35:13 -0800230 void RegisterOatFile(const OatFile& oat_file);
231
Brian Carlstrom8a487412011-08-29 20:08:52 -0700232 const std::vector<const DexFile*>& GetBootClassPath() {
233 return boot_class_path_;
234 }
235
Elliott Hughesa2155262011-11-16 16:26:58 -0800236 void VisitClasses(ClassVisitor* visitor, void* arg) const;
237
Elliott Hughes410c0c82011-09-01 17:58:25 -0700238 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700239
buzbeec143c552011-08-20 17:38:58 -0700240 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700241 DexCache* FindDexCache(const DexFile& dex_file) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700242 bool IsDexFileRegistered(const DexFile& dex_file) const;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700243
jeffhao262bf462011-10-20 18:36:32 -0700244 // Generate an oat file from a dex file
Brian Carlstromd601af82012-01-06 10:15:19 -0800245 bool GenerateOatFile(const std::string& dex_filename,
246 int oat_fd,
247 const std::string& oat_cache_filename);
jeffhao262bf462011-10-20 18:36:32 -0700248
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700249 // Find, possibily opening, an OatFile corresponding to a DexFile
Brian Carlstromae826982011-11-09 01:33:42 -0800250 const OatFile* FindOatFileForDexFile(const DexFile& dex_file);
251 const OatFile* FindOatFileFromOatLocation(const std::string& location);
buzbeec143c552011-08-20 17:38:58 -0700252
Elliott Hughes418d20f2011-09-22 14:00:39 -0700253 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700254 template <class T>
255 ObjectArray<T>* AllocObjectArray(size_t length) {
256 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
257 }
258
Elliott Hughes418d20f2011-09-22 14:00:39 -0700259 ObjectArray<Class>* AllocClassArray(size_t length) {
260 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
261 }
262
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700263 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
264
jeffhao98eacac2011-09-14 16:11:53 -0700265 void VerifyClass(Class* klass);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800266 bool VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass);
267 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass);
268 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* klass);
jeffhao98eacac2011-09-14 16:11:53 -0700269
Jesse Wilson95caa792011-10-12 18:14:17 -0400270 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
272 std::string GetDescriptorForProxy(const Class* proxy_class);
Jesse Wilson95caa792011-10-12 18:14:17 -0400273
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700274 pid_t GetClassesLockOwner(); // For SignalCatcher.
275 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700276
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700277 private:
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800278 explicit ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700279
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700280 // Initialize class linker by bootstraping from dex files
281 void Init(const std::string& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700282
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700283 // Initialize class linker from one or more images.
284 void InitFromImage();
285 OatFile* OpenOat(const Space* space);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700286 static void InitFromImageCallback(Object* obj, void* arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700287 struct InitFromImageCallbackState;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700288
289 void FinishInit();
290
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700291 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700292 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700293
294 // Alloc* convenience functions to avoid needing to pass in Class*
295 // values that are known to the ClassLinker such as
296 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700297 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700298 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400299 Field* AllocField();
Ian Rogersbdb03912011-09-14 00:55:44 -0700300
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700301 Method* AllocMethod();
Ian Rogersbdb03912011-09-14 00:55:44 -0700302
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700303 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700304 InterfaceEntry* AllocInterfaceEntry(Class* interface);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700305
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700306 Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700307 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
308 }
309 Class* InitializePrimitiveClass(Class* primitive_class,
310 const char* descriptor,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700311 Primitive::Type type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700312
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700313
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800314 Class* CreateArrayClass(const std::string& descriptor, const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700315
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700316 void AppendToBootClassPath(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700317 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700318
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700319 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers0571d352011-11-03 19:51:38 -0700320 Class* c, std::map<uint32_t, Field*>& field_map);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700321
Brian Carlstrom4873d462011-08-21 15:23:39 -0700322 size_t SizeOfClass(const DexFile& dex_file,
323 const DexFile::ClassDef& dex_class_def);
324
Brian Carlstromf615a612011-07-23 12:50:34 -0700325 void LoadClass(const DexFile& dex_file,
326 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700327 SirtRef<Class>& klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700328 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700329
Ian Rogers0571d352011-11-03 19:51:38 -0700330 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700331 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700332
Ian Rogers0571d352011-11-03 19:51:38 -0700333 void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
334 SirtRef<Class>& klass, SirtRef<Method>& dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700335
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800336 // Attempts to insert a class into a class table. Returns NULL if
337 // the class was inserted, otherwise returns an existing class with
338 // the same descriptor and ClassLoader.
339 Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700340
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700341 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700342 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
Brian Carlstrom866c8622012-01-06 16:35:13 -0800343 void RegisterOatFileLocked(const OatFile& oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700344
Brian Carlstromd1422f82011-09-28 11:37:09 -0700345 bool InitializeClass(Class* klass, bool can_run_clinit);
346 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700347 bool ValidateSuperClassDescriptors(const Class* klass);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700348 bool InitializeSuperClass(Class* klass, bool can_run_clinit);
349 void InitializeStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700350
Ian Rogers595799e2012-01-11 17:32:51 -0800351 bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
352 const Class* klass1,
353 const Class* klass2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700354
Ian Rogers595799e2012-01-11 17:32:51 -0800355 bool IsSameMethodSignatureInDifferentClassContexts(const Method* descriptor,
356 const Class* klass1,
357 const Class* klass2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700358
Ian Rogersc2b44472011-12-14 21:17:17 -0800359 bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700360
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700361 bool LinkSuperClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700362
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700363 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700364
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800365 bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700366
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700367 bool LinkVirtualMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700368
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800369 bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700370
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700371 bool LinkStaticFields(SirtRef<Class>& klass);
372 bool LinkInstanceFields(SirtRef<Class>& klass);
373 bool LinkFields(SirtRef<Class>& klass, bool is_static);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700375
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700376 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
377 void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
378 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700381 // For use by ImageWriter to find DexCaches for its roots
382 const std::vector<DexCache*>& GetDexCaches() {
383 return dex_caches_;
384 }
385
Brian Carlstromae826982011-11-09 01:33:42 -0800386 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file);
387 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location);
Brian Carlstromfad71432011-10-16 20:25:10 -0700388
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800389 Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class);
390 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype);
Jesse Wilson95caa792011-10-12 18:14:17 -0400391
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700392 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700393
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700394 std::vector<const DexFile*> dex_files_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700395 std::vector<DexCache*> dex_caches_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700396 std::vector<const OatFile*> oat_files_;
397 // lock to protect concurrent access to dex_files_, dex_caches_, and oat_files_
398 mutable Mutex dex_lock_;
399
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700400
Brian Carlstromaded5f72011-10-07 17:15:04 -0700401 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700402 // Class* instances. Results should be compared for a matching
403 // Class::descriptor_ and Class::class_loader_.
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700404 // Protected by classes_lock_
Elliott Hughese5448b52012-01-18 16:44:06 -0800405 typedef std::multimap<size_t, Class*> Table;
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800406 Class* LookupClass(const char* descriptor, const ClassLoader* class_loader,
407 size_t hash, const Table& classes);
Ian Rogers5d76c432011-10-31 21:42:49 -0700408 Table image_classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700409 Table classes_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700410 mutable Mutex classes_lock_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700411
Brian Carlstroma663ea52011-08-19 23:33:41 -0700412 // indexes into class_roots_.
413 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700414 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700415 kJavaLangClass,
416 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700417 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700418 kObjectArrayClass,
419 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700420 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700421 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700422 kJavaLangReflectField,
423 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700424 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700425 kJavaLangClassLoader,
426 kDalvikSystemBaseDexClassLoader,
427 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700428 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700429 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700430 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700431 kPrimitiveChar,
432 kPrimitiveDouble,
433 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700434 kPrimitiveInt,
435 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700436 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700437 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700438 kBooleanArrayClass,
439 kByteArrayClass,
440 kCharArrayClass,
441 kDoubleArrayClass,
442 kFloatArrayClass,
443 kIntArrayClass,
444 kLongArrayClass,
445 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700446 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700447 kClassRootsMax,
448 };
449 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700450
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700451 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700452 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700453 Class* klass = class_roots_->Get(class_root);
454 DCHECK(klass != NULL);
455 return klass;
456 }
457
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800458 void SetClassRoot(ClassRoot class_root, Class* klass);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700459
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700460 ObjectArray<Class>* GetClassRoots() {
461 DCHECK(class_roots_ != NULL);
462 return class_roots_;
463 }
464
Elliott Hughes418d20f2011-09-22 14:00:39 -0700465 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700466
467 const char* GetClassRootDescriptor(ClassRoot class_root) {
468 const char* descriptor = class_roots_descriptors_[class_root];
469 CHECK(descriptor != NULL);
470 return descriptor;
471 }
472
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700473 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700474
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700475 bool init_done_;
476
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700477 InternTable* intern_table_;
478
Brian Carlstromf734cf52011-08-17 16:28:14 -0700479 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700480 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800481 friend class ObjectTest;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800482 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Brian Carlstromae826982011-11-09 01:33:42 -0800483 FRIEND_TEST(DexCacheTest, Open);
484 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
485 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700486 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
487};
488
489} // namespace art
490
491#endif // ART_SRC_CLASS_LINKER_H_