blob: 687ebe817ba5c2c8cff06966bcaa711c720ae905 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070027#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "art_method-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070029#include "base/dumpable.h"
Alex Lighta59dd802014-07-02 16:28:08 -070030#include "base/scoped_flock.h"
Alex Light53cb16b2014-06-12 11:26:29 -070031#include "base/stringpiece.h"
32#include "base/stringprintf.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070033#include "base/unix_file/fd_file.h"
Alex Light53cb16b2014-06-12 11:26:29 -070034#include "elf_utils.h"
35#include "elf_file.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070036#include "elf_file_impl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "gc/space/image_space.h"
Alex Light53cb16b2014-06-12 11:26:29 -070038#include "image.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070039#include "mirror/abstract_method.h"
Alex Light53cb16b2014-06-12 11:26:29 -070040#include "mirror/object-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070041#include "mirror/method.h"
Alex Light53cb16b2014-06-12 11:26:29 -070042#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
Alex Lightcf4bf382014-07-24 11:29:14 -070053static bool LocationToFilename(const std::string& location, InstructionSet isa,
54 std::string* filename) {
55 bool has_system = false;
56 bool has_cache = false;
57 // image_location = /system/framework/boot.art
Igor Murashkin46774762014-10-22 11:37:02 -070058 // system_image_filename = /system/framework/<image_isa>/boot.art
Alex Lightcf4bf382014-07-24 11:29:14 -070059 std::string system_filename(GetSystemImageFilename(location.c_str(), isa));
60 if (OS::FileExists(system_filename.c_str())) {
61 has_system = true;
62 }
63
64 bool have_android_data = false;
65 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -070066 bool is_global_cache = false;
Alex Lightcf4bf382014-07-24 11:29:14 -070067 std::string dalvik_cache;
68 GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -070069 &have_android_data, &dalvik_cache_exists, &is_global_cache);
Alex Lightcf4bf382014-07-24 11:29:14 -070070
71 std::string cache_filename;
72 if (have_android_data && dalvik_cache_exists) {
73 // Always set output location even if it does not exist,
74 // so that the caller knows where to create the image.
75 //
76 // image_location = /system/framework/boot.art
77 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
78 std::string error_msg;
79 if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(),
80 &cache_filename, &error_msg)) {
81 has_cache = true;
82 }
83 }
84 if (has_system) {
85 *filename = system_filename;
86 return true;
87 } else if (has_cache) {
88 *filename = cache_filename;
89 return true;
90 } else {
91 return false;
92 }
93}
94
Alex Light53cb16b2014-06-12 11:26:29 -070095bool PatchOat::Patch(const std::string& image_location, off_t delta,
96 File* output_image, InstructionSet isa,
Alex Lighteefbe392014-07-08 09:53:18 -070097 TimingLogger* timings) {
Alex Light53cb16b2014-06-12 11:26:29 -070098 CHECK(Runtime::Current() == nullptr);
99 CHECK(output_image != nullptr);
100 CHECK_GE(output_image->Fd(), 0);
101 CHECK(!image_location.empty()) << "image file must have a filename.";
102 CHECK_NE(isa, kNone);
103
Alex Lighteefbe392014-07-08 09:53:18 -0700104 TimingLogger::ScopedTiming t("Runtime Setup", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700105 const char *isa_name = GetInstructionSetString(isa);
Alex Lightcf4bf382014-07-24 11:29:14 -0700106 std::string image_filename;
107 if (!LocationToFilename(image_location, isa, &image_filename)) {
108 LOG(ERROR) << "Unable to find image at location " << image_location;
109 return false;
110 }
Alex Light53cb16b2014-06-12 11:26:29 -0700111 std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str()));
112 if (input_image.get() == nullptr) {
Alex Lightcf4bf382014-07-24 11:29:14 -0700113 LOG(ERROR) << "unable to open input image file at " << image_filename
114 << " for location " << image_location;
Alex Light53cb16b2014-06-12 11:26:29 -0700115 return false;
116 }
Igor Murashkin46774762014-10-22 11:37:02 -0700117
Alex Light53cb16b2014-06-12 11:26:29 -0700118 int64_t image_len = input_image->GetLength();
119 if (image_len < 0) {
120 LOG(ERROR) << "Error while getting image length";
121 return false;
122 }
123 ImageHeader image_header;
124 if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 sizeof(image_header), 0)) {
Alex Light53cb16b2014-06-12 11:26:29 -0700126 LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
127 return false;
128 }
129
Igor Murashkin46774762014-10-22 11:37:02 -0700130 /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath());
131 // Nothing special to do right now since the image always needs to get patched.
132 // Perhaps in some far-off future we may have images with relative addresses that are true-PIC.
133
Alex Light53cb16b2014-06-12 11:26:29 -0700134 // Set up the runtime
Ian Rogerse63db272014-07-15 15:36:11 -0700135 RuntimeOptions options;
Alex Light53cb16b2014-06-12 11:26:29 -0700136 NoopCompilerCallbacks callbacks;
137 options.push_back(std::make_pair("compilercallbacks", &callbacks));
138 std::string img = "-Ximage:" + image_location;
139 options.push_back(std::make_pair(img.c_str(), nullptr));
140 options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
141 if (!Runtime::Create(options, false)) {
142 LOG(ERROR) << "Unable to initialize runtime";
143 return false;
144 }
145 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
146 // give it away now and then switch to a more manageable ScopedObjectAccess.
147 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
148 ScopedObjectAccess soa(Thread::Current());
149
150 t.NewTiming("Image and oat Patching setup");
151 // Create the map where we will write the image patches to.
Alex Lighteefbe392014-07-08 09:53:18 -0700152 std::string error_msg;
Alex Light53cb16b2014-06-12 11:26:29 -0700153 std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
154 input_image->Fd(), 0,
155 input_image->GetPath().c_str(),
156 &error_msg));
157 if (image.get() == nullptr) {
158 LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg;
159 return false;
160 }
161 gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace();
162
Mathieu Chartier2d721012014-11-10 11:08:06 -0800163 PatchOat p(isa, image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(),
Alex Light53cb16b2014-06-12 11:26:29 -0700164 delta, timings);
165 t.NewTiming("Patching files");
166 if (!p.PatchImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700167 LOG(ERROR) << "Failed to patch image file " << input_image->GetPath();
Alex Light53cb16b2014-06-12 11:26:29 -0700168 return false;
169 }
170
171 t.NewTiming("Writing files");
172 if (!p.WriteImage(output_image)) {
173 return false;
174 }
175 return true;
176}
177
Igor Murashkin46774762014-10-22 11:37:02 -0700178bool PatchOat::Patch(File* input_oat, const std::string& image_location, off_t delta,
Alex Light53cb16b2014-06-12 11:26:29 -0700179 File* output_oat, File* output_image, InstructionSet isa,
Igor Murashkin46774762014-10-22 11:37:02 -0700180 TimingLogger* timings,
181 bool output_oat_opened_from_fd,
182 bool new_oat_out) {
Alex Light53cb16b2014-06-12 11:26:29 -0700183 CHECK(Runtime::Current() == nullptr);
184 CHECK(output_image != nullptr);
185 CHECK_GE(output_image->Fd(), 0);
186 CHECK(input_oat != nullptr);
187 CHECK(output_oat != nullptr);
188 CHECK_GE(input_oat->Fd(), 0);
189 CHECK_GE(output_oat->Fd(), 0);
190 CHECK(!image_location.empty()) << "image file must have a filename.";
191
Alex Lighteefbe392014-07-08 09:53:18 -0700192 TimingLogger::ScopedTiming t("Runtime Setup", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700193
194 if (isa == kNone) {
195 Elf32_Ehdr elf_hdr;
196 if (sizeof(elf_hdr) != input_oat->Read(reinterpret_cast<char*>(&elf_hdr), sizeof(elf_hdr), 0)) {
197 LOG(ERROR) << "unable to read elf header";
198 return false;
199 }
Andreas Gampe6f611412015-01-21 22:25:24 -0800200 isa = GetInstructionSetFromELF(elf_hdr.e_machine, elf_hdr.e_flags);
Alex Light53cb16b2014-06-12 11:26:29 -0700201 }
202 const char* isa_name = GetInstructionSetString(isa);
Alex Lightcf4bf382014-07-24 11:29:14 -0700203 std::string image_filename;
204 if (!LocationToFilename(image_location, isa, &image_filename)) {
205 LOG(ERROR) << "Unable to find image at location " << image_location;
206 return false;
207 }
Alex Light53cb16b2014-06-12 11:26:29 -0700208 std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str()));
209 if (input_image.get() == nullptr) {
Alex Lightcf4bf382014-07-24 11:29:14 -0700210 LOG(ERROR) << "unable to open input image file at " << image_filename
211 << " for location " << image_location;
Alex Light53cb16b2014-06-12 11:26:29 -0700212 return false;
213 }
214 int64_t image_len = input_image->GetLength();
215 if (image_len < 0) {
216 LOG(ERROR) << "Error while getting image length";
217 return false;
218 }
219 ImageHeader image_header;
220 if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
221 sizeof(image_header), 0)) {
222 LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
223 }
224
Igor Murashkin46774762014-10-22 11:37:02 -0700225 /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath());
226 // Nothing special to do right now since the image always needs to get patched.
227 // Perhaps in some far-off future we may have images with relative addresses that are true-PIC.
228
Alex Light53cb16b2014-06-12 11:26:29 -0700229 // Set up the runtime
Ian Rogerse63db272014-07-15 15:36:11 -0700230 RuntimeOptions options;
Alex Light53cb16b2014-06-12 11:26:29 -0700231 NoopCompilerCallbacks callbacks;
232 options.push_back(std::make_pair("compilercallbacks", &callbacks));
233 std::string img = "-Ximage:" + image_location;
234 options.push_back(std::make_pair(img.c_str(), nullptr));
235 options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
236 if (!Runtime::Create(options, false)) {
237 LOG(ERROR) << "Unable to initialize runtime";
238 return false;
239 }
240 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
241 // give it away now and then switch to a more manageable ScopedObjectAccess.
242 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
243 ScopedObjectAccess soa(Thread::Current());
244
245 t.NewTiming("Image and oat Patching setup");
246 // Create the map where we will write the image patches to.
Alex Lighteefbe392014-07-08 09:53:18 -0700247 std::string error_msg;
Alex Light53cb16b2014-06-12 11:26:29 -0700248 std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
249 input_image->Fd(), 0,
250 input_image->GetPath().c_str(),
251 &error_msg));
252 if (image.get() == nullptr) {
253 LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg;
254 return false;
255 }
256 gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace();
257
Igor Murashkin46774762014-10-22 11:37:02 -0700258 std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat,
Alex Light53cb16b2014-06-12 11:26:29 -0700259 PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
260 if (elf.get() == nullptr) {
261 LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
262 return false;
263 }
264
Igor Murashkin46774762014-10-22 11:37:02 -0700265 bool skip_patching_oat = false;
266 MaybePic is_oat_pic = IsOatPic(elf.get());
267 if (is_oat_pic >= ERROR_FIRST) {
268 // Error logged by IsOatPic
269 return false;
270 } else if (is_oat_pic == PIC) {
271 // Do not need to do ELF-file patching. Create a symlink and skip the ELF patching.
272 if (!ReplaceOatFileWithSymlink(input_oat->GetPath(),
273 output_oat->GetPath(),
274 output_oat_opened_from_fd,
275 new_oat_out)) {
276 // Errors already logged by above call.
277 return false;
278 }
279 // Don't patch the OAT, since we just symlinked it. Image still needs patching.
280 skip_patching_oat = true;
281 } else {
282 CHECK(is_oat_pic == NOT_PIC);
283 }
284
Mathieu Chartier2d721012014-11-10 11:08:06 -0800285 PatchOat p(isa, elf.release(), image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(),
Alex Light53cb16b2014-06-12 11:26:29 -0700286 delta, timings);
287 t.NewTiming("Patching files");
Igor Murashkin46774762014-10-22 11:37:02 -0700288 if (!skip_patching_oat && !p.PatchElf()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700289 LOG(ERROR) << "Failed to patch oat file " << input_oat->GetPath();
Alex Light53cb16b2014-06-12 11:26:29 -0700290 return false;
291 }
292 if (!p.PatchImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700293 LOG(ERROR) << "Failed to patch image file " << input_image->GetPath();
Alex Light53cb16b2014-06-12 11:26:29 -0700294 return false;
295 }
296
297 t.NewTiming("Writing files");
Igor Murashkin46774762014-10-22 11:37:02 -0700298 if (!skip_patching_oat && !p.WriteElf(output_oat)) {
299 LOG(ERROR) << "Failed to write oat file " << input_oat->GetPath();
Alex Light53cb16b2014-06-12 11:26:29 -0700300 return false;
301 }
302 if (!p.WriteImage(output_image)) {
Igor Murashkin46774762014-10-22 11:37:02 -0700303 LOG(ERROR) << "Failed to write image file " << input_image->GetPath();
Alex Light53cb16b2014-06-12 11:26:29 -0700304 return false;
305 }
306 return true;
307}
308
309bool PatchOat::WriteElf(File* out) {
Alex Lighteefbe392014-07-08 09:53:18 -0700310 TimingLogger::ScopedTiming t("Writing Elf File", timings_);
Alex Lighta59dd802014-07-02 16:28:08 -0700311
Alex Light53cb16b2014-06-12 11:26:29 -0700312 CHECK(oat_file_.get() != nullptr);
313 CHECK(out != nullptr);
314 size_t expect = oat_file_->Size();
315 if (out->WriteFully(reinterpret_cast<char*>(oat_file_->Begin()), expect) &&
316 out->SetLength(expect) == 0) {
317 return true;
318 } else {
319 LOG(ERROR) << "Writing to oat file " << out->GetPath() << " failed.";
320 return false;
321 }
322}
323
324bool PatchOat::WriteImage(File* out) {
Alex Lighteefbe392014-07-08 09:53:18 -0700325 TimingLogger::ScopedTiming t("Writing image File", timings_);
Alex Lighta59dd802014-07-02 16:28:08 -0700326 std::string error_msg;
327
Alex Lightcf4bf382014-07-24 11:29:14 -0700328 ScopedFlock img_flock;
329 img_flock.Init(out, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700330
Alex Light53cb16b2014-06-12 11:26:29 -0700331 CHECK(image_ != nullptr);
332 CHECK(out != nullptr);
333 size_t expect = image_->Size();
334 if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) &&
335 out->SetLength(expect) == 0) {
336 return true;
337 } else {
338 LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed.";
339 return false;
340 }
341}
342
Igor Murashkin46774762014-10-22 11:37:02 -0700343bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) {
344 if (!image_header.CompilePic()) {
345 if (kIsDebugBuild) {
346 LOG(INFO) << "image at location " << image_path << " was *not* compiled pic";
347 }
348 return false;
349 }
350
351 if (kIsDebugBuild) {
352 LOG(INFO) << "image at location " << image_path << " was compiled PIC";
353 }
354
355 return true;
356}
357
358PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) {
359 if (oat_in == nullptr) {
360 LOG(ERROR) << "No ELF input oat fie available";
361 return ERROR_OAT_FILE;
362 }
363
364 const std::string& file_path = oat_in->GetFile().GetPath();
365
366 const OatHeader* oat_header = GetOatHeader(oat_in);
367 if (oat_header == nullptr) {
368 LOG(ERROR) << "Failed to find oat header in oat file " << file_path;
369 return ERROR_OAT_FILE;
370 }
371
372 if (!oat_header->IsValid()) {
373 LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header";
374 return ERROR_OAT_FILE;
375 }
376
377 bool is_pic = oat_header->IsPic();
378 if (kIsDebugBuild) {
379 LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic");
380 }
381
382 return is_pic ? PIC : NOT_PIC;
383}
384
385bool PatchOat::ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
386 const std::string& output_oat_filename,
387 bool output_oat_opened_from_fd,
388 bool new_oat_out) {
389 // Need a file when we are PIC, since we symlink over it. Refusing to symlink into FD.
390 if (output_oat_opened_from_fd) {
391 // TODO: installd uses --output-oat-fd. Should we change class linking logic for PIC?
392 LOG(ERROR) << "No output oat filename specified, needs filename for when we are PIC";
393 return false;
394 }
395
396 // Image was PIC. Create symlink where the oat is supposed to go.
397 if (!new_oat_out) {
398 LOG(ERROR) << "Oat file " << output_oat_filename << " already exists, refusing to overwrite";
399 return false;
400 }
401
402 // Delete the original file, since we won't need it.
403 TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str()));
404
405 // Create a symlink from the old oat to the new oat
406 if (symlink(input_oat_filename.c_str(), output_oat_filename.c_str()) < 0) {
407 int err = errno;
408 LOG(ERROR) << "Failed to create symlink at " << output_oat_filename
409 << " error(" << err << "): " << strerror(err);
410 return false;
411 }
412
413 if (kIsDebugBuild) {
414 LOG(INFO) << "Created symlink " << output_oat_filename << " -> " << input_oat_filename;
415 }
416
417 return true;
418}
419
Mathieu Chartierc7853442015-03-27 14:35:38 -0700420void PatchOat::PatchArtFields(const ImageHeader* image_header) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 const auto& section = image_header->GetImageSection(ImageHeader::kSectionArtFields);
422 for (size_t pos = 0; pos < section.Size(); pos += sizeof(ArtField)) {
423 auto* src = reinterpret_cast<ArtField*>(heap_->Begin() + section.Offset() + pos);
424 auto* dest = RelocatedCopyOf(src);
425 dest->SetDeclaringClass(RelocatedAddressOfPointer(src->GetDeclaringClass()));
426 }
427}
428
429void PatchOat::PatchArtMethods(const ImageHeader* image_header) {
430 const auto& section = image_header->GetMethodsSection();
431 const size_t pointer_size = InstructionSetPointerSize(isa_);
432 size_t method_size = ArtMethod::ObjectSize(pointer_size);
433 for (size_t pos = 0; pos < section.Size(); pos += method_size) {
434 auto* src = reinterpret_cast<ArtMethod*>(heap_->Begin() + section.Offset() + pos);
435 auto* dest = RelocatedCopyOf(src);
436 FixupMethod(src, dest);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700437 }
438}
439
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700440class FixupRootVisitor : public RootVisitor {
441 public:
442 explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) {
443 }
444
445 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED)
446 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
447 for (size_t i = 0; i < count; ++i) {
448 *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]);
449 }
450 }
451
452 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
453 const RootInfo& info ATTRIBUTE_UNUSED)
454 OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
455 for (size_t i = 0; i < count; ++i) {
456 roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr()));
457 }
458 }
459
460 private:
461 const PatchOat* const patch_oat_;
462};
463
464void PatchOat::PatchInternedStrings(const ImageHeader* image_header) {
465 const auto& section = image_header->GetImageSection(ImageHeader::kSectionInternedStrings);
466 InternTable temp_table;
467 // Note that we require that ReadFromMemory does not make an internal copy of the elements.
468 // This also relies on visit roots not doing any verification which could fail after we update
469 // the roots to be the image addresses.
470 temp_table.ReadFromMemory(image_->Begin() + section.Offset());
471 FixupRootVisitor visitor(this);
472 temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots);
473}
474
Mathieu Chartierc7853442015-03-27 14:35:38 -0700475void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) {
476 auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>(
477 img_roots->Get(ImageHeader::kDexCaches));
478 for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) {
479 auto* dex_cache = dex_caches->GetWithoutChecks(i);
480 auto* fields = dex_cache->GetResolvedFields();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700481 if (fields != nullptr) {
482 CHECK(!fields->IsObjectArray());
483 CHECK(fields->IsArrayInstance());
484 FixupNativePointerArray(fields);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700485 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486 auto* methods = dex_cache->GetResolvedMethods();
487 if (methods != nullptr) {
488 CHECK(!methods->IsObjectArray());
489 CHECK(methods->IsArrayInstance());
490 FixupNativePointerArray(methods);
491 }
492 }
493}
494
495void PatchOat::FixupNativePointerArray(mirror::PointerArray* object) {
496 if (object->IsIntArray()) {
497 mirror::IntArray* arr = object->AsIntArray();
498 mirror::IntArray* copy_arr = down_cast<mirror::IntArray*>(RelocatedCopyOf(arr));
499 for (size_t j = 0, count2 = arr->GetLength(); j < count2; ++j) {
500 copy_arr->SetWithoutChecks<false>(
501 j, RelocatedAddressOfIntPointer(arr->GetWithoutChecks(j)));
502 }
503 } else {
504 CHECK(object->IsLongArray());
505 mirror::LongArray* arr = object->AsLongArray();
506 mirror::LongArray* copy_arr = down_cast<mirror::LongArray*>(RelocatedCopyOf(arr));
507 for (size_t j = 0, count2 = arr->GetLength(); j < count2; ++j) {
508 copy_arr->SetWithoutChecks<false>(
509 j, RelocatedAddressOfIntPointer(arr->GetWithoutChecks(j)));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700510 }
511 }
512}
513
Alex Light53cb16b2014-06-12 11:26:29 -0700514bool PatchOat::PatchImage() {
515 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
516 CHECK_GT(image_->Size(), sizeof(ImageHeader));
517 // These are the roots from the original file.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700518 auto* img_roots = image_header->GetImageRoots();
Alex Light53cb16b2014-06-12 11:26:29 -0700519 image_header->RelocateImage(delta_);
520
Mathieu Chartierc7853442015-03-27 14:35:38 -0700521 PatchArtFields(image_header);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522 PatchArtMethods(image_header);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700523 PatchInternedStrings(image_header);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700524 // Patch dex file int/long arrays which point to ArtFields.
525 PatchDexFileArrays(img_roots);
526
Alex Light53cb16b2014-06-12 11:26:29 -0700527 VisitObject(img_roots);
528 if (!image_header->IsValid()) {
529 LOG(ERROR) << "reloction renders image header invalid";
530 return false;
531 }
532
533 {
Alex Lighteefbe392014-07-08 09:53:18 -0700534 TimingLogger::ScopedTiming t("Walk Bitmap", timings_);
Alex Light53cb16b2014-06-12 11:26:29 -0700535 // Walk the bitmap.
536 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
537 bitmap_->Walk(PatchOat::BitmapCallback, this);
538 }
539 return true;
540}
541
542bool PatchOat::InHeap(mirror::Object* o) {
543 uintptr_t begin = reinterpret_cast<uintptr_t>(heap_->Begin());
544 uintptr_t end = reinterpret_cast<uintptr_t>(heap_->End());
545 uintptr_t obj = reinterpret_cast<uintptr_t>(o);
546 return o == nullptr || (begin <= obj && obj < end);
547}
548
549void PatchOat::PatchVisitor::operator() (mirror::Object* obj, MemberOffset off,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700550 bool is_static_unused ATTRIBUTE_UNUSED) const {
Alex Light53cb16b2014-06-12 11:26:29 -0700551 mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off);
552 DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
Mathieu Chartierc7853442015-03-27 14:35:38 -0700553 mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent);
Alex Light53cb16b2014-06-12 11:26:29 -0700554 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
555}
556
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700557void PatchOat::PatchVisitor::operator() (mirror::Class* cls ATTRIBUTE_UNUSED,
558 mirror::Reference* ref) const {
Alex Light53cb16b2014-06-12 11:26:29 -0700559 MemberOffset off = mirror::Reference::ReferentOffset();
560 mirror::Object* referent = ref->GetReferent();
561 DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
Mathieu Chartierc7853442015-03-27 14:35:38 -0700562 mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent);
Alex Light53cb16b2014-06-12 11:26:29 -0700563 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
564}
565
Igor Murashkin46774762014-10-22 11:37:02 -0700566const OatHeader* PatchOat::GetOatHeader(const ElfFile* elf_file) {
567 if (elf_file->Is64Bit()) {
568 return GetOatHeader<ElfFileImpl64>(elf_file->GetImpl64());
569 } else {
570 return GetOatHeader<ElfFileImpl32>(elf_file->GetImpl32());
571 }
572}
573
574template <typename ElfFileImpl>
575const OatHeader* PatchOat::GetOatHeader(const ElfFileImpl* elf_file) {
576 auto rodata_sec = elf_file->FindSectionByName(".rodata");
577 if (rodata_sec == nullptr) {
578 return nullptr;
579 }
580
581 OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf_file->Begin() + rodata_sec->sh_offset);
582 return oat_header;
583}
584
Alex Light53cb16b2014-06-12 11:26:29 -0700585// Called by BitmapCallback
586void PatchOat::VisitObject(mirror::Object* object) {
587 mirror::Object* copy = RelocatedCopyOf(object);
588 CHECK(copy != nullptr);
589 if (kUseBakerOrBrooksReadBarrier) {
590 object->AssertReadBarrierPointer();
591 if (kUseBrooksReadBarrier) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700592 mirror::Object* moved_to = RelocatedAddressOfPointer(object);
Alex Light53cb16b2014-06-12 11:26:29 -0700593 copy->SetReadBarrierPointer(moved_to);
594 DCHECK_EQ(copy->GetReadBarrierPointer(), moved_to);
595 }
596 }
597 PatchOat::PatchVisitor visitor(this, copy);
598 object->VisitReferences<true, kVerifyNone>(visitor, visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 if (object->IsClass<kVerifyNone>()) {
600 auto* klass = object->AsClass();
601 auto* copy_klass = down_cast<mirror::Class*>(copy);
602 copy_klass->SetSFieldsUnchecked(RelocatedAddressOfPointer(klass->GetSFields()));
603 copy_klass->SetIFieldsUnchecked(RelocatedAddressOfPointer(klass->GetIFields()));
604 copy_klass->SetDirectMethodsPtrUnchecked(
605 RelocatedAddressOfPointer(klass->GetDirectMethodsPtr()));
606 copy_klass->SetVirtualMethodsPtr(RelocatedAddressOfPointer(klass->GetVirtualMethodsPtr()));
607 auto* vtable = klass->GetVTable();
608 if (vtable != nullptr) {
609 FixupNativePointerArray(vtable);
610 }
611 auto* iftable = klass->GetIfTable();
612 if (iftable != nullptr) {
613 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
614 if (iftable->GetMethodArrayCount(i) > 0) {
615 auto* method_array = iftable->GetMethodArray(i);
616 CHECK(method_array != nullptr);
617 FixupNativePointerArray(method_array);
618 }
619 }
620 }
621 if (klass->ShouldHaveEmbeddedImtAndVTable()) {
622 const size_t pointer_size = InstructionSetPointerSize(isa_);
623 for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) {
624 copy_klass->SetEmbeddedVTableEntryUnchecked(i, RelocatedAddressOfPointer(
625 klass->GetEmbeddedVTableEntry(i, pointer_size)), pointer_size);
626 }
627 for (size_t i = 0; i < mirror::Class::kImtSize; ++i) {
628 copy_klass->SetEmbeddedImTableEntry(i, RelocatedAddressOfPointer(
629 klass->GetEmbeddedImTableEntry(i, pointer_size)), pointer_size);
630 }
631 }
632 }
633 if (object->GetClass() == mirror::Method::StaticClass() ||
634 object->GetClass() == mirror::Constructor::StaticClass()) {
635 // Need to go update the ArtMethod.
636 auto* dest = down_cast<mirror::AbstractMethod*>(copy);
637 auto* src = down_cast<mirror::AbstractMethod*>(object);
638 dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod()));
Alex Light53cb16b2014-06-12 11:26:29 -0700639 }
640}
641
Mathieu Chartiere401d142015-04-22 13:56:20 -0700642void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) {
Mathieu Chartier2d721012014-11-10 11:08:06 -0800643 const size_t pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644 copy->CopyFrom(object, pointer_size);
Alex Light53cb16b2014-06-12 11:26:29 -0700645 // Just update the entry points if it looks like we should.
Alex Lighteefbe392014-07-08 09:53:18 -0700646 // TODO: sanity check all the pointers' values
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647 copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass()));
648 copy->SetDexCacheResolvedMethods(RelocatedAddressOfPointer(object->GetDexCacheResolvedMethods()));
649 copy->SetDexCacheResolvedTypes(RelocatedAddressOfPointer(object->GetDexCacheResolvedTypes()));
650 copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer(
651 object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700652 copy->SetEntryPointFromJniPtrSize(RelocatedAddressOfPointer(
653 object->GetEntryPointFromJniPtrSize(pointer_size)), pointer_size);
Alex Light53cb16b2014-06-12 11:26:29 -0700654}
655
Igor Murashkin46774762014-10-22 11:37:02 -0700656bool PatchOat::Patch(File* input_oat, off_t delta, File* output_oat, TimingLogger* timings,
657 bool output_oat_opened_from_fd, bool new_oat_out) {
Alex Light53cb16b2014-06-12 11:26:29 -0700658 CHECK(input_oat != nullptr);
659 CHECK(output_oat != nullptr);
660 CHECK_GE(input_oat->Fd(), 0);
661 CHECK_GE(output_oat->Fd(), 0);
Alex Lighteefbe392014-07-08 09:53:18 -0700662 TimingLogger::ScopedTiming t("Setup Oat File Patching", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700663
664 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -0700665 std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat,
Alex Light53cb16b2014-06-12 11:26:29 -0700666 PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
667 if (elf.get() == nullptr) {
668 LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
669 return false;
670 }
671
Igor Murashkin46774762014-10-22 11:37:02 -0700672 MaybePic is_oat_pic = IsOatPic(elf.get());
673 if (is_oat_pic >= ERROR_FIRST) {
674 // Error logged by IsOatPic
675 return false;
676 } else if (is_oat_pic == PIC) {
677 // Do not need to do ELF-file patching. Create a symlink and skip the rest.
678 // Any errors will be logged by the function call.
679 return ReplaceOatFileWithSymlink(input_oat->GetPath(),
680 output_oat->GetPath(),
681 output_oat_opened_from_fd,
682 new_oat_out);
683 } else {
684 CHECK(is_oat_pic == NOT_PIC);
685 }
686
Alex Light53cb16b2014-06-12 11:26:29 -0700687 PatchOat p(elf.release(), delta, timings);
688 t.NewTiming("Patch Oat file");
689 if (!p.PatchElf()) {
690 return false;
691 }
692
693 t.NewTiming("Writing oat file");
694 if (!p.WriteElf(output_oat)) {
695 return false;
696 }
697 return true;
698}
699
Tong Shen62d1ca32014-09-03 17:24:56 -0700700template <typename ElfFileImpl>
701bool PatchOat::PatchOatHeader(ElfFileImpl* oat_file) {
702 auto rodata_sec = oat_file->FindSectionByName(".rodata");
Alex Lighta59dd802014-07-02 16:28:08 -0700703 if (rodata_sec == nullptr) {
704 return false;
705 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700706 OatHeader* oat_header = reinterpret_cast<OatHeader*>(oat_file->Begin() + rodata_sec->sh_offset);
Alex Lighta59dd802014-07-02 16:28:08 -0700707 if (!oat_header->IsValid()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700708 LOG(ERROR) << "Elf file " << oat_file->GetFile().GetPath() << " has an invalid oat header";
Alex Lighta59dd802014-07-02 16:28:08 -0700709 return false;
710 }
711 oat_header->RelocateOat(delta_);
712 return true;
713}
714
Alex Light53cb16b2014-06-12 11:26:29 -0700715bool PatchOat::PatchElf() {
Ian Rogersd4c4d952014-10-16 20:31:53 -0700716 if (oat_file_->Is64Bit())
Tong Shen62d1ca32014-09-03 17:24:56 -0700717 return PatchElf<ElfFileImpl64>(oat_file_->GetImpl64());
718 else
719 return PatchElf<ElfFileImpl32>(oat_file_->GetImpl32());
720}
721
722template <typename ElfFileImpl>
723bool PatchOat::PatchElf(ElfFileImpl* oat_file) {
Alex Lighta59dd802014-07-02 16:28:08 -0700724 TimingLogger::ScopedTiming t("Fixup Elf Text Section", timings_);
Vladimir Marko3fc99032015-05-13 19:06:30 +0100725
726 // Fix up absolute references to locations within the boot image.
David Srbecky2f6cdb02015-04-11 00:17:53 +0100727 if (!oat_file->ApplyOatPatchesTo(".text", delta_)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700728 return false;
729 }
730
Vladimir Marko3fc99032015-05-13 19:06:30 +0100731 // Update the OatHeader fields referencing the boot image.
Tong Shen62d1ca32014-09-03 17:24:56 -0700732 if (!PatchOatHeader<ElfFileImpl>(oat_file)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700733 return false;
734 }
735
Vladimir Marko3fc99032015-05-13 19:06:30 +0100736 bool need_boot_oat_fixup = true;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700737 for (unsigned int i = 0; i < oat_file->GetProgramHeaderNum(); ++i) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700738 auto hdr = oat_file->GetProgramHeader(i);
Vladimir Marko3fc99032015-05-13 19:06:30 +0100739 if (hdr->p_type == PT_LOAD && hdr->p_vaddr == 0u) {
740 need_boot_oat_fixup = false;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700741 break;
Alex Light53cb16b2014-06-12 11:26:29 -0700742 }
743 }
Vladimir Marko3fc99032015-05-13 19:06:30 +0100744 if (!need_boot_oat_fixup) {
745 // This is an app oat file that can be loaded at an arbitrary address in memory.
746 // Boot image references were patched above and there's nothing else to do.
Alex Lighta59dd802014-07-02 16:28:08 -0700747 return true;
748 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700749
Vladimir Marko3fc99032015-05-13 19:06:30 +0100750 // This is a boot oat file that's loaded at a particular address and we need
751 // to patch all absolute addresses, starting with ELF program headers.
752
Tong Shen62d1ca32014-09-03 17:24:56 -0700753 t.NewTiming("Fixup Elf Headers");
754 // Fixup Phdr's
755 oat_file->FixupProgramHeaders(delta_);
756
Alex Lighta59dd802014-07-02 16:28:08 -0700757 t.NewTiming("Fixup Section Headers");
Tong Shen62d1ca32014-09-03 17:24:56 -0700758 // Fixup Shdr's
759 oat_file->FixupSectionHeaders(delta_);
Alex Light53cb16b2014-06-12 11:26:29 -0700760
Alex Lighta59dd802014-07-02 16:28:08 -0700761 t.NewTiming("Fixup Dynamics");
Tong Shen62d1ca32014-09-03 17:24:56 -0700762 oat_file->FixupDynamic(delta_);
Alex Light53cb16b2014-06-12 11:26:29 -0700763
764 t.NewTiming("Fixup Elf Symbols");
765 // Fixup dynsym
Tong Shen62d1ca32014-09-03 17:24:56 -0700766 if (!oat_file->FixupSymbols(delta_, true)) {
Alex Light53cb16b2014-06-12 11:26:29 -0700767 return false;
768 }
Alex Light53cb16b2014-06-12 11:26:29 -0700769 // Fixup symtab
Tong Shen62d1ca32014-09-03 17:24:56 -0700770 if (!oat_file->FixupSymbols(delta_, false)) {
771 return false;
Alex Light53cb16b2014-06-12 11:26:29 -0700772 }
773
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700774 t.NewTiming("Fixup Debug Sections");
Tong Shen62d1ca32014-09-03 17:24:56 -0700775 if (!oat_file->FixupDebugSections(delta_)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700776 return false;
777 }
778
Alex Light53cb16b2014-06-12 11:26:29 -0700779 return true;
780}
781
Alex Light53cb16b2014-06-12 11:26:29 -0700782static int orig_argc;
783static char** orig_argv;
784
785static std::string CommandLine() {
786 std::vector<std::string> command;
787 for (int i = 0; i < orig_argc; ++i) {
788 command.push_back(orig_argv[i]);
789 }
790 return Join(command, ' ');
791}
792
793static void UsageErrorV(const char* fmt, va_list ap) {
794 std::string error;
795 StringAppendV(&error, fmt, ap);
796 LOG(ERROR) << error;
797}
798
799static void UsageError(const char* fmt, ...) {
800 va_list ap;
801 va_start(ap, fmt);
802 UsageErrorV(fmt, ap);
803 va_end(ap);
804}
805
Andreas Gampe794ad762015-02-23 08:12:24 -0800806NO_RETURN static void Usage(const char *fmt, ...) {
Alex Light53cb16b2014-06-12 11:26:29 -0700807 va_list ap;
808 va_start(ap, fmt);
809 UsageErrorV(fmt, ap);
810 va_end(ap);
811
812 UsageError("Command: %s", CommandLine().c_str());
813 UsageError("Usage: patchoat [options]...");
814 UsageError("");
815 UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is");
816 UsageError(" compiled for. Required if you use --input-oat-location");
817 UsageError("");
818 UsageError(" --input-oat-file=<file.oat>: Specifies the exact filename of the oat file to be");
819 UsageError(" patched.");
820 UsageError("");
821 UsageError(" --input-oat-fd=<file-descriptor>: Specifies the file-descriptor of the oat file");
822 UsageError(" to be patched.");
823 UsageError("");
824 UsageError(" --input-oat-location=<file.oat>: Specifies the 'location' to read the patched");
825 UsageError(" oat file from. If used one must also supply the --instruction-set");
826 UsageError("");
827 UsageError(" --input-image-location=<file.art>: Specifies the 'location' of the image file to");
828 UsageError(" be patched. If --instruction-set is not given it will use the instruction set");
829 UsageError(" extracted from the --input-oat-file.");
830 UsageError("");
831 UsageError(" --output-oat-file=<file.oat>: Specifies the exact file to write the patched oat");
832 UsageError(" file to.");
833 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700834 UsageError(" --output-oat-fd=<file-descriptor>: Specifies the file-descriptor to write the");
835 UsageError(" the patched oat file to.");
836 UsageError("");
837 UsageError(" --output-image-file=<file.art>: Specifies the exact file to write the patched");
838 UsageError(" image file to.");
839 UsageError("");
840 UsageError(" --output-image-fd=<file-descriptor>: Specifies the file-descriptor to write the");
841 UsageError(" the patched image file to.");
842 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700843 UsageError(" --orig-base-offset=<original-base-offset>: Specify the base offset the input file");
844 UsageError(" was compiled with. This is needed if one is specifying a --base-offset");
845 UsageError("");
846 UsageError(" --base-offset=<new-base-offset>: Specify the base offset we will repatch the");
847 UsageError(" given files to use. This requires that --orig-base-offset is also given.");
848 UsageError("");
849 UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by.");
850 UsageError(" This value may be negative.");
851 UsageError("");
852 UsageError(" --patched-image-file=<file.art>: Use the same patch delta as was used to patch");
853 UsageError(" the given image file.");
854 UsageError("");
855 UsageError(" --patched-image-location=<file.art>: Use the same patch delta as was used to");
856 UsageError(" patch the given image location. If used one must also specify the");
Alex Lighta59dd802014-07-02 16:28:08 -0700857 UsageError(" --instruction-set flag. It will search for this image in the same way that");
858 UsageError(" is done when loading one.");
Alex Light53cb16b2014-06-12 11:26:29 -0700859 UsageError("");
Alex Lightcf4bf382014-07-24 11:29:14 -0700860 UsageError(" --lock-output: Obtain a flock on output oat file before starting.");
861 UsageError("");
862 UsageError(" --no-lock-output: Do not attempt to obtain a flock on output oat file.");
863 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700864 UsageError(" --dump-timings: dump out patch timing information");
865 UsageError("");
866 UsageError(" --no-dump-timings: do not dump out patch timing information");
867 UsageError("");
868
869 exit(EXIT_FAILURE);
870}
871
Alex Lighteefbe392014-07-08 09:53:18 -0700872static bool ReadBaseDelta(const char* name, off_t* delta, std::string* error_msg) {
Alex Light53cb16b2014-06-12 11:26:29 -0700873 CHECK(name != nullptr);
874 CHECK(delta != nullptr);
875 std::unique_ptr<File> file;
876 if (OS::FileExists(name)) {
877 file.reset(OS::OpenFileForReading(name));
878 if (file.get() == nullptr) {
Alex Lighteefbe392014-07-08 09:53:18 -0700879 *error_msg = "Failed to open file %s for reading";
Alex Light53cb16b2014-06-12 11:26:29 -0700880 return false;
881 }
882 } else {
Alex Lighteefbe392014-07-08 09:53:18 -0700883 *error_msg = "File %s does not exist";
Alex Light53cb16b2014-06-12 11:26:29 -0700884 return false;
885 }
886 CHECK(file.get() != nullptr);
887 ImageHeader hdr;
888 if (sizeof(hdr) != file->Read(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0)) {
Alex Lighteefbe392014-07-08 09:53:18 -0700889 *error_msg = "Failed to read file %s";
Alex Light53cb16b2014-06-12 11:26:29 -0700890 return false;
891 }
892 if (!hdr.IsValid()) {
Alex Lighteefbe392014-07-08 09:53:18 -0700893 *error_msg = "%s does not contain a valid image header.";
Alex Light53cb16b2014-06-12 11:26:29 -0700894 return false;
895 }
896 *delta = hdr.GetPatchDelta();
897 return true;
898}
899
900static File* CreateOrOpen(const char* name, bool* created) {
901 if (OS::FileExists(name)) {
902 *created = false;
903 return OS::OpenFileReadWrite(name);
904 } else {
905 *created = true;
Alex Lightcf4bf382014-07-24 11:29:14 -0700906 std::unique_ptr<File> f(OS::CreateEmptyFile(name));
907 if (f.get() != nullptr) {
908 if (fchmod(f->Fd(), 0644) != 0) {
909 PLOG(ERROR) << "Unable to make " << name << " world readable";
Brian Carlstrom8c52a3f2014-09-30 16:18:01 -0700910 TEMP_FAILURE_RETRY(unlink(name));
Alex Lightcf4bf382014-07-24 11:29:14 -0700911 return nullptr;
912 }
913 }
914 return f.release();
Alex Light53cb16b2014-06-12 11:26:29 -0700915 }
916}
917
Andreas Gampe4303ba92014-11-06 01:00:46 -0800918// Either try to close the file (close=true), or erase it.
919static bool FinishFile(File* file, bool close) {
920 if (close) {
921 if (file->FlushCloseOrErase() != 0) {
922 PLOG(ERROR) << "Failed to flush and close file.";
923 return false;
924 }
925 return true;
926 } else {
927 file->Erase();
928 return false;
929 }
930}
931
Alex Lighteefbe392014-07-08 09:53:18 -0700932static int patchoat(int argc, char **argv) {
Alex Light53cb16b2014-06-12 11:26:29 -0700933 InitLogging(argv);
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700934 MemMap::Init();
Alex Light53cb16b2014-06-12 11:26:29 -0700935 const bool debug = kIsDebugBuild;
936 orig_argc = argc;
937 orig_argv = argv;
938 TimingLogger timings("patcher", false, false);
939
940 InitLogging(argv);
941
942 // Skip over the command name.
943 argv++;
944 argc--;
945
946 if (argc == 0) {
947 Usage("No arguments specified");
948 }
949
950 timings.StartTiming("Patchoat");
951
952 // cmd line args
953 bool isa_set = false;
954 InstructionSet isa = kNone;
955 std::string input_oat_filename;
956 std::string input_oat_location;
957 int input_oat_fd = -1;
958 bool have_input_oat = false;
959 std::string input_image_location;
960 std::string output_oat_filename;
Alex Light53cb16b2014-06-12 11:26:29 -0700961 int output_oat_fd = -1;
962 bool have_output_oat = false;
963 std::string output_image_filename;
Alex Light53cb16b2014-06-12 11:26:29 -0700964 int output_image_fd = -1;
965 bool have_output_image = false;
966 uintptr_t base_offset = 0;
967 bool base_offset_set = false;
968 uintptr_t orig_base_offset = 0;
969 bool orig_base_offset_set = false;
970 off_t base_delta = 0;
971 bool base_delta_set = false;
972 std::string patched_image_filename;
973 std::string patched_image_location;
974 bool dump_timings = kIsDebugBuild;
Alex Lightcf4bf382014-07-24 11:29:14 -0700975 bool lock_output = true;
Alex Light53cb16b2014-06-12 11:26:29 -0700976
Ian Rogersd4c4d952014-10-16 20:31:53 -0700977 for (int i = 0; i < argc; ++i) {
Alex Light53cb16b2014-06-12 11:26:29 -0700978 const StringPiece option(argv[i]);
979 const bool log_options = false;
980 if (log_options) {
981 LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i];
982 }
Alex Light53cb16b2014-06-12 11:26:29 -0700983 if (option.starts_with("--instruction-set=")) {
984 isa_set = true;
985 const char* isa_str = option.substr(strlen("--instruction-set=")).data();
Andreas Gampe20c89302014-08-19 17:28:06 -0700986 isa = GetInstructionSetFromString(isa_str);
987 if (isa == kNone) {
988 Usage("Unknown or invalid instruction set %s", isa_str);
Alex Light53cb16b2014-06-12 11:26:29 -0700989 }
990 } else if (option.starts_with("--input-oat-location=")) {
991 if (have_input_oat) {
992 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
993 }
994 have_input_oat = true;
995 input_oat_location = option.substr(strlen("--input-oat-location=")).data();
996 } else if (option.starts_with("--input-oat-file=")) {
997 if (have_input_oat) {
998 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
999 }
1000 have_input_oat = true;
1001 input_oat_filename = option.substr(strlen("--input-oat-file=")).data();
1002 } else if (option.starts_with("--input-oat-fd=")) {
1003 if (have_input_oat) {
1004 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
1005 }
1006 have_input_oat = true;
1007 const char* oat_fd_str = option.substr(strlen("--input-oat-fd=")).data();
1008 if (!ParseInt(oat_fd_str, &input_oat_fd)) {
1009 Usage("Failed to parse --input-oat-fd argument '%s' as an integer", oat_fd_str);
1010 }
1011 if (input_oat_fd < 0) {
1012 Usage("--input-oat-fd pass a negative value %d", input_oat_fd);
1013 }
1014 } else if (option.starts_with("--input-image-location=")) {
1015 input_image_location = option.substr(strlen("--input-image-location=")).data();
Alex Light53cb16b2014-06-12 11:26:29 -07001016 } else if (option.starts_with("--output-oat-file=")) {
1017 if (have_output_oat) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001018 Usage("Only one of --output-oat-file, and --output-oat-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -07001019 }
1020 have_output_oat = true;
1021 output_oat_filename = option.substr(strlen("--output-oat-file=")).data();
1022 } else if (option.starts_with("--output-oat-fd=")) {
1023 if (have_output_oat) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001024 Usage("Only one of --output-oat-file, --output-oat-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -07001025 }
1026 have_output_oat = true;
1027 const char* oat_fd_str = option.substr(strlen("--output-oat-fd=")).data();
1028 if (!ParseInt(oat_fd_str, &output_oat_fd)) {
1029 Usage("Failed to parse --output-oat-fd argument '%s' as an integer", oat_fd_str);
1030 }
1031 if (output_oat_fd < 0) {
1032 Usage("--output-oat-fd pass a negative value %d", output_oat_fd);
1033 }
Alex Light53cb16b2014-06-12 11:26:29 -07001034 } else if (option.starts_with("--output-image-file=")) {
1035 if (have_output_image) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001036 Usage("Only one of --output-image-file, and --output-image-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -07001037 }
1038 have_output_image = true;
1039 output_image_filename = option.substr(strlen("--output-image-file=")).data();
1040 } else if (option.starts_with("--output-image-fd=")) {
1041 if (have_output_image) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001042 Usage("Only one of --output-image-file, and --output-image-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -07001043 }
1044 have_output_image = true;
1045 const char* image_fd_str = option.substr(strlen("--output-image-fd=")).data();
1046 if (!ParseInt(image_fd_str, &output_image_fd)) {
1047 Usage("Failed to parse --output-image-fd argument '%s' as an integer", image_fd_str);
1048 }
1049 if (output_image_fd < 0) {
1050 Usage("--output-image-fd pass a negative value %d", output_image_fd);
1051 }
1052 } else if (option.starts_with("--orig-base-offset=")) {
1053 const char* orig_base_offset_str = option.substr(strlen("--orig-base-offset=")).data();
1054 orig_base_offset_set = true;
1055 if (!ParseUint(orig_base_offset_str, &orig_base_offset)) {
1056 Usage("Failed to parse --orig-base-offset argument '%s' as an uintptr_t",
1057 orig_base_offset_str);
1058 }
1059 } else if (option.starts_with("--base-offset=")) {
1060 const char* base_offset_str = option.substr(strlen("--base-offset=")).data();
1061 base_offset_set = true;
1062 if (!ParseUint(base_offset_str, &base_offset)) {
1063 Usage("Failed to parse --base-offset argument '%s' as an uintptr_t", base_offset_str);
1064 }
1065 } else if (option.starts_with("--base-offset-delta=")) {
1066 const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data();
1067 base_delta_set = true;
1068 if (!ParseInt(base_delta_str, &base_delta)) {
1069 Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str);
1070 }
1071 } else if (option.starts_with("--patched-image-location=")) {
1072 patched_image_location = option.substr(strlen("--patched-image-location=")).data();
1073 } else if (option.starts_with("--patched-image-file=")) {
1074 patched_image_filename = option.substr(strlen("--patched-image-file=")).data();
Alex Lightcf4bf382014-07-24 11:29:14 -07001075 } else if (option == "--lock-output") {
1076 lock_output = true;
1077 } else if (option == "--no-lock-output") {
1078 lock_output = false;
Alex Light53cb16b2014-06-12 11:26:29 -07001079 } else if (option == "--dump-timings") {
1080 dump_timings = true;
1081 } else if (option == "--no-dump-timings") {
1082 dump_timings = false;
1083 } else {
1084 Usage("Unknown argument %s", option.data());
1085 }
1086 }
1087
1088 {
1089 // Only 1 of these may be set.
1090 uint32_t cnt = 0;
1091 cnt += (base_delta_set) ? 1 : 0;
1092 cnt += (base_offset_set && orig_base_offset_set) ? 1 : 0;
1093 cnt += (!patched_image_filename.empty()) ? 1 : 0;
1094 cnt += (!patched_image_location.empty()) ? 1 : 0;
1095 if (cnt > 1) {
1096 Usage("Only one of --base-offset/--orig-base-offset, --base-offset-delta, "
1097 "--patched-image-filename or --patched-image-location may be used.");
1098 } else if (cnt == 0) {
1099 Usage("Must specify --base-offset-delta, --base-offset and --orig-base-offset, "
1100 "--patched-image-location or --patched-image-file");
1101 }
1102 }
1103
1104 if (have_input_oat != have_output_oat) {
1105 Usage("Either both input and output oat must be supplied or niether must be.");
1106 }
1107
1108 if ((!input_image_location.empty()) != have_output_image) {
1109 Usage("Either both input and output image must be supplied or niether must be.");
1110 }
1111
1112 // We know we have both the input and output so rename for clarity.
1113 bool have_image_files = have_output_image;
1114 bool have_oat_files = have_output_oat;
1115
1116 if (!have_oat_files && !have_image_files) {
1117 Usage("Must be patching either an oat or an image file or both.");
1118 }
1119
1120 if (!have_oat_files && !isa_set) {
1121 Usage("Must include ISA if patching an image file without an oat file.");
1122 }
1123
1124 if (!input_oat_location.empty()) {
1125 if (!isa_set) {
1126 Usage("specifying a location requires specifying an instruction set");
1127 }
Alex Lightcf4bf382014-07-24 11:29:14 -07001128 if (!LocationToFilename(input_oat_location, isa, &input_oat_filename)) {
1129 Usage("Unable to find filename for input oat location %s", input_oat_location.c_str());
1130 }
Alex Light53cb16b2014-06-12 11:26:29 -07001131 if (debug) {
1132 LOG(INFO) << "Using input-oat-file " << input_oat_filename;
1133 }
1134 }
Alex Light53cb16b2014-06-12 11:26:29 -07001135 if (!patched_image_location.empty()) {
1136 if (!isa_set) {
1137 Usage("specifying a location requires specifying an instruction set");
1138 }
Alex Lighta59dd802014-07-02 16:28:08 -07001139 std::string system_filename;
1140 bool has_system = false;
1141 std::string cache_filename;
1142 bool has_cache = false;
1143 bool has_android_data_unused = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -07001144 bool is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -07001145 if (!gc::space::ImageSpace::FindImageFilename(patched_image_location.c_str(), isa,
1146 &system_filename, &has_system, &cache_filename,
Andreas Gampe3c13a792014-09-18 20:56:04 -07001147 &has_android_data_unused, &has_cache,
1148 &is_global_cache)) {
Alex Lighta59dd802014-07-02 16:28:08 -07001149 Usage("Unable to determine image file for location %s", patched_image_location.c_str());
1150 }
1151 if (has_cache) {
1152 patched_image_filename = cache_filename;
1153 } else if (has_system) {
1154 LOG(WARNING) << "Only image file found was in /system for image location "
1155 << patched_image_location;
1156 patched_image_filename = system_filename;
1157 } else {
1158 Usage("Unable to determine image file for location %s", patched_image_location.c_str());
1159 }
Alex Light53cb16b2014-06-12 11:26:29 -07001160 if (debug) {
1161 LOG(INFO) << "Using patched-image-file " << patched_image_filename;
1162 }
1163 }
1164
1165 if (!base_delta_set) {
1166 if (orig_base_offset_set && base_offset_set) {
1167 base_delta_set = true;
1168 base_delta = base_offset - orig_base_offset;
1169 } else if (!patched_image_filename.empty()) {
1170 base_delta_set = true;
1171 std::string error_msg;
Alex Lighteefbe392014-07-08 09:53:18 -07001172 if (!ReadBaseDelta(patched_image_filename.c_str(), &base_delta, &error_msg)) {
Alex Light53cb16b2014-06-12 11:26:29 -07001173 Usage(error_msg.c_str(), patched_image_filename.c_str());
1174 }
1175 } else {
1176 if (base_offset_set) {
1177 Usage("Unable to determine original base offset.");
1178 } else {
1179 Usage("Must supply a desired new offset or delta.");
1180 }
1181 }
1182 }
1183
1184 if (!IsAligned<kPageSize>(base_delta)) {
1185 Usage("Base offset/delta must be alligned to a pagesize (0x%08x) boundary.", kPageSize);
1186 }
1187
1188 // Do we need to cleanup output files if we fail?
1189 bool new_image_out = false;
1190 bool new_oat_out = false;
1191
1192 std::unique_ptr<File> input_oat;
1193 std::unique_ptr<File> output_oat;
1194 std::unique_ptr<File> output_image;
1195
1196 if (have_image_files) {
1197 CHECK(!input_image_location.empty());
1198
1199 if (output_image_fd != -1) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001200 if (output_image_filename.empty()) {
1201 output_image_filename = "output-image-file";
1202 }
Andreas Gampe4303ba92014-11-06 01:00:46 -08001203 output_image.reset(new File(output_image_fd, output_image_filename, true));
Alex Light53cb16b2014-06-12 11:26:29 -07001204 } else {
1205 CHECK(!output_image_filename.empty());
1206 output_image.reset(CreateOrOpen(output_image_filename.c_str(), &new_image_out));
1207 }
1208 } else {
1209 CHECK(output_image_filename.empty() && output_image_fd == -1 && input_image_location.empty());
1210 }
1211
1212 if (have_oat_files) {
1213 if (input_oat_fd != -1) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001214 if (input_oat_filename.empty()) {
1215 input_oat_filename = "input-oat-file";
1216 }
Andreas Gampe4303ba92014-11-06 01:00:46 -08001217 input_oat.reset(new File(input_oat_fd, input_oat_filename, false));
Julien Delayena473f512015-03-05 16:37:52 +01001218 if (input_oat_fd == output_oat_fd) {
1219 input_oat.get()->DisableAutoClose();
1220 }
Igor Murashkin46774762014-10-22 11:37:02 -07001221 if (input_oat == nullptr) {
1222 // Unlikely, but ensure exhaustive logging in non-0 exit code case
1223 LOG(ERROR) << "Failed to open input oat file by its FD" << input_oat_fd;
1224 }
Alex Light53cb16b2014-06-12 11:26:29 -07001225 } else {
1226 CHECK(!input_oat_filename.empty());
1227 input_oat.reset(OS::OpenFileForReading(input_oat_filename.c_str()));
Igor Murashkin46774762014-10-22 11:37:02 -07001228 if (input_oat == nullptr) {
1229 int err = errno;
1230 LOG(ERROR) << "Failed to open input oat file " << input_oat_filename
1231 << ": " << strerror(err) << "(" << err << ")";
Andreas Gampe1c83cbc2014-07-22 18:52:29 -07001232 }
Alex Light53cb16b2014-06-12 11:26:29 -07001233 }
1234
1235 if (output_oat_fd != -1) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001236 if (output_oat_filename.empty()) {
1237 output_oat_filename = "output-oat-file";
Alex Lighta59dd802014-07-02 16:28:08 -07001238 }
Andreas Gampe4303ba92014-11-06 01:00:46 -08001239 output_oat.reset(new File(output_oat_fd, output_oat_filename, true));
Igor Murashkin46774762014-10-22 11:37:02 -07001240 if (output_oat == nullptr) {
1241 // Unlikely, but ensure exhaustive logging in non-0 exit code case
1242 LOG(ERROR) << "Failed to open output oat file by its FD" << output_oat_fd;
1243 }
Alex Light53cb16b2014-06-12 11:26:29 -07001244 } else {
1245 CHECK(!output_oat_filename.empty());
1246 output_oat.reset(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out));
Igor Murashkin46774762014-10-22 11:37:02 -07001247 if (output_oat == nullptr) {
1248 int err = errno;
1249 LOG(ERROR) << "Failed to open output oat file " << output_oat_filename
1250 << ": " << strerror(err) << "(" << err << ")";
1251 }
Alex Light53cb16b2014-06-12 11:26:29 -07001252 }
1253 }
1254
Igor Murashkin46774762014-10-22 11:37:02 -07001255 // TODO: get rid of this.
Alex Light53cb16b2014-06-12 11:26:29 -07001256 auto cleanup = [&output_image_filename, &output_oat_filename,
1257 &new_oat_out, &new_image_out, &timings, &dump_timings](bool success) {
1258 timings.EndTiming();
1259 if (!success) {
1260 if (new_oat_out) {
1261 CHECK(!output_oat_filename.empty());
Brian Carlstrom8c52a3f2014-09-30 16:18:01 -07001262 TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str()));
Alex Light53cb16b2014-06-12 11:26:29 -07001263 }
1264 if (new_image_out) {
1265 CHECK(!output_image_filename.empty());
Brian Carlstrom8c52a3f2014-09-30 16:18:01 -07001266 TEMP_FAILURE_RETRY(unlink(output_image_filename.c_str()));
Alex Light53cb16b2014-06-12 11:26:29 -07001267 }
1268 }
1269 if (dump_timings) {
1270 LOG(INFO) << Dumpable<TimingLogger>(timings);
1271 }
Igor Murashkin46774762014-10-22 11:37:02 -07001272
1273 if (kIsDebugBuild) {
1274 LOG(INFO) << "Cleaning up.. success? " << success;
1275 }
Alex Light53cb16b2014-06-12 11:26:29 -07001276 };
1277
Igor Murashkin46774762014-10-22 11:37:02 -07001278 if (have_oat_files && (input_oat.get() == nullptr || output_oat.get() == nullptr)) {
1279 LOG(ERROR) << "Failed to open input/output oat files";
1280 cleanup(false);
1281 return EXIT_FAILURE;
1282 } else if (have_image_files && output_image.get() == nullptr) {
1283 LOG(ERROR) << "Failed to open output image file";
Alex Lightcf4bf382014-07-24 11:29:14 -07001284 cleanup(false);
1285 return EXIT_FAILURE;
1286 }
1287
Igor Murashkin46774762014-10-22 11:37:02 -07001288 if (debug) {
1289 LOG(INFO) << "moving offset by " << base_delta
1290 << " (0x" << std::hex << base_delta << ") bytes or "
1291 << std::dec << (base_delta/kPageSize) << " pages.";
1292 }
1293
1294 // TODO: is it going to be promatic to unlink a file that was flock-ed?
Alex Lightcf4bf382014-07-24 11:29:14 -07001295 ScopedFlock output_oat_lock;
1296 if (lock_output) {
1297 std::string error_msg;
1298 if (have_oat_files && !output_oat_lock.Init(output_oat.get(), &error_msg)) {
1299 LOG(ERROR) << "Unable to lock output oat " << output_image->GetPath() << ": " << error_msg;
1300 cleanup(false);
1301 return EXIT_FAILURE;
1302 }
1303 }
1304
Alex Light53cb16b2014-06-12 11:26:29 -07001305 bool ret;
1306 if (have_image_files && have_oat_files) {
1307 TimingLogger::ScopedTiming pt("patch image and oat", &timings);
1308 ret = PatchOat::Patch(input_oat.get(), input_image_location, base_delta,
Igor Murashkin46774762014-10-22 11:37:02 -07001309 output_oat.get(), output_image.get(), isa, &timings,
1310 output_oat_fd >= 0, // was it opened from FD?
1311 new_oat_out);
Andreas Gampe4303ba92014-11-06 01:00:46 -08001312 // The order here doesn't matter. If the first one is successfully saved and the second one
1313 // erased, ImageSpace will still detect a problem and not use the files.
1314 ret = ret && FinishFile(output_image.get(), ret);
1315 ret = ret && FinishFile(output_oat.get(), ret);
Alex Light53cb16b2014-06-12 11:26:29 -07001316 } else if (have_oat_files) {
1317 TimingLogger::ScopedTiming pt("patch oat", &timings);
Igor Murashkin46774762014-10-22 11:37:02 -07001318 ret = PatchOat::Patch(input_oat.get(), base_delta, output_oat.get(), &timings,
1319 output_oat_fd >= 0, // was it opened from FD?
1320 new_oat_out);
Andreas Gampe4303ba92014-11-06 01:00:46 -08001321 ret = ret && FinishFile(output_oat.get(), ret);
Igor Murashkin46774762014-10-22 11:37:02 -07001322 } else if (have_image_files) {
Alex Light53cb16b2014-06-12 11:26:29 -07001323 TimingLogger::ScopedTiming pt("patch image", &timings);
Alex Lighteefbe392014-07-08 09:53:18 -07001324 ret = PatchOat::Patch(input_image_location, base_delta, output_image.get(), isa, &timings);
Andreas Gampe4303ba92014-11-06 01:00:46 -08001325 ret = ret && FinishFile(output_image.get(), ret);
Igor Murashkin46774762014-10-22 11:37:02 -07001326 } else {
1327 CHECK(false);
1328 ret = true;
1329 }
1330
1331 if (kIsDebugBuild) {
1332 LOG(INFO) << "Exiting with return ... " << ret;
Alex Light53cb16b2014-06-12 11:26:29 -07001333 }
1334 cleanup(ret);
Alex Light53cb16b2014-06-12 11:26:29 -07001335 return (ret) ? EXIT_SUCCESS : EXIT_FAILURE;
1336}
1337
1338} // namespace art
1339
1340int main(int argc, char **argv) {
1341 return art::patchoat(argc, argv);
1342}