blob: 14c719e8fe7a4e511ceb1d26ee596ecfcf1a28cd [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
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700375 // Get the oat code for a method from a method index.
376 const void* GetOatCodeFor(const DexFile& dex_file, uint32_t method_idx);
377
Logan Chien0c717dd2012-03-28 18:31:07 +0800378 // Relocate the OatFiles (ELF images)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 void RelocateExecutable() LOCKS_EXCLUDED(dex_lock_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800380
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700381 pid_t GetClassesLockOwner(); // For SignalCatcher.
382 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700383
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700384 bool IsDirty() const {
385 return is_dirty_;
386 }
387
388 void Dirty() {
389 is_dirty_ = true;
390 }
391
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700392 private:
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800393 explicit ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700394
Mathieu Chartier66f19252012-09-18 08:57:04 -0700395 const OatFile::OatMethod GetOatMethodFor(const AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700396 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa12785321912012-04-01 15:24:56 -0700397
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700398 // Initialize class linker by bootstraping from dex files
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700399 void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700400 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700401
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700402 // Initialize class linker from one or more images.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700403 void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 OatFile* OpenOat(const ImageSpace* space)
405 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700406 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 static void InitFromImageCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700408 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700409
Ian Rogersb726dcb2012-09-05 08:57:23 -0700410 void FinishInit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700411
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700412 // For early bootstrapping by Init
Ian Rogers50b35e22012-10-04 10:09:15 -0700413 Class* AllocClass(Thread* self, Class* java_lang_Class, size_t class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700414 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700415
416 // Alloc* convenience functions to avoid needing to pass in Class*
417 // values that are known to the ClassLinker such as
418 // kObjectArrayClass and kJavaLangString etc.
Ian Rogers50b35e22012-10-04 10:09:15 -0700419 Class* AllocClass(Thread* self, size_t class_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
420 DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700422 Field* AllocField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
423 Method* AllocMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
424 Constructor* AllocConstructor(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700425
Ian Rogers50b35e22012-10-04 10:09:15 -0700426 Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700428 return InitializePrimitiveClass(AllocClass(self, sizeof(Class)), type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700429 }
Ian Rogersc8982582012-09-07 16:53:25 -0700430 Class* InitializePrimitiveClass(Class* primitive_class, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700431 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700432
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700433
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700434 Class* CreateArrayClass(const std::string& descriptor, ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700436
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700437 void AppendToBootClassPath(const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700441
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700442 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700443 Class* c, SafeMap<uint32_t, Field*>& field_map)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700444 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700445
Brian Carlstrom4873d462011-08-21 15:23:39 -0700446 size_t SizeOfClass(const DexFile& dex_file,
447 const DexFile::ClassDef& dex_class_def);
448
Brian Carlstromf615a612011-07-23 12:50:34 -0700449 void LoadClass(const DexFile& dex_file,
450 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700451 SirtRef<Class>& klass,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700454
Ian Rogers0571d352011-11-03 19:51:38 -0700455 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Ian Rogers23435d02012-09-24 11:23:12 -0700456 SirtRef<Field>& dst) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700457
Ian Rogers50b35e22012-10-04 10:09:15 -0700458 AbstractMethod* LoadMethod(Thread* self, const DexFile& dex_file,
459 const ClassDataItemIterator& dex_method,
460 SirtRef<Class>& klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700461
Ian Rogers23435d02012-09-24 11:23:12 -0700462 void FixupStaticTrampolines(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800463
464 // Finds the associated oat class for a dex_file and descriptor
465 const OatFile::OatClass* GetOatClass(const DexFile& dex_file, const char* descriptor);
466
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800467 // Attempts to insert a class into a class table. Returns NULL if
468 // the class was inserted, otherwise returns an existing class with
469 // the same descriptor and ClassLoader.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700471 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
472 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700473
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
475 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700477 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
479 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700481 bool InitializeClass(Class* klass, bool can_run_clinit, bool can_init_statics)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700483 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484 bool ValidateSuperClassDescriptors(const Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700486 bool InitializeSuperClass(Class* klass, bool can_run_clinit, bool can_init_fields)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0045a292012-03-31 21:08:41 -0700488 // Initialize static fields, returns true if fields were initialized.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700489 bool InitializeStaticFields(Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700490 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700491
Ian Rogers595799e2012-01-11 17:32:51 -0800492 bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
493 const Class* klass1,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700494 const Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700495 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700496
Mathieu Chartier66f19252012-09-18 08:57:04 -0700497 bool IsSameMethodSignatureInDifferentClassContexts(const AbstractMethod* descriptor,
Ian Rogers595799e2012-01-11 17:32:51 -0800498 const Class* klass1,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700499 const Class* klass2)
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 LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
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 LinkSuperClass(SirtRef<Class>& klass)
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 LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file)
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 LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
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 LinkVirtualMethods(SirtRef<Class>& klass)
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 LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700519
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 bool LinkStaticFields(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700522 bool LinkInstanceFields(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700523 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700524 bool LinkFields(SirtRef<Class>& klass, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700525 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700526
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700527
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700528 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700529 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 void CreateReferenceStaticOffsets(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700532 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533 uint32_t reference_offsets)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700534 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700536 // For use by ImageWriter to find DexCaches for its roots
537 const std::vector<DexCache*>& GetDexCaches() {
538 return dex_caches_;
539 }
540
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700541 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
542 LOCKS_EXCLUDED(dex_lock_);
543 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location)
544 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
545 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
546 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstromafe25512012-06-27 17:02:58 -0700547 const DexFile* VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
548 const std::string& dex_location,
549 uint32_t dex_location_checksum)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700550 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromfad71432011-10-16 20:25:10 -0700552
Ian Rogers50b35e22012-10-04 10:09:15 -0700553 AbstractMethod* CreateProxyConstructor(Thread* self, SirtRef<Class>& klass, Class* proxy_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700554 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers50b35e22012-10-04 10:09:15 -0700555 AbstractMethod* CreateProxyMethod(Thread* self, SirtRef<Class>& klass,
556 SirtRef<AbstractMethod>& prototype)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700557 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400558
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700559 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700560
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700561 mutable Mutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700562 std::vector<DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
563 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700564
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700565
Brian Carlstromaded5f72011-10-07 17:15:04 -0700566 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700567 // Class* instances. Results should be compared for a matching
568 // Class::descriptor_ and Class::class_loader_.
Elliott Hughesf8349362012-06-18 15:00:06 -0700569 typedef std::multimap<size_t, Class*> Table;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700570 Table image_classes_ GUARDED_BY(Locks::classlinker_classes_lock_);
571 Table classes_ GUARDED_BY(Locks::classlinker_classes_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700572
573 Class* LookupClassLocked(const char* descriptor, const ClassLoader* class_loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700574 size_t hash, const Table& classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
576 EXCLUSIVE_LOCKS_REQUIRED(Locks::classlinker_classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700577
Brian Carlstroma663ea52011-08-19 23:33:41 -0700578 // indexes into class_roots_.
579 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700580 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700581 kJavaLangClass,
582 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700583 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700584 kObjectArrayClass,
585 kJavaLangString,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700586 kJavaLangDexCache,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700587 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700588 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700589 kJavaLangReflectField,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700590 kJavaLangReflectAbstractMethod,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700591 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700592 kJavaLangReflectProxy,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700593 kJavaLangStringArrayClass,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700594 kJavaLangReflectAbstractMethodArrayClass,
Ian Rogers4445a7e2012-10-05 17:19:13 -0700595 kJavaLangReflectFieldArrayClass,
596 kJavaLangReflectMethodArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700597 kJavaLangClassLoader,
Ian Rogers5167c972012-02-03 10:41:20 -0800598 kJavaLangThrowable,
jeffhao8cd6dda2012-02-22 10:15:34 -0800599 kJavaLangClassNotFoundException,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700600 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700601 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700602 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700603 kPrimitiveChar,
604 kPrimitiveDouble,
605 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700606 kPrimitiveInt,
607 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700608 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700609 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700610 kBooleanArrayClass,
611 kByteArrayClass,
612 kCharArrayClass,
613 kDoubleArrayClass,
614 kFloatArrayClass,
615 kIntArrayClass,
616 kLongArrayClass,
617 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700618 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700619 kClassRootsMax,
620 };
621 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700622
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 Class* GetClassRoot(ClassRoot class_root)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700624 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700625 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700626 Class* klass = class_roots_->Get(class_root);
627 DCHECK(klass != NULL);
628 return klass;
629 }
630
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 void SetClassRoot(ClassRoot class_root, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700632 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700633
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700634 ObjectArray<Class>* GetClassRoots() {
635 DCHECK(class_roots_ != NULL);
636 return class_roots_;
637 }
638
Elliott Hughes418d20f2011-09-22 14:00:39 -0700639 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700640
641 const char* GetClassRootDescriptor(ClassRoot class_root) {
642 const char* descriptor = class_roots_descriptors_[class_root];
643 CHECK(descriptor != NULL);
644 return descriptor;
645 }
646
Ian Rogers9bc81912012-10-11 21:43:36 -0700647 IfTable* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700648
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700649 bool init_done_;
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700650 bool is_dirty_;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700651
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700652 InternTable* intern_table_;
653
Brian Carlstromf734cf52011-08-17 16:28:14 -0700654 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700655 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800656 friend class ObjectTest;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800657 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Brian Carlstromae826982011-11-09 01:33:42 -0800658 FRIEND_TEST(DexCacheTest, Open);
659 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
660 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700661 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
662};
663
664} // namespace art
665
666#endif // ART_SRC_CLASS_LINKER_H_