blob: a6d3903f19aac59994f22611fef7b701e99bb6f7 [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
Alex Klyubin3856af02017-10-23 13:53:13 -070018#include <openssl/sha.h>
Alex Light53cb16b2014-06-12 11:26:29 -070019#include <stdio.h>
20#include <stdlib.h>
Alex Lighta59dd802014-07-02 16:28:08 -070021#include <sys/file.h>
Alex Light53cb16b2014-06-12 11:26:29 -070022#include <sys/stat.h>
Alex Lighta59dd802014-07-02 16:28:08 -070023#include <unistd.h>
Alex Light53cb16b2014-06-12 11:26:29 -070024
25#include <string>
26#include <vector>
27
Chris Morin754b7572018-01-19 18:04:46 -080028#include "android-base/file.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080030#include "android-base/strings.h"
31
Mathieu Chartierc7853442015-03-27 14:35:38 -070032#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "art_method-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070034#include "base/dumpable.h"
Chris Morin754b7572018-01-19 18:04:46 -080035#include "base/file_utils.h"
David Sehr67bf42e2018-02-26 16:43:04 -080036#include "base/leb128.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080037#include "base/logging.h" // For InitLogging.
David Sehrc431b9d2018-03-02 12:01:51 -080038#include "base/mutex.h"
Andreas Gampeb8cc1752017-04-26 21:28:50 -070039#include "base/memory_tool.h"
David Sehrc431b9d2018-03-02 12:01:51 -080040#include "base/os.h"
Alex Lighta59dd802014-07-02 16:28:08 -070041#include "base/scoped_flock.h"
Alex Light53cb16b2014-06-12 11:26:29 -070042#include "base/stringpiece.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070043#include "base/unix_file/fd_file.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010044#include "base/unix_file/random_access_file_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080045#include "base/utils.h"
Vladimir Marko679730e2018-05-25 15:06:48 +010046#include "class_root.h"
Alex Light53cb16b2014-06-12 11:26:29 -070047#include "elf_file.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070048#include "elf_file_impl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070049#include "elf_utils.h"
Ian Rogerse63db272014-07-15 15:36:11 -070050#include "gc/space/image_space.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080051#include "image-inl.h"
Andreas Gampeb2d18fa2017-06-06 20:46:10 -070052#include "intern_table.h"
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070053#include "mirror/dex_cache.h"
Neil Fuller0e844392016-09-08 13:43:31 +010054#include "mirror/executable.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070055#include "mirror/method.h"
Alex Light53cb16b2014-06-12 11:26:29 -070056#include "mirror/object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080057#include "mirror/object-refvisitor-inl.h"
Alex Light53cb16b2014-06-12 11:26:29 -070058#include "mirror/reference.h"
59#include "noop_compiler_callbacks.h"
60#include "offsets.h"
Alex Light53cb16b2014-06-12 11:26:29 -070061#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070062#include "scoped_thread_state_change-inl.h"
Alex Light53cb16b2014-06-12 11:26:29 -070063#include "thread.h"
Alex Light53cb16b2014-06-12 11:26:29 -070064
65namespace art {
66
Alex Klyubin3856af02017-10-23 13:53:13 -070067using android::base::StringPrintf;
68
Andreas Gampec7d25082018-03-09 12:06:45 -080069namespace {
70
Alex Light0eb76d22015-08-11 18:03:47 -070071static const OatHeader* GetOatHeader(const ElfFile* elf_file) {
72 uint64_t off = 0;
73 if (!elf_file->GetSectionOffsetAndSize(".rodata", &off, nullptr)) {
74 return nullptr;
75 }
76
77 OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf_file->Begin() + off);
78 return oat_header;
79}
80
Richard Uhler4bc11d02017-02-01 09:53:54 +000081static File* CreateOrOpen(const char* name) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080082 if (OS::FileExists(name)) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080083 return OS::OpenFileReadWrite(name);
84 } else {
Jeff Haodcdc85b2015-12-04 14:06:18 -080085 std::unique_ptr<File> f(OS::CreateEmptyFile(name));
86 if (f.get() != nullptr) {
87 if (fchmod(f->Fd(), 0644) != 0) {
88 PLOG(ERROR) << "Unable to make " << name << " world readable";
Dimitry Ivanov7a1c0142016-03-17 15:59:38 -070089 unlink(name);
Jeff Haodcdc85b2015-12-04 14:06:18 -080090 return nullptr;
91 }
92 }
93 return f.release();
94 }
95}
96
97// Either try to close the file (close=true), or erase it.
98static bool FinishFile(File* file, bool close) {
99 if (close) {
100 if (file->FlushCloseOrErase() != 0) {
101 PLOG(ERROR) << "Failed to flush and close file.";
102 return false;
103 }
104 return true;
105 } else {
106 file->Erase();
107 return false;
108 }
109}
110
David Brazdil7b49e6c2016-09-01 11:06:18 +0100111static bool SymlinkFile(const std::string& input_filename, const std::string& output_filename) {
112 if (input_filename == output_filename) {
113 // Input and output are the same, nothing to do.
114 return true;
115 }
116
117 // Unlink the original filename, since we are overwriting it.
118 unlink(output_filename.c_str());
119
120 // Create a symlink from the source file to the target path.
121 if (symlink(input_filename.c_str(), output_filename.c_str()) < 0) {
122 PLOG(ERROR) << "Failed to create symlink " << output_filename << " -> " << input_filename;
123 return false;
124 }
125
126 if (kIsDebugBuild) {
127 LOG(INFO) << "Created symlink " << output_filename << " -> " << input_filename;
128 }
129
130 return true;
131}
132
Andreas Gampec7d25082018-03-09 12:06:45 -0800133// Holder class for runtime options and related objects.
134class PatchoatRuntimeOptionsHolder {
135 public:
136 PatchoatRuntimeOptionsHolder(const std::string& image_location, InstructionSet isa) {
137 options_.push_back(std::make_pair("compilercallbacks", &callbacks_));
138 img_ = "-Ximage:" + image_location;
139 options_.push_back(std::make_pair(img_.c_str(), nullptr));
140 isa_name_ = GetInstructionSetString(isa);
141 options_.push_back(std::make_pair("imageinstructionset",
142 reinterpret_cast<const void*>(isa_name_.c_str())));
143 options_.push_back(std::make_pair("-Xno-sig-chain", nullptr));
144 // We do not want the runtime to attempt to patch the image.
145 options_.push_back(std::make_pair("-Xnorelocate", nullptr));
146 // Don't try to compile.
147 options_.push_back(std::make_pair("-Xnoimage-dex2oat", nullptr));
148 // Do not accept broken image.
149 options_.push_back(std::make_pair("-Xno-dex-file-fallback", nullptr));
150 }
151
152 const RuntimeOptions& GetRuntimeOptions() {
153 return options_;
154 }
155
156 private:
157 RuntimeOptions options_;
158 NoopCompilerCallbacks callbacks_;
159 std::string isa_name_;
160 std::string img_;
161};
162
163} // namespace
164
Alex Klyubin3856af02017-10-23 13:53:13 -0700165bool PatchOat::GeneratePatch(
166 const MemMap& original,
167 const MemMap& relocated,
168 std::vector<uint8_t>* output,
169 std::string* error_msg) {
170 // FORMAT of the patch (aka image relocation) file:
171 // * SHA-256 digest (32 bytes) of original/unrelocated file (e.g., the one from /system)
172 // * List of monotonically increasing offsets (max value defined by uint32_t) at which relocations
173 // occur.
174 // Each element is represented as the delta from the previous offset in the list (first element
175 // is a delta from 0). Each delta is encoded using unsigned LEB128: little-endian
176 // variable-length 7 bits per byte encoding, where all bytes have the highest bit (0x80) set
177 // except for the final byte which does not have that bit set. For example, 0x3f is offset 0x3f,
178 // whereas 0xbf 0x05 is offset (0x3f & 0x7f) | (0x5 << 7) which is 0x2bf. Most deltas end up
179 // being encoding using just one byte, achieving ~4x decrease in relocation file size compared
180 // to the encoding where offsets are stored verbatim, as uint32_t.
181
182 size_t original_size = original.Size();
183 size_t relocated_size = relocated.Size();
184 if (original_size != relocated_size) {
185 *error_msg =
186 StringPrintf(
187 "Original and relocated image sizes differ: %zu vs %zu", original_size, relocated_size);
188 return false;
189 }
190 if ((original_size % 4) != 0) {
191 *error_msg = StringPrintf("Image size not multiple of 4: %zu", original_size);
192 return false;
193 }
194 if (original_size > UINT32_MAX) {
195 *error_msg = StringPrintf("Image too large: %zu" , original_size);
196 return false;
197 }
198
199 const ImageHeader& relocated_header =
200 *reinterpret_cast<const ImageHeader*>(relocated.Begin());
201 // Offsets are supposed to differ between original and relocated by this value
202 off_t expected_diff = relocated_header.GetPatchDelta();
203 if (expected_diff == 0) {
204 // Can't identify offsets which are supposed to differ due to relocation
205 *error_msg = "Relocation delta is 0";
206 return false;
207 }
208
209 // Output the SHA-256 digest of the original
210 output->resize(SHA256_DIGEST_LENGTH);
211 const uint8_t* original_bytes = original.Begin();
212 SHA256(original_bytes, original_size, output->data());
213
214 // Output the list of offsets at which the original and patched images differ
215 size_t last_diff_offset = 0;
216 size_t diff_offset_count = 0;
217 const uint8_t* relocated_bytes = relocated.Begin();
218 for (size_t offset = 0; offset < original_size; offset += 4) {
219 uint32_t original_value = *reinterpret_cast<const uint32_t*>(original_bytes + offset);
220 uint32_t relocated_value = *reinterpret_cast<const uint32_t*>(relocated_bytes + offset);
221 off_t diff = relocated_value - original_value;
222 if (diff == 0) {
223 continue;
224 } else if (diff != expected_diff) {
225 *error_msg =
226 StringPrintf(
227 "Unexpected diff at offset %zu. Expected: %jd, but was: %jd",
228 offset,
229 (intmax_t) expected_diff,
230 (intmax_t) diff);
231 return false;
232 }
233
234 uint32_t offset_diff = offset - last_diff_offset;
235 last_diff_offset = offset;
236 diff_offset_count++;
237
238 EncodeUnsignedLeb128(output, offset_diff);
239 }
240
241 if (diff_offset_count == 0) {
242 *error_msg = "Original and patched images are identical";
243 return false;
244 }
245
246 return true;
247}
248
249static bool WriteRelFile(
250 const MemMap& original,
251 const MemMap& relocated,
252 const std::string& rel_filename,
253 std::string* error_msg) {
254 std::vector<uint8_t> output;
255 if (!PatchOat::GeneratePatch(original, relocated, &output, error_msg)) {
256 return false;
257 }
258
259 std::unique_ptr<File> rel_file(OS::CreateEmptyFileWriteOnly(rel_filename.c_str()));
260 if (rel_file.get() == nullptr) {
261 *error_msg = StringPrintf("Failed to create/open output file %s", rel_filename.c_str());
262 return false;
263 }
264 if (!rel_file->WriteFully(output.data(), output.size())) {
265 *error_msg = StringPrintf("Failed to write to %s", rel_filename.c_str());
266 return false;
267 }
268 if (rel_file->FlushCloseOrErase() != 0) {
269 *error_msg = StringPrintf("Failed to flush and close %s", rel_filename.c_str());
270 return false;
271 }
272
273 return true;
274}
275
Chris Morin754b7572018-01-19 18:04:46 -0800276static bool CheckImageIdenticalToOriginalExceptForRelocation(
277 const std::string& relocated_filename,
278 const std::string& original_filename,
279 std::string* error_msg) {
280 *error_msg = "";
281 std::string rel_filename = original_filename + ".rel";
282 std::unique_ptr<File> rel_file(OS::OpenFileForReading(rel_filename.c_str()));
283 if (rel_file.get() == nullptr) {
284 *error_msg = StringPrintf("Failed to open image relocation file %s", rel_filename.c_str());
285 return false;
286 }
287 int64_t rel_size = rel_file->GetLength();
288 if (rel_size < 0) {
289 *error_msg = StringPrintf("Error while getting size of image relocation file %s",
290 rel_filename.c_str());
291 return false;
292 }
293 std::unique_ptr<uint8_t[]> rel(new uint8_t[rel_size]);
294 if (!rel_file->ReadFully(rel.get(), rel_size)) {
295 *error_msg = StringPrintf("Failed to read image relocation file %s", rel_filename.c_str());
296 return false;
297 }
298
299 std::unique_ptr<File> image_file(OS::OpenFileForReading(relocated_filename.c_str()));
300 if (image_file.get() == nullptr) {
301 *error_msg = StringPrintf("Unable to open relocated image file %s",
302 relocated_filename.c_str());
303 return false;
304 }
305
306 int64_t image_size = image_file->GetLength();
307 if (image_size < 0) {
308 *error_msg = StringPrintf("Error while getting size of relocated image file %s",
309 relocated_filename.c_str());
310 return false;
311 }
312 if ((image_size % 4) != 0) {
313 *error_msg =
314 StringPrintf(
Orion Hodsonb348b3b2018-01-26 09:02:49 +0000315 "Relocated image file %s size not multiple of 4: %" PRId64,
Chris Morin754b7572018-01-19 18:04:46 -0800316 relocated_filename.c_str(), image_size);
317 return false;
318 }
Orion Hodsonb348b3b2018-01-26 09:02:49 +0000319 if (image_size > std::numeric_limits<uint32_t>::max()) {
Chris Morin754b7572018-01-19 18:04:46 -0800320 *error_msg =
321 StringPrintf(
Orion Hodsonb348b3b2018-01-26 09:02:49 +0000322 "Relocated image file %s too large: %" PRId64, relocated_filename.c_str(), image_size);
Chris Morin754b7572018-01-19 18:04:46 -0800323 return false;
324 }
325
326 std::unique_ptr<uint8_t[]> image(new uint8_t[image_size]);
327 if (!image_file->ReadFully(image.get(), image_size)) {
328 *error_msg = StringPrintf("Failed to read relocated image file %s", relocated_filename.c_str());
329 return false;
330 }
331
332 const uint8_t* original_image_digest = rel.get();
333 if (rel_size < SHA256_DIGEST_LENGTH) {
334 *error_msg = StringPrintf("Malformed image relocation file %s: too short",
335 rel_filename.c_str());
336 return false;
337 }
338
339 const ImageHeader& image_header = *reinterpret_cast<const ImageHeader*>(image.get());
340 off_t expected_diff = image_header.GetPatchDelta();
341
342 if (expected_diff == 0) {
343 *error_msg = StringPrintf("Unsuported patch delta of zero in %s",
344 relocated_filename.c_str());
345 return false;
346 }
347
348 // Relocated image is expected to differ from the original due to relocation.
349 // Unrelocate the image in memory to compensate.
350 uint8_t* image_start = image.get();
351 const uint8_t* rel_end = &rel[rel_size];
352 const uint8_t* rel_ptr = &rel[SHA256_DIGEST_LENGTH];
353 // The remaining .rel file consists of offsets at which relocation should've occurred.
354 // For each offset, we "unrelocate" the image by subtracting the expected relocation
355 // diff value (as specified in the image header).
356 //
357 // Each offset is encoded as a delta/diff relative to the previous offset. With the
358 // very first offset being encoded relative to offset 0.
359 // Deltas are encoded using little-endian 7 bits per byte encoding, with all bytes except
360 // the last one having the highest bit set.
361 uint32_t offset = 0;
362 while (rel_ptr != rel_end) {
363 uint32_t offset_delta = 0;
364 if (DecodeUnsignedLeb128Checked(&rel_ptr, rel_end, &offset_delta)) {
365 offset += offset_delta;
366 uint32_t *image_value = reinterpret_cast<uint32_t*>(image_start + offset);
367 *image_value -= expected_diff;
368 } else {
369 *error_msg =
370 StringPrintf(
371 "Malformed image relocation file %s: "
372 "last byte has it's most significant bit set",
373 rel_filename.c_str());
374 return false;
375 }
376 }
377
378 // Image in memory is now supposed to be identical to the original. We
379 // confirm this by comparing the digest of the in-memory image to the expected
380 // digest from relocation file.
381 uint8_t image_digest[SHA256_DIGEST_LENGTH];
382 SHA256(image.get(), image_size, image_digest);
383 if (memcmp(image_digest, original_image_digest, SHA256_DIGEST_LENGTH) != 0) {
384 *error_msg =
385 StringPrintf(
386 "Relocated image %s does not match the original %s after unrelocation",
387 relocated_filename.c_str(),
388 original_filename.c_str());
389 return false;
390 }
391
392 // Relocated image is identical to the original, once relocations are taken into account
393 return true;
394}
395
Chris Morinae6832f2018-02-09 19:12:35 -0800396static bool VerifySymlink(const std::string& intended_target, const std::string& link_name) {
397 std::string actual_target;
398 if (!android::base::Readlink(link_name, &actual_target)) {
399 PLOG(ERROR) << "Readlink on " << link_name << " failed.";
400 return false;
401 }
402 return actual_target == intended_target;
403}
404
405static bool VerifyVdexAndOatSymlinks(const std::string& input_image_filename,
406 const std::string& output_image_filename) {
407 return VerifySymlink(ImageHeader::GetVdexLocationFromImageLocation(input_image_filename),
408 ImageHeader::GetVdexLocationFromImageLocation(output_image_filename))
409 && VerifySymlink(ImageHeader::GetOatLocationFromImageLocation(input_image_filename),
410 ImageHeader::GetOatLocationFromImageLocation(output_image_filename));
411}
412
413bool PatchOat::CreateVdexAndOatSymlinks(const std::string& input_image_filename,
414 const std::string& output_image_filename) {
415 std::string input_vdex_filename =
416 ImageHeader::GetVdexLocationFromImageLocation(input_image_filename);
417 std::string input_oat_filename =
418 ImageHeader::GetOatLocationFromImageLocation(input_image_filename);
419
420 std::unique_ptr<File> input_oat_file(OS::OpenFileForReading(input_oat_filename.c_str()));
421 if (input_oat_file.get() == nullptr) {
422 LOG(ERROR) << "Unable to open input oat file at " << input_oat_filename;
423 return false;
424 }
425 std::string error_msg;
426 std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat_file.get(),
427 PROT_READ | PROT_WRITE,
428 MAP_PRIVATE,
429 &error_msg));
430 if (elf.get() == nullptr) {
431 LOG(ERROR) << "Unable to open oat file " << input_oat_filename << " : " << error_msg;
432 return false;
433 }
434
435 MaybePic is_oat_pic = IsOatPic(elf.get());
436 if (is_oat_pic >= ERROR_FIRST) {
437 // Error logged by IsOatPic
438 return false;
439 } else if (is_oat_pic == NOT_PIC) {
440 LOG(ERROR) << "patchoat cannot be used on non-PIC oat file: " << input_oat_filename;
441 return false;
442 }
443
444 CHECK(is_oat_pic == PIC);
445
446 std::string output_vdex_filename =
447 ImageHeader::GetVdexLocationFromImageLocation(output_image_filename);
448 std::string output_oat_filename =
449 ImageHeader::GetOatLocationFromImageLocation(output_image_filename);
450
451 return SymlinkFile(input_oat_filename, output_oat_filename) &&
452 SymlinkFile(input_vdex_filename, output_vdex_filename);
453}
454
Andreas Gampe6eb6a392016-02-10 20:18:37 -0800455bool PatchOat::Patch(const std::string& image_location,
456 off_t delta,
Alex Klyubin3856af02017-10-23 13:53:13 -0700457 const std::string& output_image_directory,
458 const std::string& output_image_relocation_directory,
Andreas Gampe6eb6a392016-02-10 20:18:37 -0800459 InstructionSet isa,
460 TimingLogger* timings) {
Alex Klyubin3856af02017-10-23 13:53:13 -0700461 bool output_image = !output_image_directory.empty();
462 bool output_image_relocation = !output_image_relocation_directory.empty();
463 if ((!output_image) && (!output_image_relocation)) {
464 // Nothing to do
465 return true;
466 }
467 if ((output_image_relocation) && (delta == 0)) {
468 LOG(ERROR) << "Cannot output image relocation information when requested relocation delta is 0";
469 return false;
470 }
471
Alex Light53cb16b2014-06-12 11:26:29 -0700472 CHECK(Runtime::Current() == nullptr);
Alex Light53cb16b2014-06-12 11:26:29 -0700473 CHECK(!image_location.empty()) << "image file must have a filename.";
474
Alex Lighteefbe392014-07-08 09:53:18 -0700475 TimingLogger::ScopedTiming t("Runtime Setup", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700476
Vladimir Marko33bff252017-11-01 14:35:42 +0000477 CHECK_NE(isa, InstructionSet::kNone);
Igor Murashkin46774762014-10-22 11:37:02 -0700478
Alex Light53cb16b2014-06-12 11:26:29 -0700479 // Set up the runtime
Andreas Gampec7d25082018-03-09 12:06:45 -0800480 PatchoatRuntimeOptionsHolder options_holder(image_location, isa);
481 if (!Runtime::Create(options_holder.GetRuntimeOptions(), false)) {
Alex Light53cb16b2014-06-12 11:26:29 -0700482 LOG(ERROR) << "Unable to initialize runtime";
483 return false;
484 }
Andreas Gampeb8cc1752017-04-26 21:28:50 -0700485 std::unique_ptr<Runtime> runtime(Runtime::Current());
486
Alex Light53cb16b2014-06-12 11:26:29 -0700487 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
488 // give it away now and then switch to a more manageable ScopedObjectAccess.
489 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
490 ScopedObjectAccess soa(Thread::Current());
491
Jeff Haodcdc85b2015-12-04 14:06:18 -0800492 std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800493 std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>> space_to_memmap_map;
Alex Light53cb16b2014-06-12 11:26:29 -0700494
Jeff Haodcdc85b2015-12-04 14:06:18 -0800495 for (size_t i = 0; i < spaces.size(); ++i) {
Chris Morinae6832f2018-02-09 19:12:35 -0800496 t.NewTiming("Image Patching setup");
Jeff Haodcdc85b2015-12-04 14:06:18 -0800497 gc::space::ImageSpace* space = spaces[i];
498 std::string input_image_filename = space->GetImageFilename();
499 std::unique_ptr<File> input_image(OS::OpenFileForReading(input_image_filename.c_str()));
500 if (input_image.get() == nullptr) {
501 LOG(ERROR) << "Unable to open input image file at " << input_image_filename;
Igor Murashkin46774762014-10-22 11:37:02 -0700502 return false;
503 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800504
505 int64_t image_len = input_image->GetLength();
506 if (image_len < 0) {
507 LOG(ERROR) << "Error while getting image length";
508 return false;
509 }
510 ImageHeader image_header;
511 if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
512 sizeof(image_header), 0)) {
513 LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
514 }
515
516 /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath());
517 // Nothing special to do right now since the image always needs to get patched.
518 // Perhaps in some far-off future we may have images with relative addresses that are true-PIC.
519
520 // Create the map where we will write the image patches to.
521 std::string error_msg;
522 std::unique_ptr<MemMap> image(MemMap::MapFile(image_len,
523 PROT_READ | PROT_WRITE,
524 MAP_PRIVATE,
525 input_image->Fd(),
526 0,
527 /*low_4gb*/false,
528 input_image->GetPath().c_str(),
529 &error_msg));
530 if (image.get() == nullptr) {
531 LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg;
532 return false;
533 }
Chris Morinae6832f2018-02-09 19:12:35 -0800534
535
Jeff Haodcdc85b2015-12-04 14:06:18 -0800536 space_to_memmap_map.emplace(space, std::move(image));
Chris Morinae6832f2018-02-09 19:12:35 -0800537 PatchOat p = PatchOat(isa,
538 space_to_memmap_map.at(space).get(),
539 space->GetLiveBitmap(),
540 space->GetMemMap(),
541 delta,
542 &space_to_memmap_map,
543 timings);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800544
Richard Uhler4bc11d02017-02-01 09:53:54 +0000545 t.NewTiming("Patching image");
Jeff Haodcdc85b2015-12-04 14:06:18 -0800546 if (!p.PatchImage(i == 0)) {
547 LOG(ERROR) << "Failed to patch image file " << input_image_filename;
548 return false;
549 }
Alex Light53cb16b2014-06-12 11:26:29 -0700550
Alex Klyubin3856af02017-10-23 13:53:13 -0700551 // Write the patched image spaces.
Chris Morinae6832f2018-02-09 19:12:35 -0800552 if (output_image) {
553 std::string output_image_filename;
554 if (!GetDalvikCacheFilename(space->GetImageLocation().c_str(),
555 output_image_directory.c_str(),
556 &output_image_filename,
557 &error_msg)) {
558 LOG(ERROR) << "Failed to find relocated image file name: " << error_msg;
559 return false;
560 }
561
562 if (!CreateVdexAndOatSymlinks(input_image_filename, output_image_filename))
563 return false;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800564
Alex Klyubin3856af02017-10-23 13:53:13 -0700565 t.NewTiming("Writing image");
Alex Klyubin3856af02017-10-23 13:53:13 -0700566 std::unique_ptr<File> output_image_file(CreateOrOpen(output_image_filename.c_str()));
567 if (output_image_file.get() == nullptr) {
568 LOG(ERROR) << "Failed to open output image file at " << output_image_filename;
569 return false;
570 }
571
Alex Klyubin3856af02017-10-23 13:53:13 -0700572 bool success = p.WriteImage(output_image_file.get());
573 success = FinishFile(output_image_file.get(), success);
574 if (!success) {
575 return false;
576 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800577 }
578
Chris Morinae6832f2018-02-09 19:12:35 -0800579 if (output_image_relocation) {
Alex Klyubin3856af02017-10-23 13:53:13 -0700580 t.NewTiming("Writing image relocation");
581 std::string original_image_filename(space->GetImageLocation() + ".rel");
582 std::string image_relocation_filename =
583 output_image_relocation_directory
584 + (android::base::StartsWith(original_image_filename, "/") ? "" : "/")
585 + original_image_filename.substr(original_image_filename.find_last_of("/"));
Chris Morinae6832f2018-02-09 19:12:35 -0800586 int64_t input_image_size = input_image->GetLength();
Alex Klyubin3856af02017-10-23 13:53:13 -0700587 if (input_image_size < 0) {
588 LOG(ERROR) << "Error while getting input image size";
589 return false;
590 }
Alex Klyubin3856af02017-10-23 13:53:13 -0700591 std::unique_ptr<MemMap> original(MemMap::MapFile(input_image_size,
592 PROT_READ,
593 MAP_PRIVATE,
Chris Morinae6832f2018-02-09 19:12:35 -0800594 input_image->Fd(),
Alex Klyubin3856af02017-10-23 13:53:13 -0700595 0,
596 /*low_4gb*/false,
Chris Morinae6832f2018-02-09 19:12:35 -0800597 input_image->GetPath().c_str(),
Alex Klyubin3856af02017-10-23 13:53:13 -0700598 &error_msg));
599 if (original.get() == nullptr) {
Chris Morinae6832f2018-02-09 19:12:35 -0800600 LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg;
Alex Klyubin3856af02017-10-23 13:53:13 -0700601 return false;
602 }
603
Alex Klyubin3856af02017-10-23 13:53:13 -0700604 const MemMap* relocated = p.image_;
605
606 if (!WriteRelFile(*original, *relocated, image_relocation_filename, &error_msg)) {
607 LOG(ERROR) << "Failed to create image relocation file " << image_relocation_filename
608 << ": " << error_msg;
609 return false;
610 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800611 }
Alex Light53cb16b2014-06-12 11:26:29 -0700612 }
Andreas Gampeb8cc1752017-04-26 21:28:50 -0700613
Andreas Gampe8b362a82018-05-22 20:54:14 +0000614 if (!kIsDebugBuild && !(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
Andreas Gampeb8cc1752017-04-26 21:28:50 -0700615 // We want to just exit on non-debug builds, not bringing the runtime down
616 // in an orderly fashion. So release the following fields.
617 runtime.release();
618 }
619
Alex Light53cb16b2014-06-12 11:26:29 -0700620 return true;
621}
622
Chris Morin754b7572018-01-19 18:04:46 -0800623bool PatchOat::Verify(const std::string& image_location,
624 const std::string& output_image_directory,
625 InstructionSet isa,
626 TimingLogger* timings) {
627 if (image_location.empty()) {
628 LOG(ERROR) << "Original image file not provided";
629 return false;
630 }
631 if (output_image_directory.empty()) {
632 LOG(ERROR) << "Relocated image directory not provided";
633 return false;
634 }
635
636 TimingLogger::ScopedTiming t("Runtime Setup", timings);
637
638 CHECK_NE(isa, InstructionSet::kNone);
Chris Morin754b7572018-01-19 18:04:46 -0800639
640 // Set up the runtime
Andreas Gampec7d25082018-03-09 12:06:45 -0800641 PatchoatRuntimeOptionsHolder options_holder(image_location, isa);
642 if (!Runtime::Create(options_holder.GetRuntimeOptions(), false)) {
Chris Morin754b7572018-01-19 18:04:46 -0800643 LOG(ERROR) << "Unable to initialize runtime";
644 return false;
645 }
646 std::unique_ptr<Runtime> runtime(Runtime::Current());
647
648 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
649 // give it away now and then switch to a more manageable ScopedObjectAccess.
650 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
651 ScopedObjectAccess soa(Thread::Current());
652
653 t.NewTiming("Image Verification setup");
654 std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
655
656 // TODO: Check that no other .rel files exist in the original dir
657
658 bool success = true;
659 std::string image_location_dir = android::base::Dirname(image_location);
660 for (size_t i = 0; i < spaces.size(); ++i) {
661 gc::space::ImageSpace* space = spaces[i];
Chris Morin754b7572018-01-19 18:04:46 -0800662
663 std::string relocated_image_filename;
664 std::string error_msg;
Chris Morinae6832f2018-02-09 19:12:35 -0800665 if (!GetDalvikCacheFilename(space->GetImageLocation().c_str(),
Chris Morin754b7572018-01-19 18:04:46 -0800666 output_image_directory.c_str(), &relocated_image_filename, &error_msg)) {
667 LOG(ERROR) << "Failed to find relocated image file name: " << error_msg;
668 success = false;
669 break;
670 }
671 // location: /system/framework/boot.art
672 // isa: arm64
673 // basename: boot.art
674 // original: /system/framework/arm64/boot.art
675 // relocation: /system/framework/arm64/boot.art.rel
Chris Morinae6832f2018-02-09 19:12:35 -0800676 std::string original_image_filename =
677 GetSystemImageFilename(space->GetImageLocation().c_str(), isa);
Chris Morin754b7572018-01-19 18:04:46 -0800678
679 if (!CheckImageIdenticalToOriginalExceptForRelocation(
680 relocated_image_filename, original_image_filename, &error_msg)) {
681 LOG(ERROR) << error_msg;
682 success = false;
683 break;
684 }
Chris Morinae6832f2018-02-09 19:12:35 -0800685
686 if (!VerifyVdexAndOatSymlinks(original_image_filename, relocated_image_filename)) {
687 LOG(ERROR) << "Verification of vdex and oat symlinks for "
688 << space->GetImageLocation() << " failed.";
689 success = false;
690 break;
691 }
Chris Morin754b7572018-01-19 18:04:46 -0800692 }
693
Andreas Gampe8b362a82018-05-22 20:54:14 +0000694 if (!kIsDebugBuild && !(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
Chris Morin754b7572018-01-19 18:04:46 -0800695 // We want to just exit on non-debug builds, not bringing the runtime down
696 // in an orderly fashion. So release the following fields.
697 runtime.release();
698 }
699
700 return success;
701}
702
Alex Light53cb16b2014-06-12 11:26:29 -0700703bool PatchOat::WriteImage(File* out) {
Greg Kaiser4eb17792018-04-06 14:26:23 -0700704 CHECK(out != nullptr);
Alex Lighteefbe392014-07-08 09:53:18 -0700705 TimingLogger::ScopedTiming t("Writing image File", timings_);
Alex Lighta59dd802014-07-02 16:28:08 -0700706 std::string error_msg;
707
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100708 // No error checking here, this is best effort. The locking may or may not
709 // succeed and we don't really care either way.
710 ScopedFlock img_flock = LockedFile::DupOf(out->Fd(), out->GetPath(),
711 true /* read_only_mode */, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700712
Alex Light53cb16b2014-06-12 11:26:29 -0700713 CHECK(image_ != nullptr);
Alex Light53cb16b2014-06-12 11:26:29 -0700714 size_t expect = image_->Size();
715 if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) &&
716 out->SetLength(expect) == 0) {
717 return true;
718 } else {
719 LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed.";
720 return false;
721 }
722}
723
Igor Murashkin46774762014-10-22 11:37:02 -0700724bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) {
725 if (!image_header.CompilePic()) {
726 if (kIsDebugBuild) {
727 LOG(INFO) << "image at location " << image_path << " was *not* compiled pic";
728 }
729 return false;
730 }
731
732 if (kIsDebugBuild) {
733 LOG(INFO) << "image at location " << image_path << " was compiled PIC";
734 }
735
736 return true;
737}
738
739PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) {
740 if (oat_in == nullptr) {
741 LOG(ERROR) << "No ELF input oat fie available";
742 return ERROR_OAT_FILE;
743 }
744
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700745 const std::string& file_path = oat_in->GetFilePath();
Igor Murashkin46774762014-10-22 11:37:02 -0700746
747 const OatHeader* oat_header = GetOatHeader(oat_in);
748 if (oat_header == nullptr) {
749 LOG(ERROR) << "Failed to find oat header in oat file " << file_path;
750 return ERROR_OAT_FILE;
751 }
752
753 if (!oat_header->IsValid()) {
754 LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header";
755 return ERROR_OAT_FILE;
756 }
757
758 bool is_pic = oat_header->IsPic();
759 if (kIsDebugBuild) {
760 LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic");
761 }
762
763 return is_pic ? PIC : NOT_PIC;
764}
765
Vladimir Markoad06b982016-11-17 16:38:59 +0000766class PatchOat::PatchOatArtFieldVisitor : public ArtFieldVisitor {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700767 public:
768 explicit PatchOatArtFieldVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
769
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700770 void Visit(ArtField* field) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700771 ArtField* const dest = patch_oat_->RelocatedCopyOf(field);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700772 dest->SetDeclaringClass(
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700773 patch_oat_->RelocatedAddressOfPointer(field->GetDeclaringClass().Ptr()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700774 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700775
776 private:
777 PatchOat* const patch_oat_;
778};
779
780void PatchOat::PatchArtFields(const ImageHeader* image_header) {
781 PatchOatArtFieldVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700782 image_header->VisitPackedArtFields(&visitor, heap_->Begin());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700783}
784
Vladimir Markoad06b982016-11-17 16:38:59 +0000785class PatchOat::PatchOatArtMethodVisitor : public ArtMethodVisitor {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700786 public:
787 explicit PatchOatArtMethodVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
788
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700789 void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700790 ArtMethod* const dest = patch_oat_->RelocatedCopyOf(method);
791 patch_oat_->FixupMethod(method, dest);
792 }
793
794 private:
795 PatchOat* const patch_oat_;
796};
797
Mathieu Chartiere401d142015-04-22 13:56:20 -0700798void PatchOat::PatchArtMethods(const ImageHeader* image_header) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700799 const PointerSize pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700800 PatchOatArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700801 image_header->VisitPackedArtMethods(&visitor, heap_->Begin(), pointer_size);
802}
803
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000804void PatchOat::PatchImTables(const ImageHeader* image_header) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700805 const PointerSize pointer_size = InstructionSetPointerSize(isa_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000806 // We can safely walk target image since the conflict tables are independent.
807 image_header->VisitPackedImTables(
808 [this](ArtMethod* method) {
809 return RelocatedAddressOfPointer(method);
810 },
811 image_->Begin(),
812 pointer_size);
813}
814
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700815void PatchOat::PatchImtConflictTables(const ImageHeader* image_header) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700816 const PointerSize pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700817 // We can safely walk target image since the conflict tables are independent.
818 image_header->VisitPackedImtConflictTables(
819 [this](ArtMethod* method) {
820 return RelocatedAddressOfPointer(method);
821 },
822 image_->Begin(),
823 pointer_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700824}
825
Vladimir Markoad06b982016-11-17 16:38:59 +0000826class PatchOat::FixupRootVisitor : public RootVisitor {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700827 public:
828 explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) {
829 }
830
831 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700832 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700833 for (size_t i = 0; i < count; ++i) {
834 *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]);
835 }
836 }
837
838 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
839 const RootInfo& info ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700840 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700841 for (size_t i = 0; i < count; ++i) {
842 roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr()));
843 }
844 }
845
846 private:
847 const PatchOat* const patch_oat_;
848};
849
850void PatchOat::PatchInternedStrings(const ImageHeader* image_header) {
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100851 const auto& section = image_header->GetInternedStringsSection();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100852 if (section.Size() == 0) {
853 return;
854 }
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700855 InternTable temp_table;
856 // Note that we require that ReadFromMemory does not make an internal copy of the elements.
857 // This also relies on visit roots not doing any verification which could fail after we update
858 // the roots to be the image addresses.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800859 temp_table.AddTableFromMemory(image_->Begin() + section.Offset());
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700860 FixupRootVisitor visitor(this);
861 temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots);
862}
863
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800864void PatchOat::PatchClassTable(const ImageHeader* image_header) {
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100865 const auto& section = image_header->GetClassTableSection();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800866 if (section.Size() == 0) {
867 return;
868 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800869 // Note that we require that ReadFromMemory does not make an internal copy of the elements.
870 // This also relies on visit roots not doing any verification which could fail after we update
871 // the roots to be the image addresses.
872 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
873 ClassTable temp_table;
874 temp_table.ReadFromMemory(image_->Begin() + section.Offset());
875 FixupRootVisitor visitor(this);
Mathieu Chartier58c3f6a2016-12-01 14:21:11 -0800876 temp_table.VisitRoots(UnbufferedRootVisitor(&visitor, RootInfo(kRootUnknown)));
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800877}
878
879
Vladimir Markoad06b982016-11-17 16:38:59 +0000880class PatchOat::RelocatedPointerVisitor {
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800881 public:
882 explicit RelocatedPointerVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
883
884 template <typename T>
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800885 T* operator()(T* ptr, void** dest_addr ATTRIBUTE_UNUSED = 0) const {
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800886 return patch_oat_->RelocatedAddressOfPointer(ptr);
887 }
888
889 private:
890 PatchOat* const patch_oat_;
891};
892
Mathieu Chartierc7853442015-03-27 14:35:38 -0700893void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) {
894 auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>(
895 img_roots->Get(ImageHeader::kDexCaches));
Andreas Gampe542451c2016-07-26 09:02:02 -0700896 const PointerSize pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700897 for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100898 auto* orig_dex_cache = dex_caches->GetWithoutChecks(i);
899 auto* copy_dex_cache = RelocatedCopyOf(orig_dex_cache);
Vladimir Marko05792b92015-08-03 11:56:49 +0100900 // Though the DexCache array fields are usually treated as native pointers, we set the full
901 // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is
902 // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e.
903 // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))).
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700904 mirror::StringDexCacheType* orig_strings = orig_dex_cache->GetStrings();
905 mirror::StringDexCacheType* relocated_strings = RelocatedAddressOfPointer(orig_strings);
Vladimir Marko05792b92015-08-03 11:56:49 +0100906 copy_dex_cache->SetField64<false>(
907 mirror::DexCache::StringsOffset(),
908 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_strings)));
909 if (orig_strings != nullptr) {
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800910 orig_dex_cache->FixupStrings(RelocatedCopyOf(orig_strings), RelocatedPointerVisitor(this));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700911 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000912 mirror::TypeDexCacheType* orig_types = orig_dex_cache->GetResolvedTypes();
913 mirror::TypeDexCacheType* relocated_types = RelocatedAddressOfPointer(orig_types);
Vladimir Marko05792b92015-08-03 11:56:49 +0100914 copy_dex_cache->SetField64<false>(
915 mirror::DexCache::ResolvedTypesOffset(),
916 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_types)));
917 if (orig_types != nullptr) {
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800918 orig_dex_cache->FixupResolvedTypes(RelocatedCopyOf(orig_types),
919 RelocatedPointerVisitor(this));
Vladimir Marko05792b92015-08-03 11:56:49 +0100920 }
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100921 mirror::MethodDexCacheType* orig_methods = orig_dex_cache->GetResolvedMethods();
922 mirror::MethodDexCacheType* relocated_methods = RelocatedAddressOfPointer(orig_methods);
Vladimir Marko05792b92015-08-03 11:56:49 +0100923 copy_dex_cache->SetField64<false>(
924 mirror::DexCache::ResolvedMethodsOffset(),
925 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_methods)));
926 if (orig_methods != nullptr) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100927 mirror::MethodDexCacheType* copy_methods = RelocatedCopyOf(orig_methods);
Vladimir Marko05792b92015-08-03 11:56:49 +0100928 for (size_t j = 0, num = orig_dex_cache->NumResolvedMethods(); j != num; ++j) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100929 mirror::MethodDexCachePair orig =
930 mirror::DexCache::GetNativePairPtrSize(orig_methods, j, pointer_size);
931 mirror::MethodDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index);
932 mirror::DexCache::SetNativePairPtrSize(copy_methods, j, copy, pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100933 }
934 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000935 mirror::FieldDexCacheType* orig_fields = orig_dex_cache->GetResolvedFields();
936 mirror::FieldDexCacheType* relocated_fields = RelocatedAddressOfPointer(orig_fields);
Vladimir Marko05792b92015-08-03 11:56:49 +0100937 copy_dex_cache->SetField64<false>(
938 mirror::DexCache::ResolvedFieldsOffset(),
939 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_fields)));
940 if (orig_fields != nullptr) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000941 mirror::FieldDexCacheType* copy_fields = RelocatedCopyOf(orig_fields);
Vladimir Marko05792b92015-08-03 11:56:49 +0100942 for (size_t j = 0, num = orig_dex_cache->NumResolvedFields(); j != num; ++j) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000943 mirror::FieldDexCachePair orig =
944 mirror::DexCache::GetNativePairPtrSize(orig_fields, j, pointer_size);
945 mirror::FieldDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index);
946 mirror::DexCache::SetNativePairPtrSize(copy_fields, j, copy, pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100947 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700948 }
Narayan Kamath7fe56582016-10-14 18:49:12 +0100949 mirror::MethodTypeDexCacheType* orig_method_types = orig_dex_cache->GetResolvedMethodTypes();
950 mirror::MethodTypeDexCacheType* relocated_method_types =
951 RelocatedAddressOfPointer(orig_method_types);
952 copy_dex_cache->SetField64<false>(
953 mirror::DexCache::ResolvedMethodTypesOffset(),
954 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_method_types)));
955 if (orig_method_types != nullptr) {
956 orig_dex_cache->FixupResolvedMethodTypes(RelocatedCopyOf(orig_method_types),
957 RelocatedPointerVisitor(this));
958 }
Orion Hodsonc069a302017-01-18 09:23:12 +0000959
960 GcRoot<mirror::CallSite>* orig_call_sites = orig_dex_cache->GetResolvedCallSites();
961 GcRoot<mirror::CallSite>* relocated_call_sites = RelocatedAddressOfPointer(orig_call_sites);
962 copy_dex_cache->SetField64<false>(
963 mirror::DexCache::ResolvedCallSitesOffset(),
964 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_call_sites)));
965 if (orig_call_sites != nullptr) {
966 orig_dex_cache->FixupResolvedCallSites(RelocatedCopyOf(orig_call_sites),
967 RelocatedPointerVisitor(this));
968 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700969 }
970}
971
Jeff Haodcdc85b2015-12-04 14:06:18 -0800972bool PatchOat::PatchImage(bool primary_image) {
Alex Light53cb16b2014-06-12 11:26:29 -0700973 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
974 CHECK_GT(image_->Size(), sizeof(ImageHeader));
975 // These are the roots from the original file.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700976 auto* img_roots = image_header->GetImageRoots();
Alex Light53cb16b2014-06-12 11:26:29 -0700977 image_header->RelocateImage(delta_);
978
Mathieu Chartierc7853442015-03-27 14:35:38 -0700979 PatchArtFields(image_header);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700980 PatchArtMethods(image_header);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000981 PatchImTables(image_header);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700982 PatchImtConflictTables(image_header);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700983 PatchInternedStrings(image_header);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800984 PatchClassTable(image_header);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700985 // Patch dex file int/long arrays which point to ArtFields.
986 PatchDexFileArrays(img_roots);
987
Jeff Haodcdc85b2015-12-04 14:06:18 -0800988 if (primary_image) {
989 VisitObject(img_roots);
990 }
991
Alex Light53cb16b2014-06-12 11:26:29 -0700992 if (!image_header->IsValid()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800993 LOG(ERROR) << "relocation renders image header invalid";
Alex Light53cb16b2014-06-12 11:26:29 -0700994 return false;
995 }
996
997 {
Alex Lighteefbe392014-07-08 09:53:18 -0700998 TimingLogger::ScopedTiming t("Walk Bitmap", timings_);
Alex Light53cb16b2014-06-12 11:26:29 -0700999 // Walk the bitmap.
1000 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
Andreas Gampe0c183382017-07-13 22:26:24 -07001001 auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1002 VisitObject(obj);
1003 };
1004 bitmap_->Walk(visitor);
Alex Light53cb16b2014-06-12 11:26:29 -07001005 }
1006 return true;
1007}
1008
Alex Light53cb16b2014-06-12 11:26:29 -07001009
Mathieu Chartier31e88222016-10-14 18:43:19 -07001010void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Object> obj,
1011 MemberOffset off,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001012 bool is_static_unused ATTRIBUTE_UNUSED) const {
Alex Light53cb16b2014-06-12 11:26:29 -07001013 mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001014 mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent);
Alex Light53cb16b2014-06-12 11:26:29 -07001015 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
1016}
1017
Mathieu Chartier31e88222016-10-14 18:43:19 -07001018void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Class> cls ATTRIBUTE_UNUSED,
1019 ObjPtr<mirror::Reference> ref) const {
Alex Light53cb16b2014-06-12 11:26:29 -07001020 MemberOffset off = mirror::Reference::ReferentOffset();
1021 mirror::Object* referent = ref->GetReferent();
Mathieu Chartiera13abba2016-04-21 10:23:16 -07001022 DCHECK(referent == nullptr ||
1023 Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(referent)) << referent;
Mathieu Chartierc7853442015-03-27 14:35:38 -07001024 mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent);
Alex Light53cb16b2014-06-12 11:26:29 -07001025 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
1026}
1027
Andreas Gampe0c183382017-07-13 22:26:24 -07001028// Called by PatchImage.
Alex Light53cb16b2014-06-12 11:26:29 -07001029void PatchOat::VisitObject(mirror::Object* object) {
1030 mirror::Object* copy = RelocatedCopyOf(object);
1031 CHECK(copy != nullptr);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001032 if (kUseBakerReadBarrier) {
1033 object->AssertReadBarrierState();
Alex Light53cb16b2014-06-12 11:26:29 -07001034 }
1035 PatchOat::PatchVisitor visitor(this, copy);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001036 object->VisitReferences<kVerifyNone>(visitor, visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001037 if (object->IsClass<kVerifyNone>()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001038 const PointerSize pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -08001039 mirror::Class* klass = object->AsClass();
1040 mirror::Class* copy_klass = down_cast<mirror::Class*>(copy);
1041 RelocatedPointerVisitor native_visitor(this);
1042 klass->FixupNativePointers(copy_klass, pointer_size, native_visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001043 auto* vtable = klass->GetVTable();
1044 if (vtable != nullptr) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001045 vtable->Fixup(RelocatedCopyOfFollowImages(vtable), pointer_size, native_visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001046 }
Mathieu Chartier6beced42016-11-15 15:51:31 -08001047 mirror::IfTable* iftable = klass->GetIfTable();
1048 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
1049 if (iftable->GetMethodArrayCount(i) > 0) {
1050 auto* method_array = iftable->GetMethodArray(i);
1051 CHECK(method_array != nullptr);
1052 method_array->Fixup(RelocatedCopyOfFollowImages(method_array),
1053 pointer_size,
1054 native_visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001055 }
1056 }
Vladimir Marko679730e2018-05-25 15:06:48 +01001057 } else if (object->GetClass() == GetClassRoot<mirror::Method>() ||
1058 object->GetClass() == GetClassRoot<mirror::Constructor>()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001059 // Need to go update the ArtMethod.
Neil Fuller0e844392016-09-08 13:43:31 +01001060 auto* dest = down_cast<mirror::Executable*>(copy);
1061 auto* src = down_cast<mirror::Executable*>(object);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001062 dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod()));
Alex Light53cb16b2014-06-12 11:26:29 -07001063 }
1064}
1065
Mathieu Chartiere401d142015-04-22 13:56:20 -07001066void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001067 const PointerSize pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001068 copy->CopyFrom(object, pointer_size);
Alex Light53cb16b2014-06-12 11:26:29 -07001069 // Just update the entry points if it looks like we should.
Alex Lighteefbe392014-07-08 09:53:18 -07001070 // TODO: sanity check all the pointers' values
Mathieu Chartiere401d142015-04-22 13:56:20 -07001071 copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001072 copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer(
1073 object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001074 // No special handling for IMT conflict table since all pointers are moved by the same offset.
Andreas Gampe75f08852016-07-19 08:06:07 -07001075 copy->SetDataPtrSize(RelocatedAddressOfPointer(
1076 object->GetDataPtrSize(pointer_size)), pointer_size);
Alex Light53cb16b2014-06-12 11:26:29 -07001077}
1078
Alex Light53cb16b2014-06-12 11:26:29 -07001079static int orig_argc;
1080static char** orig_argv;
1081
1082static std::string CommandLine() {
1083 std::vector<std::string> command;
1084 for (int i = 0; i < orig_argc; ++i) {
1085 command.push_back(orig_argv[i]);
1086 }
Andreas Gampe9186ced2016-12-12 14:28:21 -08001087 return android::base::Join(command, ' ');
Alex Light53cb16b2014-06-12 11:26:29 -07001088}
1089
1090static void UsageErrorV(const char* fmt, va_list ap) {
1091 std::string error;
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001092 android::base::StringAppendV(&error, fmt, ap);
Alex Light53cb16b2014-06-12 11:26:29 -07001093 LOG(ERROR) << error;
1094}
1095
1096static void UsageError(const char* fmt, ...) {
1097 va_list ap;
1098 va_start(ap, fmt);
1099 UsageErrorV(fmt, ap);
1100 va_end(ap);
1101}
1102
Andreas Gampe794ad762015-02-23 08:12:24 -08001103NO_RETURN static void Usage(const char *fmt, ...) {
Alex Light53cb16b2014-06-12 11:26:29 -07001104 va_list ap;
1105 va_start(ap, fmt);
1106 UsageErrorV(fmt, ap);
1107 va_end(ap);
1108
1109 UsageError("Command: %s", CommandLine().c_str());
1110 UsageError("Usage: patchoat [options]...");
1111 UsageError("");
1112 UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is");
Richard Uhler4bc11d02017-02-01 09:53:54 +00001113 UsageError(" compiled for (required).");
Alex Light53cb16b2014-06-12 11:26:29 -07001114 UsageError("");
1115 UsageError(" --input-image-location=<file.art>: Specifies the 'location' of the image file to");
Richard Uhler4bc11d02017-02-01 09:53:54 +00001116 UsageError(" be patched.");
Alex Light53cb16b2014-06-12 11:26:29 -07001117 UsageError("");
Chris Morin88c6d262018-02-13 15:26:21 -08001118 UsageError(" --output-image-directory=<dir>: Specifies the directory to write the patched");
1119 UsageError(" image file(s) to.");
Alex Light53cb16b2014-06-12 11:26:29 -07001120 UsageError("");
Chris Morin88c6d262018-02-13 15:26:21 -08001121 UsageError(" --output-image-relocation-directory=<dir>: Specifies the directory to write");
Alex Klyubin3856af02017-10-23 13:53:13 -07001122 UsageError(" the image relocation information to.");
1123 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -07001124 UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by.");
1125 UsageError(" This value may be negative.");
1126 UsageError("");
Chris Morin754b7572018-01-19 18:04:46 -08001127 UsageError(" --verify: Verify an existing patched file instead of creating one.");
1128 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -07001129 UsageError(" --dump-timings: dump out patch timing information");
1130 UsageError("");
1131 UsageError(" --no-dump-timings: do not dump out patch timing information");
1132 UsageError("");
1133
1134 exit(EXIT_FAILURE);
1135}
1136
Chris Morin754b7572018-01-19 18:04:46 -08001137static int patchoat_patch_image(TimingLogger& timings,
1138 InstructionSet isa,
1139 const std::string& input_image_location,
1140 const std::string& output_image_directory,
Chris Morin88c6d262018-02-13 15:26:21 -08001141 const std::string& output_image_relocation_directory,
Chris Morin754b7572018-01-19 18:04:46 -08001142 off_t base_delta,
1143 bool base_delta_set,
1144 bool debug) {
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001145 CHECK(!input_image_location.empty());
Chris Morin88c6d262018-02-13 15:26:21 -08001146 if ((output_image_directory.empty()) && (output_image_relocation_directory.empty())) {
1147 Usage("Image patching requires --output-image-directory or --output-image-relocation-directory");
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001148 }
1149
1150 if (!base_delta_set) {
1151 Usage("Must supply a desired new offset or delta.");
1152 }
1153
1154 if (!IsAligned<kPageSize>(base_delta)) {
1155 Usage("Base offset/delta must be aligned to a pagesize (0x%08x) boundary.", kPageSize);
1156 }
1157
1158 if (debug) {
1159 LOG(INFO) << "moving offset by " << base_delta
1160 << " (0x" << std::hex << base_delta << ") bytes or "
1161 << std::dec << (base_delta/kPageSize) << " pages.";
1162 }
1163
1164 TimingLogger::ScopedTiming pt("patch image and oat", &timings);
1165
Alex Klyubin3856af02017-10-23 13:53:13 -07001166 bool ret =
1167 PatchOat::Patch(
1168 input_image_location,
1169 base_delta,
1170 output_image_directory,
1171 output_image_relocation_directory,
1172 isa,
1173 &timings);
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001174
1175 if (kIsDebugBuild) {
1176 LOG(INFO) << "Exiting with return ... " << ret;
1177 }
1178 return ret ? EXIT_SUCCESS : EXIT_FAILURE;
1179}
1180
Chris Morin754b7572018-01-19 18:04:46 -08001181static int patchoat_verify_image(TimingLogger& timings,
1182 InstructionSet isa,
1183 const std::string& input_image_location,
1184 const std::string& output_image_directory) {
1185 CHECK(!input_image_location.empty());
1186 TimingLogger::ScopedTiming pt("verify image and oat", &timings);
1187
1188 bool ret =
1189 PatchOat::Verify(
1190 input_image_location,
1191 output_image_directory,
1192 isa,
1193 &timings);
1194
1195 if (kIsDebugBuild) {
1196 LOG(INFO) << "Exiting with return ... " << ret;
1197 }
1198 return ret ? EXIT_SUCCESS : EXIT_FAILURE;
1199}
1200
Alex Lighteefbe392014-07-08 09:53:18 -07001201static int patchoat(int argc, char **argv) {
David Sehrc431b9d2018-03-02 12:01:51 -08001202 Locks::Init();
Andreas Gampe51d80cc2017-06-21 21:05:13 -07001203 InitLogging(argv, Runtime::Abort);
Mathieu Chartier6e88ef62014-10-14 15:01:24 -07001204 MemMap::Init();
Alex Light53cb16b2014-06-12 11:26:29 -07001205 const bool debug = kIsDebugBuild;
1206 orig_argc = argc;
1207 orig_argv = argv;
1208 TimingLogger timings("patcher", false, false);
1209
Alex Light53cb16b2014-06-12 11:26:29 -07001210 // Skip over the command name.
1211 argv++;
1212 argc--;
1213
1214 if (argc == 0) {
1215 Usage("No arguments specified");
1216 }
1217
1218 timings.StartTiming("Patchoat");
1219
1220 // cmd line args
1221 bool isa_set = false;
Vladimir Marko33bff252017-11-01 14:35:42 +00001222 InstructionSet isa = InstructionSet::kNone;
Alex Light53cb16b2014-06-12 11:26:29 -07001223 std::string input_image_location;
Chris Morin88c6d262018-02-13 15:26:21 -08001224 std::string output_image_directory;
1225 std::string output_image_relocation_directory;
Alex Light53cb16b2014-06-12 11:26:29 -07001226 off_t base_delta = 0;
1227 bool base_delta_set = false;
Alex Light53cb16b2014-06-12 11:26:29 -07001228 bool dump_timings = kIsDebugBuild;
Chris Morin754b7572018-01-19 18:04:46 -08001229 bool verify = false;
Alex Light53cb16b2014-06-12 11:26:29 -07001230
Ian Rogersd4c4d952014-10-16 20:31:53 -07001231 for (int i = 0; i < argc; ++i) {
Alex Light53cb16b2014-06-12 11:26:29 -07001232 const StringPiece option(argv[i]);
1233 const bool log_options = false;
1234 if (log_options) {
1235 LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i];
1236 }
Alex Light53cb16b2014-06-12 11:26:29 -07001237 if (option.starts_with("--instruction-set=")) {
1238 isa_set = true;
1239 const char* isa_str = option.substr(strlen("--instruction-set=")).data();
Andreas Gampe20c89302014-08-19 17:28:06 -07001240 isa = GetInstructionSetFromString(isa_str);
Vladimir Marko33bff252017-11-01 14:35:42 +00001241 if (isa == InstructionSet::kNone) {
Andreas Gampe20c89302014-08-19 17:28:06 -07001242 Usage("Unknown or invalid instruction set %s", isa_str);
Alex Light53cb16b2014-06-12 11:26:29 -07001243 }
Alex Light53cb16b2014-06-12 11:26:29 -07001244 } else if (option.starts_with("--input-image-location=")) {
1245 input_image_location = option.substr(strlen("--input-image-location=")).data();
Chris Morin88c6d262018-02-13 15:26:21 -08001246 } else if (option.starts_with("--output-image-directory=")) {
1247 output_image_directory = option.substr(strlen("--output-image-directory=")).data();
1248 } else if (option.starts_with("--output-image-relocation-directory=")) {
1249 output_image_relocation_directory =
1250 option.substr(strlen("--output-image-relocation-directory=")).data();
Alex Light53cb16b2014-06-12 11:26:29 -07001251 } else if (option.starts_with("--base-offset-delta=")) {
1252 const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data();
1253 base_delta_set = true;
1254 if (!ParseInt(base_delta_str, &base_delta)) {
1255 Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str);
1256 }
Alex Light53cb16b2014-06-12 11:26:29 -07001257 } else if (option == "--dump-timings") {
1258 dump_timings = true;
1259 } else if (option == "--no-dump-timings") {
1260 dump_timings = false;
Chris Morin754b7572018-01-19 18:04:46 -08001261 } else if (option == "--verify") {
1262 verify = true;
Alex Light53cb16b2014-06-12 11:26:29 -07001263 } else {
1264 Usage("Unknown argument %s", option.data());
1265 }
1266 }
1267
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001268 // The instruction set is mandatory. This simplifies things...
1269 if (!isa_set) {
1270 Usage("Instruction set must be set.");
Alex Light53cb16b2014-06-12 11:26:29 -07001271 }
1272
Chris Morin754b7572018-01-19 18:04:46 -08001273 int ret;
1274 if (verify) {
1275 ret = patchoat_verify_image(timings,
1276 isa,
1277 input_image_location,
1278 output_image_directory);
1279 } else {
1280 ret = patchoat_patch_image(timings,
1281 isa,
1282 input_image_location,
1283 output_image_directory,
Chris Morin88c6d262018-02-13 15:26:21 -08001284 output_image_relocation_directory,
Chris Morin754b7572018-01-19 18:04:46 -08001285 base_delta,
1286 base_delta_set,
1287 debug);
1288 }
Alex Light53cb16b2014-06-12 11:26:29 -07001289
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001290 timings.EndTiming();
1291 if (dump_timings) {
1292 LOG(INFO) << Dumpable<TimingLogger>(timings);
Alex Light53cb16b2014-06-12 11:26:29 -07001293 }
1294
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001295 return ret;
Alex Light53cb16b2014-06-12 11:26:29 -07001296}
1297
1298} // namespace art
1299
1300int main(int argc, char **argv) {
1301 return art::patchoat(argc, argv);
1302}