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 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 158 | // Returns true on success, false if there's an exception pending. |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 159 | // can_run_clinit=false allows the compiler to attempt to init a class, |
| 160 | // given the restriction that no <clinit> execution is possible. |
| 161 | bool EnsureInitialized(Class* c, bool can_run_clinit); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 162 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 163 | // Initializes classes that have instances in the image but that have |
| 164 | // <clinit> methods so they could not be initialized by the compiler. |
| 165 | void RunRootClinits(); |
| 166 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 167 | void RegisterDexFile(const DexFile& dex_file); |
| 168 | void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 169 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 170 | const std::vector<const DexFile*>& GetBootClassPath() { |
| 171 | return boot_class_path_; |
| 172 | } |
| 173 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 174 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 175 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 176 | const DexFile& FindDexFile(const DexCache* dex_cache) const; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 177 | DexCache* FindDexCache(const DexFile& dex_file) const; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 178 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 179 | // 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] | 180 | template <class T> |
| 181 | ObjectArray<T>* AllocObjectArray(size_t length) { |
| 182 | return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length); |
| 183 | } |
| 184 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 185 | ObjectArray<Class>* AllocClassArray(size_t length) { |
| 186 | return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length); |
| 187 | } |
| 188 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 189 | ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length); |
| 190 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 191 | void VerifyClass(Class* klass); |
| 192 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 193 | private: |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 194 | ClassLinker(InternTable*); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 195 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 196 | // Initialize class linker by bootstraping from dex files |
| 197 | void Init(const std::string& boot_class_path); |
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 from one or more images. |
| 200 | void InitFromImage(); |
| 201 | OatFile* OpenOat(const Space* space); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 202 | static void InitFromImageCallback(Object* obj, void* arg); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 203 | struct InitFromImageCallbackState; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 204 | |
| 205 | void FinishInit(); |
| 206 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 207 | // For early bootstrapping by Init |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 208 | Class* AllocClass(Class* java_lang_Class, size_t class_size); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 209 | |
| 210 | // Alloc* convenience functions to avoid needing to pass in Class* |
| 211 | // values that are known to the ClassLinker such as |
| 212 | // kObjectArrayClass and kJavaLangString etc. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 213 | Class* AllocClass(size_t class_size); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 214 | DexCache* AllocDexCache(const DexFile& dex_file); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 215 | Field* AllocField(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 216 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 217 | Method* AllocMethod(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 218 | |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 219 | CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 220 | InterfaceEntry* AllocInterfaceEntry(Class* interface); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 221 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 222 | Class* CreatePrimitiveClass(const char* descriptor, |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 223 | Class::PrimitiveType type) { |
| 224 | return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type); |
| 225 | } |
| 226 | Class* InitializePrimitiveClass(Class* primitive_class, |
| 227 | const char* descriptor, |
| 228 | Class::PrimitiveType type); |
| 229 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 230 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 231 | Class* CreateArrayClass(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 232 | const ClassLoader* class_loader); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 233 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 234 | void AppendToBootClassPath(const DexFile& dex_file); |
| 235 | void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 236 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 237 | void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 238 | Class* c, std::map<int, Field*>& field_map); |
| 239 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 240 | size_t SizeOfClass(const DexFile& dex_file, |
| 241 | const DexFile::ClassDef& dex_class_def); |
| 242 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 243 | void LoadClass(const DexFile& dex_file, |
| 244 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 245 | Class* klass, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 246 | const ClassLoader* class_loader); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 247 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 248 | void LoadInterfaces(const DexFile& dex_file, |
| 249 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 250 | Class *klass); |
| 251 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 252 | void LoadField(const DexFile& dex_file, |
| 253 | const DexFile::Field& dex_field, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 254 | Class* klass, |
| 255 | Field* dst); |
| 256 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 257 | void LoadMethod(const DexFile& dex_file, |
| 258 | const DexFile::Method& dex_method, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 259 | Class* klass, |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 260 | Method* dst); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 261 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 262 | // Inserts a class into the class table. Returns true if the class |
| 263 | // was inserted. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 264 | bool InsertClass(const StringPiece& descriptor, Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 265 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 266 | bool InitializeClass(Class* klass, bool can_run_clinit); |
| 267 | bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 268 | bool ValidateSuperClassDescriptors(const Class* klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 269 | bool InitializeSuperClass(Class* klass, bool can_run_clinit); |
| 270 | void InitializeStaticFields(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 271 | |
| 272 | bool HasSameDescriptorClasses(const char* descriptor, |
| 273 | const Class* klass1, |
| 274 | const Class* klass2); |
| 275 | |
| 276 | bool HasSameMethodDescriptorClasses(const Method* descriptor, |
| 277 | const Class* klass1, |
| 278 | const Class* klass2); |
| 279 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 280 | bool LinkClass(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 281 | |
| 282 | bool LinkSuperClass(Class* klass); |
| 283 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 284 | bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 285 | |
| 286 | bool LinkMethods(Class* klass); |
| 287 | |
| 288 | bool LinkVirtualMethods(Class* klass); |
| 289 | |
| 290 | bool LinkInterfaceMethods(Class* klass); |
| 291 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 292 | bool LinkStaticFields(Class* klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 293 | bool LinkInstanceFields(Class* klass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 294 | bool LinkFields(Class *klass, bool instance); |
| 295 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 296 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 297 | void CreateReferenceInstanceOffsets(Class* klass); |
| 298 | void CreateReferenceStaticOffsets(Class* klass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 299 | void CreateReferenceOffsets(Class *klass, bool instance, |
| 300 | uint32_t reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 301 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 302 | // For use by ImageWriter to find DexCaches for its roots |
| 303 | const std::vector<DexCache*>& GetDexCaches() { |
| 304 | return dex_caches_; |
| 305 | } |
| 306 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 307 | // lock to protect ClassLinker state |
| 308 | mutable Mutex lock_; |
| 309 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 310 | std::vector<const OatFile*> oat_files_; |
| 311 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 312 | std::vector<const DexFile*> boot_class_path_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 313 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 314 | std::vector<const DexFile*> dex_files_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 315 | std::vector<DexCache*> dex_caches_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 316 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 317 | // multimap from a StringPiece hash code of a class descriptor to |
| 318 | // Class* instances. Results should be compared for a matching |
| 319 | // Class::descriptor_ and Class::class_loader_. |
| 320 | typedef std::tr1::unordered_multimap<size_t, Class*> Table; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 321 | Table classes_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 322 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 323 | // indexes into class_roots_. |
| 324 | // needs to be kept in sync with class_roots_descriptors_. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 325 | enum ClassRoot { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 326 | kJavaLangClass, |
| 327 | kJavaLangObject, |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 328 | kClassArrayClass, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 329 | kObjectArrayClass, |
| 330 | kJavaLangString, |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 331 | kJavaLangReflectConstructor, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 332 | kJavaLangReflectField, |
| 333 | kJavaLangReflectMethod, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 334 | kJavaLangClassLoader, |
| 335 | kDalvikSystemBaseDexClassLoader, |
| 336 | kDalvikSystemPathClassLoader, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 337 | kJavaLangStackTraceElement, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 338 | kPrimitiveBoolean, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 339 | kPrimitiveByte, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 340 | kPrimitiveChar, |
| 341 | kPrimitiveDouble, |
| 342 | kPrimitiveFloat, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 343 | kPrimitiveInt, |
| 344 | kPrimitiveLong, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 345 | kPrimitiveShort, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 346 | kPrimitiveVoid, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 347 | kBooleanArrayClass, |
| 348 | kByteArrayClass, |
| 349 | kCharArrayClass, |
| 350 | kDoubleArrayClass, |
| 351 | kFloatArrayClass, |
| 352 | kIntArrayClass, |
| 353 | kLongArrayClass, |
| 354 | kShortArrayClass, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 355 | kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 356 | kClassRootsMax, |
| 357 | }; |
| 358 | ObjectArray<Class>* class_roots_; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 359 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 360 | Class* GetClassRoot(ClassRoot class_root) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 361 | DCHECK(class_roots_ != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 362 | Class* klass = class_roots_->Get(class_root); |
| 363 | DCHECK(klass != NULL); |
| 364 | return klass; |
| 365 | } |
| 366 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 367 | void SetClassRoot(ClassRoot class_root, Class* klass) { |
| 368 | DCHECK(!init_done_); |
| 369 | |
| 370 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 371 | DCHECK(klass->GetClassLoader() == NULL); |
| 372 | DCHECK(klass->GetDescriptor() != NULL); |
| 373 | DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root))); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 374 | |
| 375 | DCHECK(class_roots_ != NULL); |
| 376 | DCHECK(class_roots_->Get(class_root) == NULL); |
| 377 | class_roots_->Set(class_root, klass); |
| 378 | } |
| 379 | |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame^] | 380 | ObjectArray<Class>* GetClassRoots() { |
| 381 | DCHECK(class_roots_ != NULL); |
| 382 | return class_roots_; |
| 383 | } |
| 384 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 385 | static const char* class_roots_descriptors_[]; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 386 | |
| 387 | const char* GetClassRootDescriptor(ClassRoot class_root) { |
| 388 | const char* descriptor = class_roots_descriptors_[class_root]; |
| 389 | CHECK(descriptor != NULL); |
| 390 | return descriptor; |
| 391 | } |
| 392 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 393 | ObjectArray<Class>* array_interfaces_; |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 394 | ObjectArray<InterfaceEntry>* array_iftable_; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 395 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 396 | bool init_done_; |
| 397 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 398 | InternTable* intern_table_; |
| 399 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 400 | friend class CommonTest; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 401 | FRIEND_TEST(DexCacheTest, Open); |
| 402 | friend class ObjectTest; |
| 403 | FRIEND_TEST(ObjectTest, AllocObjectArray); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 404 | FRIEND_TEST(ExceptionTest, FindExceptionHandler); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame^] | 405 | friend class ImageWriter; // for GetClassRoots |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 406 | DISALLOW_COPY_AND_ASSIGN(ClassLinker); |
| 407 | }; |
| 408 | |
| 409 | } // namespace art |
| 410 | |
| 411 | #endif // ART_SRC_CLASS_LINKER_H_ |