blob: eed9f6a1a9ac628bb57594816aa7354df5ddb4b7 [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)
49 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
53 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
60 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -070061
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 Class* FindSystemClass(const char* descriptor)
63 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
68 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
73 LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
74 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
78 LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
79 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes6fa602d2011-12-02 17:54:25 -080080
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
86 LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
87 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -080088
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 void DumpAllClasses(int flags) const
90 LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
91 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070092
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 void DumpForSigQuit(std::ostream& os) const
94 LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -070095
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 size_t NumLoadedClasses() const LOCKS_EXCLUDED(GlobalSynchronization::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)
102 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
116 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
122 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
133 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
146 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
166 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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 Rogers00f7d0e2012-07-19 15:28:27 -0700179 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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 Rogers00f7d0e2012-07-19 15:28:27 -0700182 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
195 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
218 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
228 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
232 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
238 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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 Rogers00f7d0e2012-07-19 15:28:27 -0700242 void RunRootClinits() SHARED_LOCKS_REQUIRED(GlobalSynchronization::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_)
246 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
247 void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
248 LOCKS_EXCLUDED(dex_lock_)
249 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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
259 LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_);
260 // 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
263 LOCKS_EXCLUDED(GlobalSynchronization::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
266 LOCKS_EXCLUDED(GlobalSynchronization::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_)
270 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
271 DexCache* FindDexCache(const DexFile& dex_file) const
272 LOCKS_EXCLUDED(dex_lock_)
273 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
274 bool IsDexFileRegistered(const DexFile& dex_file) const
275 LOCKS_EXCLUDED(dex_lock_);
276 void FixupDexCaches(Method* resolution_method) const
277 LOCKS_EXCLUDED(dex_lock_)
278 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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_)
305 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
312 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
317 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
322 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
327 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700328
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700329 void VerifyClass(Class* klass) SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
332 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
333 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass)
334 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
335 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* klass)
336 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
340 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
341 std::string GetDescriptorForProxy(const Class* proxy_class)
342 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
343 Method* FindMethodForProxy(const Class* proxy_class, const Method* proxy_method)
344 LOCKS_EXCLUDED(dex_lock_)
345 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
349 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
361 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
365 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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 Rogers00f7d0e2012-07-19 15:28:27 -0700368 void InitFromImage() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
369 OatFile* OpenOat(const ImageSpace* space)
370 LOCKS_EXCLUDED(dex_lock_)
371 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
372 static void InitFromImageCallback(Object* obj, void* arg)
373 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700374
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 void FinishInit() SHARED_LOCKS_REQUIRED(GlobalSynchronization::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)
379 SHARED_LOCKS_REQUIRED(GlobalSynchronization::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 Rogers00f7d0e2012-07-19 15:28:27 -0700384 Class* AllocClass(size_t class_size) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
385 DexCache* AllocDexCache(const DexFile& dex_file)
386 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
387 Field* AllocField() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
388 Method* AllocMethod() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700389
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 InterfaceEntry* AllocInterfaceEntry(Class* interface)
391 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700392
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type)
394 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700395 return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
396 }
397 Class* InitializePrimitiveClass(Class* primitive_class,
398 const char* descriptor,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700399 Primitive::Type type)
400 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700401
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700402
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 Class* CreateArrayClass(const std::string& descriptor, ClassLoader* class_loader)
404 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700405
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700406 void AppendToBootClassPath(const DexFile& dex_file)
407 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
408 void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
409 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700410
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700411 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700412 Class* c, SafeMap<uint32_t, Field*>& field_map)
413 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700414
Brian Carlstrom4873d462011-08-21 15:23:39 -0700415 size_t SizeOfClass(const DexFile& dex_file,
416 const DexFile::ClassDef& dex_class_def);
417
Brian Carlstromf615a612011-07-23 12:50:34 -0700418 void LoadClass(const DexFile& dex_file,
419 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700420 SirtRef<Class>& klass,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700421 ClassLoader* class_loader)
422 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700423
Ian Rogers0571d352011-11-03 19:51:38 -0700424 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700425 SirtRef<Field>& dst);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700426
Ian Rogers0571d352011-11-03 19:51:38 -0700427 void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700428 SirtRef<Class>& klass, SirtRef<Method>& dst)
429 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700430
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700431 void FixupStaticTrampolines(Class* klass)
432 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800433
434 // Finds the associated oat class for a dex_file and descriptor
435 const OatFile::OatClass* GetOatClass(const DexFile& dex_file, const char* descriptor);
436
Brian Carlstrom07bb8552012-01-18 22:10:50 -0800437 // Attempts to insert a class into a class table. Returns NULL if
438 // the class was inserted, otherwise returns an existing class with
439 // the same descriptor and ClassLoader.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700440 Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class)
441 LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
442 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700443
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700444 void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
445 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
446 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700447 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448 void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
449 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700450
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451 bool InitializeClass(Class* klass, bool can_run_clinit, bool can_init_statics)
452 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromd1422f82011-09-28 11:37:09 -0700453 bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700454 bool ValidateSuperClassDescriptors(const Class* klass)
455 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
456 bool InitializeSuperClass(Class* klass, bool can_run_clinit, bool can_init_fields)
457 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0045a292012-03-31 21:08:41 -0700458 // Initialize static fields, returns true if fields were initialized.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 bool InitializeStaticFields(Class* klass)
460 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700461
Ian Rogers595799e2012-01-11 17:32:51 -0800462 bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
463 const Class* klass1,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 const Class* klass2)
465 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700466
Ian Rogers595799e2012-01-11 17:32:51 -0800467 bool IsSameMethodSignatureInDifferentClassContexts(const Method* descriptor,
468 const Class* klass1,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700469 const Class* klass2)
470 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700471
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
473 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700474
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700475 bool LinkSuperClass(SirtRef<Class>& klass)
476 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700477
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file)
479 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700480
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700481 bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
482 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700483
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484 bool LinkVirtualMethods(SirtRef<Class>& klass)
485 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700486
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700487 bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
488 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700489
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700490 bool LinkStaticFields(SirtRef<Class>& klass)
491 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
492 bool LinkInstanceFields(SirtRef<Class>& klass)
493 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
494 bool LinkFields(SirtRef<Class>& klass, bool is_static)
495 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700496
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700497
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 void CreateReferenceInstanceOffsets(SirtRef<Class>& klass)
499 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
500 void CreateReferenceStaticOffsets(SirtRef<Class>& klass)
501 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700502 void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700503 uint32_t reference_offsets)
504 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700505
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700506 // For use by ImageWriter to find DexCaches for its roots
507 const std::vector<DexCache*>& GetDexCaches() {
508 return dex_caches_;
509 }
510
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
512 LOCKS_EXCLUDED(dex_lock_);
513 const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location)
514 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
515 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
516 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
Brian Carlstromafe25512012-06-27 17:02:58 -0700517 const DexFile* VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
518 const std::string& dex_location,
519 uint32_t dex_location_checksum)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
521 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromfad71432011-10-16 20:25:10 -0700522
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700523 Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class)
524 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
525 Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype)
526 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400527
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700528 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700529
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 mutable Mutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700531 std::vector<const DexFile*> dex_files_ GUARDED_BY(dex_lock_);
532 std::vector<DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
533 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700534
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535
Brian Carlstromaded5f72011-10-07 17:15:04 -0700536 // multimap from a string hash code of a class descriptor to
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700537 // Class* instances. Results should be compared for a matching
538 // Class::descriptor_ and Class::class_loader_.
Elliott Hughesf8349362012-06-18 15:00:06 -0700539 typedef std::multimap<size_t, Class*> Table;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700540 Table image_classes_ GUARDED_BY(GlobalSynchronization::classlinker_classes_lock_);
541 Table classes_ GUARDED_BY(GlobalSynchronization::classlinker_classes_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700542
543 Class* LookupClassLocked(const char* descriptor, const ClassLoader* class_loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700544 size_t hash, const Table& classes)
545 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
546 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::classlinker_classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700547
Brian Carlstroma663ea52011-08-19 23:33:41 -0700548 // indexes into class_roots_.
549 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700550 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700551 kJavaLangClass,
552 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700553 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700554 kObjectArrayClass,
555 kJavaLangString,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700556 kJavaLangRefReference,
Elliott Hughes80609252011-09-23 17:24:51 -0700557 kJavaLangReflectConstructor,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700558 kJavaLangReflectField,
559 kJavaLangReflectMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700560 kJavaLangReflectProxy,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700561 kJavaLangClassLoader,
Ian Rogers5167c972012-02-03 10:41:20 -0800562 kJavaLangThrowable,
jeffhao8cd6dda2012-02-22 10:15:34 -0800563 kJavaLangClassNotFoundException,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700564 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700565 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700566 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700567 kPrimitiveChar,
568 kPrimitiveDouble,
569 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700570 kPrimitiveInt,
571 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700572 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700573 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700574 kBooleanArrayClass,
575 kByteArrayClass,
576 kCharArrayClass,
577 kDoubleArrayClass,
578 kFloatArrayClass,
579 kIntArrayClass,
580 kLongArrayClass,
581 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700582 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700583 kClassRootsMax,
584 };
585 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700586
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700587 Class* GetClassRoot(ClassRoot class_root)
588 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700589 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700590 Class* klass = class_roots_->Get(class_root);
591 DCHECK(klass != NULL);
592 return klass;
593 }
594
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700595 void SetClassRoot(ClassRoot class_root, Class* klass)
596 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700597
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700598 ObjectArray<Class>* GetClassRoots() {
599 DCHECK(class_roots_ != NULL);
600 return class_roots_;
601 }
602
Elliott Hughes418d20f2011-09-22 14:00:39 -0700603 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700604
605 const char* GetClassRootDescriptor(ClassRoot class_root) {
606 const char* descriptor = class_roots_descriptors_[class_root];
607 CHECK(descriptor != NULL);
608 return descriptor;
609 }
610
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700611 ObjectArray<InterfaceEntry>* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700612
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700613 bool init_done_;
614
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700615 InternTable* intern_table_;
616
Brian Carlstromf734cf52011-08-17 16:28:14 -0700617 friend class CommonTest;
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700618 friend class ImageWriter; // for GetClassRoots
Brian Carlstromae826982011-11-09 01:33:42 -0800619 friend class ObjectTest;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800620 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Brian Carlstromae826982011-11-09 01:33:42 -0800621 FRIEND_TEST(DexCacheTest, Open);
622 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
623 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700624 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
625};
626
627} // namespace art
628
629#endif // ART_SRC_CLASS_LINKER_H_