blob: 9ba79eb7ca196c44382dbd042abcbe73b1aa5748 [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.
Brian Carlstroma004aa92012-02-08 18:05:09 -080047 static ClassLinker* CreateFromCompiler(const std::vector<const DexFile*>& boot_class_path,
48 InternTable* intern_table);
Brian Carlstrom58ae9412011-10-04 00:56:06 -070049
Brian Carlstroma004aa92012-02-08 18:05:09 -080050 // Creates the class linker from an image.
51 static ClassLinker* CreateFromImage(InternTable* intern_table);
Carl Shapiro61e019d2011-07-14 16:53:09 -070052
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070053 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070054
Elliott Hughes64bf5a32011-09-20 14:43:12 -070055 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070056 // If class_loader is null, searches boot_class_path_.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080057 Class* FindClass(const char* descriptor, const ClassLoader* class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -070058
Elliott Hughesdb7d5e92011-12-16 18:47:37 -080059 Class* FindSystemClass(const char* descriptor);
Brian Carlstromaded5f72011-10-07 17:15:04 -070060
61 // Define a new a class based on a ClassDef from a DexFile
Elliott Hughesc3b77c72011-12-15 20:56:48 -080062 Class* DefineClass(const StringPiece& descriptor, const ClassLoader* class_loader,
Brian Carlstromaded5f72011-10-07 17:15:04 -070063 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070064
65 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
66 // by the given 'class_loader'.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080067 Class* LookupClass(const char* descriptor, const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068
Elliott Hughes6fa602d2011-12-02 17:54:25 -080069 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080070 void LookupClasses(const char* descriptor, std::vector<Class*>& classes);
Elliott Hughes6fa602d2011-12-02 17:54:25 -080071
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070072 Class* FindPrimitiveClass(char type);
73
Brian Carlstromae826982011-11-09 01:33:42 -080074 // General class unloading is not supported, this is used to prune
75 // unwanted classes during image writing.
Elliott Hughesc3b77c72011-12-15 20:56:48 -080076 bool RemoveClass(const char* descriptor, const ClassLoader* class_loader);
Brian Carlstromae826982011-11-09 01:33:42 -080077
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070078 void DumpAllClasses(int flags) const;
79
Elliott Hughescac6cc72011-11-03 20:31:21 -070080 void DumpForSigQuit(std::ostream& os) const;
81
Elliott Hughese27955c2011-08-26 15:21:24 -070082 size_t NumLoadedClasses() const;
83
Brian Carlstromb63ec392011-08-27 17:38:27 -070084 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070085 // result in the DexCache. The referrer is used to identify the
86 // target DexCache and ClassLoader to use for resolution.
87 String* ResolveString(uint32_t string_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -070088 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
89 if (UNLIKELY(resolved_string == NULL)) {
90 Class* declaring_class = referrer->GetDeclaringClass();
91 DexCache* dex_cache = declaring_class->GetDexCache();
92 const DexFile& dex_file = FindDexFile(dex_cache);
93 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
94 }
95 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -070096 }
97
98 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070099 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700100 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700101
Brian Carlstromb63ec392011-08-27 17:38:27 -0700102 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103 // result in the DexCache. The referrer is used to identity the
104 // target DexCache and ClassLoader to use for resolution.
105 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800106 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700107 const Class* referrer) {
108 return ResolveType(dex_file,
109 type_idx,
110 referrer->GetDexCache(),
111 referrer->GetClassLoader());
112 }
113
Brian Carlstromb63ec392011-08-27 17:38:27 -0700114 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700115 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700116 // target DexCache and ClassLoader to use for resolution.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800117 Class* ResolveType(uint16_t type_idx, const Method* referrer) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700118 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
119 if (UNLIKELY(resolved_type == NULL)) {
120 Class* declaring_class = referrer->GetDeclaringClass();
121 DexCache* dex_cache = declaring_class->GetDexCache();
122 const ClassLoader* class_loader = declaring_class->GetClassLoader();
123 const DexFile& dex_file = FindDexFile(dex_cache);
124 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
125 }
126 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700127 }
128
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800129 Class* ResolveType(uint16_t type_idx, const Field* referrer) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130 Class* declaring_class = referrer->GetDeclaringClass();
131 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700132 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
133 if (UNLIKELY(resolved_type == NULL)) {
134 const ClassLoader* class_loader = declaring_class->GetClassLoader();
135 const DexFile& dex_file = FindDexFile(dex_cache);
136 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
137 }
138 return resolved_type;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700139 }
140
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700141 // Resolve a type with the given ID from the DexFile, storing the
142 // result in DexCache. The ClassLoader is used to search for the
143 // type, since it may be referenced from but not contained within
144 // the given DexFile.
145 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800146 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700147 DexCache* dex_cache,
148 const ClassLoader* class_loader);
149
150 // Resolve a method with a given ID from the DexFile, storing the
151 // result in DexCache. The ClassLinker and ClassLoader are used as
152 // in ResolveType. What is unique is the method type argument which
153 // is used to determine if this method is a direct, static, or
154 // virtual method.
155 Method* ResolveMethod(const DexFile& dex_file,
156 uint32_t method_idx,
157 DexCache* dex_cache,
158 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700159 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700160
Brian Carlstrom16192862011-09-12 17:50:06 -0700161 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
Ian Rogers19846512012-02-24 11:42:47 -0800162 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
163 if (UNLIKELY(resolved_method == NULL || resolved_method->IsRuntimeMethod())) {
164 Class* declaring_class = referrer->GetDeclaringClass();
165 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700166 const ClassLoader* class_loader = declaring_class->GetClassLoader();
167 const DexFile& dex_file = FindDexFile(dex_cache);
168 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
169 }
170 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700171 }
172
Brian Carlstrom845490b2011-09-19 15:56:53 -0700173 Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800174 Field* resolved_field =
175 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700176 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800177 Class* declaring_class = referrer->GetDeclaringClass();
178 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700179 const ClassLoader* class_loader = declaring_class->GetClassLoader();
180 const DexFile& dex_file = FindDexFile(dex_cache);
181 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
182 }
183 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700184 }
185
Brian Carlstrom16192862011-09-12 17:50:06 -0700186 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700187 // result in DexCache. The ClassLinker and ClassLoader are used as
188 // in ResolveType. What is unique is the is_static argument which is
189 // used to determine if we are resolving a static or non-static
190 // field.
191 Field* ResolveField(const DexFile& dex_file,
192 uint32_t field_idx,
193 DexCache* dex_cache,
194 const ClassLoader* class_loader,
195 bool is_static);
196
Ian Rogersb067ac22011-12-13 18:05:09 -0800197 Field* ResolveFieldJLS(uint32_t field_idx, const Method* referrer) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800198 Field* resolved_field =
199 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogersb067ac22011-12-13 18:05:09 -0800200 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800201 Class* declaring_class = referrer->GetDeclaringClass();
202 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogersb067ac22011-12-13 18:05:09 -0800203 const ClassLoader* class_loader = declaring_class->GetClassLoader();
204 const DexFile& dex_file = FindDexFile(dex_cache);
205 resolved_field = ResolveFieldJLS(dex_file, field_idx, dex_cache, class_loader);
206 }
207 return resolved_field;
208 }
209
210 // Resolve a field with a given ID from the DexFile, storing the
211 // result in DexCache. The ClassLinker and ClassLoader are used as
212 // in ResolveType. No is_static argument is provided so that Java
213 // field resolution semantics are followed.
214 Field* ResolveFieldJLS(const DexFile& dex_file,
215 uint32_t field_idx,
216 DexCache* dex_cache,
217 const ClassLoader* class_loader);
218
Ian Rogersad25ac52011-10-04 19:13:33 -0700219 // Get shorty from method index without resolution. Used to do handlerization.
Ian Rogers19846512012-02-24 11:42:47 -0800220 const char* MethodShorty(uint32_t method_idx, Method* referrer, uint32_t* length);
Ian Rogersad25ac52011-10-04 19:13:33 -0700221
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700222 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700223 // can_run_clinit=false allows the compiler to attempt to init a class,
224 // given the restriction that no <clinit> execution is possible.
225 bool EnsureInitialized(Class* c, bool can_run_clinit);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700226
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700227 // Initializes classes that have instances in the image but that have
228 // <clinit> methods so they could not be initialized by the compiler.
229 void RunRootClinits();
230
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700231 void RegisterDexFile(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700232 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233
Brian Carlstrom866c8622012-01-06 16:35:13 -0800234 void RegisterOatFile(const OatFile& oat_file);
235
Brian Carlstrom8a487412011-08-29 20:08:52 -0700236 const std::vector<const DexFile*>& GetBootClassPath() {
237 return boot_class_path_;
238 }
239
Elliott Hughesa2155262011-11-16 16:26:58 -0800240 void VisitClasses(ClassVisitor* visitor, void* arg) const;
241
Elliott Hughes410c0c82011-09-01 17:58:25 -0700242 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700243
buzbeec143c552011-08-20 17:38:58 -0700244 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700245 DexCache* FindDexCache(const DexFile& dex_file) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700246 bool IsDexFileRegistered(const DexFile& dex_file) const;
Ian Rogers19846512012-02-24 11:42:47 -0800247 void FixupDexCaches(Method* resolution_method) const;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700248
jeffhao262bf462011-10-20 18:36:32 -0700249 // Generate an oat file from a dex file
Brian Carlstromd601af82012-01-06 10:15:19 -0800250 bool GenerateOatFile(const std::string& dex_filename,
251 int oat_fd,
252 const std::string& oat_cache_filename);
jeffhao262bf462011-10-20 18:36:32 -0700253
Brian Carlstromae826982011-11-09 01:33:42 -0800254 const OatFile* FindOatFileFromOatLocation(const std::string& location);
buzbeec143c552011-08-20 17:38:58 -0700255
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800256 // Finds the oat file for a dex location, generating the oat file if
257 // it is missing or out of date. Returns the DexFile from within the
258 // created oat file.
259 const DexFile* FindOrCreateOatFileForDexLocation(const std::string& dex_location,
260 const std::string& oat_location);
261 // Find a DexFile within an OatFile given a DexFile location. Note
262 // that this returns null if the location checksum of the DexFile
263 // does not match the OatFile.
264 const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location);
265
jeffhaof6174e82012-01-31 16:14:17 -0800266
Elliott Hughes418d20f2011-09-22 14:00:39 -0700267 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700268 template <class T>
269 ObjectArray<T>* AllocObjectArray(size_t length) {
270 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
271 }
272
Elliott Hughes418d20f2011-09-22 14:00:39 -0700273 ObjectArray<Class>* AllocClassArray(size_t length) {
274 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
275 }
276
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700277 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
278
jeffhao98eacac2011-09-14 16:11:53 -0700279 void VerifyClass(Class* klass);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800280 bool VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass);
281 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass);
282 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* klass);
jeffhao98eacac2011-09-14 16:11:53 -0700283
Jesse Wilson95caa792011-10-12 18:14:17 -0400284 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800285 ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
286 std::string GetDescriptorForProxy(const Class* proxy_class);
Ian Rogers16f93672012-02-14 12:29:06 -0800287 Method* FindMethodForProxy(const Class* proxy_class, const Method* proxy_method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400288
Ian Rogers19846512012-02-24 11:42:47 -0800289 // Get the oat code for a method when its class isn't yet initialized
290 const void* GetOatCodeFor(const Method* method);
291
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700292 pid_t GetClassesLockOwner(); // For SignalCatcher.
293 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700294
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700295 private:
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800296 explicit ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700297
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700298 // Initialize class linker by bootstraping from dex files
Brian Carlstroma004aa92012-02-08 18:05:09 -0800299 void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700300
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700301 // Initialize class linker from one or more images.
302 void InitFromImage();
Ian Rogers30fab402012-01-23 15:43:46 -0800303 OatFile* OpenOat(const ImageSpace* space);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700304 static void InitFromImageCallback(Object* obj, void* arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700305 struct InitFromImageCallbackState;
Brian Carlstroma663ea52011-08-19 23:33:41 -0700306
307 void FinishInit();
308
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700309 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700310 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700311
312 // Alloc* convenience functions to avoid needing to pass in Class*
313 // values that are known to the ClassLinker such as
314 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700315 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700316 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400317 Field* AllocField();
Ian Rogersbdb03912011-09-14 00:55:44 -0700318
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700319 Method* AllocMethod();
Ian Rogersbdb03912011-09-14 00:55:44 -0700320
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700321 InterfaceEntry* AllocInterfaceEntry(Class* interface);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700322
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700323 Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700324 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
325 }
326 Class* InitializePrimitiveClass(Class* primitive_class,
327 const char* descriptor,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700328 Primitive::Type type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700329
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700330
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800331 Class* CreateArrayClass(const std::string& descriptor, const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700332
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700333 void AppendToBootClassPath(const DexFile& dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700334 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700335
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700336 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers0571d352011-11-03 19:51:38 -0700337 Class* c, std::map<uint32_t, Field*>& field_map);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700338
Brian Carlstrom4873d462011-08-21 15:23:39 -0700339 size_t SizeOfClass(const DexFile& dex_file,
340 const DexFile::ClassDef& dex_class_def);
341
Brian Carlstromf615a612011-07-23 12:50:34 -0700342 void LoadClass(const DexFile& dex_file,
343 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700344 SirtRef<Class>& klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700345 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700346
Ian Rogers0571d352011-11-03 19:51:38 -0700347 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700348 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700349
Ian Rogers0571d352011-11-03 19:51:38 -0700350 void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
351 SirtRef<Class>& klass, SirtRef<Method>& dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700352
Ian Rogers19846512012-02-24 11:42:47 -0800353 void FixupStaticTrampolines(Class* klass);
354
355 // Finds the associated oat class for a dex_file and descriptor
356 const OatFile::OatClass* GetOatClass(const DexFile& dex_file, const char* descriptor);
357
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800358 // Attempts to insert a class into a class table. Returns NULL if
359 // the class was inserted, otherwise returns an existing class with
360 // the same descriptor and ClassLoader.
361 Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700362
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700363 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700364 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
Brian Carlstrom866c8622012-01-06 16:35:13 -0800365 void RegisterOatFileLocked(const OatFile& oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700366
Brian Carlstromd1422f82011-09-28 11:37:09 -0700367 bool InitializeClass(Class* klass, bool can_run_clinit);
368 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700369 bool ValidateSuperClassDescriptors(const Class* klass);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700370 bool InitializeSuperClass(Class* klass, bool can_run_clinit);
371 void InitializeStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700372
Ian Rogers595799e2012-01-11 17:32:51 -0800373 bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
374 const Class* klass1,
375 const Class* klass2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700376
Ian Rogers595799e2012-01-11 17:32:51 -0800377 bool IsSameMethodSignatureInDifferentClassContexts(const Method* descriptor,
378 const Class* klass1,
379 const Class* klass2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380
Ian Rogersc2b44472011-12-14 21:17:17 -0800381 bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700382
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700383 bool LinkSuperClass(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700384
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700385 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700386
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800387 bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700388
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700389 bool LinkVirtualMethods(SirtRef<Class>& klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700390
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800391 bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700392
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700393 bool LinkStaticFields(SirtRef<Class>& klass);
394 bool LinkInstanceFields(SirtRef<Class>& klass);
395 bool LinkFields(SirtRef<Class>& klass, bool is_static);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700397
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700398 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
399 void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
400 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700401 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700402
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700403 // For use by ImageWriter to find DexCaches for its roots
404 const std::vector<DexCache*>& GetDexCaches() {
405 return dex_caches_;
406 }
407
Brian Carlstromae826982011-11-09 01:33:42 -0800408 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800409 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location);
Brian Carlstromae826982011-11-09 01:33:42 -0800410 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location);
Brian Carlstromfad71432011-10-16 20:25:10 -0700411
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800412 Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class);
413 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype);
Jesse Wilson95caa792011-10-12 18:14:17 -0400414
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700415 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700416
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700417 std::vector<const DexFile*> dex_files_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700418 std::vector<DexCache*> dex_caches_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700419 std::vector<const OatFile*> oat_files_;
420 // lock to protect concurrent access to dex_files_, dex_caches_, and oat_files_
421 mutable Mutex dex_lock_;
422
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700423
Brian Carlstromaded5f72011-10-07 17:15:04 -0700424 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700425 // Class* instances. Results should be compared for a matching
426 // Class::descriptor_ and Class::class_loader_.
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700427 // Protected by classes_lock_
Elliott Hughese5448b52012-01-18 16:44:06 -0800428 typedef std::multimap<size_t, Class*> Table;
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800429 Class* LookupClass(const char* descriptor, const ClassLoader* class_loader,
430 size_t hash, const Table& classes);
Ian Rogers5d76c432011-10-31 21:42:49 -0700431 Table image_classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700432 Table classes_;
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700433 mutable Mutex classes_lock_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700434
Brian Carlstroma663ea52011-08-19 23:33:41 -0700435 // indexes into class_roots_.
436 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700437 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700438 kJavaLangClass,
439 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700440 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700441 kObjectArrayClass,
442 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700443 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700444 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700445 kJavaLangReflectField,
446 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700447 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700448 kJavaLangClassLoader,
449 kDalvikSystemBaseDexClassLoader,
450 kDalvikSystemPathClassLoader,
Ian Rogers5167c972012-02-03 10:41:20 -0800451 kJavaLangThrowable,
jeffhao8cd6dda2012-02-22 10:15:34 -0800452 kJavaLangClassNotFoundException,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700453 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700454 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700455 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700456 kPrimitiveChar,
457 kPrimitiveDouble,
458 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700459 kPrimitiveInt,
460 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700461 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700462 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700463 kBooleanArrayClass,
464 kByteArrayClass,
465 kCharArrayClass,
466 kDoubleArrayClass,
467 kFloatArrayClass,
468 kIntArrayClass,
469 kLongArrayClass,
470 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700471 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700472 kClassRootsMax,
473 };
474 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700475
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700476 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700477 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700478 Class* klass = class_roots_->Get(class_root);
479 DCHECK(klass != NULL);
480 return klass;
481 }
482
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800483 void SetClassRoot(ClassRoot class_root, Class* klass);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700484
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700485 ObjectArray<Class>* GetClassRoots() {
486 DCHECK(class_roots_ != NULL);
487 return class_roots_;
488 }
489
Elliott Hughes418d20f2011-09-22 14:00:39 -0700490 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700491
492 const char* GetClassRootDescriptor(ClassRoot class_root) {
493 const char* descriptor = class_roots_descriptors_[class_root];
494 CHECK(descriptor != NULL);
495 return descriptor;
496 }
497
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700498 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700499
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700500 bool init_done_;
501
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700502 InternTable* intern_table_;
503
Brian Carlstromf734cf52011-08-17 16:28:14 -0700504 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700505 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800506 friend class ObjectTest;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800507 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Brian Carlstromae826982011-11-09 01:33:42 -0800508 FRIEND_TEST(DexCacheTest, Open);
509 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
510 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700511 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
512};
513
514} // namespace art
515
516#endif // ART_SRC_CLASS_LINKER_H_