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