blob: 0b165334cf4c5c8acc3828873b390e73e755aade [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;
Ian Rogers30fab402012-01-23 15:43:46 -080038class ImageSpace;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070039class InternTable;
Brian Carlstromd1422f82011-09-28 11:37:09 -070040class ObjectLock;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070041
Elliott Hughesa2155262011-11-16 16:26:58 -080042typedef bool (ClassVisitor)(Class* c, void* arg);
43
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044class ClassLinker {
45 public:
Brian Carlstrom58ae9412011-10-04 00:56:06 -070046 // Creates the class linker by boot strapping from dex files.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080047 static ClassLinker* Create(const std::string& boot_class_path, InternTable* intern_table);
Brian Carlstrom58ae9412011-10-04 00:56:06 -070048
49 // Creates the class linker from one or more images.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080050 static ClassLinker* Create(InternTable* intern_table);
Carl Shapiro61e019d2011-07-14 16:53:09 -070051
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070053
Elliott Hughes64bf5a32011-09-20 14:43:12 -070054 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070055 // If class_loader is null, searches boot_class_path_.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080056 Class* FindClass(const char* descriptor, const ClassLoader* class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -070057
Elliott Hughesdb7d5e92011-12-16 18:47:37 -080058 Class* FindSystemClass(const char* descriptor);
Brian Carlstromaded5f72011-10-07 17:15:04 -070059
60 // Define a new a class based on a ClassDef from a DexFile
Elliott Hughesc3b77c72011-12-15 20:56:48 -080061 Class* DefineClass(const StringPiece& descriptor, const ClassLoader* class_loader,
Brian Carlstromaded5f72011-10-07 17:15:04 -070062 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'.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080066 Class* LookupClass(const char* descriptor, const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070067
Elliott Hughes6fa602d2011-12-02 17:54:25 -080068 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080069 void LookupClasses(const char* descriptor, std::vector<Class*>& classes);
Elliott Hughes6fa602d2011-12-02 17:54:25 -080070
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070071 Class* FindPrimitiveClass(char type);
72
Brian Carlstromae826982011-11-09 01:33:42 -080073 // General class unloading is not supported, this is used to prune
74 // unwanted classes during image writing.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080075 bool RemoveClass(const char* descriptor, const ClassLoader* class_loader);
Brian Carlstromae826982011-11-09 01:33:42 -080076
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070077 void DumpAllClasses(int flags) const;
78
Elliott Hughescac6cc72011-11-03 20:31:21 -070079 void DumpForSigQuit(std::ostream& os) const;
80
Elliott Hughese27955c2011-08-26 15:21:24 -070081 size_t NumLoadedClasses() const;
82
Brian Carlstromb63ec392011-08-27 17:38:27 -070083 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070084 // result in the DexCache. The referrer is used to identify the
85 // target DexCache and ClassLoader to use for resolution.
86 String* ResolveString(uint32_t string_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -070087 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
88 if (UNLIKELY(resolved_string == NULL)) {
89 Class* declaring_class = referrer->GetDeclaringClass();
90 DexCache* dex_cache = declaring_class->GetDexCache();
91 const DexFile& dex_file = FindDexFile(dex_cache);
92 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
93 }
94 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -070095 }
96
97 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070098 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070099 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700100
Brian Carlstromb63ec392011-08-27 17:38:27 -0700101 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700102 // result in the DexCache. The referrer is used to identity the
103 // target DexCache and ClassLoader to use for resolution.
104 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800105 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700106 const Class* referrer) {
107 return ResolveType(dex_file,
108 type_idx,
109 referrer->GetDexCache(),
110 referrer->GetClassLoader());
111 }
112
Brian Carlstromb63ec392011-08-27 17:38:27 -0700113 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700114 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700115 // target DexCache and ClassLoader to use for resolution.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800116 Class* ResolveType(uint16_t type_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700117 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
118 if (UNLIKELY(resolved_type == NULL)) {
119 Class* declaring_class = referrer->GetDeclaringClass();
120 DexCache* dex_cache = declaring_class->GetDexCache();
121 const ClassLoader* class_loader = declaring_class->GetClassLoader();
122 const DexFile& dex_file = FindDexFile(dex_cache);
123 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
124 }
125 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700126 }
127
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128 Class* ResolveType(uint16_t type_idx, const Field* referrer) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700129 Class* declaring_class = referrer->GetDeclaringClass();
130 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700131 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
132 if (UNLIKELY(resolved_type == NULL)) {
133 const ClassLoader* class_loader = declaring_class->GetClassLoader();
134 const DexFile& dex_file = FindDexFile(dex_cache);
135 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
136 }
137 return resolved_type;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700138 }
139
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700140 // Resolve a type with the given ID from the DexFile, storing the
141 // result in DexCache. The ClassLoader is used to search for the
142 // type, since it may be referenced from but not contained within
143 // the given DexFile.
144 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800145 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700146 DexCache* dex_cache,
147 const ClassLoader* class_loader);
148
149 // Resolve a method with a given ID from the DexFile, storing the
150 // result in DexCache. The ClassLinker and ClassLoader are used as
151 // in ResolveType. What is unique is the method type argument which
152 // is used to determine if this method is a direct, static, or
153 // virtual method.
154 Method* ResolveMethod(const DexFile& dex_file,
155 uint32_t method_idx,
156 DexCache* dex_cache,
157 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700158 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700159
Brian Carlstrom16192862011-09-12 17:50:06 -0700160 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800161 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700162 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800163 Class* declaring_class = referrer->GetDeclaringClass();
164 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700165 const ClassLoader* class_loader = declaring_class->GetClassLoader();
166 const DexFile& dex_file = FindDexFile(dex_cache);
167 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
168 }
169 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700170 }
171
Brian Carlstrom845490b2011-09-19 15:56:53 -0700172 Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800173 Field* resolved_field =
174 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700175 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800176 Class* declaring_class = referrer->GetDeclaringClass();
177 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700178 const ClassLoader* class_loader = declaring_class->GetClassLoader();
179 const DexFile& dex_file = FindDexFile(dex_cache);
180 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
181 }
182 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700183 }
184
Brian Carlstrom16192862011-09-12 17:50:06 -0700185 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700186 // result in DexCache. The ClassLinker and ClassLoader are used as
187 // in ResolveType. What is unique is the is_static argument which is
188 // used to determine if we are resolving a static or non-static
189 // field.
190 Field* ResolveField(const DexFile& dex_file,
191 uint32_t field_idx,
192 DexCache* dex_cache,
193 const ClassLoader* class_loader,
194 bool is_static);
195
Ian Rogersb067ac22011-12-13 18:05:09 -0800196 Field* ResolveFieldJLS(uint32_t field_idx, const Method* referrer) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800197 Field* resolved_field =
198 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogersb067ac22011-12-13 18:05:09 -0800199 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800200 Class* declaring_class = referrer->GetDeclaringClass();
201 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogersb067ac22011-12-13 18:05:09 -0800202 const ClassLoader* class_loader = declaring_class->GetClassLoader();
203 const DexFile& dex_file = FindDexFile(dex_cache);
204 resolved_field = ResolveFieldJLS(dex_file, field_idx, dex_cache, class_loader);
205 }
206 return resolved_field;
207 }
208
209 // Resolve a field with a given ID from the DexFile, storing the
210 // result in DexCache. The ClassLinker and ClassLoader are used as
211 // in ResolveType. No is_static argument is provided so that Java
212 // field resolution semantics are followed.
213 Field* ResolveFieldJLS(const DexFile& dex_file,
214 uint32_t field_idx,
215 DexCache* dex_cache,
216 const ClassLoader* class_loader);
217
Ian Rogersad25ac52011-10-04 19:13:33 -0700218 // Get shorty from method index without resolution. Used to do handlerization.
219 const char* MethodShorty(uint32_t method_idx, Method* referrer);
220
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700221 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700222 // can_run_clinit=false allows the compiler to attempt to init a class,
223 // given the restriction that no <clinit> execution is possible.
224 bool EnsureInitialized(Class* c, bool can_run_clinit);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700225
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700226 // Initializes classes that have instances in the image but that have
227 // <clinit> methods so they could not be initialized by the compiler.
228 void RunRootClinits();
229
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700230 void RegisterDexFile(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700231 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232
Brian Carlstrom866c8622012-01-06 16:35:13 -0800233 void RegisterOatFile(const OatFile& oat_file);
234
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
Brian Carlstromd601af82012-01-06 10:15:19 -0800248 bool GenerateOatFile(const std::string& dex_filename,
249 int oat_fd,
250 const std::string& oat_cache_filename);
jeffhao262bf462011-10-20 18:36:32 -0700251
Brian Carlstromae826982011-11-09 01:33:42 -0800252 const OatFile* FindOatFileFromOatLocation(const std::string& location);
buzbeec143c552011-08-20 17:38:58 -0700253
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800254 // Finds the oat file for a dex location, generating the oat file if
255 // it is missing or out of date. Returns the DexFile from within the
256 // created oat file.
257 const DexFile* FindOrCreateOatFileForDexLocation(const std::string& dex_location,
258 const std::string& oat_location);
259 // Find a DexFile within an OatFile given a DexFile location. Note
260 // that this returns null if the location checksum of the DexFile
261 // does not match the OatFile.
262 const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location);
263
jeffhaof6174e82012-01-31 16:14:17 -0800264
Elliott Hughes418d20f2011-09-22 14:00:39 -0700265 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700266 template <class T>
267 ObjectArray<T>* AllocObjectArray(size_t length) {
268 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
269 }
270
Elliott Hughes418d20f2011-09-22 14:00:39 -0700271 ObjectArray<Class>* AllocClassArray(size_t length) {
272 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
273 }
274
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700275 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
276
jeffhao98eacac2011-09-14 16:11:53 -0700277 void VerifyClass(Class* klass);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800278 bool VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass);
279 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass);
280 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* klass);
jeffhao98eacac2011-09-14 16:11:53 -0700281
Jesse Wilson95caa792011-10-12 18:14:17 -0400282 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
284 std::string GetDescriptorForProxy(const Class* proxy_class);
Jesse Wilson95caa792011-10-12 18:14:17 -0400285
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700286 pid_t GetClassesLockOwner(); // For SignalCatcher.
287 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700288
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700289 private:
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800290 explicit ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700291
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700292 // Initialize class linker by bootstraping from dex files
293 void Init(const std::string& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700294
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700295 // Initialize class linker from one or more images.
296 void InitFromImage();
Ian Rogers30fab402012-01-23 15:43:46 -0800297 OatFile* OpenOat(const ImageSpace* space);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700298 static void InitFromImageCallback(Object* obj, void* arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700299 struct InitFromImageCallbackState;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700300
301 void FinishInit();
302
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700303 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700304 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700305
306 // Alloc* convenience functions to avoid needing to pass in Class*
307 // values that are known to the ClassLinker such as
308 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700309 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700310 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400311 Field* AllocField();
Ian Rogersbdb03912011-09-14 00:55:44 -0700312
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700313 Method* AllocMethod();
Ian Rogersbdb03912011-09-14 00:55:44 -0700314
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700315 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700316 InterfaceEntry* AllocInterfaceEntry(Class* interface);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700317
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700318 Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700319 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
320 }
321 Class* InitializePrimitiveClass(Class* primitive_class,
322 const char* descriptor,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700323 Primitive::Type type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700324
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700325
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800326 Class* CreateArrayClass(const std::string& descriptor, const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700327
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700328 void AppendToBootClassPath(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700329 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700330
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700331 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers0571d352011-11-03 19:51:38 -0700332 Class* c, std::map<uint32_t, Field*>& field_map);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700333
Brian Carlstrom4873d462011-08-21 15:23:39 -0700334 size_t SizeOfClass(const DexFile& dex_file,
335 const DexFile::ClassDef& dex_class_def);
336
Brian Carlstromf615a612011-07-23 12:50:34 -0700337 void LoadClass(const DexFile& dex_file,
338 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700339 SirtRef<Class>& klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700340 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700341
Ian Rogers0571d352011-11-03 19:51:38 -0700342 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700343 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700344
Ian Rogers0571d352011-11-03 19:51:38 -0700345 void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
346 SirtRef<Class>& klass, SirtRef<Method>& dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700347
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800348 // Attempts to insert a class into a class table. Returns NULL if
349 // the class was inserted, otherwise returns an existing class with
350 // the same descriptor and ClassLoader.
351 Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700352
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700353 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700354 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
Brian Carlstrom866c8622012-01-06 16:35:13 -0800355 void RegisterOatFileLocked(const OatFile& oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700356
Brian Carlstromd1422f82011-09-28 11:37:09 -0700357 bool InitializeClass(Class* klass, bool can_run_clinit);
358 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700359 bool ValidateSuperClassDescriptors(const Class* klass);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700360 bool InitializeSuperClass(Class* klass, bool can_run_clinit);
361 void InitializeStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700362
Ian Rogers595799e2012-01-11 17:32:51 -0800363 bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
364 const Class* klass1,
365 const Class* klass2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700366
Ian Rogers595799e2012-01-11 17:32:51 -0800367 bool IsSameMethodSignatureInDifferentClassContexts(const Method* descriptor,
368 const Class* klass1,
369 const Class* klass2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700370
Ian Rogersc2b44472011-12-14 21:17:17 -0800371 bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700372
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700373 bool LinkSuperClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700374
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700375 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700376
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800377 bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700378
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700379 bool LinkVirtualMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800381 bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700382
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700383 bool LinkStaticFields(SirtRef<Class>& klass);
384 bool LinkInstanceFields(SirtRef<Class>& klass);
385 bool LinkFields(SirtRef<Class>& klass, bool is_static);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700387
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700388 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
389 void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
390 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700391 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700392
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700393 // For use by ImageWriter to find DexCaches for its roots
394 const std::vector<DexCache*>& GetDexCaches() {
395 return dex_caches_;
396 }
397
Brian Carlstromae826982011-11-09 01:33:42 -0800398 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800399 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location,
400 uint32_t dex_location_checksum);
Brian Carlstromae826982011-11-09 01:33:42 -0800401 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location);
Brian Carlstromfad71432011-10-16 20:25:10 -0700402
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800403 Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class);
404 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype);
Jesse Wilson95caa792011-10-12 18:14:17 -0400405
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700406 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700407
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700408 std::vector<const DexFile*> dex_files_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700409 std::vector<DexCache*> dex_caches_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700410 std::vector<const OatFile*> oat_files_;
411 // lock to protect concurrent access to dex_files_, dex_caches_, and oat_files_
412 mutable Mutex dex_lock_;
413
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700414
Brian Carlstromaded5f72011-10-07 17:15:04 -0700415 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700416 // Class* instances. Results should be compared for a matching
417 // Class::descriptor_ and Class::class_loader_.
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700418 // Protected by classes_lock_
Elliott Hughese5448b52012-01-18 16:44:06 -0800419 typedef std::multimap<size_t, Class*> Table;
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800420 Class* LookupClass(const char* descriptor, const ClassLoader* class_loader,
421 size_t hash, const Table& classes);
Ian Rogers5d76c432011-10-31 21:42:49 -0700422 Table image_classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700423 Table classes_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700424 mutable Mutex classes_lock_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700425
Brian Carlstroma663ea52011-08-19 23:33:41 -0700426 // indexes into class_roots_.
427 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700428 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700429 kJavaLangClass,
430 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700431 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700432 kObjectArrayClass,
433 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700434 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700435 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700436 kJavaLangReflectField,
437 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700438 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700439 kJavaLangClassLoader,
440 kDalvikSystemBaseDexClassLoader,
441 kDalvikSystemPathClassLoader,
Ian Rogers5167c972012-02-03 10:41:20 -0800442 kJavaLangThrowable,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700443 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700444 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700445 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700446 kPrimitiveChar,
447 kPrimitiveDouble,
448 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700449 kPrimitiveInt,
450 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700451 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700452 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700453 kBooleanArrayClass,
454 kByteArrayClass,
455 kCharArrayClass,
456 kDoubleArrayClass,
457 kFloatArrayClass,
458 kIntArrayClass,
459 kLongArrayClass,
460 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700461 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700462 kClassRootsMax,
463 };
464 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700465
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700466 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700467 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700468 Class* klass = class_roots_->Get(class_root);
469 DCHECK(klass != NULL);
470 return klass;
471 }
472
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800473 void SetClassRoot(ClassRoot class_root, Class* klass);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700474
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700475 ObjectArray<Class>* GetClassRoots() {
476 DCHECK(class_roots_ != NULL);
477 return class_roots_;
478 }
479
Elliott Hughes418d20f2011-09-22 14:00:39 -0700480 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700481
482 const char* GetClassRootDescriptor(ClassRoot class_root) {
483 const char* descriptor = class_roots_descriptors_[class_root];
484 CHECK(descriptor != NULL);
485 return descriptor;
486 }
487
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700488 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700489
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700490 bool init_done_;
491
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700492 InternTable* intern_table_;
493
Brian Carlstromf734cf52011-08-17 16:28:14 -0700494 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700495 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800496 friend class ObjectTest;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800497 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Brian Carlstromae826982011-11-09 01:33:42 -0800498 FRIEND_TEST(DexCacheTest, Open);
499 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
500 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700501 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
502};
503
504} // namespace art
505
506#endif // ART_SRC_CLASS_LINKER_H_