summaryrefslogtreecommitdiffstats
path: root/compiler/elf_patcher.h
blob: 0a9f0a013e8b938aae5234bf414d56f1afb80d69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_COMPILER_ELF_PATCHER_H_
#define ART_COMPILER_ELF_PATCHER_H_

#include "base/mutex.h"
#include "driver/compiler_driver.h"
#include "elf_file.h"
#include "mirror/art_method.h"
#include "mirror/class.h"
#include "mirror/object.h"
#include "oat_file.h"
#include "oat.h"
#include "os.h"

namespace art {

class ElfPatcher {
 public:
  typedef void* (*ImageAddressCallback)(void* data, mirror::Object* obj);

  static bool Patch(const CompilerDriver* driver, ElfFile* elf_file,
                    const std::string& oat_location,
                    ImageAddressCallback cb, void* cb_data,
                    std::string* error_msg)
      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);

  static bool Patch(const CompilerDriver* driver, ElfFile* elf_file,
                    const OatFile* oat_file, uintptr_t oat_data_begin,
                    ImageAddressCallback cb, void* cb_data,
                    std::string* error_msg)
      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);

  static bool Patch(const CompilerDriver* driver, ElfFile* elf_file,
                    const std::string& oat_location,
                    std::string* error_msg)
      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    return ElfPatcher::Patch(driver, elf_file, oat_location,
                             DefaultImageAddressCallback, nullptr, error_msg);
  }

  static bool Patch(const CompilerDriver* driver, ElfFile* elf_file,
                    const OatFile* oat_file, uintptr_t oat_data_begin,
                    std::string* error_msg)
      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    return ElfPatcher::Patch(driver, elf_file, oat_file, oat_data_begin,
                             DefaultImageAddressCallback, nullptr, error_msg);
  }

 private:
  ElfPatcher(const CompilerDriver* driver, ElfFile* elf_file, const OatFile* oat_file,
             OatHeader* oat_header, uintptr_t oat_data_begin,
             ImageAddressCallback cb, void* cb_data, std::string* error_msg)
      : compiler_driver_(driver), elf_file_(elf_file), oat_file_(oat_file),
        oat_header_(oat_header), oat_data_begin_(oat_data_begin), get_image_address_(cb),
        cb_data_(cb_data), error_msg_(error_msg),
        write_patches_(compiler_driver_->GetCompilerOptions().GetIncludePatchInformation()) {}
  ~ElfPatcher() {}

  static void* DefaultImageAddressCallback(void* data_unused, mirror::Object* obj) {
    return static_cast<void*>(obj);
  }

  bool PatchElf()
      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);

  mirror::ArtMethod* GetTargetMethod(const CompilerDriver::CallPatchInformation* patch)
      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);

  mirror::Class* GetTargetType(const CompilerDriver::TypePatchInformation* patch)
      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);

  void AddPatch(uintptr_t off);

  void SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value)
      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);

  // Takes the pointer into the oat_file_ and get the pointer in to the ElfFile.
  uint32_t* GetPatchLocation(uintptr_t patch_ptr);

  bool WriteOutPatchData();

  uintptr_t GetBaseAddressFor(const OatFile* f) {
    if (f == oat_file_) {
      return oat_data_begin_;
    } else {
      return reinterpret_cast<uintptr_t>(f->Begin());
    }
  }

  const CompilerDriver* compiler_driver_;

  // The elf_file containing the oat_data we are patching up
  ElfFile* elf_file_;

  // The oat_file that is actually loaded.
  const OatFile* oat_file_;

  // The oat_header_ within the elf_file_
  OatHeader* oat_header_;

  // Where the elf_file will be loaded during normal runs.
  uintptr_t oat_data_begin_;

  // Callback to get image addresses.
  ImageAddressCallback get_image_address_;
  void* cb_data_;

  std::string* error_msg_;
  std::vector<uintptr_t> patches_;
  std::set<uintptr_t> patches_set_;
  bool write_patches_;

  DISALLOW_COPY_AND_ASSIGN(ElfPatcher);
};

}  // namespace art
#endif  // ART_COMPILER_ELF_PATCHER_H_