patchoat.cc revision e63db27db913f1a88e2095a1ee8239b2bb9124e8
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#include "patchoat.h"
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <sys/stat.h>
21
22#include <string>
23#include <vector>
24
25#include "base/stringpiece.h"
26#include "base/stringprintf.h"
27#include "elf_utils.h"
28#include "elf_file.h"
29#include "gc/space/image_space.h"
30#include "image.h"
31#include "instruction_set.h"
32#include "mirror/art_field.h"
33#include "mirror/art_field-inl.h"
34#include "mirror/art_method.h"
35#include "mirror/art_method-inl.h"
36#include "mirror/object.h"
37#include "mirror/object-inl.h"
38#include "mirror/reference.h"
39#include "noop_compiler_callbacks.h"
40#include "offsets.h"
41#include "os.h"
42#include "runtime.h"
43#include "scoped_thread_state_change.h"
44#include "thread.h"
45#include "utils.h"
46
47namespace art {
48
49static InstructionSet ElfISAToInstructionSet(Elf32_Word isa) {
50  switch (isa) {
51    case EM_ARM:
52      return kArm;
53    case EM_AARCH64:
54      return kArm64;
55    case EM_386:
56      return kX86;
57    case EM_X86_64:
58      return kX86_64;
59    case EM_MIPS:
60      return kMips;
61    default:
62      return kNone;
63  }
64}
65
66bool PatchOat::Patch(const std::string& image_location, off_t delta,
67                     File* output_image, InstructionSet isa,
68                     TimingLogger* timings) {
69  CHECK(Runtime::Current() == nullptr);
70  CHECK(output_image != nullptr);
71  CHECK_GE(output_image->Fd(), 0);
72  CHECK(!image_location.empty()) << "image file must have a filename.";
73  CHECK_NE(isa, kNone);
74
75  TimingLogger::ScopedTiming t("Runtime Setup", timings);
76  const char *isa_name = GetInstructionSetString(isa);
77  std::string image_filename(GetSystemImageFilename(image_location.c_str(), isa));
78  std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str()));
79  if (input_image.get() == nullptr) {
80    LOG(ERROR) << "unable to open input image file.";
81    return false;
82  }
83  int64_t image_len = input_image->GetLength();
84  if (image_len < 0) {
85    LOG(ERROR) << "Error while getting image length";
86    return false;
87  }
88  ImageHeader image_header;
89  if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
90                                              sizeof(image_header), 0)) {
91    LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
92    return false;
93  }
94
95  // Set up the runtime
96  RuntimeOptions options;
97  NoopCompilerCallbacks callbacks;
98  options.push_back(std::make_pair("compilercallbacks", &callbacks));
99  std::string img = "-Ximage:" + image_location;
100  options.push_back(std::make_pair(img.c_str(), nullptr));
101  options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
102  if (!Runtime::Create(options, false)) {
103    LOG(ERROR) << "Unable to initialize runtime";
104    return false;
105  }
106  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
107  // give it away now and then switch to a more manageable ScopedObjectAccess.
108  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
109  ScopedObjectAccess soa(Thread::Current());
110
111  t.NewTiming("Image and oat Patching setup");
112  // Create the map where we will write the image patches to.
113  std::string error_msg;
114  std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
115                                                input_image->Fd(), 0,
116                                                input_image->GetPath().c_str(),
117                                                &error_msg));
118  if (image.get() == nullptr) {
119    LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg;
120    return false;
121  }
122  gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace();
123
124  PatchOat p(image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(),
125             delta, timings);
126  t.NewTiming("Patching files");
127  if (!p.PatchImage()) {
128    LOG(INFO) << "Failed to patch image file " << input_image->GetPath();
129    return false;
130  }
131
132  t.NewTiming("Writing files");
133  if (!p.WriteImage(output_image)) {
134    return false;
135  }
136  return true;
137}
138
139bool PatchOat::Patch(const File* input_oat, const std::string& image_location, off_t delta,
140                     File* output_oat, File* output_image, InstructionSet isa,
141                     TimingLogger* timings) {
142  CHECK(Runtime::Current() == nullptr);
143  CHECK(output_image != nullptr);
144  CHECK_GE(output_image->Fd(), 0);
145  CHECK(input_oat != nullptr);
146  CHECK(output_oat != nullptr);
147  CHECK_GE(input_oat->Fd(), 0);
148  CHECK_GE(output_oat->Fd(), 0);
149  CHECK(!image_location.empty()) << "image file must have a filename.";
150
151  TimingLogger::ScopedTiming t("Runtime Setup", timings);
152
153  if (isa == kNone) {
154    Elf32_Ehdr elf_hdr;
155    if (sizeof(elf_hdr) != input_oat->Read(reinterpret_cast<char*>(&elf_hdr), sizeof(elf_hdr), 0)) {
156      LOG(ERROR) << "unable to read elf header";
157      return false;
158    }
159    isa = ElfISAToInstructionSet(elf_hdr.e_machine);
160  }
161  const char* isa_name = GetInstructionSetString(isa);
162  std::string image_filename(GetSystemImageFilename(image_location.c_str(), isa));
163  std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str()));
164  if (input_image.get() == nullptr) {
165    LOG(ERROR) << "unable to open input image file.";
166    return false;
167  }
168  int64_t image_len = input_image->GetLength();
169  if (image_len < 0) {
170    LOG(ERROR) << "Error while getting image length";
171    return false;
172  }
173  ImageHeader image_header;
174  if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
175                                              sizeof(image_header), 0)) {
176    LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
177  }
178
179  // Set up the runtime
180  RuntimeOptions options;
181  NoopCompilerCallbacks callbacks;
182  options.push_back(std::make_pair("compilercallbacks", &callbacks));
183  std::string img = "-Ximage:" + image_location;
184  options.push_back(std::make_pair(img.c_str(), nullptr));
185  options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
186  if (!Runtime::Create(options, false)) {
187    LOG(ERROR) << "Unable to initialize runtime";
188    return false;
189  }
190  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
191  // give it away now and then switch to a more manageable ScopedObjectAccess.
192  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
193  ScopedObjectAccess soa(Thread::Current());
194
195  t.NewTiming("Image and oat Patching setup");
196  // Create the map where we will write the image patches to.
197  std::string error_msg;
198  std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
199                                                input_image->Fd(), 0,
200                                                input_image->GetPath().c_str(),
201                                                &error_msg));
202  if (image.get() == nullptr) {
203    LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg;
204    return false;
205  }
206  gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace();
207
208  std::unique_ptr<ElfFile> elf(ElfFile::Open(const_cast<File*>(input_oat),
209                                             PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
210  if (elf.get() == nullptr) {
211    LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
212    return false;
213  }
214
215  PatchOat p(elf.release(), image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(),
216             delta, timings);
217  t.NewTiming("Patching files");
218  if (!p.PatchElf()) {
219    LOG(INFO) << "Failed to patch oat file " << input_oat->GetPath();
220    return false;
221  }
222  if (!p.PatchImage()) {
223    LOG(INFO) << "Failed to patch image file " << input_image->GetPath();
224    return false;
225  }
226
227  t.NewTiming("Writing files");
228  if (!p.WriteElf(output_oat)) {
229    return false;
230  }
231  if (!p.WriteImage(output_image)) {
232    return false;
233  }
234  return true;
235}
236
237bool PatchOat::WriteElf(File* out) {
238  TimingLogger::ScopedTiming t("Writing Elf File", timings_);
239  CHECK(oat_file_.get() != nullptr);
240  CHECK(out != nullptr);
241  size_t expect = oat_file_->Size();
242  if (out->WriteFully(reinterpret_cast<char*>(oat_file_->Begin()), expect) &&
243      out->SetLength(expect) == 0) {
244    return true;
245  } else {
246    LOG(ERROR) << "Writing to oat file " << out->GetPath() << " failed.";
247    return false;
248  }
249}
250
251bool PatchOat::WriteImage(File* out) {
252  TimingLogger::ScopedTiming t("Writing image File", timings_);
253  CHECK(image_ != nullptr);
254  CHECK(out != nullptr);
255  size_t expect = image_->Size();
256  if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) &&
257      out->SetLength(expect) == 0) {
258    return true;
259  } else {
260    LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed.";
261    return false;
262  }
263}
264
265bool PatchOat::PatchImage() {
266  ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
267  CHECK_GT(image_->Size(), sizeof(ImageHeader));
268  // These are the roots from the original file.
269  mirror::Object* img_roots = image_header->GetImageRoots();
270  image_header->RelocateImage(delta_);
271
272  VisitObject(img_roots);
273  if (!image_header->IsValid()) {
274    LOG(ERROR) << "reloction renders image header invalid";
275    return false;
276  }
277
278  {
279    TimingLogger::ScopedTiming t("Walk Bitmap", timings_);
280    // Walk the bitmap.
281    WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
282    bitmap_->Walk(PatchOat::BitmapCallback, this);
283  }
284  return true;
285}
286
287bool PatchOat::InHeap(mirror::Object* o) {
288  uintptr_t begin = reinterpret_cast<uintptr_t>(heap_->Begin());
289  uintptr_t end = reinterpret_cast<uintptr_t>(heap_->End());
290  uintptr_t obj = reinterpret_cast<uintptr_t>(o);
291  return o == nullptr || (begin <= obj && obj < end);
292}
293
294void PatchOat::PatchVisitor::operator() (mirror::Object* obj, MemberOffset off,
295                                         bool is_static_unused) const {
296  mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off);
297  DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
298  mirror::Object* moved_object = patcher_->RelocatedAddressOf(referent);
299  copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
300}
301
302void PatchOat::PatchVisitor::operator() (mirror::Class* cls, mirror::Reference* ref) const {
303  MemberOffset off = mirror::Reference::ReferentOffset();
304  mirror::Object* referent = ref->GetReferent();
305  DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
306  mirror::Object* moved_object = patcher_->RelocatedAddressOf(referent);
307  copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
308}
309
310mirror::Object* PatchOat::RelocatedCopyOf(mirror::Object* obj) {
311  if (obj == nullptr) {
312    return nullptr;
313  }
314  DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
315  DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
316  uintptr_t heap_off =
317      reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
318  DCHECK_LT(heap_off, image_->Size());
319  return reinterpret_cast<mirror::Object*>(image_->Begin() + heap_off);
320}
321
322mirror::Object* PatchOat::RelocatedAddressOf(mirror::Object* obj) {
323  if (obj == nullptr) {
324    return nullptr;
325  } else {
326    return reinterpret_cast<mirror::Object*>(reinterpret_cast<byte*>(obj) + delta_);
327  }
328}
329
330// Called by BitmapCallback
331void PatchOat::VisitObject(mirror::Object* object) {
332  mirror::Object* copy = RelocatedCopyOf(object);
333  CHECK(copy != nullptr);
334  if (kUseBakerOrBrooksReadBarrier) {
335    object->AssertReadBarrierPointer();
336    if (kUseBrooksReadBarrier) {
337      mirror::Object* moved_to = RelocatedAddressOf(object);
338      copy->SetReadBarrierPointer(moved_to);
339      DCHECK_EQ(copy->GetReadBarrierPointer(), moved_to);
340    }
341  }
342  PatchOat::PatchVisitor visitor(this, copy);
343  object->VisitReferences<true, kVerifyNone>(visitor, visitor);
344  if (object->IsArtMethod<kVerifyNone>()) {
345    FixupMethod(static_cast<mirror::ArtMethod*>(object),
346                static_cast<mirror::ArtMethod*>(copy));
347  }
348}
349
350void PatchOat::FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy) {
351  // Just update the entry points if it looks like we should.
352  // TODO: sanity check all the pointers' values
353  uintptr_t portable = reinterpret_cast<uintptr_t>(
354      object->GetEntryPointFromPortableCompiledCode<kVerifyNone>());
355  if (portable != 0) {
356    copy->SetEntryPointFromPortableCompiledCode(reinterpret_cast<void*>(portable + delta_));
357  }
358  uintptr_t quick= reinterpret_cast<uintptr_t>(
359      object->GetEntryPointFromQuickCompiledCode<kVerifyNone>());
360  if (quick != 0) {
361    copy->SetEntryPointFromQuickCompiledCode(reinterpret_cast<void*>(quick + delta_));
362  }
363  uintptr_t interpreter = reinterpret_cast<uintptr_t>(
364      object->GetEntryPointFromInterpreter<kVerifyNone>());
365  if (interpreter != 0) {
366    copy->SetEntryPointFromInterpreter(
367        reinterpret_cast<mirror::EntryPointFromInterpreter*>(interpreter + delta_));
368  }
369
370  uintptr_t native_method = reinterpret_cast<uintptr_t>(object->GetNativeMethod());
371  if (native_method != 0) {
372    copy->SetNativeMethod(reinterpret_cast<void*>(native_method + delta_));
373  }
374
375  uintptr_t native_gc_map = reinterpret_cast<uintptr_t>(object->GetNativeGcMap());
376  if (native_gc_map != 0) {
377    copy->SetNativeGcMap(reinterpret_cast<uint8_t*>(native_gc_map + delta_));
378  }
379}
380
381bool PatchOat::Patch(File* input_oat, off_t delta, File* output_oat, TimingLogger* timings) {
382  CHECK(input_oat != nullptr);
383  CHECK(output_oat != nullptr);
384  CHECK_GE(input_oat->Fd(), 0);
385  CHECK_GE(output_oat->Fd(), 0);
386  TimingLogger::ScopedTiming t("Setup Oat File Patching", timings);
387
388  std::string error_msg;
389  std::unique_ptr<ElfFile> elf(ElfFile::Open(const_cast<File*>(input_oat),
390                                             PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
391  if (elf.get() == nullptr) {
392    LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
393    return false;
394  }
395
396  PatchOat p(elf.release(), delta, timings);
397  t.NewTiming("Patch Oat file");
398  if (!p.PatchElf()) {
399    return false;
400  }
401
402  t.NewTiming("Writing oat file");
403  if (!p.WriteElf(output_oat)) {
404    return false;
405  }
406  return true;
407}
408
409bool PatchOat::CheckOatFile() {
410  Elf32_Shdr* patches_sec = oat_file_->FindSectionByName(".oat_patches");
411  if (patches_sec == nullptr) {
412    return false;
413  }
414  if (patches_sec->sh_type != SHT_OAT_PATCH) {
415    return false;
416  }
417  uintptr_t* patches = reinterpret_cast<uintptr_t*>(oat_file_->Begin() + patches_sec->sh_offset);
418  uintptr_t* patches_end = patches + (patches_sec->sh_size/sizeof(uintptr_t));
419  Elf32_Shdr* oat_data_sec = oat_file_->FindSectionByName(".rodata");
420  Elf32_Shdr* oat_text_sec = oat_file_->FindSectionByName(".text");
421  if (oat_data_sec == nullptr) {
422    return false;
423  }
424  if (oat_text_sec == nullptr) {
425    return false;
426  }
427  if (oat_text_sec->sh_offset <= oat_data_sec->sh_offset) {
428    return false;
429  }
430
431  for (; patches < patches_end; patches++) {
432    if (oat_text_sec->sh_size <= *patches) {
433      return false;
434    }
435  }
436
437  return true;
438}
439
440bool PatchOat::PatchElf() {
441  TimingLogger::ScopedTiming t("Fixup Elf Headers", timings_);
442  // Fixup Phdr's
443  for (unsigned int i = 0; i < oat_file_->GetProgramHeaderNum(); i++) {
444    Elf32_Phdr& hdr = oat_file_->GetProgramHeader(i);
445    if (hdr.p_vaddr != 0) {
446      hdr.p_vaddr += delta_;
447    }
448    if (hdr.p_paddr != 0) {
449      hdr.p_paddr += delta_;
450    }
451  }
452  // Fixup Shdr's
453  for (unsigned int i = 0; i < oat_file_->GetSectionHeaderNum(); i++) {
454    Elf32_Shdr& hdr = oat_file_->GetSectionHeader(i);
455    if (hdr.sh_addr != 0) {
456      hdr.sh_addr += delta_;
457    }
458  }
459
460  // Fixup Dynamics.
461  for (Elf32_Word i = 0; i < oat_file_->GetDynamicNum(); i++) {
462    Elf32_Dyn& dyn = oat_file_->GetDynamic(i);
463    if (IsDynamicSectionPointer(dyn.d_tag, oat_file_->GetHeader().e_machine)) {
464      dyn.d_un.d_ptr += delta_;
465    }
466  }
467
468  t.NewTiming("Fixup Elf Symbols");
469  // Fixup dynsym
470  Elf32_Shdr* dynsym_sec = oat_file_->FindSectionByName(".dynsym");
471  CHECK(dynsym_sec != nullptr);
472  if (!PatchSymbols(dynsym_sec)) {
473    return false;
474  }
475
476  // Fixup symtab
477  Elf32_Shdr* symtab_sec = oat_file_->FindSectionByName(".symtab");
478  if (symtab_sec != nullptr) {
479    if (!PatchSymbols(symtab_sec)) {
480      return false;
481    }
482  }
483
484  t.NewTiming("Fixup Elf Text Section");
485  // Fixup text
486  if (!PatchTextSection()) {
487    return false;
488  }
489
490  return true;
491}
492
493bool PatchOat::PatchSymbols(Elf32_Shdr* section) {
494  Elf32_Sym* syms = reinterpret_cast<Elf32_Sym*>(oat_file_->Begin() + section->sh_offset);
495  const Elf32_Sym* last_sym =
496      reinterpret_cast<Elf32_Sym*>(oat_file_->Begin() + section->sh_offset + section->sh_size);
497  CHECK_EQ(section->sh_size % sizeof(Elf32_Sym), 0u)
498      << "Symtab section size is not multiple of symbol size";
499  for (; syms < last_sym; syms++) {
500    uint8_t sttype = ELF32_ST_TYPE(syms->st_info);
501    Elf32_Word shndx = syms->st_shndx;
502    if (shndx != SHN_ABS && shndx != SHN_COMMON && shndx != SHN_UNDEF &&
503        (sttype == STT_FUNC || sttype == STT_OBJECT)) {
504      CHECK_NE(syms->st_value, 0u);
505      syms->st_value += delta_;
506    }
507  }
508  return true;
509}
510
511bool PatchOat::PatchTextSection() {
512  Elf32_Shdr* patches_sec = oat_file_->FindSectionByName(".oat_patches");
513  if (patches_sec == nullptr) {
514    LOG(INFO) << ".oat_patches section not found. Aborting patch";
515    return false;
516  }
517  DCHECK(CheckOatFile()) << "Oat file invalid";
518  CHECK_EQ(patches_sec->sh_type, SHT_OAT_PATCH) << "Unexpected type of .oat_patches";
519  uintptr_t* patches = reinterpret_cast<uintptr_t*>(oat_file_->Begin() + patches_sec->sh_offset);
520  uintptr_t* patches_end = patches + (patches_sec->sh_size/sizeof(uintptr_t));
521  Elf32_Shdr* oat_text_sec = oat_file_->FindSectionByName(".text");
522  CHECK(oat_text_sec != nullptr);
523  byte* to_patch = oat_file_->Begin() + oat_text_sec->sh_offset;
524  uintptr_t to_patch_end = reinterpret_cast<uintptr_t>(to_patch) + oat_text_sec->sh_size;
525
526  for (; patches < patches_end; patches++) {
527    CHECK_LT(*patches, oat_text_sec->sh_size) << "Bad Patch";
528    uint32_t* patch_loc = reinterpret_cast<uint32_t*>(to_patch + *patches);
529    CHECK_LT(reinterpret_cast<uintptr_t>(patch_loc), to_patch_end);
530    *patch_loc += delta_;
531  }
532
533  return true;
534}
535
536static int orig_argc;
537static char** orig_argv;
538
539static std::string CommandLine() {
540  std::vector<std::string> command;
541  for (int i = 0; i < orig_argc; ++i) {
542    command.push_back(orig_argv[i]);
543  }
544  return Join(command, ' ');
545}
546
547static void UsageErrorV(const char* fmt, va_list ap) {
548  std::string error;
549  StringAppendV(&error, fmt, ap);
550  LOG(ERROR) << error;
551}
552
553static void UsageError(const char* fmt, ...) {
554  va_list ap;
555  va_start(ap, fmt);
556  UsageErrorV(fmt, ap);
557  va_end(ap);
558}
559
560static void Usage(const char *fmt, ...) {
561  va_list ap;
562  va_start(ap, fmt);
563  UsageErrorV(fmt, ap);
564  va_end(ap);
565
566  UsageError("Command: %s", CommandLine().c_str());
567  UsageError("Usage: patchoat [options]...");
568  UsageError("");
569  UsageError("  --instruction-set=<isa>: Specifies the instruction set the patched code is");
570  UsageError("      compiled for. Required if you use --input-oat-location");
571  UsageError("");
572  UsageError("  --input-oat-file=<file.oat>: Specifies the exact filename of the oat file to be");
573  UsageError("      patched.");
574  UsageError("");
575  UsageError("  --input-oat-fd=<file-descriptor>: Specifies the file-descriptor of the oat file");
576  UsageError("      to be patched.");
577  UsageError("");
578  UsageError("  --input-oat-location=<file.oat>: Specifies the 'location' to read the patched");
579  UsageError("      oat file from. If used one must also supply the --instruction-set");
580  UsageError("");
581  UsageError("  --input-image-location=<file.art>: Specifies the 'location' of the image file to");
582  UsageError("      be patched. If --instruction-set is not given it will use the instruction set");
583  UsageError("      extracted from the --input-oat-file.");
584  UsageError("");
585  UsageError("  --output-oat-file=<file.oat>: Specifies the exact file to write the patched oat");
586  UsageError("      file to.");
587  UsageError("");
588  UsageError("  --output-oat-location=<file.oat>: Specifies the 'location' to write the patched");
589  UsageError("      oat file to. If used one must also specify the --instruction-set");
590  UsageError("");
591  UsageError("  --output-oat-fd=<file-descriptor>: Specifies the file-descriptor to write the");
592  UsageError("      the patched oat file to.");
593  UsageError("");
594  UsageError("  --output-image-file=<file.art>: Specifies the exact file to write the patched");
595  UsageError("      image file to.");
596  UsageError("");
597  UsageError("  --output-image-fd=<file-descriptor>: Specifies the file-descriptor to write the");
598  UsageError("      the patched image file to.");
599  UsageError("");
600  UsageError("  --output-image-location=<file.art>: Specifies the 'location' to write the patched");
601  UsageError("      image file to. If used one must also specify the --instruction-set");
602  UsageError("");
603  UsageError("  --orig-base-offset=<original-base-offset>: Specify the base offset the input file");
604  UsageError("      was compiled with. This is needed if one is specifying a --base-offset");
605  UsageError("");
606  UsageError("  --base-offset=<new-base-offset>: Specify the base offset we will repatch the");
607  UsageError("      given files to use. This requires that --orig-base-offset is also given.");
608  UsageError("");
609  UsageError("  --base-offset-delta=<delta>: Specify the amount to change the old base-offset by.");
610  UsageError("      This value may be negative.");
611  UsageError("");
612  UsageError("  --patched-image-file=<file.art>: Use the same patch delta as was used to patch");
613  UsageError("      the given image file.");
614  UsageError("");
615  UsageError("  --patched-image-location=<file.art>: Use the same patch delta as was used to");
616  UsageError("      patch the given image location. If used one must also specify the");
617  UsageError("      --instruction-set flag.");
618  UsageError("");
619  UsageError("  --dump-timings: dump out patch timing information");
620  UsageError("");
621  UsageError("  --no-dump-timings: do not dump out patch timing information");
622  UsageError("");
623
624  exit(EXIT_FAILURE);
625}
626
627static bool ReadBaseDelta(const char* name, off_t* delta, std::string* error_msg) {
628  CHECK(name != nullptr);
629  CHECK(delta != nullptr);
630  std::unique_ptr<File> file;
631  if (OS::FileExists(name)) {
632    file.reset(OS::OpenFileForReading(name));
633    if (file.get() == nullptr) {
634      *error_msg = "Failed to open file %s for reading";
635      return false;
636    }
637  } else {
638    *error_msg = "File %s does not exist";
639    return false;
640  }
641  CHECK(file.get() != nullptr);
642  ImageHeader hdr;
643  if (sizeof(hdr) != file->Read(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0)) {
644    *error_msg = "Failed to read file %s";
645    return false;
646  }
647  if (!hdr.IsValid()) {
648    *error_msg = "%s does not contain a valid image header.";
649    return false;
650  }
651  *delta = hdr.GetPatchDelta();
652  return true;
653}
654
655static File* CreateOrOpen(const char* name, bool* created) {
656  if (OS::FileExists(name)) {
657    *created = false;
658    return OS::OpenFileReadWrite(name);
659  } else {
660    *created = true;
661    return OS::CreateEmptyFile(name);
662  }
663}
664
665static int patchoat(int argc, char **argv) {
666  InitLogging(argv);
667  const bool debug = kIsDebugBuild;
668  orig_argc = argc;
669  orig_argv = argv;
670  TimingLogger timings("patcher", false, false);
671
672  InitLogging(argv);
673
674  // Skip over the command name.
675  argv++;
676  argc--;
677
678  if (argc == 0) {
679    Usage("No arguments specified");
680  }
681
682  timings.StartTiming("Patchoat");
683
684  // cmd line args
685  bool isa_set = false;
686  InstructionSet isa = kNone;
687  std::string input_oat_filename;
688  std::string input_oat_location;
689  int input_oat_fd = -1;
690  bool have_input_oat = false;
691  std::string input_image_location;
692  std::string output_oat_filename;
693  std::string output_oat_location;
694  int output_oat_fd = -1;
695  bool have_output_oat = false;
696  std::string output_image_filename;
697  std::string output_image_location;
698  int output_image_fd = -1;
699  bool have_output_image = false;
700  uintptr_t base_offset = 0;
701  bool base_offset_set = false;
702  uintptr_t orig_base_offset = 0;
703  bool orig_base_offset_set = false;
704  off_t base_delta = 0;
705  bool base_delta_set = false;
706  std::string patched_image_filename;
707  std::string patched_image_location;
708  bool dump_timings = kIsDebugBuild;
709
710  for (int i = 0; i < argc; i++) {
711    const StringPiece option(argv[i]);
712    const bool log_options = false;
713    if (log_options) {
714      LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i];
715    }
716    // TODO: GetInstructionSetFromString shouldn't LOG(FATAL).
717    if (option.starts_with("--instruction-set=")) {
718      isa_set = true;
719      const char* isa_str = option.substr(strlen("--instruction-set=")).data();
720      if (!strcmp("arm", isa_str)) {
721        isa = kArm;
722      } else if (!strcmp("arm64", isa_str)) {
723        isa = kArm64;
724      } else if (!strcmp("x86", isa_str)) {
725        isa = kX86;
726      } else if (!strcmp("x86_64", isa_str)) {
727        isa = kX86_64;
728      } else if (!strcmp("mips", isa_str)) {
729        isa = kMips;
730      } else {
731        Usage("Unknown instruction set %s", isa_str);
732      }
733    } else if (option.starts_with("--input-oat-location=")) {
734      if (have_input_oat) {
735        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
736      }
737      have_input_oat = true;
738      input_oat_location = option.substr(strlen("--input-oat-location=")).data();
739    } else if (option.starts_with("--input-oat-file=")) {
740      if (have_input_oat) {
741        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
742      }
743      have_input_oat = true;
744      input_oat_filename = option.substr(strlen("--input-oat-file=")).data();
745    } else if (option.starts_with("--input-oat-fd=")) {
746      if (have_input_oat) {
747        Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
748      }
749      have_input_oat = true;
750      const char* oat_fd_str = option.substr(strlen("--input-oat-fd=")).data();
751      if (!ParseInt(oat_fd_str, &input_oat_fd)) {
752        Usage("Failed to parse --input-oat-fd argument '%s' as an integer", oat_fd_str);
753      }
754      if (input_oat_fd < 0) {
755        Usage("--input-oat-fd pass a negative value %d", input_oat_fd);
756      }
757    } else if (option.starts_with("--input-image-location=")) {
758      input_image_location = option.substr(strlen("--input-image-location=")).data();
759    } else if (option.starts_with("--output-oat-location=")) {
760      if (have_output_oat) {
761        Usage("Only one of --output-oat-file, --output-oat-location and --output-oat-fd may "
762              "be used.");
763      }
764      have_output_oat = true;
765      output_oat_location = option.substr(strlen("--output-oat-location=")).data();
766    } else if (option.starts_with("--output-oat-file=")) {
767      if (have_output_oat) {
768        Usage("Only one of --output-oat-file, --output-oat-location and --output-oat-fd may "
769              "be used.");
770      }
771      have_output_oat = true;
772      output_oat_filename = option.substr(strlen("--output-oat-file=")).data();
773    } else if (option.starts_with("--output-oat-fd=")) {
774      if (have_output_oat) {
775        Usage("Only one of --output-oat-file, --output-oat-location and --output-oat-fd may "
776              "be used.");
777      }
778      have_output_oat = true;
779      const char* oat_fd_str = option.substr(strlen("--output-oat-fd=")).data();
780      if (!ParseInt(oat_fd_str, &output_oat_fd)) {
781        Usage("Failed to parse --output-oat-fd argument '%s' as an integer", oat_fd_str);
782      }
783      if (output_oat_fd < 0) {
784        Usage("--output-oat-fd pass a negative value %d", output_oat_fd);
785      }
786    } else if (option.starts_with("--output-image-location=")) {
787      if (have_output_image) {
788        Usage("Only one of --output-image-file, --output-image-location and --output-image-fd may "
789              "be used.");
790      }
791      have_output_image = true;
792      output_image_location= option.substr(strlen("--output-image-location=")).data();
793    } else if (option.starts_with("--output-image-file=")) {
794      if (have_output_image) {
795        Usage("Only one of --output-image-file, --output-image-location and --output-image-fd may "
796              "be used.");
797      }
798      have_output_image = true;
799      output_image_filename = option.substr(strlen("--output-image-file=")).data();
800    } else if (option.starts_with("--output-image-fd=")) {
801      if (have_output_image) {
802        Usage("Only one of --output-image-file, --output-image-location and --output-image-fd "
803              "may be used.");
804      }
805      have_output_image = true;
806      const char* image_fd_str = option.substr(strlen("--output-image-fd=")).data();
807      if (!ParseInt(image_fd_str, &output_image_fd)) {
808        Usage("Failed to parse --output-image-fd argument '%s' as an integer", image_fd_str);
809      }
810      if (output_image_fd < 0) {
811        Usage("--output-image-fd pass a negative value %d", output_image_fd);
812      }
813    } else if (option.starts_with("--orig-base-offset=")) {
814      const char* orig_base_offset_str = option.substr(strlen("--orig-base-offset=")).data();
815      orig_base_offset_set = true;
816      if (!ParseUint(orig_base_offset_str, &orig_base_offset)) {
817        Usage("Failed to parse --orig-base-offset argument '%s' as an uintptr_t",
818              orig_base_offset_str);
819      }
820    } else if (option.starts_with("--base-offset=")) {
821      const char* base_offset_str = option.substr(strlen("--base-offset=")).data();
822      base_offset_set = true;
823      if (!ParseUint(base_offset_str, &base_offset)) {
824        Usage("Failed to parse --base-offset argument '%s' as an uintptr_t", base_offset_str);
825      }
826    } else if (option.starts_with("--base-offset-delta=")) {
827      const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data();
828      base_delta_set = true;
829      if (!ParseInt(base_delta_str, &base_delta)) {
830        Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str);
831      }
832    } else if (option.starts_with("--patched-image-location=")) {
833      patched_image_location = option.substr(strlen("--patched-image-location=")).data();
834    } else if (option.starts_with("--patched-image-file=")) {
835      patched_image_filename = option.substr(strlen("--patched-image-file=")).data();
836    } else if (option == "--dump-timings") {
837      dump_timings = true;
838    } else if (option == "--no-dump-timings") {
839      dump_timings = false;
840    } else {
841      Usage("Unknown argument %s", option.data());
842    }
843  }
844
845  {
846    // Only 1 of these may be set.
847    uint32_t cnt = 0;
848    cnt += (base_delta_set) ? 1 : 0;
849    cnt += (base_offset_set && orig_base_offset_set) ? 1 : 0;
850    cnt += (!patched_image_filename.empty()) ? 1 : 0;
851    cnt += (!patched_image_location.empty()) ? 1 : 0;
852    if (cnt > 1) {
853      Usage("Only one of --base-offset/--orig-base-offset, --base-offset-delta, "
854            "--patched-image-filename or --patched-image-location may be used.");
855    } else if (cnt == 0) {
856      Usage("Must specify --base-offset-delta, --base-offset and --orig-base-offset, "
857            "--patched-image-location or --patched-image-file");
858    }
859  }
860
861  if (have_input_oat != have_output_oat) {
862    Usage("Either both input and output oat must be supplied or niether must be.");
863  }
864
865  if ((!input_image_location.empty()) != have_output_image) {
866    Usage("Either both input and output image must be supplied or niether must be.");
867  }
868
869  // We know we have both the input and output so rename for clarity.
870  bool have_image_files = have_output_image;
871  bool have_oat_files = have_output_oat;
872
873  if (!have_oat_files && !have_image_files) {
874    Usage("Must be patching either an oat or an image file or both.");
875  }
876
877  if (!have_oat_files && !isa_set) {
878    Usage("Must include ISA if patching an image file without an oat file.");
879  }
880
881  if (!input_oat_location.empty()) {
882    if (!isa_set) {
883      Usage("specifying a location requires specifying an instruction set");
884    }
885    input_oat_filename = GetSystemImageFilename(input_oat_location.c_str(), isa);
886    if (debug) {
887      LOG(INFO) << "Using input-oat-file " << input_oat_filename;
888    }
889  }
890  if (!output_oat_location.empty()) {
891    if (!isa_set) {
892      Usage("specifying a location requires specifying an instruction set");
893    }
894    output_oat_filename = GetSystemImageFilename(output_oat_location.c_str(), isa);
895    if (debug) {
896      LOG(INFO) << "Using output-oat-file " << output_oat_filename;
897    }
898  }
899  if (!output_image_location.empty()) {
900    if (!isa_set) {
901      Usage("specifying a location requires specifying an instruction set");
902    }
903    output_image_filename = GetSystemImageFilename(output_image_location.c_str(), isa);
904    if (debug) {
905      LOG(INFO) << "Using output-image-file " << output_image_filename;
906    }
907  }
908  if (!patched_image_location.empty()) {
909    if (!isa_set) {
910      Usage("specifying a location requires specifying an instruction set");
911    }
912    patched_image_filename = GetSystemImageFilename(patched_image_location.c_str(), isa);
913    if (debug) {
914      LOG(INFO) << "Using patched-image-file " << patched_image_filename;
915    }
916  }
917
918  if (!base_delta_set) {
919    if (orig_base_offset_set && base_offset_set) {
920      base_delta_set = true;
921      base_delta = base_offset - orig_base_offset;
922    } else if (!patched_image_filename.empty()) {
923      base_delta_set = true;
924      std::string error_msg;
925      if (!ReadBaseDelta(patched_image_filename.c_str(), &base_delta, &error_msg)) {
926        Usage(error_msg.c_str(), patched_image_filename.c_str());
927      }
928    } else {
929      if (base_offset_set) {
930        Usage("Unable to determine original base offset.");
931      } else {
932        Usage("Must supply a desired new offset or delta.");
933      }
934    }
935  }
936
937  if (!IsAligned<kPageSize>(base_delta)) {
938    Usage("Base offset/delta must be alligned to a pagesize (0x%08x) boundary.", kPageSize);
939  }
940
941  // Do we need to cleanup output files if we fail?
942  bool new_image_out = false;
943  bool new_oat_out = false;
944
945  std::unique_ptr<File> input_oat;
946  std::unique_ptr<File> output_oat;
947  std::unique_ptr<File> output_image;
948
949  if (have_image_files) {
950    CHECK(!input_image_location.empty());
951
952    if (output_image_fd != -1) {
953      output_image.reset(new File(output_image_fd, output_image_filename));
954    } else {
955      CHECK(!output_image_filename.empty());
956      output_image.reset(CreateOrOpen(output_image_filename.c_str(), &new_image_out));
957    }
958  } else {
959    CHECK(output_image_filename.empty() && output_image_fd == -1 && input_image_location.empty());
960  }
961
962  if (have_oat_files) {
963    if (input_oat_fd != -1) {
964      input_oat.reset(new File(input_oat_fd, input_oat_filename));
965    } else {
966      CHECK(!input_oat_filename.empty());
967      input_oat.reset(OS::OpenFileForReading(input_oat_filename.c_str()));
968    }
969
970    if (output_oat_fd != -1) {
971      output_oat.reset(new File(output_oat_fd, output_oat_filename));
972    } else {
973      CHECK(!output_oat_filename.empty());
974      output_oat.reset(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out));
975    }
976  }
977
978  auto cleanup = [&output_image_filename, &output_oat_filename,
979                  &new_oat_out, &new_image_out, &timings, &dump_timings](bool success) {
980    timings.EndTiming();
981    if (!success) {
982      if (new_oat_out) {
983        CHECK(!output_oat_filename.empty());
984        unlink(output_oat_filename.c_str());
985      }
986      if (new_image_out) {
987        CHECK(!output_image_filename.empty());
988        unlink(output_image_filename.c_str());
989      }
990    }
991    if (dump_timings) {
992      LOG(INFO) << Dumpable<TimingLogger>(timings);
993    }
994  };
995
996  if (debug) {
997    LOG(INFO) << "moving offset by " << base_delta << " (0x" << std::hex << base_delta << ") bytes";
998  }
999
1000  bool ret;
1001  if (have_image_files && have_oat_files) {
1002    TimingLogger::ScopedTiming pt("patch image and oat", &timings);
1003    ret = PatchOat::Patch(input_oat.get(), input_image_location, base_delta,
1004                          output_oat.get(), output_image.get(), isa, &timings);
1005  } else if (have_oat_files) {
1006    TimingLogger::ScopedTiming pt("patch oat", &timings);
1007    ret = PatchOat::Patch(input_oat.get(), base_delta, output_oat.get(), &timings);
1008  } else {
1009    TimingLogger::ScopedTiming pt("patch image", &timings);
1010    CHECK(have_image_files);
1011    ret = PatchOat::Patch(input_image_location, base_delta, output_image.get(), isa, &timings);
1012  }
1013  cleanup(ret);
1014  return (ret) ? EXIT_SUCCESS : EXIT_FAILURE;
1015}
1016
1017}  // namespace art
1018
1019int main(int argc, char **argv) {
1020  return art::patchoat(argc, argv);
1021}
1022