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 | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 28 | #include "object.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 29 | #include "unordered_map.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 30 | #include "unordered_set.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 31 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 32 | #include "gtest/gtest.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 36 | class ClassLoader; |
| 37 | class InternTable; |
| 38 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 39 | class ClassLinker { |
| 40 | public: |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 41 | // Initializes the class linker using DexFiles and an optional an image. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 42 | static ClassLinker* Create(const std::vector<const DexFile*>& boot_class_path, |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 43 | const std::vector<const DexFile*>& class_path, |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 44 | InternTable* intern_table, bool image); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 46 | ~ClassLinker(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 47 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 48 | // Finds a class by its descriptor, loading it if necessary. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 49 | // If class_loader is null, searches boot_class_path_. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 50 | Class* FindClass(const StringPiece& descriptor, const ClassLoader* class_loader); |
| 51 | |
| 52 | // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded |
| 53 | // by the given 'class_loader'. |
| 54 | Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 55 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 56 | Class* FindPrimitiveClass(char type); |
| 57 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 58 | Class* FindSystemClass(const StringPiece& descriptor) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 59 | return FindClass(descriptor, NULL); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 62 | void DumpAllClasses(int flags) const; |
| 63 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 64 | size_t NumLoadedClasses() const; |
| 65 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 66 | // Resolve a String with the given index from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 67 | // result in the DexCache. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 68 | 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] | 69 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 70 | // Resolve a Type with the given index from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 71 | // result in the DexCache. The referrer is used to identity the |
| 72 | // target DexCache and ClassLoader to use for resolution. |
| 73 | Class* ResolveType(const DexFile& dex_file, |
| 74 | uint32_t type_idx, |
| 75 | const Class* referrer) { |
| 76 | return ResolveType(dex_file, |
| 77 | type_idx, |
| 78 | referrer->GetDexCache(), |
| 79 | referrer->GetClassLoader()); |
| 80 | } |
| 81 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 82 | // Resolve a Type with the given index from the DexFile, storing the |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 83 | // result in the DexCache. The referrer is used to identify the |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 84 | // target DexCache and ClassLoader to use for resolution. |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 85 | Class* ResolveType(uint32_t type_idx, const Method* referrer) { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 86 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 87 | DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 88 | // TODO: we could check for a dex cache hit here |
| 89 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 90 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 91 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 92 | } |
| 93 | |
| 94 | Class* ResolveType(uint32_t type_idx, const Field* referrer) { |
| 95 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 96 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 97 | // TODO: we could check for a dex cache hit here |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 98 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 99 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 100 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 101 | } |
| 102 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 103 | // Resolve a type with the given ID from the DexFile, storing the |
| 104 | // result in DexCache. The ClassLoader is used to search for the |
| 105 | // type, since it may be referenced from but not contained within |
| 106 | // the given DexFile. |
| 107 | Class* ResolveType(const DexFile& dex_file, |
| 108 | uint32_t type_idx, |
| 109 | DexCache* dex_cache, |
| 110 | const ClassLoader* class_loader); |
| 111 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 112 | static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx, |
| 113 | const Method* referrer); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 114 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 115 | // Resolve a method with a given ID from the DexFile, storing the |
| 116 | // result in DexCache. The ClassLinker and ClassLoader are used as |
| 117 | // in ResolveType. What is unique is the method type argument which |
| 118 | // is used to determine if this method is a direct, static, or |
| 119 | // virtual method. |
| 120 | Method* ResolveMethod(const DexFile& dex_file, |
| 121 | uint32_t method_idx, |
| 122 | DexCache* dex_cache, |
| 123 | const ClassLoader* class_loader, |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 124 | bool is_direct); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 125 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 126 | Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) { |
| 127 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 128 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 129 | // TODO: we could check for a dex cache hit here |
| 130 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 131 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 132 | return ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct); |
| 133 | } |
| 134 | |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 135 | Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) { |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 136 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 137 | DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 138 | // TODO: we could check for a dex cache hit here |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 139 | const ClassLoader* class_loader = declaring_class->GetClassLoader(); |
| 140 | const DexFile& dex_file = FindDexFile(dex_cache); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 141 | return ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 144 | // Resolve a field with a given ID from the DexFile, storing the |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 145 | // result in DexCache. The ClassLinker and ClassLoader are used as |
| 146 | // in ResolveType. What is unique is the is_static argument which is |
| 147 | // used to determine if we are resolving a static or non-static |
| 148 | // field. |
| 149 | Field* ResolveField(const DexFile& dex_file, |
| 150 | uint32_t field_idx, |
| 151 | DexCache* dex_cache, |
| 152 | const ClassLoader* class_loader, |
| 153 | bool is_static); |
| 154 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 155 | // Returns true on success, false if there's an exception pending. |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 156 | // can_run_clinit=false allows the compiler to attempt to init a class, |
| 157 | // given the restriction that no <clinit> execution is possible. |
| 158 | bool EnsureInitialized(Class* c, bool can_run_clinit); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 159 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame^] | 160 | // Initializes classes that have instances in the image but that have |
| 161 | // <clinit> methods so they could not be initialized by the compiler. |
| 162 | void RunRootClinits(); |
| 163 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 164 | void RegisterDexFile(const DexFile& dex_file); |
| 165 | void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 166 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 167 | const std::vector<const DexFile*>& GetBootClassPath() { |
| 168 | return boot_class_path_; |
| 169 | } |
| 170 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 171 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 172 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 173 | const DexFile& FindDexFile(const DexCache* dex_cache) const; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 174 | DexCache* FindDexCache(const DexFile& dex_file) const; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 175 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 176 | // 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] | 177 | template <class T> |
| 178 | ObjectArray<T>* AllocObjectArray(size_t length) { |
| 179 | return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length); |
| 180 | } |
| 181 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 182 | ObjectArray<Class>* AllocClassArray(size_t length) { |
| 183 | return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length); |
| 184 | } |
| 185 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 186 | ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length); |
| 187 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 188 | void VerifyClass(Class* klass); |
| 189 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 190 | private: |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 191 | ClassLinker(InternTable*); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 192 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 193 | // Initialize class linker from DexFile instances. |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 194 | void Init(const std::vector<const DexFile*>& boot_class_path_, |
| 195 | const std::vector<const DexFile*>& class_path_); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 196 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 197 | // Initialize class linker from pre-initialized image. |
| 198 | void InitFromImage(const std::vector<const DexFile*>& boot_class_path_, |
| 199 | const std::vector<const DexFile*>& class_path_); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 200 | static void InitFromImageCallback(Object* obj, void* arg); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 201 | struct InitFromImageCallbackState; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 202 | |
| 203 | void FinishInit(); |
| 204 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 205 | bool InitializeClass(Class* klass, bool can_run_clinit); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 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 | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 266 | bool InitializeSuperClass(Class* klass, bool can_run_clinit); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 267 | |
| 268 | void InitializeStaticFields(Class* klass); |
| 269 | |
| 270 | bool ValidateSuperClassDescriptors(const Class* klass); |
| 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 | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 302 | // lock to protect ClassLinker state |
| 303 | mutable Mutex lock_; |
| 304 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 305 | std::vector<const DexFile*> boot_class_path_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 306 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 307 | std::vector<const DexFile*> dex_files_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 308 | std::vector<DexCache*> dex_caches_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 309 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 310 | // multimap from a StringPiece hash code of a class descriptor to |
| 311 | // Class* instances. Results should be compared for a matching |
| 312 | // Class::descriptor_ and Class::class_loader_. |
| 313 | typedef std::tr1::unordered_multimap<size_t, Class*> Table; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 314 | Table classes_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 315 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 316 | // indexes into class_roots_. |
| 317 | // needs to be kept in sync with class_roots_descriptors_. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 318 | enum ClassRoot { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 319 | kJavaLangClass, |
| 320 | kJavaLangObject, |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 321 | kClassArrayClass, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 322 | kObjectArrayClass, |
| 323 | kJavaLangString, |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 324 | kJavaLangReflectConstructor, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 325 | kJavaLangReflectField, |
| 326 | kJavaLangReflectMethod, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 327 | kJavaLangClassLoader, |
| 328 | kDalvikSystemBaseDexClassLoader, |
| 329 | kDalvikSystemPathClassLoader, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 330 | kJavaLangStackTraceElement, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 331 | kPrimitiveBoolean, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 332 | kPrimitiveByte, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 333 | kPrimitiveChar, |
| 334 | kPrimitiveDouble, |
| 335 | kPrimitiveFloat, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 336 | kPrimitiveInt, |
| 337 | kPrimitiveLong, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 338 | kPrimitiveShort, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 339 | kPrimitiveVoid, |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 340 | kBooleanArrayClass, |
| 341 | kByteArrayClass, |
| 342 | kCharArrayClass, |
| 343 | kDoubleArrayClass, |
| 344 | kFloatArrayClass, |
| 345 | kIntArrayClass, |
| 346 | kLongArrayClass, |
| 347 | kShortArrayClass, |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 348 | kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 349 | kClassRootsMax, |
| 350 | }; |
| 351 | ObjectArray<Class>* class_roots_; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 352 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 353 | Class* GetClassRoot(ClassRoot class_root) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 354 | DCHECK(class_roots_ != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 355 | Class* klass = class_roots_->Get(class_root); |
| 356 | DCHECK(klass != NULL); |
| 357 | return klass; |
| 358 | } |
| 359 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 360 | void SetClassRoot(ClassRoot class_root, Class* klass) { |
| 361 | DCHECK(!init_done_); |
| 362 | |
| 363 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 364 | DCHECK(klass->GetClassLoader() == NULL); |
| 365 | DCHECK(klass->GetDescriptor() != NULL); |
| 366 | DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root))); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 367 | |
| 368 | DCHECK(class_roots_ != NULL); |
| 369 | DCHECK(class_roots_->Get(class_root) == NULL); |
| 370 | class_roots_->Set(class_root, klass); |
| 371 | } |
| 372 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 373 | static const char* class_roots_descriptors_[]; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 374 | |
| 375 | const char* GetClassRootDescriptor(ClassRoot class_root) { |
| 376 | const char* descriptor = class_roots_descriptors_[class_root]; |
| 377 | CHECK(descriptor != NULL); |
| 378 | return descriptor; |
| 379 | } |
| 380 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 381 | ObjectArray<Class>* array_interfaces_; |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 382 | ObjectArray<InterfaceEntry>* array_iftable_; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 383 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 384 | bool init_done_; |
| 385 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 386 | InternTable* intern_table_; |
| 387 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 388 | friend class CommonTest; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 389 | FRIEND_TEST(DexCacheTest, Open); |
| 390 | friend class ObjectTest; |
| 391 | FRIEND_TEST(ObjectTest, AllocObjectArray); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 392 | FRIEND_TEST(ExceptionTest, FindExceptionHandler); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 393 | DISALLOW_COPY_AND_ASSIGN(ClassLinker); |
| 394 | }; |
| 395 | |
| 396 | } // namespace art |
| 397 | |
| 398 | #endif // ART_SRC_CLASS_LINKER_H_ |