• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 "elf_patcher.h"
18 
19 #include <vector>
20 #include <set>
21 
22 #include "elf_file.h"
23 #include "elf_utils.h"
24 #include "mirror/art_field-inl.h"
25 #include "mirror/art_method-inl.h"
26 #include "mirror/array-inl.h"
27 #include "mirror/class-inl.h"
28 #include "mirror/class_loader.h"
29 #include "mirror/dex_cache-inl.h"
30 #include "mirror/object-inl.h"
31 #include "mirror/object_array-inl.h"
32 #include "mirror/string-inl.h"
33 #include "oat.h"
34 #include "os.h"
35 #include "utils.h"
36 
37 namespace art {
38 
Patch(const CompilerDriver * driver,ElfFile * elf_file,const std::string & oat_location,ImageAddressCallback cb,void * cb_data,std::string * error_msg)39 bool ElfPatcher::Patch(const CompilerDriver* driver, ElfFile* elf_file,
40                        const std::string& oat_location,
41                        ImageAddressCallback cb, void* cb_data,
42                        std::string* error_msg) {
43   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
44   const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
45   if (oat_file == nullptr) {
46     CHECK(Runtime::Current()->IsCompiler());
47     oat_file = OatFile::Open(oat_location, oat_location, NULL, false, error_msg);
48     if (oat_file == nullptr) {
49       *error_msg = StringPrintf("Unable to find or open oat file at '%s': %s", oat_location.c_str(),
50                                 error_msg->c_str());
51       return false;
52     }
53     CHECK_EQ(class_linker->RegisterOatFile(oat_file), oat_file);
54   }
55   return ElfPatcher::Patch(driver, elf_file, oat_file,
56                            reinterpret_cast<uintptr_t>(oat_file->Begin()), cb, cb_data, error_msg);
57 }
58 
Patch(const CompilerDriver * driver,ElfFile * elf,const OatFile * oat_file,uintptr_t oat_data_start,ImageAddressCallback cb,void * cb_data,std::string * error_msg)59 bool ElfPatcher::Patch(const CompilerDriver* driver, ElfFile* elf, const OatFile* oat_file,
60                        uintptr_t oat_data_start, ImageAddressCallback cb, void* cb_data,
61                        std::string* error_msg) {
62   Elf32_Shdr* data_sec = elf->FindSectionByName(".rodata");
63   if (data_sec == nullptr) {
64     *error_msg = "Unable to find .rodata section and oat header";
65     return false;
66   }
67   OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf->Begin() + data_sec->sh_offset);
68   if (!oat_header->IsValid()) {
69     *error_msg = "Oat header was not valid";
70     return false;
71   }
72 
73   ElfPatcher p(driver, elf, oat_file, oat_header, oat_data_start, cb, cb_data, error_msg);
74   return p.PatchElf();
75 }
76 
GetTargetMethod(const CompilerDriver::CallPatchInformation * patch)77 mirror::ArtMethod* ElfPatcher::GetTargetMethod(const CompilerDriver::CallPatchInformation* patch) {
78   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
79   StackHandleScope<1> hs(Thread::Current());
80   Handle<mirror::DexCache> dex_cache(
81       hs.NewHandle(class_linker->FindDexCache(*patch->GetTargetDexFile())));
82   mirror::ArtMethod* method = class_linker->ResolveMethod(*patch->GetTargetDexFile(),
83                                                           patch->GetTargetMethodIdx(),
84                                                           dex_cache,
85                                                           NullHandle<mirror::ClassLoader>(),
86                                                           NullHandle<mirror::ArtMethod>(),
87                                                           patch->GetTargetInvokeType());
88   CHECK(method != NULL)
89     << patch->GetTargetDexFile()->GetLocation() << " " << patch->GetTargetMethodIdx();
90   CHECK(!method->IsRuntimeMethod())
91     << patch->GetTargetDexFile()->GetLocation() << " " << patch->GetTargetMethodIdx();
92   CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
93     << patch->GetTargetDexFile()->GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
94     << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
95     << PrettyMethod(method);
96   return method;
97 }
98 
GetTargetType(const CompilerDriver::TypePatchInformation * patch)99 mirror::Class* ElfPatcher::GetTargetType(const CompilerDriver::TypePatchInformation* patch) {
100   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
101   StackHandleScope<2> hs(Thread::Current());
102   Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(patch->GetDexFile())));
103   mirror::Class* klass = class_linker->ResolveType(patch->GetDexFile(), patch->GetTargetTypeIdx(),
104                                                    dex_cache, NullHandle<mirror::ClassLoader>());
105   CHECK(klass != NULL)
106     << patch->GetDexFile().GetLocation() << " " << patch->GetTargetTypeIdx();
107   CHECK(dex_cache->GetResolvedTypes()->Get(patch->GetTargetTypeIdx()) == klass)
108     << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
109     << PrettyClass(dex_cache->GetResolvedTypes()->Get(patch->GetTargetTypeIdx())) << " "
110     << PrettyClass(klass);
111   return klass;
112 }
113 
AddPatch(uintptr_t p)114 void ElfPatcher::AddPatch(uintptr_t p) {
115   if (write_patches_ && patches_set_.find(p) == patches_set_.end()) {
116     patches_set_.insert(p);
117     patches_.push_back(p);
118   }
119 }
120 
GetPatchLocation(uintptr_t patch_ptr)121 uint32_t* ElfPatcher::GetPatchLocation(uintptr_t patch_ptr) {
122   CHECK_GE(patch_ptr, reinterpret_cast<uintptr_t>(oat_file_->Begin()));
123   CHECK_LE(patch_ptr, reinterpret_cast<uintptr_t>(oat_file_->End()));
124   uintptr_t off = patch_ptr - reinterpret_cast<uintptr_t>(oat_file_->Begin());
125   uintptr_t ret = reinterpret_cast<uintptr_t>(oat_header_) + off;
126 
127   CHECK_GE(ret, reinterpret_cast<uintptr_t>(elf_file_->Begin()));
128   CHECK_LT(ret, reinterpret_cast<uintptr_t>(elf_file_->End()));
129   return reinterpret_cast<uint32_t*>(ret);
130 }
131 
SetPatchLocation(const CompilerDriver::PatchInformation * patch,uint32_t value)132 void ElfPatcher::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) {
133   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
134   const void* quick_oat_code = class_linker->GetQuickOatCodeFor(patch->GetDexFile(),
135                                                                 patch->GetReferrerClassDefIdx(),
136                                                                 patch->GetReferrerMethodIdx());
137   // TODO: make this Thumb2 specific
138   uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(quick_oat_code) & ~0x1);
139   uintptr_t patch_ptr = reinterpret_cast<uintptr_t>(base + patch->GetLiteralOffset());
140   uint32_t* patch_location = GetPatchLocation(patch_ptr);
141   if (kIsDebugBuild) {
142     if (patch->IsCall()) {
143       const CompilerDriver::CallPatchInformation* cpatch = patch->AsCall();
144       const DexFile::MethodId& id =
145           cpatch->GetTargetDexFile()->GetMethodId(cpatch->GetTargetMethodIdx());
146       uint32_t expected = reinterpret_cast<uintptr_t>(&id) & 0xFFFFFFFF;
147       uint32_t actual = *patch_location;
148       CHECK(actual == expected || actual == value) << "Patching call failed: " << std::hex
149           << " actual=" << actual
150           << " expected=" << expected
151           << " value=" << value;
152     }
153     if (patch->IsType()) {
154       const CompilerDriver::TypePatchInformation* tpatch = patch->AsType();
155       const DexFile::TypeId& id = tpatch->GetDexFile().GetTypeId(tpatch->GetTargetTypeIdx());
156       uint32_t expected = reinterpret_cast<uintptr_t>(&id) & 0xFFFFFFFF;
157       uint32_t actual = *patch_location;
158       CHECK(actual == expected || actual == value) << "Patching type failed: " << std::hex
159           << " actual=" << actual
160           << " expected=" << expected
161           << " value=" << value;
162     }
163   }
164   *patch_location = value;
165   oat_header_->UpdateChecksum(patch_location, sizeof(value));
166 
167   if (patch->IsCall() && patch->AsCall()->IsRelative()) {
168     // We never record relative patches.
169     return;
170   }
171   uintptr_t loc = patch_ptr - (reinterpret_cast<uintptr_t>(oat_file_->Begin()) +
172                                oat_header_->GetExecutableOffset());
173   CHECK_GT(patch_ptr, reinterpret_cast<uintptr_t>(oat_file_->Begin()) +
174                       oat_header_->GetExecutableOffset());
175   CHECK_LT(loc, oat_file_->Size() - oat_header_->GetExecutableOffset());
176   AddPatch(loc);
177 }
178 
PatchElf()179 bool ElfPatcher::PatchElf() {
180   // TODO if we are adding patches the resulting ELF file might have a
181   // potentially rather large amount of free space where patches might have been
182   // placed. We should adjust the ELF file to get rid of this excess space.
183   if (write_patches_) {
184     patches_.reserve(compiler_driver_->GetCodeToPatch().size() +
185                      compiler_driver_->GetMethodsToPatch().size() +
186                      compiler_driver_->GetClassesToPatch().size());
187   }
188   Thread* self = Thread::Current();
189   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
190   const char* old_cause = self->StartAssertNoThreadSuspension("ElfPatcher");
191 
192   typedef std::vector<const CompilerDriver::CallPatchInformation*> CallPatches;
193   const CallPatches& code_to_patch = compiler_driver_->GetCodeToPatch();
194   for (size_t i = 0; i < code_to_patch.size(); i++) {
195     const CompilerDriver::CallPatchInformation* patch = code_to_patch[i];
196 
197     mirror::ArtMethod* target = GetTargetMethod(patch);
198     uintptr_t quick_code = reinterpret_cast<uintptr_t>(class_linker->GetQuickOatCodeFor(target));
199     DCHECK_NE(quick_code, 0U) << PrettyMethod(target);
200     const OatFile* target_oat =
201         class_linker->FindOpenedOatDexFileForDexFile(*patch->GetTargetDexFile())->GetOatFile();
202     // Get where the data actually starts. if target is this oat_file_ it is oat_data_start_,
203     // otherwise it is wherever target_oat is loaded.
204     uintptr_t oat_data_addr = GetBaseAddressFor(target_oat);
205     uintptr_t code_base = reinterpret_cast<uintptr_t>(target_oat->Begin());
206     uintptr_t code_offset = quick_code - code_base;
207     bool is_quick_offset = false;
208     if (quick_code == reinterpret_cast<uintptr_t>(GetQuickToInterpreterBridge())) {
209       is_quick_offset = true;
210       code_offset = oat_header_->GetQuickToInterpreterBridgeOffset();
211     } else if (quick_code ==
212         reinterpret_cast<uintptr_t>(class_linker->GetQuickGenericJniTrampoline())) {
213       CHECK(target->IsNative());
214       is_quick_offset = true;
215       code_offset = oat_header_->GetQuickGenericJniTrampolineOffset();
216     }
217     uintptr_t value;
218     if (patch->IsRelative()) {
219       // value to patch is relative to the location being patched
220       const void* quick_oat_code =
221         class_linker->GetQuickOatCodeFor(patch->GetDexFile(),
222                                          patch->GetReferrerClassDefIdx(),
223                                          patch->GetReferrerMethodIdx());
224       if (is_quick_offset) {
225         // If its a quick offset it means that we are doing a relative patch from the class linker
226         // oat_file to the elf_patcher oat_file so we need to adjust the quick oat code to be the
227         // one in the output oat_file (ie where it is actually going to be loaded).
228         quick_code = PointerToLowMemUInt32(reinterpret_cast<void*>(oat_data_addr + code_offset));
229         quick_oat_code =
230             reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(quick_oat_code) +
231                 oat_data_addr - code_base);
232       }
233       uintptr_t base = reinterpret_cast<uintptr_t>(quick_oat_code);
234       uintptr_t patch_location = base + patch->GetLiteralOffset();
235       value = quick_code - patch_location + patch->RelativeOffset();
236     } else if (code_offset != 0) {
237       value = PointerToLowMemUInt32(reinterpret_cast<void*>(oat_data_addr + code_offset));
238     } else {
239       value = 0;
240     }
241     SetPatchLocation(patch, value);
242   }
243 
244   const CallPatches& methods_to_patch = compiler_driver_->GetMethodsToPatch();
245   for (size_t i = 0; i < methods_to_patch.size(); i++) {
246     const CompilerDriver::CallPatchInformation* patch = methods_to_patch[i];
247     mirror::ArtMethod* target = GetTargetMethod(patch);
248     SetPatchLocation(patch, PointerToLowMemUInt32(get_image_address_(cb_data_, target)));
249   }
250 
251   const std::vector<const CompilerDriver::TypePatchInformation*>& classes_to_patch =
252       compiler_driver_->GetClassesToPatch();
253   for (size_t i = 0; i < classes_to_patch.size(); i++) {
254     const CompilerDriver::TypePatchInformation* patch = classes_to_patch[i];
255     mirror::Class* target = GetTargetType(patch);
256     SetPatchLocation(patch, PointerToLowMemUInt32(get_image_address_(cb_data_, target)));
257   }
258 
259   self->EndAssertNoThreadSuspension(old_cause);
260 
261   if (write_patches_) {
262     return WriteOutPatchData();
263   }
264   return true;
265 }
266 
WriteOutPatchData()267 bool ElfPatcher::WriteOutPatchData() {
268   Elf32_Shdr* shdr = elf_file_->FindSectionByName(".oat_patches");
269   if (shdr != nullptr) {
270     CHECK_EQ(shdr, elf_file_->FindSectionByType(SHT_OAT_PATCH))
271         << "Incorrect type for .oat_patches section";
272     CHECK_LE(patches_.size() * sizeof(uintptr_t), shdr->sh_size)
273         << "We got more patches than anticipated";
274     CHECK_LE(reinterpret_cast<uintptr_t>(elf_file_->Begin()) + shdr->sh_offset + shdr->sh_size,
275               reinterpret_cast<uintptr_t>(elf_file_->End())) << "section is too large";
276     CHECK(shdr == elf_file_->GetSectionHeader(elf_file_->GetSectionHeaderNum() - 1) ||
277           shdr->sh_offset + shdr->sh_size <= (shdr + 1)->sh_offset)
278         << "Section overlaps onto next section";
279     // It's mmap'd so we can just memcpy.
280     memcpy(elf_file_->Begin() + shdr->sh_offset, patches_.data(),
281            patches_.size() * sizeof(uintptr_t));
282     // TODO We should fill in the newly empty space between the last patch and
283     // the start of the next section by moving the following sections down if
284     // possible.
285     shdr->sh_size = patches_.size() * sizeof(uintptr_t);
286     return true;
287   } else {
288     LOG(ERROR) << "Unable to find section header for SHT_OAT_PATCH";
289     *error_msg_ = "Unable to find section to write patch information to in ";
290     *error_msg_ += elf_file_->GetFile().GetPath();
291     return false;
292   }
293 }
294 
295 }  // namespace art
296