class_linker.h revision 8532191c4fc8e005f7d432fec40fd9f2936275c6
1418d20fc407052d4152157f61e7453359f902383Elliott Hughes/*
2418d20fc407052d4152157f61e7453359f902383Elliott Hughes * Copyright (C) 2011 The Android Open Source Project
3418d20fc407052d4152157f61e7453359f902383Elliott Hughes *
4418d20fc407052d4152157f61e7453359f902383Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5418d20fc407052d4152157f61e7453359f902383Elliott Hughes * you may not use this file except in compliance with the License.
6418d20fc407052d4152157f61e7453359f902383Elliott Hughes * You may obtain a copy of the License at
7418d20fc407052d4152157f61e7453359f902383Elliott Hughes *
8418d20fc407052d4152157f61e7453359f902383Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9418d20fc407052d4152157f61e7453359f902383Elliott Hughes *
10418d20fc407052d4152157f61e7453359f902383Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11418d20fc407052d4152157f61e7453359f902383Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12418d20fc407052d4152157f61e7453359f902383Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13418d20fc407052d4152157f61e7453359f902383Elliott Hughes * See the License for the specific language governing permissions and
14418d20fc407052d4152157f61e7453359f902383Elliott Hughes * limitations under the License.
15418d20fc407052d4152157f61e7453359f902383Elliott Hughes */
160e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
170e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#ifndef ART_SRC_CLASS_LINKER_H_
180e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#define ART_SRC_CLASS_LINKER_H_
190e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
200e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#include <map>
216d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include <string>
220e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#include <utility>
230e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#include <vector>
240e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
25caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers#include "dex_cache.h"
267e93b50433cde2a44d99212e8040299bde498546Brian Carlstrom#include "dex_file.h"
27e5448b5a12003b405b22cde3b94f962ab4888a87Elliott Hughes#include "gtest/gtest.h"
284a96b60e45fba4a9d4a2e9c8fc849660eacef684Brian Carlstrom#include "heap.h"
29578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "macros.h"
308daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "mutex.h"
3158ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom#include "oat_file.h"
32578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "object.h"
3340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom#include "stack_indirect_reference_table.h"
340e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
350e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapironamespace art {
360e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
37cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughesclass ClassLoader;
3830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass ImageSpace;
39cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughesclass InternTable;
40d1422f81bf9b35cb2aad3fb5615d3f5209014709Brian Carlstromclass ObjectLock;
41cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes
42a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughestypedef bool (ClassVisitor)(Class* c, void* arg);
43a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
440e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiroclass ClassLinker {
450e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro public:
4658ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  // Creates the class linker by boot strapping from dex files.
47a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  static ClassLinker* CreateFromCompiler(const std::vector<const DexFile*>& boot_class_path,
48a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom                                         InternTable* intern_table);
4958ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom
50a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  // Creates the class linker from an image.
51a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  static ClassLinker* CreateFromImage(InternTable* intern_table);
529ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
539ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  ~ClassLinker();
54565f50731db360584d0080af7f14f0b7ca10371cCarl Shapiro
5564bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes  // Finds a class by its descriptor, loading it if necessary.
5674eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom  // If class_loader is null, searches boot_class_path_.
57c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughes  Class* FindClass(const char* descriptor, const ClassLoader* class_loader);
58aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
59db7d5e96d3e4090e69fac7b4ec1fc1bdcc0b436cElliott Hughes  Class* FindSystemClass(const char* descriptor);
60aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
61aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  // Define a new a class based on a ClassDef from a DexFile
62c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughes  Class* DefineClass(const StringPiece& descriptor, const ClassLoader* class_loader,
63aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom                     const DexFile& dex_file, const DexFile::ClassDef& dex_class_def);
6464bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes
6564bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes  // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
6664bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes  // by the given 'class_loader'.
67c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughes  Class* LookupClass(const char* descriptor, const ClassLoader* class_loader);
680e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
696fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  // Finds all the classes with the given descriptor, regardless of ClassLoader.
70c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughes  void LookupClasses(const char* descriptor, std::vector<Class*>& classes);
716fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes
72d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes  Class* FindPrimitiveClass(char type);
73d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes
74ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // General class unloading is not supported, this is used to prune
75ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // unwanted classes during image writing.
76c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughes  bool RemoveClass(const char* descriptor, const ClassLoader* class_loader);
77ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
789d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  void DumpAllClasses(int flags) const;
799d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
80cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes  void DumpForSigQuit(std::ostream& os) const;
81cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes
82e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes  size_t NumLoadedClasses() const;
83e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
84b63ec393a5c4ba2be1d34dd871cda811eaa803c7Brian Carlstrom  // Resolve a String with the given index from the DexFile, storing the
85aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  // result in the DexCache. The referrer is used to identify the
86aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  // target DexCache and ClassLoader to use for resolution.
87aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  String* ResolveString(uint32_t string_idx, const Method* referrer) {
88caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
89caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    if (UNLIKELY(resolved_string == NULL)) {
90caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      Class* declaring_class = referrer->GetDeclaringClass();
91caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      DexCache* dex_cache = declaring_class->GetDexCache();
92caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const DexFile& dex_file = FindDexFile(dex_cache);
93caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      resolved_string = ResolveString(dex_file, string_idx, dex_cache);
94caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    }
95caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    return resolved_string;
96aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
97aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
98aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  // Resolve a String with the given index from the DexFile, storing the
999ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // result in the DexCache.
1000cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
1019ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
102b63ec393a5c4ba2be1d34dd871cda811eaa803c7Brian Carlstrom  // Resolve a Type with the given index from the DexFile, storing the
1039ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // result in the DexCache. The referrer is used to identity the
1049ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // target DexCache and ClassLoader to use for resolution.
1059ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  Class* ResolveType(const DexFile& dex_file,
1066d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers                     uint16_t type_idx,
1079ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                     const Class* referrer) {
1089ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom    return ResolveType(dex_file,
1099ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                       type_idx,
1109ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                       referrer->GetDexCache(),
1119ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                       referrer->GetClassLoader());
1129ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  }
1139ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
114b63ec393a5c4ba2be1d34dd871cda811eaa803c7Brian Carlstrom  // Resolve a Type with the given index from the DexFile, storing the
1150cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  // result in the DexCache. The referrer is used to identify the
116b63ec393a5c4ba2be1d34dd871cda811eaa803c7Brian Carlstrom  // target DexCache and ClassLoader to use for resolution.
1176d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  Class* ResolveType(uint16_t type_idx, const Method* referrer) {
118caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
119caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    if (UNLIKELY(resolved_type == NULL)) {
120caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      Class* declaring_class = referrer->GetDeclaringClass();
121caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      DexCache* dex_cache = declaring_class->GetDexCache();
122caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const ClassLoader* class_loader = declaring_class->GetClassLoader();
123caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const DexFile& dex_file = FindDexFile(dex_cache);
124caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
125caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    }
126caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    return resolved_type;
1270cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers  }
1280cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
1296d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  Class* ResolveType(uint16_t type_idx, const Field* referrer) {
1300cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    Class* declaring_class = referrer->GetDeclaringClass();
1310cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    DexCache* dex_cache = declaring_class->GetDexCache();
132caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    Class* resolved_type = dex_cache->GetResolvedType(type_idx);
133caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    if (UNLIKELY(resolved_type == NULL)) {
134caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const ClassLoader* class_loader = declaring_class->GetClassLoader();
135caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const DexFile& dex_file = FindDexFile(dex_cache);
136caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
137caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    }
138caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    return resolved_type;
139b63ec393a5c4ba2be1d34dd871cda811eaa803c7Brian Carlstrom  }
140b63ec393a5c4ba2be1d34dd871cda811eaa803c7Brian Carlstrom
1419ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // Resolve a type with the given ID from the DexFile, storing the
1429ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // result in DexCache. The ClassLoader is used to search for the
1439ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // type, since it may be referenced from but not contained within
1449ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // the given DexFile.
1459ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  Class* ResolveType(const DexFile& dex_file,
1466d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers                     uint16_t type_idx,
1479ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                     DexCache* dex_cache,
1489ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                     const ClassLoader* class_loader);
1499ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
1509ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // Resolve a method with a given ID from the DexFile, storing the
1519ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // result in DexCache. The ClassLinker and ClassLoader are used as
1529ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // in ResolveType. What is unique is the method type argument which
1539ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // is used to determine if this method is a direct, static, or
1549ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // virtual method.
1559ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  Method* ResolveMethod(const DexFile& dex_file,
1569ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                        uint32_t method_idx,
1579ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                        DexCache* dex_cache,
1589ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                        const ClassLoader* class_loader,
15920cfffabdc9e02b2df798bc4e6b6035d14bf4e36Brian Carlstrom                        bool is_direct);
1609ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
161161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom  Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
1621984651929744dd603fd082e23eacd877b9bc177Ian Rogers    Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
1631984651929744dd603fd082e23eacd877b9bc177Ian Rogers    if (UNLIKELY(resolved_method == NULL || resolved_method->IsRuntimeMethod())) {
1641984651929744dd603fd082e23eacd877b9bc177Ian Rogers      Class* declaring_class = referrer->GetDeclaringClass();
1651984651929744dd603fd082e23eacd877b9bc177Ian Rogers      DexCache* dex_cache = declaring_class->GetDexCache();
166caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const ClassLoader* class_loader = declaring_class->GetClassLoader();
167caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const DexFile& dex_file = FindDexFile(dex_cache);
168caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, is_direct);
169caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    }
170caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    return resolved_method;
171161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom  }
172161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom
173845490bda68f7d025ea7f45775c847d2932e00dcBrian Carlstrom  Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
174b5d6a4983732387dd9fe9f0024d19a766819755dIan Rogers    Field* resolved_field =
175b5d6a4983732387dd9fe9f0024d19a766819755dIan Rogers        referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
176caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    if (UNLIKELY(resolved_field == NULL)) {
177b5d6a4983732387dd9fe9f0024d19a766819755dIan Rogers      Class* declaring_class = referrer->GetDeclaringClass();
178b5d6a4983732387dd9fe9f0024d19a766819755dIan Rogers      DexCache* dex_cache = declaring_class->GetDexCache();
179caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const ClassLoader* class_loader = declaring_class->GetClassLoader();
180caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      const DexFile& dex_file = FindDexFile(dex_cache);
181caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers      resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
182caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    }
183caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers    return resolved_field;
184b9edb841423dfe60e193fcffd25994398c91baa2Brian Carlstrom  }
185b9edb841423dfe60e193fcffd25994398c91baa2Brian Carlstrom
186161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom  // Resolve a field with a given ID from the DexFile, storing the
1879ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // result in DexCache. The ClassLinker and ClassLoader are used as
1889ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // in ResolveType. What is unique is the is_static argument which is
1899ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // used to determine if we are resolving a static or non-static
1909ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  // field.
1919ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  Field* ResolveField(const DexFile& dex_file,
1929ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                      uint32_t field_idx,
1939ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                      DexCache* dex_cache,
1949ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                      const ClassLoader* class_loader,
1959ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                      bool is_static);
1969ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom
197b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers  Field* ResolveFieldJLS(uint32_t field_idx, const Method* referrer) {
198b5d6a4983732387dd9fe9f0024d19a766819755dIan Rogers    Field* resolved_field =
199b5d6a4983732387dd9fe9f0024d19a766819755dIan Rogers        referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
200b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers    if (UNLIKELY(resolved_field == NULL)) {
201b5d6a4983732387dd9fe9f0024d19a766819755dIan Rogers      Class* declaring_class = referrer->GetDeclaringClass();
202b5d6a4983732387dd9fe9f0024d19a766819755dIan Rogers      DexCache* dex_cache = declaring_class->GetDexCache();
203b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers      const ClassLoader* class_loader = declaring_class->GetClassLoader();
204b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers      const DexFile& dex_file = FindDexFile(dex_cache);
205b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers      resolved_field = ResolveFieldJLS(dex_file, field_idx, dex_cache, class_loader);
206b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers    }
207b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers    return resolved_field;
208b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers  }
209b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers
210b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers  // Resolve a field with a given ID from the DexFile, storing the
211b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers  // result in DexCache. The ClassLinker and ClassLoader are used as
212b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers  // in ResolveType. No is_static argument is provided so that Java
213b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers  // field resolution semantics are followed.
214b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers  Field* ResolveFieldJLS(const DexFile& dex_file,
215b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers                         uint32_t field_idx,
216b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers                         DexCache* dex_cache,
217b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers                         const ClassLoader* class_loader);
218b067ac2fe225c76fc9eb9434ef62cbb797bfa425Ian Rogers
219ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers  // Get shorty from method index without resolution. Used to do handlerization.
2201984651929744dd603fd082e23eacd877b9bc177Ian Rogers  const char* MethodShorty(uint32_t method_idx, Method* referrer, uint32_t* length);
221ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers
222f4c21c9f6440c3980c47a297519f758796dbc039Elliott Hughes  // Returns true on success, false if there's an exception pending.
22325c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom  // can_run_clinit=false allows the compiler to attempt to init a class,
22425c3325bf95036bf325fc7cb21b4fd6d40282857Brian Carlstrom  // given the restriction that no <clinit> execution is possible.
2250045a290e6b79a274250e3112880c04bde437d4aIan Rogers  bool EnsureInitialized(Class* c, bool can_run_clinit, bool can_init_fields);
2260e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
2272a20cfd0b7fc81099f5de0da782ebcc1cb262792Elliott Hughes  // Initializes classes that have instances in the image but that have
2282a20cfd0b7fc81099f5de0da782ebcc1cb262792Elliott Hughes  // <clinit> methods so they could not be initialized by the compiler.
2292a20cfd0b7fc81099f5de0da782ebcc1cb262792Elliott Hughes  void RunRootClinits();
2302a20cfd0b7fc81099f5de0da782ebcc1cb262792Elliott Hughes
2319ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  void RegisterDexFile(const DexFile& dex_file);
23240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
2330e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
234866c8627626b7c47fb69b498b341772d7dfa7f60Brian Carlstrom  void RegisterOatFile(const OatFile& oat_file);
235866c8627626b7c47fb69b498b341772d7dfa7f60Brian Carlstrom
2368a48741b96ca9cc5835cac72ac133c4ca480930fBrian Carlstrom  const std::vector<const DexFile*>& GetBootClassPath() {
2378a48741b96ca9cc5835cac72ac133c4ca480930fBrian Carlstrom    return boot_class_path_;
2388a48741b96ca9cc5835cac72ac133c4ca480930fBrian Carlstrom  }
2398a48741b96ca9cc5835cac72ac133c4ca480930fBrian Carlstrom
240a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  void VisitClasses(ClassVisitor* visitor, void* arg) const;
241a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
242410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes  void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
24375cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom
244c143c55718342519db5398e41dda31422cf16c79buzbee  const DexFile& FindDexFile(const DexCache* dex_cache) const;
2459ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  DexCache* FindDexCache(const DexFile& dex_file) const;
246aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  bool IsDexFileRegistered(const DexFile& dex_file) const;
2471984651929744dd603fd082e23eacd877b9bc177Ian Rogers  void FixupDexCaches(Method* resolution_method) const;
2481d9f52b7ca91c6d30b7acfac1c9ab24d93fff470Brian Carlstrom
249262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao  // Generate an oat file from a dex file
250d601af8d9549ca95a911afb2fc67c4e562da661fBrian Carlstrom  bool GenerateOatFile(const std::string& dex_filename,
251d601af8d9549ca95a911afb2fc67c4e562da661fBrian Carlstrom                       int oat_fd,
252d601af8d9549ca95a911afb2fc67c4e562da661fBrian Carlstrom                       const std::string& oat_cache_filename);
253262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao
254ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  const OatFile* FindOatFileFromOatLocation(const std::string& location);
255c143c55718342519db5398e41dda31422cf16c79buzbee
2565b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  // Finds the oat file for a dex location, generating the oat file if
2575b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  // it is missing or out of date. Returns the DexFile from within the
2585b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  // created oat file.
2595b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  const DexFile* FindOrCreateOatFileForDexLocation(const std::string& dex_location,
2605b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom                                                   const std::string& oat_location);
2615b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  // Find a DexFile within an OatFile given a DexFile location. Note
2625b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  // that this returns null if the location checksum of the DexFile
2635b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  // does not match the OatFile.
2645b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location);
2655b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom
266f6174e8a1566bb357e82506f7ec97dc359c90eb2jeffhao
267418d20fc407052d4152157f61e7453359f902383Elliott Hughes  // TODO: replace this with multiple methods that allocate the correct managed type.
2684417536522fd2a9d8215d8672331984769c9520bShih-wei Liao  template <class T>
2694417536522fd2a9d8215d8672331984769c9520bShih-wei Liao  ObjectArray<T>* AllocObjectArray(size_t length) {
2704417536522fd2a9d8215d8672331984769c9520bShih-wei Liao    return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
2714417536522fd2a9d8215d8672331984769c9520bShih-wei Liao  }
2724417536522fd2a9d8215d8672331984769c9520bShih-wei Liao
273418d20fc407052d4152157f61e7453359f902383Elliott Hughes  ObjectArray<Class>* AllocClassArray(size_t length) {
274418d20fc407052d4152157f61e7453359f902383Elliott Hughes    return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
275418d20fc407052d4152157f61e7453359f902383Elliott Hughes  }
276418d20fc407052d4152157f61e7453359f902383Elliott Hughes
27755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao  ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
27855df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao
27998eacac683b78e60799323e8c7d59e7214808639jeffhao  void VerifyClass(Class* klass);
280634eb2eb14f87753519d0ef2c5f256e55888f378Elliott Hughes  bool VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass,
281634eb2eb14f87753519d0ef2c5f256e55888f378Elliott Hughes                               Class::Status& oat_file_class_status);
282e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass);
283e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* klass);
28498eacac683b78e60799323e8c7d59e7214808639jeffhao
28595caa791e560da97363c0c0d22bfda4a7e7377c3Jesse Wilson  Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
2866d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers                          ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
2876d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string GetDescriptorForProxy(const Class* proxy_class);
28816f93671923979733cb46fc6f053c8f65754d5ebIan Rogers  Method* FindMethodForProxy(const Class* proxy_class, const Method* proxy_method);
28995caa791e560da97363c0c0d22bfda4a7e7377c3Jesse Wilson
2901984651929744dd603fd082e23eacd877b9bc177Ian Rogers  // Get the oat code for a method when its class isn't yet initialized
2911984651929744dd603fd082e23eacd877b9bc177Ian Rogers  const void* GetOatCodeFor(const Method* method);
2921984651929744dd603fd082e23eacd877b9bc177Ian Rogers
2938532191c4fc8e005f7d432fec40fd9f2936275c6TDYa  void LinkOatCodeFor(Method* method);
2948532191c4fc8e005f7d432fec40fd9f2936275c6TDYa
2950c717dd1c56bd29cf860d0feda8e629dab2cadb3Logan Chien  // Relocate the OatFiles (ELF images)
2960c717dd1c56bd29cf860d0feda8e629dab2cadb3Logan Chien  void RelocateExecutable();
2970c717dd1c56bd29cf860d0feda8e629dab2cadb3Logan Chien
29847d237a4b1a41772005c60083e72fe75c0aa0743Brian Carlstrom  pid_t GetClassesLockOwner(); // For SignalCatcher.
29947d237a4b1a41772005c60083e72fe75c0aa0743Brian Carlstrom  pid_t GetDexLockOwner(); // For SignalCatcher.
30024a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom
3010e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro private:
3024dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  explicit ClassLinker(InternTable*);
30361e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
3048532191c4fc8e005f7d432fec40fd9f2936275c6TDYa  const OatFile::OatMethod GetOatMethodFor(const Method* method);
3058532191c4fc8e005f7d432fec40fd9f2936275c6TDYa
30658ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  // Initialize class linker by bootstraping from dex files
307a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path);
30861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
30958ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  // Initialize class linker from one or more images.
31058ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  void InitFromImage();
31130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  OatFile* OpenOat(const ImageSpace* space);
31278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  static void InitFromImageCallback(Object* obj, void* arg);
313c74255fffb035001304c9a058a2e730a5a1a9604Brian Carlstrom  struct InitFromImageCallbackState;
314a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom
315a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom  void FinishInit();
316a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom
31775cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  // For early bootstrapping by Init
3184873d465a1eb6dfbdeddb085c81239d39db60c42Brian Carlstrom  Class* AllocClass(Class* java_lang_Class, size_t class_size);
31975cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom
32075cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  // Alloc* convenience functions to avoid needing to pass in Class*
32175cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  // values that are known to the ClassLinker such as
32275cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  // kObjectArrayClass and kJavaLangString etc.
3234873d465a1eb6dfbdeddb085c81239d39db60c42Brian Carlstrom  Class* AllocClass(size_t class_size);
3249ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  DexCache* AllocDexCache(const DexFile& dex_file);
32535baaab2a79014f35e225b189f6dbec1b4ba9542Jesse Wilson  Field* AllocField();
326bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
32775cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  Method* AllocMethod();
328bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
3294b620ffb1b4d0c96a94bb3afe314f35d53990ec6Brian Carlstrom  InterfaceEntry* AllocInterfaceEntry(Class* interface);
33075cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom
3316b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom  Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
3325b8e4c810a97c9dc417142b8c6e07871ae15c797Brian Carlstrom    return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
3335b8e4c810a97c9dc417142b8c6e07871ae15c797Brian Carlstrom  }
3345b8e4c810a97c9dc417142b8c6e07871ae15c797Brian Carlstrom  Class* InitializePrimitiveClass(Class* primitive_class,
3355b8e4c810a97c9dc417142b8c6e07871ae15c797Brian Carlstrom                                  const char* descriptor,
3366b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom                                  Primitive::Type type);
3375b8e4c810a97c9dc417142b8c6e07871ae15c797Brian Carlstrom
338a331b3cc392132c7333d36649a8310f38f4822ccBrian Carlstrom
339c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughes  Class* CreateArrayClass(const std::string& descriptor, const ClassLoader* class_loader);
340578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom
3419ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom  void AppendToBootClassPath(const DexFile& dex_file);
34240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
343578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom
3445fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
3450571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers                         Class* c, std::map<uint32_t, Field*>& field_map);
3465fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes
3474873d465a1eb6dfbdeddb085c81239d39db60c42Brian Carlstrom  size_t SizeOfClass(const DexFile& dex_file,
3484873d465a1eb6dfbdeddb085c81239d39db60c42Brian Carlstrom                     const DexFile::ClassDef& dex_class_def);
3494873d465a1eb6dfbdeddb085c81239d39db60c42Brian Carlstrom
350f615a61aef972cfc1dc23931ac2ed0da14c3fedbBrian Carlstrom  void LoadClass(const DexFile& dex_file,
351f615a61aef972cfc1dc23931ac2ed0da14c3fedbBrian Carlstrom                 const DexFile::ClassDef& dex_class_def,
35240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom                 SirtRef<Class>& klass,
3539ea1cb1a22be5b85dc2622e3836c46a1c48e3f25Brian Carlstrom                 const ClassLoader* class_loader);
354565f50731db360584d0080af7f14f0b7ca10371cCarl Shapiro
3550571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
35640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom                 SirtRef<Field>& dst);
357934486cf07c578b6494417ca5dcbae89cf04b019Brian Carlstrom
3580571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers  void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
3590571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers                  SirtRef<Class>& klass, SirtRef<Method>& dst);
360934486cf07c578b6494417ca5dcbae89cf04b019Brian Carlstrom
3611984651929744dd603fd082e23eacd877b9bc177Ian Rogers  void FixupStaticTrampolines(Class* klass);
3621984651929744dd603fd082e23eacd877b9bc177Ian Rogers
3631984651929744dd603fd082e23eacd877b9bc177Ian Rogers  // Finds the associated oat class for a dex_file and descriptor
3641984651929744dd603fd082e23eacd877b9bc177Ian Rogers  const OatFile::OatClass* GetOatClass(const DexFile& dex_file, const char* descriptor);
3651984651929744dd603fd082e23eacd877b9bc177Ian Rogers
36607bb855daaad20f2d300418cee5a2d977758a3a1Brian Carlstrom  // Attempts to insert a class into a class table.  Returns NULL if
36707bb855daaad20f2d300418cee5a2d977758a3a1Brian Carlstrom  // the class was inserted, otherwise returns an existing class with
36807bb855daaad20f2d300418cee5a2d977758a3a1Brian Carlstrom  // the same descriptor and ClassLoader.
36907bb855daaad20f2d300418cee5a2d977758a3a1Brian Carlstrom  Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class);
370aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
37140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
372aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
373866c8627626b7c47fb69b498b341772d7dfa7f60Brian Carlstrom  void RegisterOatFileLocked(const OatFile& oat_file);
374aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3750045a290e6b79a274250e3112880c04bde437d4aIan Rogers  bool InitializeClass(Class* klass, bool can_run_clinit, bool can_init_statics);
376d1422f81bf9b35cb2aad3fb5615d3f5209014709Brian Carlstrom  bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
377d1422f81bf9b35cb2aad3fb5615d3f5209014709Brian Carlstrom  bool ValidateSuperClassDescriptors(const Class* klass);
3780045a290e6b79a274250e3112880c04bde437d4aIan Rogers  bool InitializeSuperClass(Class* klass, bool can_run_clinit, bool can_init_fields);
3790045a290e6b79a274250e3112880c04bde437d4aIan Rogers  // Initialize static fields, returns true if fields were initialized.
3800045a290e6b79a274250e3112880c04bde437d4aIan Rogers  bool InitializeStaticFields(Class* klass);
3810e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
382595799e000e95f999a6fd4ac3d32f2f2a16f9947Ian Rogers  bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
383595799e000e95f999a6fd4ac3d32f2f2a16f9947Ian Rogers                                                const Class* klass1,
384595799e000e95f999a6fd4ac3d32f2f2a16f9947Ian Rogers                                                const Class* klass2);
3850e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
386595799e000e95f999a6fd4ac3d32f2f2a16f9947Ian Rogers  bool IsSameMethodSignatureInDifferentClassContexts(const Method* descriptor,
387595799e000e95f999a6fd4ac3d32f2f2a16f9947Ian Rogers                                                     const Class* klass1,
388595799e000e95f999a6fd4ac3d32f2f2a16f9947Ian Rogers                                                     const Class* klass2);
3890e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
390c2b4447ae9c0c1e77595620acac6508999df6698Ian Rogers  bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
3910e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
39240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  bool LinkSuperClass(SirtRef<Class>& klass);
3930e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
39440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
3950e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
3966d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
3970e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
39840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  bool LinkVirtualMethods(SirtRef<Class>& klass);
3990e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
4006d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
4010e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
40240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  bool LinkStaticFields(SirtRef<Class>& klass);
40340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  bool LinkInstanceFields(SirtRef<Class>& klass);
40440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  bool LinkFields(SirtRef<Class>& klass, bool is_static);
4050cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
4064873d465a1eb6dfbdeddb085c81239d39db60c42Brian Carlstrom
40740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
40840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
40940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
4100cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers                              uint32_t reference_offsets);
4110e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
41258ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  // For use by ImageWriter to find DexCaches for its roots
41358ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  const std::vector<DexCache*>& GetDexCaches() {
41458ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom    return dex_caches_;
41558ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  }
41658ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom
417ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file);
418a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location);
419ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location);
420fad714344ca81a21e775ef45ae09d9a035723cafBrian Carlstrom
4216d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class);
4226d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype);
42395caa791e560da97363c0c0d22bfda4a7e7377c3Jesse Wilson
4244a96b60e45fba4a9d4a2e9c8fc849660eacef684Brian Carlstrom  std::vector<const DexFile*> boot_class_path_;
425578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom
4264a96b60e45fba4a9d4a2e9c8fc849660eacef684Brian Carlstrom  std::vector<const DexFile*> dex_files_;
4277e49dca262933bc30cbc8b9f07cfc8cce2343389Brian Carlstrom  std::vector<DexCache*> dex_caches_;
42847d237a4b1a41772005c60083e72fe75c0aa0743Brian Carlstrom  std::vector<const OatFile*> oat_files_;
42947d237a4b1a41772005c60083e72fe75c0aa0743Brian Carlstrom  // lock to protect concurrent access to dex_files_, dex_caches_, and oat_files_
43047d237a4b1a41772005c60083e72fe75c0aa0743Brian Carlstrom  mutable Mutex dex_lock_;
43147d237a4b1a41772005c60083e72fe75c0aa0743Brian Carlstrom
4320e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
433aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  // multimap from a string hash code of a class descriptor to
4349cff8e13d41825c4f3f0127af061e94b06114fc8Brian Carlstrom  // Class* instances. Results should be compared for a matching
4359cff8e13d41825c4f3f0127af061e94b06114fc8Brian Carlstrom  // Class::descriptor_ and Class::class_loader_.
43647d237a4b1a41772005c60083e72fe75c0aa0743Brian Carlstrom  // Protected by classes_lock_
437e5448b5a12003b405b22cde3b94f962ab4888a87Elliott Hughes  typedef std::multimap<size_t, Class*> Table;
43807bb855daaad20f2d300418cee5a2d977758a3a1Brian Carlstrom  Class* LookupClass(const char* descriptor, const ClassLoader* class_loader,
43907bb855daaad20f2d300418cee5a2d977758a3a1Brian Carlstrom                     size_t hash, const Table& classes);
4405d76c435082332ef79a22962386fa92a0870e378Ian Rogers  Table image_classes_;
4410e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  Table classes_;
44247d237a4b1a41772005c60083e72fe75c0aa0743Brian Carlstrom  mutable Mutex classes_lock_;
4430e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
444a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom  // indexes into class_roots_.
445a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom  // needs to be kept in sync with class_roots_descriptors_.
44674eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom  enum ClassRoot {
44775cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kJavaLangClass,
44875cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kJavaLangObject,
449418d20fc407052d4152157f61e7453359f902383Elliott Hughes    kClassArrayClass,
45074eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom    kObjectArrayClass,
45174eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom    kJavaLangString,
452bf61ba3dcd9d9895b86a05e88762ff034b09b263Elliott Hughes    kJavaLangRefReference,
4538060925c45cc2607ab92390d7366c6c0cfdfe4bbElliott Hughes    kJavaLangReflectConstructor,
45475cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kJavaLangReflectField,
45575cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kJavaLangReflectMethod,
456466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers    kJavaLangReflectProxy,
45774eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom    kJavaLangClassLoader,
45874eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom    kDalvikSystemBaseDexClassLoader,
45974eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom    kDalvikSystemPathClassLoader,
4605167c97a4f672ba821453418e3943988fabbfc43Ian Rogers    kJavaLangThrowable,
4618cd6ddaeb78cc904e13bac88753654a04b2e15b8jeffhao    kJavaLangClassNotFoundException,
46255df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao    kJavaLangStackTraceElement,
46375cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kPrimitiveBoolean,
464d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kPrimitiveByte,
46575cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kPrimitiveChar,
46675cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kPrimitiveDouble,
467d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kPrimitiveFloat,
46875cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kPrimitiveInt,
46975cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kPrimitiveLong,
470d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kPrimitiveShort,
47175cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kPrimitiveVoid,
472d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kBooleanArrayClass,
473d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kByteArrayClass,
474d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kCharArrayClass,
475d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kDoubleArrayClass,
476d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kFloatArrayClass,
477d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kIntArrayClass,
478d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kLongArrayClass,
479d8ddfd5eadde1d5f53ef1419f529c799233eaa62Elliott Hughes    kShortArrayClass,
48055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao    kJavaLangStackTraceElementArrayClass,
48175cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom    kClassRootsMax,
48275cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  };
48375cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  ObjectArray<Class>* class_roots_;
484913af1bd90a6e8fd56f1f851db1f098636dae6a5Brian Carlstrom
48574eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom  Class* GetClassRoot(ClassRoot class_root) {
486a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom    DCHECK(class_roots_ != NULL);
48774eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom    Class* klass = class_roots_->Get(class_root);
48874eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom    DCHECK(klass != NULL);
48974eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom    return klass;
49074eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom  }
49174eb46ab15c5b9cb39bf15246db84aea7fd4cc27Brian Carlstrom
4926d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  void SetClassRoot(ClassRoot class_root, Class* klass);
493a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom
49434f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom  ObjectArray<Class>* GetClassRoots() {
49534f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom    DCHECK(class_roots_ != NULL);
49634f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom    return class_roots_;
49734f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom  }
49834f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom
499418d20fc407052d4152157f61e7453359f902383Elliott Hughes  static const char* class_roots_descriptors_[];
500a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom
501a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom  const char* GetClassRootDescriptor(ClassRoot class_root) {
502a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom    const char* descriptor = class_roots_descriptors_[class_root];
503a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom    CHECK(descriptor != NULL);
504a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom    return descriptor;
505a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom  }
506a663ea5de4c9ab6b1510fdebd6d8eca77ba699aeBrian Carlstrom
5074b620ffb1b4d0c96a94bb3afe314f35d53990ec6Brian Carlstrom  ObjectArray<InterfaceEntry>* array_iftable_;
508565f50731db360584d0080af7f14f0b7ca10371cCarl Shapiro
50975cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  bool init_done_;
51075cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom
511cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes  InternTable* intern_table_;
512cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes
513f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom  friend class CommonTest;
514ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  friend class ImageWriter;  // for GetClassRoots
51575cb3b477be3757a0351fb6ab1cb70751a71e2bfBrian Carlstrom  friend class ObjectTest;
5166d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
517ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  FRIEND_TEST(DexCacheTest, Open);
5181a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
519ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  FRIEND_TEST(ObjectTest, AllocObjectArray);
5200e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
5210e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro};
5220e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
5230e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro}  // namespace art
5240e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro
5250e5d75d5ca2b8a44fab0c862276a466cbab39859Carl Shapiro#endif  // ART_SRC_CLASS_LINKER_H_
526