• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   */
16  
17  #include "oat_file.h"
18  
19  #include <dlfcn.h>
20  #include <sstream>
21  #include <string.h>
22  #include <unistd.h>
23  
24  #include "base/bit_vector.h"
25  #include "base/stl_util.h"
26  #include "base/unix_file/fd_file.h"
27  #include "elf_file.h"
28  #include "elf_utils.h"
29  #include "oat.h"
30  #include "mirror/art_method.h"
31  #include "mirror/art_method-inl.h"
32  #include "mirror/class.h"
33  #include "mirror/object-inl.h"
34  #include "os.h"
35  #include "runtime.h"
36  #include "utils.h"
37  #include "vmap_table.h"
38  
39  namespace art {
40  
CheckLocation(const std::string & location)41  void OatFile::CheckLocation(const std::string& location) {
42    CHECK(!location.empty());
43  }
44  
OpenWithElfFile(ElfFile * elf_file,const std::string & location,std::string * error_msg)45  OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
46                                    const std::string& location,
47                                    std::string* error_msg) {
48    std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
49    oat_file->elf_file_.reset(elf_file);
50    Elf32_Shdr* hdr = elf_file->FindSectionByName(".rodata");
51    oat_file->begin_ = elf_file->Begin() + hdr->sh_offset;
52    oat_file->end_ = elf_file->Begin() + hdr->sh_size + hdr->sh_offset;
53    return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
54  }
55  
OpenMemory(std::vector<uint8_t> & oat_contents,const std::string & location,std::string * error_msg)56  OatFile* OatFile::OpenMemory(std::vector<uint8_t>& oat_contents,
57                               const std::string& location,
58                               std::string* error_msg) {
59    CHECK(!oat_contents.empty()) << location;
60    CheckLocation(location);
61    std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
62    oat_file->begin_ = &oat_contents[0];
63    oat_file->end_ = &oat_contents[oat_contents.size()];
64    return oat_file->Setup(error_msg) ? oat_file.release() : nullptr;
65  }
66  
Open(const std::string & filename,const std::string & location,byte * requested_base,bool executable,std::string * error_msg)67  OatFile* OatFile::Open(const std::string& filename,
68                         const std::string& location,
69                         byte* requested_base,
70                         bool executable,
71                         std::string* error_msg) {
72    CHECK(!filename.empty()) << location;
73    CheckLocation(filename);
74    std::unique_ptr<OatFile> ret;
75    if (kUsePortableCompiler && executable) {
76      // If we are using PORTABLE, use dlopen to deal with relocations.
77      //
78      // We use our own ELF loader for Quick to deal with legacy apps that
79      // open a generated dex file by name, remove the file, then open
80      // another generated dex file with the same name. http://b/10614658
81      ret.reset(OpenDlopen(filename, location, requested_base, error_msg));
82    } else {
83      // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
84      //
85      // On target, dlopen may fail when compiling due to selinux restrictions on installd.
86      //
87      // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
88      // This won't work for portable runtime execution because it doesn't process relocations.
89      std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
90      if (file.get() == NULL) {
91        *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
92        return nullptr;
93      }
94      ret.reset(OpenElfFile(file.get(), location, requested_base, false, executable, error_msg));
95  
96      // It would be nice to unlink here. But we might have opened the file created by the
97      // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API
98      // to allow removal when we know the ELF must be borked.
99    }
100    return ret.release();
101  }
102  
OpenWritable(File * file,const std::string & location,std::string * error_msg)103  OatFile* OatFile::OpenWritable(File* file, const std::string& location, std::string* error_msg) {
104    CheckLocation(location);
105    return OpenElfFile(file, location, NULL, true, false, error_msg);
106  }
107  
OpenReadable(File * file,const std::string & location,std::string * error_msg)108  OatFile* OatFile::OpenReadable(File* file, const std::string& location, std::string* error_msg) {
109    CheckLocation(location);
110    return OpenElfFile(file, location, NULL, false, false, error_msg);
111  }
112  
OpenDlopen(const std::string & elf_filename,const std::string & location,byte * requested_base,std::string * error_msg)113  OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
114                               const std::string& location,
115                               byte* requested_base,
116                               std::string* error_msg) {
117    std::unique_ptr<OatFile> oat_file(new OatFile(location, true));
118    bool success = oat_file->Dlopen(elf_filename, requested_base, error_msg);
119    if (!success) {
120      return nullptr;
121    }
122    return oat_file.release();
123  }
124  
OpenElfFile(File * file,const std::string & location,byte * requested_base,bool writable,bool executable,std::string * error_msg)125  OatFile* OatFile::OpenElfFile(File* file,
126                                const std::string& location,
127                                byte* requested_base,
128                                bool writable,
129                                bool executable,
130                                std::string* error_msg) {
131    std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
132    bool success = oat_file->ElfFileOpen(file, requested_base, writable, executable, error_msg);
133    if (!success) {
134      CHECK(!error_msg->empty());
135      return nullptr;
136    }
137    return oat_file.release();
138  }
139  
OatFile(const std::string & location,bool is_executable)140  OatFile::OatFile(const std::string& location, bool is_executable)
141      : location_(location), begin_(NULL), end_(NULL), is_executable_(is_executable),
142        dlopen_handle_(NULL),
143        secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
144    CHECK(!location_.empty());
145  }
146  
~OatFile()147  OatFile::~OatFile() {
148    STLDeleteElements(&oat_dex_files_storage_);
149    if (dlopen_handle_ != NULL) {
150      dlclose(dlopen_handle_);
151    }
152  }
153  
Dlopen(const std::string & elf_filename,byte * requested_base,std::string * error_msg)154  bool OatFile::Dlopen(const std::string& elf_filename, byte* requested_base,
155                       std::string* error_msg) {
156    char* absolute_path = realpath(elf_filename.c_str(), NULL);
157    if (absolute_path == NULL) {
158      *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
159      return false;
160    }
161    dlopen_handle_ = dlopen(absolute_path, RTLD_NOW);
162    free(absolute_path);
163    if (dlopen_handle_ == NULL) {
164      *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
165      return false;
166    }
167    begin_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatdata"));
168    if (begin_ == NULL) {
169      *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
170                                dlerror());
171      return false;
172    }
173    if (requested_base != NULL && begin_ != requested_base) {
174      *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
175                                "oatdata=%p != expected=%p /proc/self/maps:\n",
176                                begin_, requested_base);
177      ReadFileToString("/proc/self/maps", error_msg);
178      return false;
179    }
180    end_ = reinterpret_cast<byte*>(dlsym(dlopen_handle_, "oatlastword"));
181    if (end_ == NULL) {
182      *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
183                                dlerror());
184      return false;
185    }
186    // Readjust to be non-inclusive upper bound.
187    end_ += sizeof(uint32_t);
188    return Setup(error_msg);
189  }
190  
ElfFileOpen(File * file,byte * requested_base,bool writable,bool executable,std::string * error_msg)191  bool OatFile::ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
192                            std::string* error_msg) {
193    elf_file_.reset(ElfFile::Open(file, writable, true, error_msg));
194    if (elf_file_.get() == nullptr) {
195      DCHECK(!error_msg->empty());
196      return false;
197    }
198    bool loaded = elf_file_->Load(executable, error_msg);
199    if (!loaded) {
200      DCHECK(!error_msg->empty());
201      return false;
202    }
203    begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
204    if (begin_ == NULL) {
205      *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
206      return false;
207    }
208    if (requested_base != NULL && begin_ != requested_base) {
209      *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
210                                "oatdata=%p != expected=%p /proc/self/maps:\n",
211                                begin_, requested_base);
212      ReadFileToString("/proc/self/maps", error_msg);
213      return false;
214    }
215    end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
216    if (end_ == NULL) {
217      *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
218      return false;
219    }
220    // Readjust to be non-inclusive upper bound.
221    end_ += sizeof(uint32_t);
222    return Setup(error_msg);
223  }
224  
Setup(std::string * error_msg)225  bool OatFile::Setup(std::string* error_msg) {
226    if (!GetOatHeader().IsValid()) {
227      *error_msg = StringPrintf("Invalid oat magic for '%s'", GetLocation().c_str());
228      return false;
229    }
230    const byte* oat = Begin();
231    oat += sizeof(OatHeader);
232    if (oat > End()) {
233      *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
234      return false;
235    }
236  
237    oat += GetOatHeader().GetKeyValueStoreSize();
238    if (oat > End()) {
239      *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
240                                "%p + %zd + %ud <= %p", GetLocation().c_str(),
241                                Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
242                                End());
243      return false;
244    }
245  
246    uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
247    oat_dex_files_storage_.reserve(dex_file_count);
248    for (size_t i = 0; i < dex_file_count; i++) {
249      uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
250      if (UNLIKELY(dex_file_location_size == 0U)) {
251        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
252                                  GetLocation().c_str(), i);
253        return false;
254      }
255      oat += sizeof(dex_file_location_size);
256      if (UNLIKELY(oat > End())) {
257        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
258                                  "location size", GetLocation().c_str(), i);
259        return false;
260      }
261  
262      const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
263      oat += dex_file_location_size;
264      if (UNLIKELY(oat > End())) {
265        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
266                                  "location", GetLocation().c_str(), i);
267        return false;
268      }
269  
270      std::string dex_file_location(dex_file_location_data, dex_file_location_size);
271  
272      uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
273      oat += sizeof(dex_file_checksum);
274      if (UNLIKELY(oat > End())) {
275        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
276                                  "dex file checksum", GetLocation().c_str(), i,
277                                  dex_file_location.c_str());
278        return false;
279      }
280  
281      uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
282      if (UNLIKELY(dex_file_offset == 0U)) {
283        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
284                                  "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
285        return false;
286      }
287      if (UNLIKELY(dex_file_offset > Size())) {
288        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
289                                  "offset %ud > %zd", GetLocation().c_str(), i,
290                                  dex_file_location.c_str(), dex_file_offset, Size());
291        return false;
292      }
293      oat += sizeof(dex_file_offset);
294      if (UNLIKELY(oat > End())) {
295        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
296                                  " after dex file offsets", GetLocation().c_str(), i,
297                                  dex_file_location.c_str());
298        return false;
299      }
300  
301      const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
302      if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
303        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
304                                  " dex file magic '%s'", GetLocation().c_str(), i,
305                                  dex_file_location.c_str(), dex_file_pointer);
306        return false;
307      }
308      if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
309        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
310                                  " dex file version '%s'", GetLocation().c_str(), i,
311                                  dex_file_location.c_str(), dex_file_pointer);
312        return false;
313      }
314      const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
315      const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
316  
317      oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
318      if (UNLIKELY(oat > End())) {
319        *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
320                                  " method offsets", GetLocation().c_str(), i,
321                                  dex_file_location.c_str());
322        return false;
323      }
324  
325      std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
326  
327      // Create the OatDexFile and add it to the owning container.
328      OatDexFile* oat_dex_file = new OatDexFile(this,
329                                                dex_file_location,
330                                                canonical_location,
331                                                dex_file_checksum,
332                                                dex_file_pointer,
333                                                methods_offsets_pointer);
334      oat_dex_files_storage_.push_back(oat_dex_file);
335  
336      // Add the location and canonical location (if different) to the oat_dex_files_ table.
337      StringPiece key(oat_dex_file->GetDexFileLocation());
338      oat_dex_files_.Put(key, oat_dex_file);
339      if (canonical_location != dex_file_location) {
340        StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
341        oat_dex_files_.Put(canonical_key, oat_dex_file);
342      }
343    }
344    return true;
345  }
346  
GetOatHeader() const347  const OatHeader& OatFile::GetOatHeader() const {
348    return *reinterpret_cast<const OatHeader*>(Begin());
349  }
350  
Begin() const351  const byte* OatFile::Begin() const {
352    CHECK(begin_ != NULL);
353    return begin_;
354  }
355  
End() const356  const byte* OatFile::End() const {
357    CHECK(end_ != NULL);
358    return end_;
359  }
360  
GetOatDexFile(const char * dex_location,const uint32_t * dex_location_checksum,bool warn_if_not_found) const361  const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
362                                                    const uint32_t* dex_location_checksum,
363                                                    bool warn_if_not_found) const {
364    // NOTE: We assume here that the canonical location for a given dex_location never
365    // changes. If it does (i.e. some symlink used by the filename changes) we may return
366    // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
367    // an identical file or fail; otherwise we may see some unpredictable failures.
368  
369    // TODO: Additional analysis of usage patterns to see if this can be simplified
370    // without any performance loss, for example by not doing the first lock-free lookup.
371  
372    const OatFile::OatDexFile* oat_dex_file = nullptr;
373    StringPiece key(dex_location);
374    // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
375    // directly mentioned in the oat file and doesn't require locking.
376    auto primary_it = oat_dex_files_.find(key);
377    if (primary_it != oat_dex_files_.end()) {
378      oat_dex_file = primary_it->second;
379      DCHECK(oat_dex_file != nullptr);
380    } else {
381      // This dex_location is not one of the dex locations directly mentioned in the
382      // oat file. The correct lookup is via the canonical location but first see in
383      // the secondary_oat_dex_files_ whether we've looked up this location before.
384      MutexLock mu(Thread::Current(), secondary_lookup_lock_);
385      auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
386      if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
387        oat_dex_file = secondary_lb->second;  // May be nullptr.
388      } else {
389        // We haven't seen this dex_location before, we must check the canonical location.
390        std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
391        if (dex_canonical_location != dex_location) {
392          StringPiece canonical_key(dex_canonical_location);
393          auto canonical_it = oat_dex_files_.find(canonical_key);
394          if (canonical_it != oat_dex_files_.end()) {
395            oat_dex_file = canonical_it->second;
396          }  // else keep nullptr.
397        }  // else keep nullptr.
398  
399        // Copy the key to the string_cache_ and store the result in secondary map.
400        string_cache_.emplace_back(key.data(), key.length());
401        StringPiece key_copy(string_cache_.back());
402        secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
403      }
404    }
405    if (oat_dex_file != nullptr &&
406        (dex_location_checksum == nullptr ||
407         oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
408      return oat_dex_file;
409    }
410  
411    if (warn_if_not_found) {
412      std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
413      std::string checksum("<unspecified>");
414      if (dex_location_checksum != NULL) {
415        checksum = StringPrintf("0x%08x", *dex_location_checksum);
416      }
417      LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
418                   << " ( canonical path " << dex_canonical_location << ")"
419                   << " with checksum " << checksum << " in OatFile " << GetLocation();
420      if (kIsDebugBuild) {
421        for (const OatDexFile* odf : oat_dex_files_storage_) {
422          LOG(WARNING) << "OatFile " << GetLocation()
423                       << " contains OatDexFile " << odf->GetDexFileLocation()
424                       << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
425                       << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
426        }
427      }
428    }
429  
430    return NULL;
431  }
432  
OatDexFile(const OatFile * oat_file,const std::string & dex_file_location,const std::string & canonical_dex_file_location,uint32_t dex_file_location_checksum,const byte * dex_file_pointer,const uint32_t * oat_class_offsets_pointer)433  OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
434                                  const std::string& dex_file_location,
435                                  const std::string& canonical_dex_file_location,
436                                  uint32_t dex_file_location_checksum,
437                                  const byte* dex_file_pointer,
438                                  const uint32_t* oat_class_offsets_pointer)
439      : oat_file_(oat_file),
440        dex_file_location_(dex_file_location),
441        canonical_dex_file_location_(canonical_dex_file_location),
442        dex_file_location_checksum_(dex_file_location_checksum),
443        dex_file_pointer_(dex_file_pointer),
444        oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
445  
~OatDexFile()446  OatFile::OatDexFile::~OatDexFile() {}
447  
FileSize() const448  size_t OatFile::OatDexFile::FileSize() const {
449    return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
450  }
451  
OpenDexFile(std::string * error_msg) const452  const DexFile* OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
453    return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
454                         dex_file_location_checksum_, error_msg);
455  }
456  
GetOatClassOffset(uint16_t class_def_index) const457  uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
458    return oat_class_offsets_pointer_[class_def_index];
459  }
460  
GetOatClass(uint16_t class_def_index) const461  OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
462    uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
463  
464    const byte* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
465    CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
466  
467    const byte* status_pointer = oat_class_pointer;
468    CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
469    mirror::Class::Status status =
470        static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
471    CHECK_LT(status, mirror::Class::kStatusMax);
472  
473    const byte* type_pointer = status_pointer + sizeof(uint16_t);
474    CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
475    OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
476    CHECK_LT(type, kOatClassMax);
477  
478    const byte* after_type_pointer = type_pointer + sizeof(int16_t);
479    CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
480  
481    uint32_t bitmap_size = 0;
482    const byte* bitmap_pointer = nullptr;
483    const byte* methods_pointer = nullptr;
484    if (type == kOatClassSomeCompiled) {
485      bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
486      bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
487      CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
488      methods_pointer = bitmap_pointer + bitmap_size;
489    } else {
490      methods_pointer = after_type_pointer;
491    }
492    CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
493  
494    return OatClass(oat_file_,
495                    status,
496                    type,
497                    bitmap_size,
498                    reinterpret_cast<const uint32_t*>(bitmap_pointer),
499                    reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
500  }
501  
OatClass(const OatFile * oat_file,mirror::Class::Status status,OatClassType type,uint32_t bitmap_size,const uint32_t * bitmap_pointer,const OatMethodOffsets * methods_pointer)502  OatFile::OatClass::OatClass(const OatFile* oat_file,
503                              mirror::Class::Status status,
504                              OatClassType type,
505                              uint32_t bitmap_size,
506                              const uint32_t* bitmap_pointer,
507                              const OatMethodOffsets* methods_pointer)
508      : oat_file_(oat_file), status_(status), type_(type),
509        bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
510      CHECK(methods_pointer != nullptr);
511      switch (type_) {
512        case kOatClassAllCompiled: {
513          CHECK_EQ(0U, bitmap_size);
514          CHECK(bitmap_pointer == nullptr);
515          break;
516        }
517        case kOatClassSomeCompiled: {
518          CHECK_NE(0U, bitmap_size);
519          CHECK(bitmap_pointer != nullptr);
520          break;
521        }
522        case kOatClassNoneCompiled: {
523          CHECK_EQ(0U, bitmap_size);
524          CHECK(bitmap_pointer == nullptr);
525          methods_pointer_ = nullptr;
526          break;
527        }
528        case kOatClassMax: {
529          LOG(FATAL) << "Invalid OatClassType " << type_;
530          break;
531        }
532      }
533  }
534  
GetOatMethodOffsetsOffset(uint32_t method_index) const535  uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
536    const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
537    if (oat_method_offsets == nullptr) {
538      return 0u;
539    }
540    return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
541  }
542  
GetOatMethodOffsets(uint32_t method_index) const543  const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
544    // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index.
545    if (methods_pointer_ == nullptr) {
546      CHECK_EQ(kOatClassNoneCompiled, type_);
547      return nullptr;
548    }
549    size_t methods_pointer_index;
550    if (bitmap_ == nullptr) {
551      CHECK_EQ(kOatClassAllCompiled, type_);
552      methods_pointer_index = method_index;
553    } else {
554      CHECK_EQ(kOatClassSomeCompiled, type_);
555      if (!BitVector::IsBitSet(bitmap_, method_index)) {
556        return nullptr;
557      }
558      size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
559      methods_pointer_index = num_set_bits;
560    }
561    const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
562    return &oat_method_offsets;
563  }
564  
GetOatMethod(uint32_t method_index) const565  const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
566    const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
567    if (oat_method_offsets == nullptr) {
568      return OatMethod(nullptr, 0, 0);
569    }
570    if (oat_file_->IsExecutable() ||
571        Runtime::Current() == nullptr ||        // This case applies for oatdump.
572        Runtime::Current()->IsCompiler()) {
573      return OatMethod(
574          oat_file_->Begin(),
575          oat_method_offsets->code_offset_,
576          oat_method_offsets->gc_map_offset_);
577    } else {
578      // We aren't allowed to use the compiled code. We just force it down the interpreted version.
579      return OatMethod(oat_file_->Begin(), 0, 0);
580    }
581  }
582  
OatMethod(const byte * base,const uint32_t code_offset,const uint32_t gc_map_offset)583  OatFile::OatMethod::OatMethod(const byte* base,
584                                const uint32_t code_offset,
585                                const uint32_t gc_map_offset)
586    : begin_(base),
587      code_offset_(code_offset),
588      native_gc_map_offset_(gc_map_offset) {
589  }
590  
~OatMethod()591  OatFile::OatMethod::~OatMethod() {}
592  
LinkMethod(mirror::ArtMethod * method) const593  void OatFile::OatMethod::LinkMethod(mirror::ArtMethod* method) const {
594    CHECK(method != NULL);
595  #if defined(ART_USE_PORTABLE_COMPILER)
596    method->SetEntryPointFromPortableCompiledCode(GetPortableCode());
597  #endif
598    method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
599    method->SetNativeGcMap(GetNativeGcMap());
600  }
601  
602  }  // namespace art
603