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