blob: 3cee7f003a1cb6b4efb68281a4261752aaed555d [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"
Brian Carlstrom40381fb2011-10-19 14:13:40 -070033#include "stack_indirect_reference_table.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
35namespace art {
36
Elliott Hughescf4c6c42011-09-01 15:16:42 -070037class ClassLoader;
Ian Rogers30fab402012-01-23 15:43:46 -080038class ImageSpace;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070039class InternTable;
Brian Carlstromd1422f82011-09-28 11:37:09 -070040class ObjectLock;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070041
Elliott Hughesa2155262011-11-16 16:26:58 -080042typedef bool (ClassVisitor)(Class* c, void* arg);
43
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044class ClassLinker {
45 public:
Brian Carlstrom58ae9412011-10-04 00:56:06 -070046 // Creates the class linker by boot strapping from dex files.
Brian Carlstroma004aa92012-02-08 18:05:09 -080047 static ClassLinker* CreateFromCompiler(const std::vector<const DexFile*>& boot_class_path,
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.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 String* ResolveString(uint32_t string_idx, const Method* 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();
107 const DexFile& dex_file = FindDexFile(dex_cache);
108 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.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 Class* ResolveType(uint16_t type_idx, const Method* 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 Rogerscaab8c42011-10-12 12:11:18 -0700139 const DexFile& dex_file = FindDexFile(dex_cache);
140 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 Rogerscaab8c42011-10-12 12:11:18 -0700152 const DexFile& dex_file = FindDexFile(dex_cache);
153 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.
173 Method* ResolveMethod(const DexFile& dex_file,
174 uint32_t method_idx,
175 DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700176 ClassLoader* class_loader,
jeffhaoc0228b82012-08-29 18:15:05 -0700177 const Method* 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
Ian Rogers08f753d2012-08-24 14:35:25 -0700181 Method* ResolveMethod(uint32_t method_idx, const Method* referrer, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers19846512012-02-24 11:42:47 -0800183 Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
184 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 Rogerscaab8c42011-10-12 12:11:18 -0700188 const DexFile& dex_file = FindDexFile(dex_cache);
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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194 Field* ResolveField(uint32_t field_idx, const Method* 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 Rogerscaab8c42011-10-12 12:11:18 -0700202 const DexFile& dex_file = FindDexFile(dex_cache);
203 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.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 const char* MethodShorty(uint32_t method_idx, Method* 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const
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 const DexFile& FindDexFile(const DexCache* dex_cache) 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 DexCache* FindDexCache(const DexFile& dex_file) const
272 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 bool IsDexFileRegistered(const DexFile& dex_file) const
275 LOCKS_EXCLUDED(dex_lock_);
276 void FixupDexCaches(Method* resolution_method) const
277 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700279
jeffhao262bf462011-10-20 18:36:32 -0700280 // Generate an oat file from a dex file
Brian Carlstromd601af82012-01-06 10:15:19 -0800281 bool GenerateOatFile(const std::string& dex_filename,
282 int oat_fd,
283 const std::string& oat_cache_filename);
jeffhao262bf462011-10-20 18:36:32 -0700284
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 const OatFile* FindOatFileFromOatLocation(const std::string& location)
286 LOCKS_EXCLUDED(dex_lock_);
287
288 const OatFile* FindOatFileFromOatLocationLocked(const std::string& location)
289 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
buzbeec143c552011-08-20 17:38:58 -0700290
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800291 // Finds the oat file for a dex location, generating the oat file if
292 // it is missing or out of date. Returns the DexFile from within the
293 // created oat file.
294 const DexFile* FindOrCreateOatFileForDexLocation(const std::string& dex_location,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 const std::string& oat_location)
296 LOCKS_EXCLUDED(dex_lock_);
297 const DexFile* FindOrCreateOatFileForDexLocationLocked(const std::string& dex_location,
298 const std::string& oat_location)
299 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800300 // Find a DexFile within an OatFile given a DexFile location. Note
301 // that this returns null if the location checksum of the DexFile
302 // does not match the OatFile.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location)
304 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800306
jeffhaof6174e82012-01-31 16:14:17 -0800307
Brian Carlstromafe25512012-06-27 17:02:58 -0700308 // Returns true if oat file contains the dex file with the given location and checksum
309 static bool VerifyOatFileChecksums(const OatFile* oat_file,
310 const std::string& dex_location,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 uint32_t dex_location_checksum)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromafe25512012-06-27 17:02:58 -0700313
Elliott Hughes418d20f2011-09-22 14:00:39 -0700314 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700315 template <class T>
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 ObjectArray<T>* AllocObjectArray(size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Shih-wei Liao44175362011-08-28 16:59:17 -0700318 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
319 }
320
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 ObjectArray<Class>* AllocClassArray(size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700322 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700323 return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
324 }
325
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700328
Ian Rogersb726dcb2012-09-05 08:57:23 -0700329 void VerifyClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes634eb2e2012-03-22 16:06:28 -0700330 bool VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331 Class::Status& oat_file_class_status)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao98eacac2011-09-14 16:11:53 -0700337
Jesse Wilson95caa792011-10-12 18:14:17 -0400338 Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339 ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 std::string GetDescriptorForProxy(const Class* proxy_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343 Method* FindMethodForProxy(const Class* proxy_class, const Method* proxy_method)
344 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400346
Ian Rogers19846512012-02-24 11:42:47 -0800347 // Get the oat code for a method when its class isn't yet initialized
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 const void* GetOatCodeFor(const Method* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700349 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800350
Logan Chien0c717dd2012-03-28 18:31:07 +0800351 // Relocate the OatFiles (ELF images)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 void RelocateExecutable() LOCKS_EXCLUDED(dex_lock_);
Logan Chien0c717dd2012-03-28 18:31:07 +0800353
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700354 pid_t GetClassesLockOwner(); // For SignalCatcher.
355 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700356
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700357 private:
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800358 explicit ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700359
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 const OatFile::OatMethod GetOatMethodFor(const Method* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700361 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa12785321912012-04-01 15:24:56 -0700362
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700363 // Initialize class linker by bootstraping from dex files
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364 void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700365 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700366
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700367 // Initialize class linker from one or more images.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700368 void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 OatFile* OpenOat(const ImageSpace* space)
370 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700371 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 static void InitFromImageCallback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700374
Ian Rogersb726dcb2012-09-05 08:57:23 -0700375 void FinishInit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700376
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700377 // For early bootstrapping by Init
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 Class* AllocClass(Class* java_lang_Class, size_t class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700379 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700380
381 // Alloc* convenience functions to avoid needing to pass in Class*
382 // values that are known to the ClassLinker such as
383 // kObjectArrayClass and kJavaLangString etc.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700384 Class* AllocClass(size_t class_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 DexCache* AllocDexCache(const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700386 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
387 Field* AllocField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
388 Method* AllocMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700389
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 InterfaceEntry* AllocInterfaceEntry(Class* interface)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700392
Ian Rogersc8982582012-09-07 16:53:25 -0700393 Class* CreatePrimitiveClass(Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700394 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc8982582012-09-07 16:53:25 -0700395 return InitializePrimitiveClass(AllocClass(sizeof(Class)), type);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700396 }
Ian Rogersc8982582012-09-07 16:53:25 -0700397 Class* InitializePrimitiveClass(Class* primitive_class, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700399
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700400
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 Class* CreateArrayClass(const std::string& descriptor, ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700402 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700403
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 void AppendToBootClassPath(const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700405 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700406 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700408
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700409 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 Class* c, SafeMap<uint32_t, Field*>& field_map)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700411 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700412
Brian Carlstrom4873d462011-08-21 15:23:39 -0700413 size_t SizeOfClass(const DexFile& dex_file,
414 const DexFile::ClassDef& dex_class_def);
415
Brian Carlstromf615a612011-07-23 12:50:34 -0700416 void LoadClass(const DexFile& dex_file,
417 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700418 SirtRef<Class>& klass,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700419 ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700420 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700421
Ian Rogers0571d352011-11-03 19:51:38 -0700422 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700423 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700424
Ian Rogers0571d352011-11-03 19:51:38 -0700425 void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700426 SirtRef<Class>& klass, SirtRef<Method>& dst)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700428
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700429 void FixupStaticTrampolines(Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800431
432 // Finds the associated oat class for a dex_file and descriptor
433 const OatFile::OatClass* GetOatClass(const DexFile& dex_file, const char* descriptor);
434
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800435 // Attempts to insert a class into a class table. Returns NULL if
436 // the class was inserted, otherwise returns an existing class with
437 // the same descriptor and ClassLoader.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700439 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700441
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700442 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
443 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700444 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700445 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
447 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700448
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700449 bool InitializeClass(Class* klass, bool can_run_clinit, bool can_init_statics)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700451 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 bool ValidateSuperClassDescriptors(const Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700454 bool InitializeSuperClass(Class* klass, bool can_run_clinit, bool can_init_fields)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700455 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0045a292012-03-31 21:08:41 -0700456 // Initialize static fields, returns true if fields were initialized.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 bool InitializeStaticFields(Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700458 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700459
Ian Rogers595799e2012-01-11 17:32:51 -0800460 bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
461 const Class* klass1,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 const Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700463 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700464
Ian Rogers595799e2012-01-11 17:32:51 -0800465 bool IsSameMethodSignatureInDifferentClassContexts(const Method* descriptor,
466 const Class* klass1,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700467 const Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700469
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700471 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700472
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700473 bool LinkSuperClass(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700474 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700475
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700476 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700477 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700478
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700480 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700481
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700482 bool LinkVirtualMethods(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700483 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700484
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700485 bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700487
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 bool LinkStaticFields(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700489 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700490 bool LinkInstanceFields(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700491 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700492 bool LinkFields(SirtRef<Class>& klass, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700493 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700494
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700495
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 void CreateReferenceStaticOffsets(SirtRef<Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700500 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700501 uint32_t reference_offsets)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700503
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700504 // For use by ImageWriter to find DexCaches for its roots
505 const std::vector<DexCache*>& GetDexCaches() {
506 return dex_caches_;
507 }
508
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700509 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
510 LOCKS_EXCLUDED(dex_lock_);
511 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location)
512 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
513 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
514 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstromafe25512012-06-27 17:02:58 -0700515 const DexFile* VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
516 const std::string& dex_location,
517 uint32_t dex_location_checksum)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromfad71432011-10-16 20:25:10 -0700520
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700521 Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700523 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400525
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700526 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700527
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700528 mutable Mutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700529 std::vector<const DexFile*> dex_files_ GUARDED_BY(dex_lock_);
530 std::vector<DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
531 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700532
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700533
Brian Carlstromaded5f72011-10-07 17:15:04 -0700534 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700535 // Class* instances. Results should be compared for a matching
536 // Class::descriptor_ and Class::class_loader_.
Elliott Hughesf8349362012-06-18 15:00:06 -0700537 typedef std::multimap<size_t, Class*> Table;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700538 Table image_classes_ GUARDED_BY(Locks::classlinker_classes_lock_);
539 Table classes_ GUARDED_BY(Locks::classlinker_classes_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700540
541 Class* LookupClassLocked(const char* descriptor, const ClassLoader* class_loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700542 size_t hash, const Table& classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
544 EXCLUSIVE_LOCKS_REQUIRED(Locks::classlinker_classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700545
Brian Carlstroma663ea52011-08-19 23:33:41 -0700546 // indexes into class_roots_.
547 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700548 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700549 kJavaLangClass,
550 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700551 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700552 kObjectArrayClass,
553 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700554 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700555 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700556 kJavaLangReflectField,
557 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700558 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700559 kJavaLangClassLoader,
Ian Rogers5167c972012-02-03 10:41:20 -0800560 kJavaLangThrowable,
jeffhao8cd6dda2012-02-22 10:15:34 -0800561 kJavaLangClassNotFoundException,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700562 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700563 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700564 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700565 kPrimitiveChar,
566 kPrimitiveDouble,
567 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700568 kPrimitiveInt,
569 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700570 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700571 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700572 kBooleanArrayClass,
573 kByteArrayClass,
574 kCharArrayClass,
575 kDoubleArrayClass,
576 kFloatArrayClass,
577 kIntArrayClass,
578 kLongArrayClass,
579 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700580 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700581 kClassRootsMax,
582 };
583 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700584
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 Class* GetClassRoot(ClassRoot class_root)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700586 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700587 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700588 Class* klass = class_roots_->Get(class_root);
589 DCHECK(klass != NULL);
590 return klass;
591 }
592
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700593 void SetClassRoot(ClassRoot class_root, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700594 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700595
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700596 ObjectArray<Class>* GetClassRoots() {
597 DCHECK(class_roots_ != NULL);
598 return class_roots_;
599 }
600
Elliott Hughes418d20f2011-09-22 14:00:39 -0700601 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700602
603 const char* GetClassRootDescriptor(ClassRoot class_root) {
604 const char* descriptor = class_roots_descriptors_[class_root];
605 CHECK(descriptor != NULL);
606 return descriptor;
607 }
608
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700609 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700610
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700611 bool init_done_;
612
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700613 InternTable* intern_table_;
614
Brian Carlstromf734cf52011-08-17 16:28:14 -0700615 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700616 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800617 friend class ObjectTest;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800618 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Brian Carlstromae826982011-11-09 01:33:42 -0800619 FRIEND_TEST(DexCacheTest, Open);
620 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
621 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
623};
624
625} // namespace art
626
627#endif // ART_SRC_CLASS_LINKER_H_