Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_CLASS_LINKER_H_ |
| 18 | #define ART_SRC_CLASS_LINKER_H_ |
| 19 | |
| 20 | #include <map> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 24 | #include "dex_cache.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 25 | #include "dex_file.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 26 | #include "heap.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 27 | #include "macros.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | #include "mutex.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 29 | #include "oat_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | #include "object.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 31 | #include "unordered_map.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 32 | #include "unordered_set.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 33 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 34 | #include "gtest/gtest.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 38 | class ClassLoader; |
| 39 | class InternTable; |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 40 | class ObjectLock; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 41 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 42 | class ClassLinker { |
| 43 | public: |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 44 | // Creates the class linker by boot strapping from dex files. |
| 45 | static ClassLinker* Create(const std::string& boot_class_path, InternTable* intern_table); |
| 46 | |
| 47 | // Creates the class linker from one or more images. |
| 48 | static ClassLinker* Create(InternTable* intern_table); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 49 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 50 | ~ClassLinker(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 52 | // Finds a class by its descriptor, loading it if necessary. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 53 | // If class_loader is null, searches boot_class_path_. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 54 | Class* FindClass(const std::string& descriptor, const ClassLoader* class_loader); |
| 55 | |
| 56 | Class* FindSystemClass(const std::string& descriptor) { |
| 57 | return FindClass(descriptor, NULL); |
| 58 | } |
| 59 | |
| 60 | // Define a new a class based on a ClassDef from a DexFile |
| 61 | Class* DefineClass(const std::string& descriptor, const ClassLoader* class_loader, |
| 62 | const DexFile& dex_file, const DexFile::ClassDef& dex_class_def); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 63 | |
| 64 | // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded |
| 65 | // by the given 'class_loader'. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 66 | Class* LookupClass(const std::string& descriptor, const ClassLoader* class_loader); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 68 | Class* FindPrimitiveClass(char type); |
| 69 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 70 | void DumpAllClasses(int flags) const; |
| 71 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 72 | size_t NumLoadedClasses() const; |
| 73 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 74 | // Resolve a String with the given index from the DexFile, storing the |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 75 | // result in the DexCache. The referrer is used to identify the |
| 76 | // target DexCache and ClassLoader to use for resolution. |
| 77 | String* ResolveString(uint32_t string_idx, const Method* referrer) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 78 | String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx); |
| 79 | if (UNLIKELY(resolved_string == NULL)) { |
| 80 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 81 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 82 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 83 | resolved_string = ResolveString(dex_file, string_idx, dex_cache); |
| 84 | } |
| 85 | return resolved_string; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Resolve a String with the given index from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 89 | // result in the DexCache. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 90 | String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 91 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 92 | // Resolve a Type with the given index from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 93 | // result in the DexCache. The referrer is used to identity the |
| 94 | // target DexCache and ClassLoader to use for resolution. |
| 95 | Class* ResolveType(const DexFile& dex_file, |
| 96 | uint32_t type_idx, |
| 97 | const Class* referrer) { |
| 98 | return ResolveType(dex_file, |
| 99 | type_idx, |
| 100 | referrer->GetDexCache(), |
| 101 | referrer->GetClassLoader()); |
| 102 | } |
| 103 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 104 | // Resolve a Type with the given index from the DexFile, storing the |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 105 | // result in the DexCache. The referrer is used to identify the |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 106 | // target DexCache and ClassLoader to use for resolution. |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 107 | Class* ResolveType(uint32_t type_idx, const Method* referrer) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 108 | Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx); |
| 109 | if (UNLIKELY(resolved_type == NULL)) { |
| 110 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 111 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 112 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 113 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 114 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 115 | } |
| 116 | return resolved_type; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | Class* ResolveType(uint32_t type_idx, const Field* referrer) { |
| 120 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 121 | DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 122 | Class* resolved_type = dex_cache->GetResolvedType(type_idx); |
| 123 | if (UNLIKELY(resolved_type == NULL)) { |
| 124 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 125 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 126 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 127 | } |
| 128 | return resolved_type; |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 131 | // Resolve a type with the given ID from the DexFile, storing the |
| 132 | // result in DexCache. The ClassLoader is used to search for the |
| 133 | // type, since it may be referenced from but not contained within |
| 134 | // the given DexFile. |
| 135 | Class* ResolveType(const DexFile& dex_file, |
| 136 | uint32_t type_idx, |
| 137 | DexCache* dex_cache, |
| 138 | const ClassLoader* class_loader); |
| 139 | |
| 140 | // Resolve a method with a given ID from the DexFile, storing the |
| 141 | // result in DexCache. The ClassLinker and ClassLoader are used as |
| 142 | // in ResolveType. What is unique is the method type argument which |
| 143 | // is used to determine if this method is a direct, static, or |
| 144 | // virtual method. |
| 145 | Method* ResolveMethod(const DexFile& dex_file, |
| 146 | uint32_t method_idx, |
| 147 | DexCache* dex_cache, |
| 148 | const ClassLoader* class_loader, |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 149 | bool is_direct); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 150 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 151 | Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 152 | Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx); |
| 153 | if (UNLIKELY(resolved_method == NULL)) { |
| 154 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 155 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 156 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 157 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 158 | resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct); |
| 159 | } |
| 160 | return resolved_method; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 163 | Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 164 | Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx); |
| 165 | if (UNLIKELY(resolved_field == NULL)) { |
| 166 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 167 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 168 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 169 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 170 | resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static); |
| 171 | } |
| 172 | return resolved_field; |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 175 | // Resolve a field with a given ID from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 176 | // result in DexCache. The ClassLinker and ClassLoader are used as |
| 177 | // in ResolveType. What is unique is the is_static argument which is |
| 178 | // used to determine if we are resolving a static or non-static |
| 179 | // field. |
| 180 | Field* ResolveField(const DexFile& dex_file, |
| 181 | uint32_t field_idx, |
| 182 | DexCache* dex_cache, |
| 183 | const ClassLoader* class_loader, |
| 184 | bool is_static); |
| 185 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 186 | // Get shorty from method index without resolution. Used to do handlerization. |
| 187 | const char* MethodShorty(uint32_t method_idx, Method* referrer); |
| 188 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 189 | // Returns true on success, false if there's an exception pending. |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 190 | // can_run_clinit=false allows the compiler to attempt to init a class, |
| 191 | // given the restriction that no <clinit> execution is possible. |
| 192 | bool EnsureInitialized(Class* c, bool can_run_clinit); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 193 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 194 | // Initializes classes that have instances in the image but that have |
| 195 | // <clinit> methods so they could not be initialized by the compiler. |
| 196 | void RunRootClinits(); |
| 197 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 198 | void RegisterDexFile(const DexFile& dex_file); |
| 199 | void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 200 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 201 | const std::vector<const DexFile*>& GetBootClassPath() { |
| 202 | return boot_class_path_; |
| 203 | } |
| 204 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 205 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 206 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 207 | const DexFile& FindDexFile(const DexCache* dex_cache) const; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 208 | DexCache* FindDexCache(const DexFile& dex_file) const; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 209 | bool IsDexFileRegistered(const DexFile& dex_file) const; |
Brian Carlstrom | 1d9f52b | 2011-10-13 10:50:45 -0700 | [diff] [blame^] | 210 | |
| 211 | // Find, possibily opening, an OatFile corresponding to a DexFile |
| 212 | const OatFile* FindOatFile(const DexFile& dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 213 | const OatFile* FindOatFile(const std::string& location); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 214 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 215 | // TODO: replace this with multiple methods that allocate the correct managed type. |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 216 | template <class T> |
| 217 | ObjectArray<T>* AllocObjectArray(size_t length) { |
| 218 | return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length); |
| 219 | } |
| 220 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 221 | ObjectArray<Class>* AllocClassArray(size_t length) { |
| 222 | return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length); |
| 223 | } |
| 224 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 225 | ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length); |
| 226 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 227 | void VerifyClass(Class* klass); |
| 228 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 229 | private: |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 230 | ClassLinker(InternTable*); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 231 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 232 | // Initialize class linker by bootstraping from dex files |
| 233 | void Init(const std::string& boot_class_path); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 234 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 235 | // Initialize class linker from one or more images. |
| 236 | void InitFromImage(); |
| 237 | OatFile* OpenOat(const Space* space); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 238 | static void InitFromImageCallback(Object* obj, void* arg); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 239 | struct InitFromImageCallbackState; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 240 | |
| 241 | void FinishInit(); |
| 242 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 243 | // For early bootstrapping by Init |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 244 | Class* AllocClass(Class* java_lang_Class, size_t class_size); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 245 | |
| 246 | // Alloc* convenience functions to avoid needing to pass in Class* |
| 247 | // values that are known to the ClassLinker such as |
| 248 | // kObjectArrayClass and kJavaLangString etc. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 249 | Class* AllocClass(size_t class_size); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 250 | DexCache* AllocDexCache(const DexFile& dex_file); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 251 | Field* AllocField(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 252 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 253 | Method* AllocMethod(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 254 | |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 255 | CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 256 | InterfaceEntry* AllocInterfaceEntry(Class* interface); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 257 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 258 | Class* CreatePrimitiveClass(const char* descriptor, |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 259 | Class::PrimitiveType type) { |
| 260 | return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type); |
| 261 | } |
| 262 | Class* InitializePrimitiveClass(Class* primitive_class, |
| 263 | const char* descriptor, |
| 264 | Class::PrimitiveType type); |
| 265 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 266 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 267 | Class* CreateArrayClass(const std::string& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 268 | const ClassLoader* class_loader); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 269 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 270 | void AppendToBootClassPath(const DexFile& dex_file); |
| 271 | void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 272 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 273 | void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 274 | Class* c, std::map<int, Field*>& field_map); |
| 275 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 276 | size_t SizeOfClass(const DexFile& dex_file, |
| 277 | const DexFile::ClassDef& dex_class_def); |
| 278 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 279 | void LoadClass(const DexFile& dex_file, |
| 280 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 281 | Class* klass, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 282 | const ClassLoader* class_loader); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 283 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 284 | void LoadInterfaces(const DexFile& dex_file, |
| 285 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 286 | Class *klass); |
| 287 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 288 | void LoadField(const DexFile& dex_file, |
| 289 | const DexFile::Field& dex_field, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 290 | Class* klass, |
| 291 | Field* dst); |
| 292 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 293 | void LoadMethod(const DexFile& dex_file, |
| 294 | const DexFile::Method& dex_method, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 295 | Class* klass, |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 296 | Method* dst); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 297 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 298 | // Inserts a class into the class table. Returns true if the class |
| 299 | // was inserted. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 300 | bool InsertClass(const std::string& descriptor, Class* klass); |
| 301 | |
| 302 | void RegisterDexFileLocked(const DexFile& dex_file, DexCache* dex_cache); |
| 303 | bool IsDexFileRegisteredLocked(const DexFile& dex_file) const; |
| 304 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 305 | bool InitializeClass(Class* klass, bool can_run_clinit); |
| 306 | bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 307 | bool ValidateSuperClassDescriptors(const Class* klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 308 | bool InitializeSuperClass(Class* klass, bool can_run_clinit); |
| 309 | void InitializeStaticFields(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 310 | |
| 311 | bool HasSameDescriptorClasses(const char* descriptor, |
| 312 | const Class* klass1, |
| 313 | const Class* klass2); |
| 314 | |
| 315 | bool HasSameMethodDescriptorClasses(const Method* descriptor, |
| 316 | const Class* klass1, |
| 317 | const Class* klass2); |
| 318 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 319 | bool LinkClass(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 320 | |
| 321 | bool LinkSuperClass(Class* klass); |
| 322 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 323 | bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 324 | |
| 325 | bool LinkMethods(Class* klass); |
| 326 | |
| 327 | bool LinkVirtualMethods(Class* klass); |
| 328 | |
| 329 | bool LinkInterfaceMethods(Class* klass); |
| 330 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 331 | bool LinkStaticFields(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 332 | bool LinkInstanceFields(Class* klass); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 333 | bool LinkFields(Class *klass, bool is_static); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 334 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 335 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 336 | void CreateReferenceInstanceOffsets(Class* klass); |
| 337 | void CreateReferenceStaticOffsets(Class* klass); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 338 | void CreateReferenceOffsets(Class *klass, bool is_static, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 339 | uint32_t reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 340 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 341 | // For use by ImageWriter to find DexCaches for its roots |
| 342 | const std::vector<DexCache*>& GetDexCaches() { |
| 343 | return dex_caches_; |
| 344 | } |
| 345 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 346 | // lock to protect ClassLinker state |
| 347 | mutable Mutex lock_; |
| 348 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 349 | std::vector<const OatFile*> oat_files_; |
| 350 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 351 | std::vector<const DexFile*> boot_class_path_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 352 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 353 | std::vector<const DexFile*> dex_files_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 354 | std::vector<DexCache*> dex_caches_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 355 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 356 | // multimap from a string hash code of a class descriptor to |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 357 | // Class* instances. Results should be compared for a matching |
| 358 | // Class::descriptor_ and Class::class_loader_. |
| 359 | typedef std::tr1::unordered_multimap<size_t, Class*> Table; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 360 | Table classes_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 361 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 362 | // indexes into class_roots_. |
| 363 | // needs to be kept in sync with class_roots_descriptors_. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 364 | enum ClassRoot { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 365 | kJavaLangClass, |
| 366 | kJavaLangObject, |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 367 | kClassArrayClass, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 368 | kObjectArrayClass, |
| 369 | kJavaLangString, |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 370 | kJavaLangRefReference, |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 371 | kJavaLangReflectConstructor, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 372 | kJavaLangReflectField, |
| 373 | kJavaLangReflectMethod, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 374 | kJavaLangClassLoader, |
| 375 | kDalvikSystemBaseDexClassLoader, |
| 376 | kDalvikSystemPathClassLoader, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 377 | kJavaLangStackTraceElement, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 378 | kPrimitiveBoolean, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 379 | kPrimitiveByte, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 380 | kPrimitiveChar, |
| 381 | kPrimitiveDouble, |
| 382 | kPrimitiveFloat, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 383 | kPrimitiveInt, |
| 384 | kPrimitiveLong, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 385 | kPrimitiveShort, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 386 | kPrimitiveVoid, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 387 | kBooleanArrayClass, |
| 388 | kByteArrayClass, |
| 389 | kCharArrayClass, |
| 390 | kDoubleArrayClass, |
| 391 | kFloatArrayClass, |
| 392 | kIntArrayClass, |
| 393 | kLongArrayClass, |
| 394 | kShortArrayClass, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 395 | kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 396 | kClassRootsMax, |
| 397 | }; |
| 398 | ObjectArray<Class>* class_roots_; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 399 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 400 | Class* GetClassRoot(ClassRoot class_root) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 401 | DCHECK(class_roots_ != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 402 | Class* klass = class_roots_->Get(class_root); |
| 403 | DCHECK(klass != NULL); |
| 404 | return klass; |
| 405 | } |
| 406 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 407 | void SetClassRoot(ClassRoot class_root, Class* klass) { |
| 408 | DCHECK(!init_done_); |
| 409 | |
| 410 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 411 | DCHECK(klass->GetClassLoader() == NULL); |
| 412 | DCHECK(klass->GetDescriptor() != NULL); |
| 413 | DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root))); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 414 | |
| 415 | DCHECK(class_roots_ != NULL); |
| 416 | DCHECK(class_roots_->Get(class_root) == NULL); |
| 417 | class_roots_->Set(class_root, klass); |
| 418 | } |
| 419 | |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 420 | ObjectArray<Class>* GetClassRoots() { |
| 421 | DCHECK(class_roots_ != NULL); |
| 422 | return class_roots_; |
| 423 | } |
| 424 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 425 | static const char* class_roots_descriptors_[]; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 426 | |
| 427 | const char* GetClassRootDescriptor(ClassRoot class_root) { |
| 428 | const char* descriptor = class_roots_descriptors_[class_root]; |
| 429 | CHECK(descriptor != NULL); |
| 430 | return descriptor; |
| 431 | } |
| 432 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 433 | ObjectArray<Class>* array_interfaces_; |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 434 | ObjectArray<InterfaceEntry>* array_iftable_; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 435 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 436 | bool init_done_; |
| 437 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 438 | InternTable* intern_table_; |
| 439 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 440 | friend class CommonTest; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 441 | FRIEND_TEST(DexCacheTest, Open); |
| 442 | friend class ObjectTest; |
| 443 | FRIEND_TEST(ObjectTest, AllocObjectArray); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 444 | FRIEND_TEST(ExceptionTest, FindExceptionHandler); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 445 | friend class ImageWriter; // for GetClassRoots |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 446 | DISALLOW_COPY_AND_ASSIGN(ClassLinker); |
| 447 | }; |
| 448 | |
| 449 | } // namespace art |
| 450 | |
| 451 | #endif // ART_SRC_CLASS_LINKER_H_ |