blob: 504addc0544f27cfceba91acc1a8f1582446100f [file] [log] [blame]
Alex Light53cb16b2014-06-12 11:26:29 -07001/*
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>
Alex Lighta59dd802014-07-02 16:28:08 -070020#include <sys/file.h>
Alex Light53cb16b2014-06-12 11:26:29 -070021#include <sys/stat.h>
Alex Lighta59dd802014-07-02 16:28:08 -070022#include <unistd.h>
Alex Light53cb16b2014-06-12 11:26:29 -070023
24#include <string>
25#include <vector>
26
Alex Lighta59dd802014-07-02 16:28:08 -070027#include "base/scoped_flock.h"
Alex Light53cb16b2014-06-12 11:26:29 -070028#include "base/stringpiece.h"
29#include "base/stringprintf.h"
30#include "elf_utils.h"
31#include "elf_file.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070032#include "elf_file_impl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070033#include "gc/space/image_space.h"
Alex Light53cb16b2014-06-12 11:26:29 -070034#include "image.h"
35#include "instruction_set.h"
36#include "mirror/art_field.h"
37#include "mirror/art_field-inl.h"
38#include "mirror/art_method.h"
39#include "mirror/art_method-inl.h"
40#include "mirror/object.h"
41#include "mirror/object-inl.h"
42#include "mirror/reference.h"
43#include "noop_compiler_callbacks.h"
44#include "offsets.h"
45#include "os.h"
46#include "runtime.h"
47#include "scoped_thread_state_change.h"
48#include "thread.h"
49#include "utils.h"
50
51namespace art {
52
53static InstructionSet ElfISAToInstructionSet(Elf32_Word isa) {
54 switch (isa) {
55 case EM_ARM:
56 return kArm;
57 case EM_AARCH64:
58 return kArm64;
59 case EM_386:
60 return kX86;
61 case EM_X86_64:
62 return kX86_64;
63 case EM_MIPS:
64 return kMips;
65 default:
66 return kNone;
67 }
68}
69
Alex Lightcf4bf382014-07-24 11:29:14 -070070static bool LocationToFilename(const std::string& location, InstructionSet isa,
71 std::string* filename) {
72 bool has_system = false;
73 bool has_cache = false;
74 // image_location = /system/framework/boot.art
75 // system_image_location = /system/framework/<image_isa>/boot.art
76 std::string system_filename(GetSystemImageFilename(location.c_str(), isa));
77 if (OS::FileExists(system_filename.c_str())) {
78 has_system = true;
79 }
80
81 bool have_android_data = false;
82 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -070083 bool is_global_cache = false;
Alex Lightcf4bf382014-07-24 11:29:14 -070084 std::string dalvik_cache;
85 GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -070086 &have_android_data, &dalvik_cache_exists, &is_global_cache);
Alex Lightcf4bf382014-07-24 11:29:14 -070087
88 std::string cache_filename;
89 if (have_android_data && dalvik_cache_exists) {
90 // Always set output location even if it does not exist,
91 // so that the caller knows where to create the image.
92 //
93 // image_location = /system/framework/boot.art
94 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
95 std::string error_msg;
96 if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(),
97 &cache_filename, &error_msg)) {
98 has_cache = true;
99 }
100 }
101 if (has_system) {
102 *filename = system_filename;
103 return true;
104 } else if (has_cache) {
105 *filename = cache_filename;
106 return true;
107 } else {
108 return false;
109 }
110}
111
Alex Light53cb16b2014-06-12 11:26:29 -0700112bool PatchOat::Patch(const std::string& image_location, off_t delta,
113 File* output_image, InstructionSet isa,
Alex Lighteefbe392014-07-08 09:53:18 -0700114 TimingLogger* timings) {
Alex Light53cb16b2014-06-12 11:26:29 -0700115 CHECK(Runtime::Current() == nullptr);
116 CHECK(output_image != nullptr);
117 CHECK_GE(output_image->Fd(), 0);
118 CHECK(!image_location.empty()) << "image file must have a filename.";
119 CHECK_NE(isa, kNone);
120
Alex Lighteefbe392014-07-08 09:53:18 -0700121 TimingLogger::ScopedTiming t("Runtime Setup", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700122 const char *isa_name = GetInstructionSetString(isa);
Alex Lightcf4bf382014-07-24 11:29:14 -0700123 std::string image_filename;
124 if (!LocationToFilename(image_location, isa, &image_filename)) {
125 LOG(ERROR) << "Unable to find image at location " << image_location;
126 return false;
127 }
Alex Light53cb16b2014-06-12 11:26:29 -0700128 std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str()));
129 if (input_image.get() == nullptr) {
Alex Lightcf4bf382014-07-24 11:29:14 -0700130 LOG(ERROR) << "unable to open input image file at " << image_filename
131 << " for location " << image_location;
Alex Light53cb16b2014-06-12 11:26:29 -0700132 return false;
133 }
134 int64_t image_len = input_image->GetLength();
135 if (image_len < 0) {
136 LOG(ERROR) << "Error while getting image length";
137 return false;
138 }
139 ImageHeader image_header;
140 if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
141 sizeof(image_header), 0)) {
142 LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
143 return false;
144 }
145
146 // Set up the runtime
Ian Rogerse63db272014-07-15 15:36:11 -0700147 RuntimeOptions options;
Alex Light53cb16b2014-06-12 11:26:29 -0700148 NoopCompilerCallbacks callbacks;
149 options.push_back(std::make_pair("compilercallbacks", &callbacks));
150 std::string img = "-Ximage:" + image_location;
151 options.push_back(std::make_pair(img.c_str(), nullptr));
152 options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
153 if (!Runtime::Create(options, false)) {
154 LOG(ERROR) << "Unable to initialize runtime";
155 return false;
156 }
157 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
158 // give it away now and then switch to a more manageable ScopedObjectAccess.
159 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
160 ScopedObjectAccess soa(Thread::Current());
161
162 t.NewTiming("Image and oat Patching setup");
163 // Create the map where we will write the image patches to.
Alex Lighteefbe392014-07-08 09:53:18 -0700164 std::string error_msg;
Alex Light53cb16b2014-06-12 11:26:29 -0700165 std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
166 input_image->Fd(), 0,
167 input_image->GetPath().c_str(),
168 &error_msg));
169 if (image.get() == nullptr) {
170 LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg;
171 return false;
172 }
173 gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace();
174
175 PatchOat p(image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(),
176 delta, timings);
177 t.NewTiming("Patching files");
178 if (!p.PatchImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700179 LOG(ERROR) << "Failed to patch image file " << input_image->GetPath();
Alex Light53cb16b2014-06-12 11:26:29 -0700180 return false;
181 }
182
183 t.NewTiming("Writing files");
184 if (!p.WriteImage(output_image)) {
185 return false;
186 }
187 return true;
188}
189
190bool PatchOat::Patch(const File* input_oat, const std::string& image_location, off_t delta,
191 File* output_oat, File* output_image, InstructionSet isa,
Alex Lighteefbe392014-07-08 09:53:18 -0700192 TimingLogger* timings) {
Alex Light53cb16b2014-06-12 11:26:29 -0700193 CHECK(Runtime::Current() == nullptr);
194 CHECK(output_image != nullptr);
195 CHECK_GE(output_image->Fd(), 0);
196 CHECK(input_oat != nullptr);
197 CHECK(output_oat != nullptr);
198 CHECK_GE(input_oat->Fd(), 0);
199 CHECK_GE(output_oat->Fd(), 0);
200 CHECK(!image_location.empty()) << "image file must have a filename.";
201
Alex Lighteefbe392014-07-08 09:53:18 -0700202 TimingLogger::ScopedTiming t("Runtime Setup", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700203
204 if (isa == kNone) {
205 Elf32_Ehdr elf_hdr;
206 if (sizeof(elf_hdr) != input_oat->Read(reinterpret_cast<char*>(&elf_hdr), sizeof(elf_hdr), 0)) {
207 LOG(ERROR) << "unable to read elf header";
208 return false;
209 }
210 isa = ElfISAToInstructionSet(elf_hdr.e_machine);
211 }
212 const char* isa_name = GetInstructionSetString(isa);
Alex Lightcf4bf382014-07-24 11:29:14 -0700213 std::string image_filename;
214 if (!LocationToFilename(image_location, isa, &image_filename)) {
215 LOG(ERROR) << "Unable to find image at location " << image_location;
216 return false;
217 }
Alex Light53cb16b2014-06-12 11:26:29 -0700218 std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str()));
219 if (input_image.get() == nullptr) {
Alex Lightcf4bf382014-07-24 11:29:14 -0700220 LOG(ERROR) << "unable to open input image file at " << image_filename
221 << " for location " << image_location;
Alex Light53cb16b2014-06-12 11:26:29 -0700222 return false;
223 }
224 int64_t image_len = input_image->GetLength();
225 if (image_len < 0) {
226 LOG(ERROR) << "Error while getting image length";
227 return false;
228 }
229 ImageHeader image_header;
230 if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
231 sizeof(image_header), 0)) {
232 LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
233 }
234
235 // Set up the runtime
Ian Rogerse63db272014-07-15 15:36:11 -0700236 RuntimeOptions options;
Alex Light53cb16b2014-06-12 11:26:29 -0700237 NoopCompilerCallbacks callbacks;
238 options.push_back(std::make_pair("compilercallbacks", &callbacks));
239 std::string img = "-Ximage:" + image_location;
240 options.push_back(std::make_pair(img.c_str(), nullptr));
241 options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
242 if (!Runtime::Create(options, false)) {
243 LOG(ERROR) << "Unable to initialize runtime";
244 return false;
245 }
246 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
247 // give it away now and then switch to a more manageable ScopedObjectAccess.
248 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
249 ScopedObjectAccess soa(Thread::Current());
250
251 t.NewTiming("Image and oat Patching setup");
252 // Create the map where we will write the image patches to.
Alex Lighteefbe392014-07-08 09:53:18 -0700253 std::string error_msg;
Alex Light53cb16b2014-06-12 11:26:29 -0700254 std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
255 input_image->Fd(), 0,
256 input_image->GetPath().c_str(),
257 &error_msg));
258 if (image.get() == nullptr) {
259 LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg;
260 return false;
261 }
262 gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace();
263
264 std::unique_ptr<ElfFile> elf(ElfFile::Open(const_cast<File*>(input_oat),
265 PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
266 if (elf.get() == nullptr) {
267 LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
268 return false;
269 }
270
271 PatchOat p(elf.release(), image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(),
272 delta, timings);
273 t.NewTiming("Patching files");
274 if (!p.PatchElf()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700275 LOG(ERROR) << "Failed to patch oat file " << input_oat->GetPath();
Alex Light53cb16b2014-06-12 11:26:29 -0700276 return false;
277 }
278 if (!p.PatchImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700279 LOG(ERROR) << "Failed to patch image file " << input_image->GetPath();
Alex Light53cb16b2014-06-12 11:26:29 -0700280 return false;
281 }
282
283 t.NewTiming("Writing files");
284 if (!p.WriteElf(output_oat)) {
285 return false;
286 }
287 if (!p.WriteImage(output_image)) {
288 return false;
289 }
290 return true;
291}
292
293bool PatchOat::WriteElf(File* out) {
Alex Lighteefbe392014-07-08 09:53:18 -0700294 TimingLogger::ScopedTiming t("Writing Elf File", timings_);
Alex Lighta59dd802014-07-02 16:28:08 -0700295
Alex Light53cb16b2014-06-12 11:26:29 -0700296 CHECK(oat_file_.get() != nullptr);
297 CHECK(out != nullptr);
298 size_t expect = oat_file_->Size();
299 if (out->WriteFully(reinterpret_cast<char*>(oat_file_->Begin()), expect) &&
300 out->SetLength(expect) == 0) {
301 return true;
302 } else {
303 LOG(ERROR) << "Writing to oat file " << out->GetPath() << " failed.";
304 return false;
305 }
306}
307
308bool PatchOat::WriteImage(File* out) {
Alex Lighteefbe392014-07-08 09:53:18 -0700309 TimingLogger::ScopedTiming t("Writing image File", timings_);
Alex Lighta59dd802014-07-02 16:28:08 -0700310 std::string error_msg;
311
Alex Lightcf4bf382014-07-24 11:29:14 -0700312 ScopedFlock img_flock;
313 img_flock.Init(out, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700314
Alex Light53cb16b2014-06-12 11:26:29 -0700315 CHECK(image_ != nullptr);
316 CHECK(out != nullptr);
317 size_t expect = image_->Size();
318 if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) &&
319 out->SetLength(expect) == 0) {
320 return true;
321 } else {
322 LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed.";
323 return false;
324 }
325}
326
327bool PatchOat::PatchImage() {
328 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
329 CHECK_GT(image_->Size(), sizeof(ImageHeader));
330 // These are the roots from the original file.
331 mirror::Object* img_roots = image_header->GetImageRoots();
332 image_header->RelocateImage(delta_);
333
334 VisitObject(img_roots);
335 if (!image_header->IsValid()) {
336 LOG(ERROR) << "reloction renders image header invalid";
337 return false;
338 }
339
340 {
Alex Lighteefbe392014-07-08 09:53:18 -0700341 TimingLogger::ScopedTiming t("Walk Bitmap", timings_);
Alex Light53cb16b2014-06-12 11:26:29 -0700342 // Walk the bitmap.
343 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
344 bitmap_->Walk(PatchOat::BitmapCallback, this);
345 }
346 return true;
347}
348
349bool PatchOat::InHeap(mirror::Object* o) {
350 uintptr_t begin = reinterpret_cast<uintptr_t>(heap_->Begin());
351 uintptr_t end = reinterpret_cast<uintptr_t>(heap_->End());
352 uintptr_t obj = reinterpret_cast<uintptr_t>(o);
353 return o == nullptr || (begin <= obj && obj < end);
354}
355
356void PatchOat::PatchVisitor::operator() (mirror::Object* obj, MemberOffset off,
357 bool is_static_unused) const {
358 mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off);
359 DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
360 mirror::Object* moved_object = patcher_->RelocatedAddressOf(referent);
361 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
362}
363
364void PatchOat::PatchVisitor::operator() (mirror::Class* cls, mirror::Reference* ref) const {
365 MemberOffset off = mirror::Reference::ReferentOffset();
366 mirror::Object* referent = ref->GetReferent();
367 DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
368 mirror::Object* moved_object = patcher_->RelocatedAddressOf(referent);
369 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
370}
371
372mirror::Object* PatchOat::RelocatedCopyOf(mirror::Object* obj) {
373 if (obj == nullptr) {
374 return nullptr;
375 }
376 DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin()));
377 DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End()));
378 uintptr_t heap_off =
379 reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin());
380 DCHECK_LT(heap_off, image_->Size());
381 return reinterpret_cast<mirror::Object*>(image_->Begin() + heap_off);
382}
383
384mirror::Object* PatchOat::RelocatedAddressOf(mirror::Object* obj) {
385 if (obj == nullptr) {
386 return nullptr;
387 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700388 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uint8_t*>(obj) + delta_);
Alex Light53cb16b2014-06-12 11:26:29 -0700389 }
390}
391
392// Called by BitmapCallback
393void PatchOat::VisitObject(mirror::Object* object) {
394 mirror::Object* copy = RelocatedCopyOf(object);
395 CHECK(copy != nullptr);
396 if (kUseBakerOrBrooksReadBarrier) {
397 object->AssertReadBarrierPointer();
398 if (kUseBrooksReadBarrier) {
399 mirror::Object* moved_to = RelocatedAddressOf(object);
400 copy->SetReadBarrierPointer(moved_to);
401 DCHECK_EQ(copy->GetReadBarrierPointer(), moved_to);
402 }
403 }
404 PatchOat::PatchVisitor visitor(this, copy);
405 object->VisitReferences<true, kVerifyNone>(visitor, visitor);
406 if (object->IsArtMethod<kVerifyNone>()) {
407 FixupMethod(static_cast<mirror::ArtMethod*>(object),
408 static_cast<mirror::ArtMethod*>(copy));
409 }
410}
411
412void PatchOat::FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy) {
413 // Just update the entry points if it looks like we should.
Alex Lighteefbe392014-07-08 09:53:18 -0700414 // TODO: sanity check all the pointers' values
Alex Light53cb16b2014-06-12 11:26:29 -0700415 uintptr_t portable = reinterpret_cast<uintptr_t>(
416 object->GetEntryPointFromPortableCompiledCode<kVerifyNone>());
417 if (portable != 0) {
418 copy->SetEntryPointFromPortableCompiledCode(reinterpret_cast<void*>(portable + delta_));
419 }
420 uintptr_t quick= reinterpret_cast<uintptr_t>(
421 object->GetEntryPointFromQuickCompiledCode<kVerifyNone>());
422 if (quick != 0) {
423 copy->SetEntryPointFromQuickCompiledCode(reinterpret_cast<void*>(quick + delta_));
424 }
425 uintptr_t interpreter = reinterpret_cast<uintptr_t>(
426 object->GetEntryPointFromInterpreter<kVerifyNone>());
427 if (interpreter != 0) {
428 copy->SetEntryPointFromInterpreter(
429 reinterpret_cast<mirror::EntryPointFromInterpreter*>(interpreter + delta_));
430 }
431
432 uintptr_t native_method = reinterpret_cast<uintptr_t>(object->GetNativeMethod());
433 if (native_method != 0) {
434 copy->SetNativeMethod(reinterpret_cast<void*>(native_method + delta_));
435 }
436
437 uintptr_t native_gc_map = reinterpret_cast<uintptr_t>(object->GetNativeGcMap());
438 if (native_gc_map != 0) {
439 copy->SetNativeGcMap(reinterpret_cast<uint8_t*>(native_gc_map + delta_));
440 }
441}
442
Alex Lighteefbe392014-07-08 09:53:18 -0700443bool PatchOat::Patch(File* input_oat, off_t delta, File* output_oat, TimingLogger* timings) {
Alex Light53cb16b2014-06-12 11:26:29 -0700444 CHECK(input_oat != nullptr);
445 CHECK(output_oat != nullptr);
446 CHECK_GE(input_oat->Fd(), 0);
447 CHECK_GE(output_oat->Fd(), 0);
Alex Lighteefbe392014-07-08 09:53:18 -0700448 TimingLogger::ScopedTiming t("Setup Oat File Patching", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700449
450 std::string error_msg;
451 std::unique_ptr<ElfFile> elf(ElfFile::Open(const_cast<File*>(input_oat),
452 PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
453 if (elf.get() == nullptr) {
454 LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
455 return false;
456 }
457
458 PatchOat p(elf.release(), delta, timings);
459 t.NewTiming("Patch Oat file");
460 if (!p.PatchElf()) {
461 return false;
462 }
463
464 t.NewTiming("Writing oat file");
465 if (!p.WriteElf(output_oat)) {
466 return false;
467 }
468 return true;
469}
470
Tong Shen62d1ca32014-09-03 17:24:56 -0700471template <typename ElfFileImpl, typename ptr_t>
472bool PatchOat::CheckOatFile(ElfFileImpl* oat_file) {
473 auto patches_sec = oat_file->FindSectionByName(".oat_patches");
474 if (patches_sec->sh_type != SHT_OAT_PATCH) {
Alex Light53cb16b2014-06-12 11:26:29 -0700475 return false;
476 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700477 ptr_t* patches = reinterpret_cast<ptr_t*>(oat_file->Begin() + patches_sec->sh_offset);
478 ptr_t* patches_end = patches + (patches_sec->sh_size / sizeof(ptr_t));
479 auto oat_data_sec = oat_file->FindSectionByName(".rodata");
480 auto oat_text_sec = oat_file->FindSectionByName(".text");
Alex Light53cb16b2014-06-12 11:26:29 -0700481 if (oat_data_sec == nullptr) {
482 return false;
483 }
484 if (oat_text_sec == nullptr) {
485 return false;
486 }
487 if (oat_text_sec->sh_offset <= oat_data_sec->sh_offset) {
488 return false;
489 }
490
491 for (; patches < patches_end; patches++) {
492 if (oat_text_sec->sh_size <= *patches) {
493 return false;
494 }
495 }
496
497 return true;
498}
499
Tong Shen62d1ca32014-09-03 17:24:56 -0700500template <typename ElfFileImpl>
501bool PatchOat::PatchOatHeader(ElfFileImpl* oat_file) {
502 auto rodata_sec = oat_file->FindSectionByName(".rodata");
Alex Lighta59dd802014-07-02 16:28:08 -0700503 if (rodata_sec == nullptr) {
504 return false;
505 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700506 OatHeader* oat_header = reinterpret_cast<OatHeader*>(oat_file->Begin() + rodata_sec->sh_offset);
Alex Lighta59dd802014-07-02 16:28:08 -0700507 if (!oat_header->IsValid()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700508 LOG(ERROR) << "Elf file " << oat_file->GetFile().GetPath() << " has an invalid oat header";
Alex Lighta59dd802014-07-02 16:28:08 -0700509 return false;
510 }
511 oat_header->RelocateOat(delta_);
512 return true;
513}
514
Alex Light53cb16b2014-06-12 11:26:29 -0700515bool PatchOat::PatchElf() {
Tong Shen62d1ca32014-09-03 17:24:56 -0700516 if (oat_file_->is_elf64_)
517 return PatchElf<ElfFileImpl64>(oat_file_->GetImpl64());
518 else
519 return PatchElf<ElfFileImpl32>(oat_file_->GetImpl32());
520}
521
522template <typename ElfFileImpl>
523bool PatchOat::PatchElf(ElfFileImpl* oat_file) {
Alex Lighta59dd802014-07-02 16:28:08 -0700524 TimingLogger::ScopedTiming t("Fixup Elf Text Section", timings_);
Tong Shen62d1ca32014-09-03 17:24:56 -0700525 if (!PatchTextSection<ElfFileImpl>(oat_file)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700526 return false;
527 }
528
Tong Shen62d1ca32014-09-03 17:24:56 -0700529 if (!PatchOatHeader<ElfFileImpl>(oat_file)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700530 return false;
531 }
532
533 bool need_fixup = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700534 for (unsigned int i = 0; i < oat_file->GetProgramHeaderNum(); i++) {
535 auto hdr = oat_file->GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700536 if (hdr->p_vaddr != 0 && hdr->p_vaddr != hdr->p_offset) {
Alex Lighta59dd802014-07-02 16:28:08 -0700537 need_fixup = true;
Alex Light53cb16b2014-06-12 11:26:29 -0700538 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700539 if (hdr->p_paddr != 0 && hdr->p_paddr != hdr->p_offset) {
Alex Lighta59dd802014-07-02 16:28:08 -0700540 need_fixup = true;
Alex Light53cb16b2014-06-12 11:26:29 -0700541 }
542 }
Alex Lighta59dd802014-07-02 16:28:08 -0700543 if (!need_fixup) {
544 // This was never passed through ElfFixup so all headers/symbols just have their offset as
545 // their addr. Therefore we do not need to update these parts.
546 return true;
547 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700548
549 t.NewTiming("Fixup Elf Headers");
550 // Fixup Phdr's
551 oat_file->FixupProgramHeaders(delta_);
552
Alex Lighta59dd802014-07-02 16:28:08 -0700553 t.NewTiming("Fixup Section Headers");
Tong Shen62d1ca32014-09-03 17:24:56 -0700554 // Fixup Shdr's
555 oat_file->FixupSectionHeaders(delta_);
Alex Light53cb16b2014-06-12 11:26:29 -0700556
Alex Lighta59dd802014-07-02 16:28:08 -0700557 t.NewTiming("Fixup Dynamics");
Tong Shen62d1ca32014-09-03 17:24:56 -0700558 oat_file->FixupDynamic(delta_);
Alex Light53cb16b2014-06-12 11:26:29 -0700559
560 t.NewTiming("Fixup Elf Symbols");
561 // Fixup dynsym
Tong Shen62d1ca32014-09-03 17:24:56 -0700562 if (!oat_file->FixupSymbols(delta_, true)) {
Alex Light53cb16b2014-06-12 11:26:29 -0700563 return false;
564 }
Alex Light53cb16b2014-06-12 11:26:29 -0700565 // Fixup symtab
Tong Shen62d1ca32014-09-03 17:24:56 -0700566 if (!oat_file->FixupSymbols(delta_, false)) {
567 return false;
Alex Light53cb16b2014-06-12 11:26:29 -0700568 }
569
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700570 t.NewTiming("Fixup Debug Sections");
Tong Shen62d1ca32014-09-03 17:24:56 -0700571 if (!oat_file->FixupDebugSections(delta_)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700572 return false;
573 }
574
Alex Light53cb16b2014-06-12 11:26:29 -0700575 return true;
576}
577
Tong Shen62d1ca32014-09-03 17:24:56 -0700578template <typename ElfFileImpl>
579bool PatchOat::PatchTextSection(ElfFileImpl* oat_file) {
580 auto patches_sec = oat_file->FindSectionByName(".oat_patches");
Alex Light53cb16b2014-06-12 11:26:29 -0700581 if (patches_sec == nullptr) {
Alex Lighta59dd802014-07-02 16:28:08 -0700582 LOG(ERROR) << ".oat_patches section not found. Aborting patch";
Alex Light53cb16b2014-06-12 11:26:29 -0700583 return false;
584 }
Alex Light4b0d2d92014-08-06 13:37:23 -0700585 if (patches_sec->sh_type != SHT_OAT_PATCH) {
586 LOG(ERROR) << "Unexpected type of .oat_patches";
587 return false;
588 }
589
590 switch (patches_sec->sh_entsize) {
591 case sizeof(uint32_t):
Tong Shen62d1ca32014-09-03 17:24:56 -0700592 return PatchTextSection<ElfFileImpl, uint32_t>(oat_file);
Alex Light4b0d2d92014-08-06 13:37:23 -0700593 case sizeof(uint64_t):
Tong Shen62d1ca32014-09-03 17:24:56 -0700594 return PatchTextSection<ElfFileImpl, uint64_t>(oat_file);
Alex Light4b0d2d92014-08-06 13:37:23 -0700595 default:
596 LOG(ERROR) << ".oat_patches Entsize of " << patches_sec->sh_entsize << "bits "
597 << "is not valid";
598 return false;
599 }
600}
601
Tong Shen62d1ca32014-09-03 17:24:56 -0700602template <typename ElfFileImpl, typename patch_loc_t>
603bool PatchOat::PatchTextSection(ElfFileImpl* oat_file) {
604 bool oat_file_valid = CheckOatFile<ElfFileImpl, patch_loc_t>(oat_file);
605 CHECK(oat_file_valid) << "Oat file invalid";
606 auto patches_sec = oat_file->FindSectionByName(".oat_patches");
607 patch_loc_t* patches = reinterpret_cast<patch_loc_t*>(oat_file->Begin() + patches_sec->sh_offset);
608 patch_loc_t* patches_end = patches + (patches_sec->sh_size / sizeof(patch_loc_t));
609 auto oat_text_sec = oat_file->FindSectionByName(".text");
Alex Light53cb16b2014-06-12 11:26:29 -0700610 CHECK(oat_text_sec != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700611 uint8_t* to_patch = oat_file->Begin() + oat_text_sec->sh_offset;
Alex Light53cb16b2014-06-12 11:26:29 -0700612 uintptr_t to_patch_end = reinterpret_cast<uintptr_t>(to_patch) + oat_text_sec->sh_size;
613
614 for (; patches < patches_end; patches++) {
615 CHECK_LT(*patches, oat_text_sec->sh_size) << "Bad Patch";
616 uint32_t* patch_loc = reinterpret_cast<uint32_t*>(to_patch + *patches);
617 CHECK_LT(reinterpret_cast<uintptr_t>(patch_loc), to_patch_end);
618 *patch_loc += delta_;
619 }
Alex Light53cb16b2014-06-12 11:26:29 -0700620 return true;
621}
622
623static int orig_argc;
624static char** orig_argv;
625
626static std::string CommandLine() {
627 std::vector<std::string> command;
628 for (int i = 0; i < orig_argc; ++i) {
629 command.push_back(orig_argv[i]);
630 }
631 return Join(command, ' ');
632}
633
634static void UsageErrorV(const char* fmt, va_list ap) {
635 std::string error;
636 StringAppendV(&error, fmt, ap);
637 LOG(ERROR) << error;
638}
639
640static void UsageError(const char* fmt, ...) {
641 va_list ap;
642 va_start(ap, fmt);
643 UsageErrorV(fmt, ap);
644 va_end(ap);
645}
646
Ian Rogers7223d442014-10-10 20:05:39 -0700647[[noreturn]] static void Usage(const char *fmt, ...) {
Alex Light53cb16b2014-06-12 11:26:29 -0700648 va_list ap;
649 va_start(ap, fmt);
650 UsageErrorV(fmt, ap);
651 va_end(ap);
652
653 UsageError("Command: %s", CommandLine().c_str());
654 UsageError("Usage: patchoat [options]...");
655 UsageError("");
656 UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is");
657 UsageError(" compiled for. Required if you use --input-oat-location");
658 UsageError("");
659 UsageError(" --input-oat-file=<file.oat>: Specifies the exact filename of the oat file to be");
660 UsageError(" patched.");
661 UsageError("");
662 UsageError(" --input-oat-fd=<file-descriptor>: Specifies the file-descriptor of the oat file");
663 UsageError(" to be patched.");
664 UsageError("");
665 UsageError(" --input-oat-location=<file.oat>: Specifies the 'location' to read the patched");
666 UsageError(" oat file from. If used one must also supply the --instruction-set");
667 UsageError("");
668 UsageError(" --input-image-location=<file.art>: Specifies the 'location' of the image file to");
669 UsageError(" be patched. If --instruction-set is not given it will use the instruction set");
670 UsageError(" extracted from the --input-oat-file.");
671 UsageError("");
672 UsageError(" --output-oat-file=<file.oat>: Specifies the exact file to write the patched oat");
673 UsageError(" file to.");
674 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700675 UsageError(" --output-oat-fd=<file-descriptor>: Specifies the file-descriptor to write the");
676 UsageError(" the patched oat file to.");
677 UsageError("");
678 UsageError(" --output-image-file=<file.art>: Specifies the exact file to write the patched");
679 UsageError(" image file to.");
680 UsageError("");
681 UsageError(" --output-image-fd=<file-descriptor>: Specifies the file-descriptor to write the");
682 UsageError(" the patched image file to.");
683 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700684 UsageError(" --orig-base-offset=<original-base-offset>: Specify the base offset the input file");
685 UsageError(" was compiled with. This is needed if one is specifying a --base-offset");
686 UsageError("");
687 UsageError(" --base-offset=<new-base-offset>: Specify the base offset we will repatch the");
688 UsageError(" given files to use. This requires that --orig-base-offset is also given.");
689 UsageError("");
690 UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by.");
691 UsageError(" This value may be negative.");
692 UsageError("");
693 UsageError(" --patched-image-file=<file.art>: Use the same patch delta as was used to patch");
694 UsageError(" the given image file.");
695 UsageError("");
696 UsageError(" --patched-image-location=<file.art>: Use the same patch delta as was used to");
697 UsageError(" patch the given image location. If used one must also specify the");
Alex Lighta59dd802014-07-02 16:28:08 -0700698 UsageError(" --instruction-set flag. It will search for this image in the same way that");
699 UsageError(" is done when loading one.");
Alex Light53cb16b2014-06-12 11:26:29 -0700700 UsageError("");
Alex Lightcf4bf382014-07-24 11:29:14 -0700701 UsageError(" --lock-output: Obtain a flock on output oat file before starting.");
702 UsageError("");
703 UsageError(" --no-lock-output: Do not attempt to obtain a flock on output oat file.");
704 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700705 UsageError(" --dump-timings: dump out patch timing information");
706 UsageError("");
707 UsageError(" --no-dump-timings: do not dump out patch timing information");
708 UsageError("");
709
710 exit(EXIT_FAILURE);
711}
712
Alex Lighteefbe392014-07-08 09:53:18 -0700713static bool ReadBaseDelta(const char* name, off_t* delta, std::string* error_msg) {
Alex Light53cb16b2014-06-12 11:26:29 -0700714 CHECK(name != nullptr);
715 CHECK(delta != nullptr);
716 std::unique_ptr<File> file;
717 if (OS::FileExists(name)) {
718 file.reset(OS::OpenFileForReading(name));
719 if (file.get() == nullptr) {
Alex Lighteefbe392014-07-08 09:53:18 -0700720 *error_msg = "Failed to open file %s for reading";
Alex Light53cb16b2014-06-12 11:26:29 -0700721 return false;
722 }
723 } else {
Alex Lighteefbe392014-07-08 09:53:18 -0700724 *error_msg = "File %s does not exist";
Alex Light53cb16b2014-06-12 11:26:29 -0700725 return false;
726 }
727 CHECK(file.get() != nullptr);
728 ImageHeader hdr;
729 if (sizeof(hdr) != file->Read(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0)) {
Alex Lighteefbe392014-07-08 09:53:18 -0700730 *error_msg = "Failed to read file %s";
Alex Light53cb16b2014-06-12 11:26:29 -0700731 return false;
732 }
733 if (!hdr.IsValid()) {
Alex Lighteefbe392014-07-08 09:53:18 -0700734 *error_msg = "%s does not contain a valid image header.";
Alex Light53cb16b2014-06-12 11:26:29 -0700735 return false;
736 }
737 *delta = hdr.GetPatchDelta();
738 return true;
739}
740
741static File* CreateOrOpen(const char* name, bool* created) {
742 if (OS::FileExists(name)) {
743 *created = false;
744 return OS::OpenFileReadWrite(name);
745 } else {
746 *created = true;
Alex Lightcf4bf382014-07-24 11:29:14 -0700747 std::unique_ptr<File> f(OS::CreateEmptyFile(name));
748 if (f.get() != nullptr) {
749 if (fchmod(f->Fd(), 0644) != 0) {
750 PLOG(ERROR) << "Unable to make " << name << " world readable";
Brian Carlstrom8c52a3f2014-09-30 16:18:01 -0700751 TEMP_FAILURE_RETRY(unlink(name));
Alex Lightcf4bf382014-07-24 11:29:14 -0700752 return nullptr;
753 }
754 }
755 return f.release();
Alex Light53cb16b2014-06-12 11:26:29 -0700756 }
757}
758
Alex Lighteefbe392014-07-08 09:53:18 -0700759static int patchoat(int argc, char **argv) {
Alex Light53cb16b2014-06-12 11:26:29 -0700760 InitLogging(argv);
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700761 MemMap::Init();
Alex Light53cb16b2014-06-12 11:26:29 -0700762 const bool debug = kIsDebugBuild;
763 orig_argc = argc;
764 orig_argv = argv;
765 TimingLogger timings("patcher", false, false);
766
767 InitLogging(argv);
768
769 // Skip over the command name.
770 argv++;
771 argc--;
772
773 if (argc == 0) {
774 Usage("No arguments specified");
775 }
776
777 timings.StartTiming("Patchoat");
778
779 // cmd line args
780 bool isa_set = false;
781 InstructionSet isa = kNone;
782 std::string input_oat_filename;
783 std::string input_oat_location;
784 int input_oat_fd = -1;
785 bool have_input_oat = false;
786 std::string input_image_location;
787 std::string output_oat_filename;
Alex Light53cb16b2014-06-12 11:26:29 -0700788 int output_oat_fd = -1;
789 bool have_output_oat = false;
790 std::string output_image_filename;
Alex Light53cb16b2014-06-12 11:26:29 -0700791 int output_image_fd = -1;
792 bool have_output_image = false;
793 uintptr_t base_offset = 0;
794 bool base_offset_set = false;
795 uintptr_t orig_base_offset = 0;
796 bool orig_base_offset_set = false;
797 off_t base_delta = 0;
798 bool base_delta_set = false;
799 std::string patched_image_filename;
800 std::string patched_image_location;
801 bool dump_timings = kIsDebugBuild;
Alex Lightcf4bf382014-07-24 11:29:14 -0700802 bool lock_output = true;
Alex Light53cb16b2014-06-12 11:26:29 -0700803
804 for (int i = 0; i < argc; i++) {
805 const StringPiece option(argv[i]);
806 const bool log_options = false;
807 if (log_options) {
808 LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i];
809 }
Alex Light53cb16b2014-06-12 11:26:29 -0700810 if (option.starts_with("--instruction-set=")) {
811 isa_set = true;
812 const char* isa_str = option.substr(strlen("--instruction-set=")).data();
Andreas Gampe20c89302014-08-19 17:28:06 -0700813 isa = GetInstructionSetFromString(isa_str);
814 if (isa == kNone) {
815 Usage("Unknown or invalid instruction set %s", isa_str);
Alex Light53cb16b2014-06-12 11:26:29 -0700816 }
817 } else if (option.starts_with("--input-oat-location=")) {
818 if (have_input_oat) {
819 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
820 }
821 have_input_oat = true;
822 input_oat_location = option.substr(strlen("--input-oat-location=")).data();
823 } else if (option.starts_with("--input-oat-file=")) {
824 if (have_input_oat) {
825 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
826 }
827 have_input_oat = true;
828 input_oat_filename = option.substr(strlen("--input-oat-file=")).data();
829 } else if (option.starts_with("--input-oat-fd=")) {
830 if (have_input_oat) {
831 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
832 }
833 have_input_oat = true;
834 const char* oat_fd_str = option.substr(strlen("--input-oat-fd=")).data();
835 if (!ParseInt(oat_fd_str, &input_oat_fd)) {
836 Usage("Failed to parse --input-oat-fd argument '%s' as an integer", oat_fd_str);
837 }
838 if (input_oat_fd < 0) {
839 Usage("--input-oat-fd pass a negative value %d", input_oat_fd);
840 }
841 } else if (option.starts_with("--input-image-location=")) {
842 input_image_location = option.substr(strlen("--input-image-location=")).data();
Alex Light53cb16b2014-06-12 11:26:29 -0700843 } else if (option.starts_with("--output-oat-file=")) {
844 if (have_output_oat) {
Alex Lightcf4bf382014-07-24 11:29:14 -0700845 Usage("Only one of --output-oat-file, and --output-oat-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -0700846 }
847 have_output_oat = true;
848 output_oat_filename = option.substr(strlen("--output-oat-file=")).data();
849 } else if (option.starts_with("--output-oat-fd=")) {
850 if (have_output_oat) {
Alex Lightcf4bf382014-07-24 11:29:14 -0700851 Usage("Only one of --output-oat-file, --output-oat-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -0700852 }
853 have_output_oat = true;
854 const char* oat_fd_str = option.substr(strlen("--output-oat-fd=")).data();
855 if (!ParseInt(oat_fd_str, &output_oat_fd)) {
856 Usage("Failed to parse --output-oat-fd argument '%s' as an integer", oat_fd_str);
857 }
858 if (output_oat_fd < 0) {
859 Usage("--output-oat-fd pass a negative value %d", output_oat_fd);
860 }
Alex Light53cb16b2014-06-12 11:26:29 -0700861 } else if (option.starts_with("--output-image-file=")) {
862 if (have_output_image) {
Alex Lightcf4bf382014-07-24 11:29:14 -0700863 Usage("Only one of --output-image-file, and --output-image-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -0700864 }
865 have_output_image = true;
866 output_image_filename = option.substr(strlen("--output-image-file=")).data();
867 } else if (option.starts_with("--output-image-fd=")) {
868 if (have_output_image) {
Alex Lightcf4bf382014-07-24 11:29:14 -0700869 Usage("Only one of --output-image-file, and --output-image-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -0700870 }
871 have_output_image = true;
872 const char* image_fd_str = option.substr(strlen("--output-image-fd=")).data();
873 if (!ParseInt(image_fd_str, &output_image_fd)) {
874 Usage("Failed to parse --output-image-fd argument '%s' as an integer", image_fd_str);
875 }
876 if (output_image_fd < 0) {
877 Usage("--output-image-fd pass a negative value %d", output_image_fd);
878 }
879 } else if (option.starts_with("--orig-base-offset=")) {
880 const char* orig_base_offset_str = option.substr(strlen("--orig-base-offset=")).data();
881 orig_base_offset_set = true;
882 if (!ParseUint(orig_base_offset_str, &orig_base_offset)) {
883 Usage("Failed to parse --orig-base-offset argument '%s' as an uintptr_t",
884 orig_base_offset_str);
885 }
886 } else if (option.starts_with("--base-offset=")) {
887 const char* base_offset_str = option.substr(strlen("--base-offset=")).data();
888 base_offset_set = true;
889 if (!ParseUint(base_offset_str, &base_offset)) {
890 Usage("Failed to parse --base-offset argument '%s' as an uintptr_t", base_offset_str);
891 }
892 } else if (option.starts_with("--base-offset-delta=")) {
893 const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data();
894 base_delta_set = true;
895 if (!ParseInt(base_delta_str, &base_delta)) {
896 Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str);
897 }
898 } else if (option.starts_with("--patched-image-location=")) {
899 patched_image_location = option.substr(strlen("--patched-image-location=")).data();
900 } else if (option.starts_with("--patched-image-file=")) {
901 patched_image_filename = option.substr(strlen("--patched-image-file=")).data();
Alex Lightcf4bf382014-07-24 11:29:14 -0700902 } else if (option == "--lock-output") {
903 lock_output = true;
904 } else if (option == "--no-lock-output") {
905 lock_output = false;
Alex Light53cb16b2014-06-12 11:26:29 -0700906 } else if (option == "--dump-timings") {
907 dump_timings = true;
908 } else if (option == "--no-dump-timings") {
909 dump_timings = false;
910 } else {
911 Usage("Unknown argument %s", option.data());
912 }
913 }
914
915 {
916 // Only 1 of these may be set.
917 uint32_t cnt = 0;
918 cnt += (base_delta_set) ? 1 : 0;
919 cnt += (base_offset_set && orig_base_offset_set) ? 1 : 0;
920 cnt += (!patched_image_filename.empty()) ? 1 : 0;
921 cnt += (!patched_image_location.empty()) ? 1 : 0;
922 if (cnt > 1) {
923 Usage("Only one of --base-offset/--orig-base-offset, --base-offset-delta, "
924 "--patched-image-filename or --patched-image-location may be used.");
925 } else if (cnt == 0) {
926 Usage("Must specify --base-offset-delta, --base-offset and --orig-base-offset, "
927 "--patched-image-location or --patched-image-file");
928 }
929 }
930
931 if (have_input_oat != have_output_oat) {
932 Usage("Either both input and output oat must be supplied or niether must be.");
933 }
934
935 if ((!input_image_location.empty()) != have_output_image) {
936 Usage("Either both input and output image must be supplied or niether must be.");
937 }
938
939 // We know we have both the input and output so rename for clarity.
940 bool have_image_files = have_output_image;
941 bool have_oat_files = have_output_oat;
942
943 if (!have_oat_files && !have_image_files) {
944 Usage("Must be patching either an oat or an image file or both.");
945 }
946
947 if (!have_oat_files && !isa_set) {
948 Usage("Must include ISA if patching an image file without an oat file.");
949 }
950
951 if (!input_oat_location.empty()) {
952 if (!isa_set) {
953 Usage("specifying a location requires specifying an instruction set");
954 }
Alex Lightcf4bf382014-07-24 11:29:14 -0700955 if (!LocationToFilename(input_oat_location, isa, &input_oat_filename)) {
956 Usage("Unable to find filename for input oat location %s", input_oat_location.c_str());
957 }
Alex Light53cb16b2014-06-12 11:26:29 -0700958 if (debug) {
959 LOG(INFO) << "Using input-oat-file " << input_oat_filename;
960 }
961 }
Alex Light53cb16b2014-06-12 11:26:29 -0700962 if (!patched_image_location.empty()) {
963 if (!isa_set) {
964 Usage("specifying a location requires specifying an instruction set");
965 }
Alex Lighta59dd802014-07-02 16:28:08 -0700966 std::string system_filename;
967 bool has_system = false;
968 std::string cache_filename;
969 bool has_cache = false;
970 bool has_android_data_unused = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700971 bool is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700972 if (!gc::space::ImageSpace::FindImageFilename(patched_image_location.c_str(), isa,
973 &system_filename, &has_system, &cache_filename,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700974 &has_android_data_unused, &has_cache,
975 &is_global_cache)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700976 Usage("Unable to determine image file for location %s", patched_image_location.c_str());
977 }
978 if (has_cache) {
979 patched_image_filename = cache_filename;
980 } else if (has_system) {
981 LOG(WARNING) << "Only image file found was in /system for image location "
982 << patched_image_location;
983 patched_image_filename = system_filename;
984 } else {
985 Usage("Unable to determine image file for location %s", patched_image_location.c_str());
986 }
Alex Light53cb16b2014-06-12 11:26:29 -0700987 if (debug) {
988 LOG(INFO) << "Using patched-image-file " << patched_image_filename;
989 }
990 }
991
992 if (!base_delta_set) {
993 if (orig_base_offset_set && base_offset_set) {
994 base_delta_set = true;
995 base_delta = base_offset - orig_base_offset;
996 } else if (!patched_image_filename.empty()) {
997 base_delta_set = true;
998 std::string error_msg;
Alex Lighteefbe392014-07-08 09:53:18 -0700999 if (!ReadBaseDelta(patched_image_filename.c_str(), &base_delta, &error_msg)) {
Alex Light53cb16b2014-06-12 11:26:29 -07001000 Usage(error_msg.c_str(), patched_image_filename.c_str());
1001 }
1002 } else {
1003 if (base_offset_set) {
1004 Usage("Unable to determine original base offset.");
1005 } else {
1006 Usage("Must supply a desired new offset or delta.");
1007 }
1008 }
1009 }
1010
1011 if (!IsAligned<kPageSize>(base_delta)) {
1012 Usage("Base offset/delta must be alligned to a pagesize (0x%08x) boundary.", kPageSize);
1013 }
1014
1015 // Do we need to cleanup output files if we fail?
1016 bool new_image_out = false;
1017 bool new_oat_out = false;
1018
1019 std::unique_ptr<File> input_oat;
1020 std::unique_ptr<File> output_oat;
1021 std::unique_ptr<File> output_image;
1022
1023 if (have_image_files) {
1024 CHECK(!input_image_location.empty());
1025
1026 if (output_image_fd != -1) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001027 if (output_image_filename.empty()) {
1028 output_image_filename = "output-image-file";
1029 }
Alex Light53cb16b2014-06-12 11:26:29 -07001030 output_image.reset(new File(output_image_fd, output_image_filename));
1031 } else {
1032 CHECK(!output_image_filename.empty());
1033 output_image.reset(CreateOrOpen(output_image_filename.c_str(), &new_image_out));
1034 }
1035 } else {
1036 CHECK(output_image_filename.empty() && output_image_fd == -1 && input_image_location.empty());
1037 }
1038
1039 if (have_oat_files) {
1040 if (input_oat_fd != -1) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001041 if (input_oat_filename.empty()) {
1042 input_oat_filename = "input-oat-file";
1043 }
Alex Light53cb16b2014-06-12 11:26:29 -07001044 input_oat.reset(new File(input_oat_fd, input_oat_filename));
1045 } else {
1046 CHECK(!input_oat_filename.empty());
1047 input_oat.reset(OS::OpenFileForReading(input_oat_filename.c_str()));
Andreas Gampe1c83cbc2014-07-22 18:52:29 -07001048 if (input_oat.get() == nullptr) {
1049 LOG(ERROR) << "Could not open input oat file: " << strerror(errno);
1050 }
Alex Light53cb16b2014-06-12 11:26:29 -07001051 }
1052
1053 if (output_oat_fd != -1) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001054 if (output_oat_filename.empty()) {
1055 output_oat_filename = "output-oat-file";
Alex Lighta59dd802014-07-02 16:28:08 -07001056 }
Alex Lightcf4bf382014-07-24 11:29:14 -07001057 output_oat.reset(new File(output_oat_fd, output_oat_filename));
Alex Light53cb16b2014-06-12 11:26:29 -07001058 } else {
1059 CHECK(!output_oat_filename.empty());
1060 output_oat.reset(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out));
1061 }
1062 }
1063
1064 auto cleanup = [&output_image_filename, &output_oat_filename,
1065 &new_oat_out, &new_image_out, &timings, &dump_timings](bool success) {
1066 timings.EndTiming();
1067 if (!success) {
1068 if (new_oat_out) {
1069 CHECK(!output_oat_filename.empty());
Brian Carlstrom8c52a3f2014-09-30 16:18:01 -07001070 TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str()));
Alex Light53cb16b2014-06-12 11:26:29 -07001071 }
1072 if (new_image_out) {
1073 CHECK(!output_image_filename.empty());
Brian Carlstrom8c52a3f2014-09-30 16:18:01 -07001074 TEMP_FAILURE_RETRY(unlink(output_image_filename.c_str()));
Alex Light53cb16b2014-06-12 11:26:29 -07001075 }
1076 }
1077 if (dump_timings) {
1078 LOG(INFO) << Dumpable<TimingLogger>(timings);
1079 }
1080 };
1081
Alex Lightcf4bf382014-07-24 11:29:14 -07001082 if ((have_oat_files && (input_oat.get() == nullptr || output_oat.get() == nullptr)) ||
1083 (have_image_files && output_image.get() == nullptr)) {
1084 cleanup(false);
1085 return EXIT_FAILURE;
1086 }
1087
1088 ScopedFlock output_oat_lock;
1089 if (lock_output) {
1090 std::string error_msg;
1091 if (have_oat_files && !output_oat_lock.Init(output_oat.get(), &error_msg)) {
1092 LOG(ERROR) << "Unable to lock output oat " << output_image->GetPath() << ": " << error_msg;
1093 cleanup(false);
1094 return EXIT_FAILURE;
1095 }
1096 }
1097
Alex Light53cb16b2014-06-12 11:26:29 -07001098 if (debug) {
Alex Lighta59dd802014-07-02 16:28:08 -07001099 LOG(INFO) << "moving offset by " << base_delta
1100 << " (0x" << std::hex << base_delta << ") bytes or "
1101 << std::dec << (base_delta/kPageSize) << " pages.";
Alex Light53cb16b2014-06-12 11:26:29 -07001102 }
1103
1104 bool ret;
1105 if (have_image_files && have_oat_files) {
1106 TimingLogger::ScopedTiming pt("patch image and oat", &timings);
1107 ret = PatchOat::Patch(input_oat.get(), input_image_location, base_delta,
Alex Lighteefbe392014-07-08 09:53:18 -07001108 output_oat.get(), output_image.get(), isa, &timings);
Alex Light53cb16b2014-06-12 11:26:29 -07001109 } else if (have_oat_files) {
1110 TimingLogger::ScopedTiming pt("patch oat", &timings);
Alex Lighteefbe392014-07-08 09:53:18 -07001111 ret = PatchOat::Patch(input_oat.get(), base_delta, output_oat.get(), &timings);
Alex Light53cb16b2014-06-12 11:26:29 -07001112 } else {
1113 TimingLogger::ScopedTiming pt("patch image", &timings);
1114 CHECK(have_image_files);
Alex Lighteefbe392014-07-08 09:53:18 -07001115 ret = PatchOat::Patch(input_image_location, base_delta, output_image.get(), isa, &timings);
Alex Light53cb16b2014-06-12 11:26:29 -07001116 }
1117 cleanup(ret);
Alex Light53cb16b2014-06-12 11:26:29 -07001118 return (ret) ? EXIT_SUCCESS : EXIT_FAILURE;
1119}
1120
1121} // namespace art
1122
1123int main(int argc, char **argv) {
1124 return art::patchoat(argc, argv);
1125}