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