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