blob: f1530f3ec868bf8592c9e452a695cecce2ec1c57 [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
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include <string>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070021#include <utility>
22#include <vector>
23
Ian Rogerscaab8c42011-10-12 12:11:18 -070024#include "dex_cache.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025#include "dex_file.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080026#include "gtest/gtest.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027#include "heap.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070028#include "macros.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070029#include "mutex.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070030#include "oat_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "object.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070032#include "safe_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070033
34namespace art {
35
Elliott Hughescf4c6c42011-09-01 15:16:42 -070036class ClassLoader;
Ian Rogers30fab402012-01-23 15:43:46 -080037class ImageSpace;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070038class InternTable;
Brian Carlstromd1422f82011-09-28 11:37:09 -070039class ObjectLock;
Ian Rogers1f539342012-10-03 21:09:42 -070040template<class T> class SirtRef;
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:
Ian Rogers1f539342012-10-03 21:09:42 -070046 // Creates the class linker by bootstrapping from dex files.
Brian Carlstroma004aa92012-02-08 18:05:09 -080047 static ClassLinker* CreateFromCompiler(const std::vector<const DexFile*>& boot_class_path,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 InternTable* intern_table)
Ian Rogersb726dcb2012-09-05 08:57:23 -070049 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom58ae9412011-10-04 00:56:06 -070050
Brian Carlstroma004aa92012-02-08 18:05:09 -080051 // Creates the class linker from an image.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052 static ClassLinker* CreateFromImage(InternTable* intern_table)
Ian Rogersb726dcb2012-09-05 08:57:23 -070053 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070054
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070055 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070056
Elliott Hughes64bf5a32011-09-20 14:43:12 -070057 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070058 // If class_loader is null, searches boot_class_path_.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 Class* FindClass(const char* descriptor, ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -070060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -070061
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 Class* FindSystemClass(const char* descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070063 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -070064
65 // Define a new a class based on a ClassDef from a DexFile
Ian Rogers365c1022012-06-22 15:05:28 -070066 Class* DefineClass(const StringPiece& descriptor, ClassLoader* class_loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -070068 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070069
70 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
71 // by the given 'class_loader'.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 Class* LookupClass(const char* descriptor, const ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070075
Elliott Hughes6fa602d2011-12-02 17:54:25 -080076 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070077 void LookupClasses(const char* descriptor, std::vector<Class*>& classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -070078 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
79 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes6fa602d2011-12-02 17:54:25 -080080
Ian Rogersb726dcb2012-09-05 08:57:23 -070081 Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070082
Brian Carlstromae826982011-11-09 01:33:42 -080083 // General class unloading is not supported, this is used to prune
84 // unwanted classes during image writing.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 bool RemoveClass(const char* descriptor, const ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -070086 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -080088
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 void DumpAllClasses(int flags) const
Ian Rogersb726dcb2012-09-05 08:57:23 -070090 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
91 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070092
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 void DumpForSigQuit(std::ostream& os) const
Ian Rogersb726dcb2012-09-05 08:57:23 -070094 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -070095
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 size_t NumLoadedClasses() const LOCKS_EXCLUDED(Locks::classlinker_classes_lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -070097
Brian Carlstromb63ec392011-08-27 17:38:27 -070098 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -070099 // result in the DexCache. The referrer is used to identify the
100 // target DexCache and ClassLoader to use for resolution.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700101 String* ResolveString(uint32_t string_idx, const AbstractMethod* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700103 String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
104 if (UNLIKELY(resolved_string == NULL)) {
105 Class* declaring_class = referrer->GetDeclaringClass();
106 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700107 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700108 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
109 }
110 return resolved_string;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700111 }
112
113 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700114 // result in the DexCache.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700117
Brian Carlstromb63ec392011-08-27 17:38:27 -0700118 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700119 // result in the DexCache. The referrer is used to identity the
120 // target DexCache and ClassLoader to use for resolution.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, const Class* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700123 return ResolveType(dex_file,
124 type_idx,
125 referrer->GetDexCache(),
126 referrer->GetClassLoader());
127 }
128
Brian Carlstromb63ec392011-08-27 17:38:27 -0700129 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700131 // target DexCache and ClassLoader to use for resolution.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700132 Class* ResolveType(uint16_t type_idx, const AbstractMethod* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700134 Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
135 if (UNLIKELY(resolved_type == NULL)) {
136 Class* declaring_class = referrer->GetDeclaringClass();
137 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogers365c1022012-06-22 15:05:28 -0700138 ClassLoader* class_loader = declaring_class->GetClassLoader();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700139 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700140 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
141 }
142 return resolved_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700143 }
144
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 Class* ResolveType(uint16_t type_idx, const Field* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700147 Class* declaring_class = referrer->GetDeclaringClass();
148 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700149 Class* resolved_type = dex_cache->GetResolvedType(type_idx);
150 if (UNLIKELY(resolved_type == NULL)) {
Ian Rogers365c1022012-06-22 15:05:28 -0700151 ClassLoader* class_loader = declaring_class->GetClassLoader();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700152 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700153 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
154 }
155 return resolved_type;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700156 }
157
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700158 // Resolve a type with the given ID from the DexFile, storing the
159 // result in DexCache. The ClassLoader is used to search for the
160 // type, since it may be referenced from but not contained within
161 // the given DexFile.
162 Class* ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800163 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700164 DexCache* dex_cache,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165 ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700167
168 // Resolve a method with a given ID from the DexFile, storing the
169 // result in DexCache. The ClassLinker and ClassLoader are used as
170 // in ResolveType. What is unique is the method type argument which
171 // is used to determine if this method is a direct, static, or
172 // virtual method.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700173 AbstractMethod* ResolveMethod(const DexFile& dex_file,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700174 uint32_t method_idx,
175 DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700176 ClassLoader* class_loader,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700177 const AbstractMethod* referrer,
Ian Rogers08f753d2012-08-24 14:35:25 -0700178 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700180
Mathieu Chartier66f19252012-09-18 08:57:04 -0700181 AbstractMethod* ResolveMethod(uint32_t method_idx, const AbstractMethod* referrer, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700183 AbstractMethod* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
Ian Rogers19846512012-02-24 11:42:47 -0800184 if (UNLIKELY(resolved_method == NULL || resolved_method->IsRuntimeMethod())) {
185 Class* declaring_class = referrer->GetDeclaringClass();
186 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogers365c1022012-06-22 15:05:28 -0700187 ClassLoader* class_loader = declaring_class->GetClassLoader();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700188 const DexFile& dex_file = *dex_cache->GetDexFile();
jeffhaoc0228b82012-08-29 18:15:05 -0700189 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, referrer, type);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700190 }
191 return resolved_method;
Brian Carlstrom16192862011-09-12 17:50:06 -0700192 }
193
Mathieu Chartier66f19252012-09-18 08:57:04 -0700194 Field* ResolveField(uint32_t field_idx, const AbstractMethod* referrer, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800196 Field* resolved_field =
197 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700198 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogersb5d6a492012-02-14 04:00:51 -0800199 Class* declaring_class = referrer->GetDeclaringClass();
200 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogers365c1022012-06-22 15:05:28 -0700201 ClassLoader* class_loader = declaring_class->GetClassLoader();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700202 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700203 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
204 }
205 return resolved_field;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700206 }
207
Brian Carlstrom16192862011-09-12 17:50:06 -0700208 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700209 // result in DexCache. The ClassLinker and ClassLoader are used as
210 // in ResolveType. What is unique is the is_static argument which is
211 // used to determine if we are resolving a static or non-static
212 // field.
213 Field* ResolveField(const DexFile& dex_file,
214 uint32_t field_idx,
215 DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700216 ClassLoader* class_loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700219
Ian Rogersb067ac22011-12-13 18:05:09 -0800220 // Resolve a field with a given ID from the DexFile, storing the
221 // result in DexCache. The ClassLinker and ClassLoader are used as
222 // in ResolveType. No is_static argument is provided so that Java
223 // field resolution semantics are followed.
224 Field* ResolveFieldJLS(const DexFile& dex_file,
225 uint32_t field_idx,
226 DexCache* dex_cache,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700228 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb067ac22011-12-13 18:05:09 -0800229
Ian Rogersad25ac52011-10-04 19:13:33 -0700230 // Get shorty from method index without resolution. Used to do handlerization.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700231 const char* MethodShorty(uint32_t method_idx, AbstractMethod* referrer, uint32_t* length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad25ac52011-10-04 19:13:33 -0700233
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700234 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700235 // can_run_clinit=false allows the compiler to attempt to init a class,
236 // given the restriction that no <clinit> execution is possible.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 bool EnsureInitialized(Class* c, bool can_run_clinit, bool can_init_fields)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700240 // Initializes classes that have instances in the image but that have
241 // <clinit> methods so they could not be initialized by the compiler.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700243
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 void RegisterDexFile(const DexFile& dex_file)
245 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
248 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700250
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 void RegisterOatFile(const OatFile& oat_file)
252 LOCKS_EXCLUDED(dex_lock_);
Brian Carlstrom866c8622012-01-06 16:35:13 -0800253
Brian Carlstrom8a487412011-08-29 20:08:52 -0700254 const std::vector<const DexFile*>& GetBootClassPath() {
255 return boot_class_path_;
256 }
257
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 void VisitClasses(ClassVisitor* visitor, void* arg) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_
261 // when calling the visitor.
262 void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_);
Elliott Hughesa2155262011-11-16 16:26:58 -0800264
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700265 void VisitRoots(Heap::RootVisitor* visitor, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_, dex_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700267
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 DexCache* FindDexCache(const DexFile& dex_file) const
269 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 bool IsDexFileRegistered(const DexFile& dex_file) const
272 LOCKS_EXCLUDED(dex_lock_);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700273 void FixupDexCaches(AbstractMethod* resolution_method) const
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700276
jeffhao262bf462011-10-20 18:36:32 -0700277 // Generate an oat file from a dex file
Brian Carlstromd601af82012-01-06 10:15:19 -0800278 bool GenerateOatFile(const std::string& dex_filename,
279 int oat_fd,
280 const std::string& oat_cache_filename);
jeffhao262bf462011-10-20 18:36:32 -0700281
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 const OatFile* FindOatFileFromOatLocation(const std::string& location)
283 LOCKS_EXCLUDED(dex_lock_);
284
285 const OatFile* FindOatFileFromOatLocationLocked(const std::string& location)
286 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
buzbeec143c552011-08-20 17:38:58 -0700287
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800288 // Finds the oat file for a dex location, generating the oat file if
289 // it is missing or out of date. Returns the DexFile from within the
290 // created oat file.
291 const DexFile* FindOrCreateOatFileForDexLocation(const std::string& dex_location,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 const std::string& oat_location)
293 LOCKS_EXCLUDED(dex_lock_);
294 const DexFile* FindOrCreateOatFileForDexLocationLocked(const std::string& dex_location,
295 const std::string& oat_location)
296 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800297 // Find a DexFile within an OatFile given a DexFile location. Note
298 // that this returns null if the location checksum of the DexFile
299 // does not match the OatFile.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700300 const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location)
301 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800303
jeffhaof6174e82012-01-31 16:14:17 -0800304
Brian Carlstrom28db0122012-10-18 16:20:41 -0700305 // Returns true if oat file contains the dex file with the given location and checksum.
Brian Carlstromafe25512012-06-27 17:02:58 -0700306 static bool VerifyOatFileChecksums(const OatFile* oat_file,
307 const std::string& dex_location,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 uint32_t dex_location_checksum)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromafe25512012-06-27 17:02:58 -0700310
Elliott Hughes418d20f2011-09-22 14:00:39 -0700311 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700312 template <class T>
Ian Rogers50b35e22012-10-04 10:09:15 -0700313 ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700314 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700315 return ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
Shih-wei Liao44175362011-08-28 16:59:17 -0700316 }
317
Ian Rogers50b35e22012-10-04 10:09:15 -0700318 ObjectArray<Class>* AllocClassArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700320 return ObjectArray<Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700321 }
322
Ian Rogers50b35e22012-10-04 10:09:15 -0700323 ObjectArray<String>* AllocStringArray(Thread* self, size_t length)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700325 return ObjectArray<String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass), length);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700326 }
327
Ian Rogers4445a7e2012-10-05 17:19:13 -0700328 ObjectArray<AbstractMethod>* AllocAbstractMethodArray(Thread* self, size_t length)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700330 return ObjectArray<AbstractMethod>::Alloc(self,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700331 GetClassRoot(kJavaLangReflectAbstractMethodArrayClass), length);
332 }
333
Ian Rogers4445a7e2012-10-05 17:19:13 -0700334 ObjectArray<AbstractMethod>* AllocMethodArray(Thread* self, size_t length)
335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
336 return ObjectArray<AbstractMethod>::Alloc(self,
337 GetClassRoot(kJavaLangReflectMethodArrayClass), length);
338 }
339
Ian Rogers9bc81912012-10-11 21:43:36 -0700340 IfTable* AllocIfTable(Thread* self, size_t ifcount) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
341 return down_cast<IfTable*>(
342 IfTable::Alloc(self, GetClassRoot(kObjectArrayClass), ifcount * IfTable::kMax));
Ian Rogers23435d02012-09-24 11:23:12 -0700343 }
344
Ian Rogers50b35e22012-10-04 10:09:15 -0700345 ObjectArray<Field>* AllocFieldArray(Thread* self, size_t length)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700347 return ObjectArray<Field>::Alloc(self, GetClassRoot(kJavaLangReflectFieldArrayClass), length);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700348 }
349
Ian Rogers50b35e22012-10-04 10:09:15 -0700350 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700352
Ian Rogersb726dcb2012-09-05 08:57:23 -0700353 void VerifyClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes634eb2e2012-03-22 16:06:28 -0700354 bool VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 Class::Status& oat_file_class_status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700356 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700358 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700359 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, AbstractMethod* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700360 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao98eacac2011-09-14 16:11:53 -0700361
Jesse Wilson95caa792011-10-12 18:14:17 -0400362 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700363 ObjectArray<AbstractMethod>* methods, ObjectArray<ObjectArray<Class> >* throws)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700364 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365 std::string GetDescriptorForProxy(const Class* proxy_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700366 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700367 AbstractMethod* FindMethodForProxy(const Class* proxy_class, const AbstractMethod* proxy_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700369 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400370
Ian Rogers19846512012-02-24 11:42:47 -0800371 // Get the oat code for a method when its class isn't yet initialized
Mathieu Chartier66f19252012-09-18 08:57:04 -0700372 const void* GetOatCodeFor(const AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800374
Logan Chien0c717dd2012-03-28 18:31:07 +0800375 // Relocate the OatFiles (ELF images)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 void RelocateExecutable() LOCKS_EXCLUDED(dex_lock_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800377
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700378 pid_t GetClassesLockOwner(); // For SignalCatcher.
379 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700380
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700381 bool IsDirty() const {
382 return is_dirty_;
383 }
384
385 void Dirty() {
386 is_dirty_ = true;
387 }
388
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700389 private:
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800390 explicit ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700391
Mathieu Chartier66f19252012-09-18 08:57:04 -0700392 const OatFile::OatMethod GetOatMethodFor(const AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700393 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa12785321912012-04-01 15:24:56 -0700394
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700395 // Initialize class linker by bootstraping from dex files
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700397 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700398
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700399 // Initialize class linker from one or more images.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700400 void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 OatFile* OpenOat(const ImageSpace* space)
402 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700403 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 static void InitFromImageCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700405 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700406
Ian Rogersb726dcb2012-09-05 08:57:23 -0700407 void FinishInit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700408
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700409 // For early bootstrapping by Init
Ian Rogers50b35e22012-10-04 10:09:15 -0700410 Class* AllocClass(Thread* self, Class* java_lang_Class, size_t class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700411 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700412
413 // Alloc* convenience functions to avoid needing to pass in Class*
414 // values that are known to the ClassLinker such as
415 // kObjectArrayClass and kJavaLangString etc.
Ian Rogers50b35e22012-10-04 10:09:15 -0700416 Class* AllocClass(Thread* self, size_t class_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
417 DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700418 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700419 Field* AllocField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
420 Method* AllocMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
421 Constructor* AllocConstructor(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700422
Ian Rogers50b35e22012-10-04 10:09:15 -0700423 Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700424 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700425 return InitializePrimitiveClass(AllocClass(self, sizeof(Class)), type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700426 }
Ian Rogersc8982582012-09-07 16:53:25 -0700427 Class* InitializePrimitiveClass(Class* primitive_class, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700428 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700429
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700430
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700431 Class* CreateArrayClass(const std::string& descriptor, ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700432 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700433
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700434 void AppendToBootClassPath(const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700436 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700437 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700438
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700439 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700440 Class* c, SafeMap<uint32_t, Field*>& field_map)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700441 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700442
Brian Carlstrom4873d462011-08-21 15:23:39 -0700443 size_t SizeOfClass(const DexFile& dex_file,
444 const DexFile::ClassDef& dex_class_def);
445
Brian Carlstromf615a612011-07-23 12:50:34 -0700446 void LoadClass(const DexFile& dex_file,
447 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700448 SirtRef<Class>& klass,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700449 ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700451
Ian Rogers0571d352011-11-03 19:51:38 -0700452 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Ian Rogers23435d02012-09-24 11:23:12 -0700453 SirtRef<Field>& dst) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700454
Ian Rogers50b35e22012-10-04 10:09:15 -0700455 AbstractMethod* LoadMethod(Thread* self, const DexFile& dex_file,
456 const ClassDataItemIterator& dex_method,
457 SirtRef<Class>& klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700458
Ian Rogers23435d02012-09-24 11:23:12 -0700459 void FixupStaticTrampolines(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800460
461 // Finds the associated oat class for a dex_file and descriptor
462 const OatFile::OatClass* GetOatClass(const DexFile& dex_file, const char* descriptor);
463
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800464 // Attempts to insert a class into a class table. Returns NULL if
465 // the class was inserted, otherwise returns an existing class with
466 // the same descriptor and ClassLoader.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700467 Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700468 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
469 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700471 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
472 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700473 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700474 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700475 void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
476 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700477
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 bool InitializeClass(Class* klass, bool can_run_clinit, bool can_init_statics)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700480 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700481 bool ValidateSuperClassDescriptors(const Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700483 bool InitializeSuperClass(Class* klass, bool can_run_clinit, bool can_init_fields)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0045a292012-03-31 21:08:41 -0700485 // Initialize static fields, returns true if fields were initialized.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700486 bool InitializeStaticFields(Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700488
Ian Rogers595799e2012-01-11 17:32:51 -0800489 bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
490 const Class* klass1,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 const Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700493
Mathieu Chartier66f19252012-09-18 08:57:04 -0700494 bool IsSameMethodSignatureInDifferentClassContexts(const AbstractMethod* descriptor,
Ian Rogers595799e2012-01-11 17:32:51 -0800495 const Class* klass1,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496 const Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700498
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700499 bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700500 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700501
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700502 bool LinkSuperClass(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700503 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700504
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700505 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700507
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700508 bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700509 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 bool LinkVirtualMethods(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700512 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700513
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700514 bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700516
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517 bool LinkStaticFields(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700519 bool LinkInstanceFields(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700520 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700521 bool LinkFields(SirtRef<Class>& klass, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700523
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700524
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700525 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700527 void CreateReferenceStaticOffsets(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700529 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 uint32_t reference_offsets)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700532
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700533 // For use by ImageWriter to find DexCaches for its roots
534 const std::vector<DexCache*>& GetDexCaches() {
535 return dex_caches_;
536 }
537
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700538 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
539 LOCKS_EXCLUDED(dex_lock_);
540 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location)
541 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
542 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
543 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstromafe25512012-06-27 17:02:58 -0700544 const DexFile* VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
545 const std::string& dex_location,
546 uint32_t dex_location_checksum)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700547 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700548 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromfad71432011-10-16 20:25:10 -0700549
Ian Rogers50b35e22012-10-04 10:09:15 -0700550 AbstractMethod* CreateProxyConstructor(Thread* self, SirtRef<Class>& klass, Class* proxy_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700552 AbstractMethod* CreateProxyMethod(Thread* self, SirtRef<Class>& klass,
553 SirtRef<AbstractMethod>& prototype)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700554 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400555
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700556 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700557
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558 mutable Mutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700559 std::vector<DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
560 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700561
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700562
Brian Carlstromaded5f72011-10-07 17:15:04 -0700563 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700564 // Class* instances. Results should be compared for a matching
565 // Class::descriptor_ and Class::class_loader_.
Elliott Hughesf8349362012-06-18 15:00:06 -0700566 typedef std::multimap<size_t, Class*> Table;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700567 Table image_classes_ GUARDED_BY(Locks::classlinker_classes_lock_);
568 Table classes_ GUARDED_BY(Locks::classlinker_classes_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700569
570 Class* LookupClassLocked(const char* descriptor, const ClassLoader* class_loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 size_t hash, const Table& classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700572 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
573 EXCLUSIVE_LOCKS_REQUIRED(Locks::classlinker_classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700574
Brian Carlstroma663ea52011-08-19 23:33:41 -0700575 // indexes into class_roots_.
576 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700577 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700578 kJavaLangClass,
579 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700580 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700581 kObjectArrayClass,
582 kJavaLangString,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700583 kJavaLangDexCache,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700584 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700585 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700586 kJavaLangReflectField,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700587 kJavaLangReflectAbstractMethod,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700588 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700589 kJavaLangReflectProxy,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700590 kJavaLangStringArrayClass,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700591 kJavaLangReflectAbstractMethodArrayClass,
Ian Rogers4445a7e2012-10-05 17:19:13 -0700592 kJavaLangReflectFieldArrayClass,
593 kJavaLangReflectMethodArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700594 kJavaLangClassLoader,
Ian Rogers5167c972012-02-03 10:41:20 -0800595 kJavaLangThrowable,
jeffhao8cd6dda2012-02-22 10:15:34 -0800596 kJavaLangClassNotFoundException,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700597 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700598 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700599 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700600 kPrimitiveChar,
601 kPrimitiveDouble,
602 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700603 kPrimitiveInt,
604 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700605 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700606 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700607 kBooleanArrayClass,
608 kByteArrayClass,
609 kCharArrayClass,
610 kDoubleArrayClass,
611 kFloatArrayClass,
612 kIntArrayClass,
613 kLongArrayClass,
614 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700615 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700616 kClassRootsMax,
617 };
618 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700619
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 Class* GetClassRoot(ClassRoot class_root)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700621 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700622 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700623 Class* klass = class_roots_->Get(class_root);
624 DCHECK(klass != NULL);
625 return klass;
626 }
627
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 void SetClassRoot(ClassRoot class_root, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700629 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700630
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700631 ObjectArray<Class>* GetClassRoots() {
632 DCHECK(class_roots_ != NULL);
633 return class_roots_;
634 }
635
Elliott Hughes418d20f2011-09-22 14:00:39 -0700636 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700637
638 const char* GetClassRootDescriptor(ClassRoot class_root) {
639 const char* descriptor = class_roots_descriptors_[class_root];
640 CHECK(descriptor != NULL);
641 return descriptor;
642 }
643
Ian Rogers9bc81912012-10-11 21:43:36 -0700644 IfTable* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700645
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700646 bool init_done_;
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700647 bool is_dirty_;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700648
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700649 InternTable* intern_table_;
650
Brian Carlstromf734cf52011-08-17 16:28:14 -0700651 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700652 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800653 friend class ObjectTest;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800654 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Brian Carlstromae826982011-11-09 01:33:42 -0800655 FRIEND_TEST(DexCacheTest, Open);
656 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
657 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700658 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
659};
660
661} // namespace art
662
663#endif // ART_SRC_CLASS_LINKER_H_