Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "patchoat.h" |
| 17 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 18 | #include <openssl/sha.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 21 | #include <sys/file.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 24 | |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 28 | #include "android-base/file.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 29 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 30 | #include "android-base/strings.h" |
| 31 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 32 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | #include "art_method-inl.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 34 | #include "base/dumpable.h" |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 35 | #include "base/file_utils.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 36 | #include "base/leb128.h" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 37 | #include "base/logging.h" // For InitLogging. |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 38 | #include "base/mutex.h" |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 39 | #include "base/memory_tool.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 40 | #include "base/os.h" |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 41 | #include "base/scoped_flock.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 42 | #include "base/stringpiece.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 43 | #include "base/unix_file/fd_file.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 44 | #include "base/unix_file/random_access_file_utils.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 45 | #include "base/utils.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 46 | #include "elf_file.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 47 | #include "elf_file_impl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 48 | #include "elf_utils.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 49 | #include "gc/space/image_space.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 50 | #include "image-inl.h" |
Andreas Gampe | b2d18fa | 2017-06-06 20:46:10 -0700 | [diff] [blame] | 51 | #include "intern_table.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 52 | #include "mirror/dex_cache.h" |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 53 | #include "mirror/executable.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 54 | #include "mirror/method.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 55 | #include "mirror/object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 56 | #include "mirror/object-refvisitor-inl.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 57 | #include "mirror/reference.h" |
| 58 | #include "noop_compiler_callbacks.h" |
| 59 | #include "offsets.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 60 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 61 | #include "scoped_thread_state_change-inl.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 62 | #include "thread.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 63 | |
| 64 | namespace art { |
| 65 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 66 | using android::base::StringPrintf; |
| 67 | |
Andreas Gampe | c7d2508 | 2018-03-09 12:06:45 -0800 | [diff] [blame] | 68 | namespace { |
| 69 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 70 | static const OatHeader* GetOatHeader(const ElfFile* elf_file) { |
| 71 | uint64_t off = 0; |
| 72 | if (!elf_file->GetSectionOffsetAndSize(".rodata", &off, nullptr)) { |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf_file->Begin() + off); |
| 77 | return oat_header; |
| 78 | } |
| 79 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 80 | static File* CreateOrOpen(const char* name) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 81 | if (OS::FileExists(name)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 82 | return OS::OpenFileReadWrite(name); |
| 83 | } else { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 84 | std::unique_ptr<File> f(OS::CreateEmptyFile(name)); |
| 85 | if (f.get() != nullptr) { |
| 86 | if (fchmod(f->Fd(), 0644) != 0) { |
| 87 | PLOG(ERROR) << "Unable to make " << name << " world readable"; |
Dimitry Ivanov | 7a1c014 | 2016-03-17 15:59:38 -0700 | [diff] [blame] | 88 | unlink(name); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 89 | return nullptr; |
| 90 | } |
| 91 | } |
| 92 | return f.release(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Either try to close the file (close=true), or erase it. |
| 97 | static bool FinishFile(File* file, bool close) { |
| 98 | if (close) { |
| 99 | if (file->FlushCloseOrErase() != 0) { |
| 100 | PLOG(ERROR) << "Failed to flush and close file."; |
| 101 | return false; |
| 102 | } |
| 103 | return true; |
| 104 | } else { |
| 105 | file->Erase(); |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 110 | static bool SymlinkFile(const std::string& input_filename, const std::string& output_filename) { |
| 111 | if (input_filename == output_filename) { |
| 112 | // Input and output are the same, nothing to do. |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | // Unlink the original filename, since we are overwriting it. |
| 117 | unlink(output_filename.c_str()); |
| 118 | |
| 119 | // Create a symlink from the source file to the target path. |
| 120 | if (symlink(input_filename.c_str(), output_filename.c_str()) < 0) { |
| 121 | PLOG(ERROR) << "Failed to create symlink " << output_filename << " -> " << input_filename; |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if (kIsDebugBuild) { |
| 126 | LOG(INFO) << "Created symlink " << output_filename << " -> " << input_filename; |
| 127 | } |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
Andreas Gampe | c7d2508 | 2018-03-09 12:06:45 -0800 | [diff] [blame] | 132 | // Holder class for runtime options and related objects. |
| 133 | class PatchoatRuntimeOptionsHolder { |
| 134 | public: |
| 135 | PatchoatRuntimeOptionsHolder(const std::string& image_location, InstructionSet isa) { |
| 136 | options_.push_back(std::make_pair("compilercallbacks", &callbacks_)); |
| 137 | img_ = "-Ximage:" + image_location; |
| 138 | options_.push_back(std::make_pair(img_.c_str(), nullptr)); |
| 139 | isa_name_ = GetInstructionSetString(isa); |
| 140 | options_.push_back(std::make_pair("imageinstructionset", |
| 141 | reinterpret_cast<const void*>(isa_name_.c_str()))); |
| 142 | options_.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
| 143 | // We do not want the runtime to attempt to patch the image. |
| 144 | options_.push_back(std::make_pair("-Xnorelocate", nullptr)); |
| 145 | // Don't try to compile. |
| 146 | options_.push_back(std::make_pair("-Xnoimage-dex2oat", nullptr)); |
| 147 | // Do not accept broken image. |
| 148 | options_.push_back(std::make_pair("-Xno-dex-file-fallback", nullptr)); |
| 149 | } |
| 150 | |
| 151 | const RuntimeOptions& GetRuntimeOptions() { |
| 152 | return options_; |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | RuntimeOptions options_; |
| 157 | NoopCompilerCallbacks callbacks_; |
| 158 | std::string isa_name_; |
| 159 | std::string img_; |
| 160 | }; |
| 161 | |
| 162 | } // namespace |
| 163 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 164 | bool PatchOat::GeneratePatch( |
| 165 | const MemMap& original, |
| 166 | const MemMap& relocated, |
| 167 | std::vector<uint8_t>* output, |
| 168 | std::string* error_msg) { |
| 169 | // FORMAT of the patch (aka image relocation) file: |
| 170 | // * SHA-256 digest (32 bytes) of original/unrelocated file (e.g., the one from /system) |
| 171 | // * List of monotonically increasing offsets (max value defined by uint32_t) at which relocations |
| 172 | // occur. |
| 173 | // Each element is represented as the delta from the previous offset in the list (first element |
| 174 | // is a delta from 0). Each delta is encoded using unsigned LEB128: little-endian |
| 175 | // variable-length 7 bits per byte encoding, where all bytes have the highest bit (0x80) set |
| 176 | // except for the final byte which does not have that bit set. For example, 0x3f is offset 0x3f, |
| 177 | // whereas 0xbf 0x05 is offset (0x3f & 0x7f) | (0x5 << 7) which is 0x2bf. Most deltas end up |
| 178 | // being encoding using just one byte, achieving ~4x decrease in relocation file size compared |
| 179 | // to the encoding where offsets are stored verbatim, as uint32_t. |
| 180 | |
| 181 | size_t original_size = original.Size(); |
| 182 | size_t relocated_size = relocated.Size(); |
| 183 | if (original_size != relocated_size) { |
| 184 | *error_msg = |
| 185 | StringPrintf( |
| 186 | "Original and relocated image sizes differ: %zu vs %zu", original_size, relocated_size); |
| 187 | return false; |
| 188 | } |
| 189 | if ((original_size % 4) != 0) { |
| 190 | *error_msg = StringPrintf("Image size not multiple of 4: %zu", original_size); |
| 191 | return false; |
| 192 | } |
| 193 | if (original_size > UINT32_MAX) { |
| 194 | *error_msg = StringPrintf("Image too large: %zu" , original_size); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | const ImageHeader& relocated_header = |
| 199 | *reinterpret_cast<const ImageHeader*>(relocated.Begin()); |
| 200 | // Offsets are supposed to differ between original and relocated by this value |
| 201 | off_t expected_diff = relocated_header.GetPatchDelta(); |
| 202 | if (expected_diff == 0) { |
| 203 | // Can't identify offsets which are supposed to differ due to relocation |
| 204 | *error_msg = "Relocation delta is 0"; |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | // Output the SHA-256 digest of the original |
| 209 | output->resize(SHA256_DIGEST_LENGTH); |
| 210 | const uint8_t* original_bytes = original.Begin(); |
| 211 | SHA256(original_bytes, original_size, output->data()); |
| 212 | |
| 213 | // Output the list of offsets at which the original and patched images differ |
| 214 | size_t last_diff_offset = 0; |
| 215 | size_t diff_offset_count = 0; |
| 216 | const uint8_t* relocated_bytes = relocated.Begin(); |
| 217 | for (size_t offset = 0; offset < original_size; offset += 4) { |
| 218 | uint32_t original_value = *reinterpret_cast<const uint32_t*>(original_bytes + offset); |
| 219 | uint32_t relocated_value = *reinterpret_cast<const uint32_t*>(relocated_bytes + offset); |
| 220 | off_t diff = relocated_value - original_value; |
| 221 | if (diff == 0) { |
| 222 | continue; |
| 223 | } else if (diff != expected_diff) { |
| 224 | *error_msg = |
| 225 | StringPrintf( |
| 226 | "Unexpected diff at offset %zu. Expected: %jd, but was: %jd", |
| 227 | offset, |
| 228 | (intmax_t) expected_diff, |
| 229 | (intmax_t) diff); |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | uint32_t offset_diff = offset - last_diff_offset; |
| 234 | last_diff_offset = offset; |
| 235 | diff_offset_count++; |
| 236 | |
| 237 | EncodeUnsignedLeb128(output, offset_diff); |
| 238 | } |
| 239 | |
| 240 | if (diff_offset_count == 0) { |
| 241 | *error_msg = "Original and patched images are identical"; |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | static bool WriteRelFile( |
| 249 | const MemMap& original, |
| 250 | const MemMap& relocated, |
| 251 | const std::string& rel_filename, |
| 252 | std::string* error_msg) { |
| 253 | std::vector<uint8_t> output; |
| 254 | if (!PatchOat::GeneratePatch(original, relocated, &output, error_msg)) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | std::unique_ptr<File> rel_file(OS::CreateEmptyFileWriteOnly(rel_filename.c_str())); |
| 259 | if (rel_file.get() == nullptr) { |
| 260 | *error_msg = StringPrintf("Failed to create/open output file %s", rel_filename.c_str()); |
| 261 | return false; |
| 262 | } |
| 263 | if (!rel_file->WriteFully(output.data(), output.size())) { |
| 264 | *error_msg = StringPrintf("Failed to write to %s", rel_filename.c_str()); |
| 265 | return false; |
| 266 | } |
| 267 | if (rel_file->FlushCloseOrErase() != 0) { |
| 268 | *error_msg = StringPrintf("Failed to flush and close %s", rel_filename.c_str()); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 275 | static bool CheckImageIdenticalToOriginalExceptForRelocation( |
| 276 | const std::string& relocated_filename, |
| 277 | const std::string& original_filename, |
| 278 | std::string* error_msg) { |
| 279 | *error_msg = ""; |
| 280 | std::string rel_filename = original_filename + ".rel"; |
| 281 | std::unique_ptr<File> rel_file(OS::OpenFileForReading(rel_filename.c_str())); |
| 282 | if (rel_file.get() == nullptr) { |
| 283 | *error_msg = StringPrintf("Failed to open image relocation file %s", rel_filename.c_str()); |
| 284 | return false; |
| 285 | } |
| 286 | int64_t rel_size = rel_file->GetLength(); |
| 287 | if (rel_size < 0) { |
| 288 | *error_msg = StringPrintf("Error while getting size of image relocation file %s", |
| 289 | rel_filename.c_str()); |
| 290 | return false; |
| 291 | } |
| 292 | std::unique_ptr<uint8_t[]> rel(new uint8_t[rel_size]); |
| 293 | if (!rel_file->ReadFully(rel.get(), rel_size)) { |
| 294 | *error_msg = StringPrintf("Failed to read image relocation file %s", rel_filename.c_str()); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | std::unique_ptr<File> image_file(OS::OpenFileForReading(relocated_filename.c_str())); |
| 299 | if (image_file.get() == nullptr) { |
| 300 | *error_msg = StringPrintf("Unable to open relocated image file %s", |
| 301 | relocated_filename.c_str()); |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | int64_t image_size = image_file->GetLength(); |
| 306 | if (image_size < 0) { |
| 307 | *error_msg = StringPrintf("Error while getting size of relocated image file %s", |
| 308 | relocated_filename.c_str()); |
| 309 | return false; |
| 310 | } |
| 311 | if ((image_size % 4) != 0) { |
| 312 | *error_msg = |
| 313 | StringPrintf( |
Orion Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 314 | "Relocated image file %s size not multiple of 4: %" PRId64, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 315 | relocated_filename.c_str(), image_size); |
| 316 | return false; |
| 317 | } |
Orion Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 318 | if (image_size > std::numeric_limits<uint32_t>::max()) { |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 319 | *error_msg = |
| 320 | StringPrintf( |
Orion Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 321 | "Relocated image file %s too large: %" PRId64, relocated_filename.c_str(), image_size); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 322 | return false; |
| 323 | } |
| 324 | |
| 325 | std::unique_ptr<uint8_t[]> image(new uint8_t[image_size]); |
| 326 | if (!image_file->ReadFully(image.get(), image_size)) { |
| 327 | *error_msg = StringPrintf("Failed to read relocated image file %s", relocated_filename.c_str()); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | const uint8_t* original_image_digest = rel.get(); |
| 332 | if (rel_size < SHA256_DIGEST_LENGTH) { |
| 333 | *error_msg = StringPrintf("Malformed image relocation file %s: too short", |
| 334 | rel_filename.c_str()); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | const ImageHeader& image_header = *reinterpret_cast<const ImageHeader*>(image.get()); |
| 339 | off_t expected_diff = image_header.GetPatchDelta(); |
| 340 | |
| 341 | if (expected_diff == 0) { |
| 342 | *error_msg = StringPrintf("Unsuported patch delta of zero in %s", |
| 343 | relocated_filename.c_str()); |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | // Relocated image is expected to differ from the original due to relocation. |
| 348 | // Unrelocate the image in memory to compensate. |
| 349 | uint8_t* image_start = image.get(); |
| 350 | const uint8_t* rel_end = &rel[rel_size]; |
| 351 | const uint8_t* rel_ptr = &rel[SHA256_DIGEST_LENGTH]; |
| 352 | // The remaining .rel file consists of offsets at which relocation should've occurred. |
| 353 | // For each offset, we "unrelocate" the image by subtracting the expected relocation |
| 354 | // diff value (as specified in the image header). |
| 355 | // |
| 356 | // Each offset is encoded as a delta/diff relative to the previous offset. With the |
| 357 | // very first offset being encoded relative to offset 0. |
| 358 | // Deltas are encoded using little-endian 7 bits per byte encoding, with all bytes except |
| 359 | // the last one having the highest bit set. |
| 360 | uint32_t offset = 0; |
| 361 | while (rel_ptr != rel_end) { |
| 362 | uint32_t offset_delta = 0; |
| 363 | if (DecodeUnsignedLeb128Checked(&rel_ptr, rel_end, &offset_delta)) { |
| 364 | offset += offset_delta; |
| 365 | uint32_t *image_value = reinterpret_cast<uint32_t*>(image_start + offset); |
| 366 | *image_value -= expected_diff; |
| 367 | } else { |
| 368 | *error_msg = |
| 369 | StringPrintf( |
| 370 | "Malformed image relocation file %s: " |
| 371 | "last byte has it's most significant bit set", |
| 372 | rel_filename.c_str()); |
| 373 | return false; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // Image in memory is now supposed to be identical to the original. We |
| 378 | // confirm this by comparing the digest of the in-memory image to the expected |
| 379 | // digest from relocation file. |
| 380 | uint8_t image_digest[SHA256_DIGEST_LENGTH]; |
| 381 | SHA256(image.get(), image_size, image_digest); |
| 382 | if (memcmp(image_digest, original_image_digest, SHA256_DIGEST_LENGTH) != 0) { |
| 383 | *error_msg = |
| 384 | StringPrintf( |
| 385 | "Relocated image %s does not match the original %s after unrelocation", |
| 386 | relocated_filename.c_str(), |
| 387 | original_filename.c_str()); |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | // Relocated image is identical to the original, once relocations are taken into account |
| 392 | return true; |
| 393 | } |
| 394 | |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 395 | static bool VerifySymlink(const std::string& intended_target, const std::string& link_name) { |
| 396 | std::string actual_target; |
| 397 | if (!android::base::Readlink(link_name, &actual_target)) { |
| 398 | PLOG(ERROR) << "Readlink on " << link_name << " failed."; |
| 399 | return false; |
| 400 | } |
| 401 | return actual_target == intended_target; |
| 402 | } |
| 403 | |
| 404 | static bool VerifyVdexAndOatSymlinks(const std::string& input_image_filename, |
| 405 | const std::string& output_image_filename) { |
| 406 | return VerifySymlink(ImageHeader::GetVdexLocationFromImageLocation(input_image_filename), |
| 407 | ImageHeader::GetVdexLocationFromImageLocation(output_image_filename)) |
| 408 | && VerifySymlink(ImageHeader::GetOatLocationFromImageLocation(input_image_filename), |
| 409 | ImageHeader::GetOatLocationFromImageLocation(output_image_filename)); |
| 410 | } |
| 411 | |
| 412 | bool PatchOat::CreateVdexAndOatSymlinks(const std::string& input_image_filename, |
| 413 | const std::string& output_image_filename) { |
| 414 | std::string input_vdex_filename = |
| 415 | ImageHeader::GetVdexLocationFromImageLocation(input_image_filename); |
| 416 | std::string input_oat_filename = |
| 417 | ImageHeader::GetOatLocationFromImageLocation(input_image_filename); |
| 418 | |
| 419 | std::unique_ptr<File> input_oat_file(OS::OpenFileForReading(input_oat_filename.c_str())); |
| 420 | if (input_oat_file.get() == nullptr) { |
| 421 | LOG(ERROR) << "Unable to open input oat file at " << input_oat_filename; |
| 422 | return false; |
| 423 | } |
| 424 | std::string error_msg; |
| 425 | std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat_file.get(), |
| 426 | PROT_READ | PROT_WRITE, |
| 427 | MAP_PRIVATE, |
| 428 | &error_msg)); |
| 429 | if (elf.get() == nullptr) { |
| 430 | LOG(ERROR) << "Unable to open oat file " << input_oat_filename << " : " << error_msg; |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | MaybePic is_oat_pic = IsOatPic(elf.get()); |
| 435 | if (is_oat_pic >= ERROR_FIRST) { |
| 436 | // Error logged by IsOatPic |
| 437 | return false; |
| 438 | } else if (is_oat_pic == NOT_PIC) { |
| 439 | LOG(ERROR) << "patchoat cannot be used on non-PIC oat file: " << input_oat_filename; |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | CHECK(is_oat_pic == PIC); |
| 444 | |
| 445 | std::string output_vdex_filename = |
| 446 | ImageHeader::GetVdexLocationFromImageLocation(output_image_filename); |
| 447 | std::string output_oat_filename = |
| 448 | ImageHeader::GetOatLocationFromImageLocation(output_image_filename); |
| 449 | |
| 450 | return SymlinkFile(input_oat_filename, output_oat_filename) && |
| 451 | SymlinkFile(input_vdex_filename, output_vdex_filename); |
| 452 | } |
| 453 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 454 | bool PatchOat::Patch(const std::string& image_location, |
| 455 | off_t delta, |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 456 | const std::string& output_image_directory, |
| 457 | const std::string& output_image_relocation_directory, |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 458 | InstructionSet isa, |
| 459 | TimingLogger* timings) { |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 460 | bool output_image = !output_image_directory.empty(); |
| 461 | bool output_image_relocation = !output_image_relocation_directory.empty(); |
| 462 | if ((!output_image) && (!output_image_relocation)) { |
| 463 | // Nothing to do |
| 464 | return true; |
| 465 | } |
| 466 | if ((output_image_relocation) && (delta == 0)) { |
| 467 | LOG(ERROR) << "Cannot output image relocation information when requested relocation delta is 0"; |
| 468 | return false; |
| 469 | } |
| 470 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 471 | CHECK(Runtime::Current() == nullptr); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 472 | CHECK(!image_location.empty()) << "image file must have a filename."; |
| 473 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 474 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 475 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 476 | CHECK_NE(isa, InstructionSet::kNone); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 477 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 478 | // Set up the runtime |
Andreas Gampe | c7d2508 | 2018-03-09 12:06:45 -0800 | [diff] [blame] | 479 | PatchoatRuntimeOptionsHolder options_holder(image_location, isa); |
| 480 | if (!Runtime::Create(options_holder.GetRuntimeOptions(), false)) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 481 | LOG(ERROR) << "Unable to initialize runtime"; |
| 482 | return false; |
| 483 | } |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 484 | std::unique_ptr<Runtime> runtime(Runtime::Current()); |
| 485 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 486 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 487 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 488 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 489 | ScopedObjectAccess soa(Thread::Current()); |
| 490 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 491 | std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 492 | std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>> space_to_memmap_map; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 493 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 494 | for (size_t i = 0; i < spaces.size(); ++i) { |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 495 | t.NewTiming("Image Patching setup"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 496 | gc::space::ImageSpace* space = spaces[i]; |
| 497 | std::string input_image_filename = space->GetImageFilename(); |
| 498 | std::unique_ptr<File> input_image(OS::OpenFileForReading(input_image_filename.c_str())); |
| 499 | if (input_image.get() == nullptr) { |
| 500 | LOG(ERROR) << "Unable to open input image file at " << input_image_filename; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 501 | return false; |
| 502 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 503 | |
| 504 | int64_t image_len = input_image->GetLength(); |
| 505 | if (image_len < 0) { |
| 506 | LOG(ERROR) << "Error while getting image length"; |
| 507 | return false; |
| 508 | } |
| 509 | ImageHeader image_header; |
| 510 | if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header), |
| 511 | sizeof(image_header), 0)) { |
| 512 | LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath(); |
| 513 | } |
| 514 | |
| 515 | /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath()); |
| 516 | // Nothing special to do right now since the image always needs to get patched. |
| 517 | // Perhaps in some far-off future we may have images with relative addresses that are true-PIC. |
| 518 | |
| 519 | // Create the map where we will write the image patches to. |
| 520 | std::string error_msg; |
| 521 | std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, |
| 522 | PROT_READ | PROT_WRITE, |
| 523 | MAP_PRIVATE, |
| 524 | input_image->Fd(), |
| 525 | 0, |
| 526 | /*low_4gb*/false, |
| 527 | input_image->GetPath().c_str(), |
| 528 | &error_msg)); |
| 529 | if (image.get() == nullptr) { |
| 530 | LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg; |
| 531 | return false; |
| 532 | } |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 533 | |
| 534 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 535 | space_to_memmap_map.emplace(space, std::move(image)); |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 536 | PatchOat p = PatchOat(isa, |
| 537 | space_to_memmap_map.at(space).get(), |
| 538 | space->GetLiveBitmap(), |
| 539 | space->GetMemMap(), |
| 540 | delta, |
| 541 | &space_to_memmap_map, |
| 542 | timings); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 543 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 544 | t.NewTiming("Patching image"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 545 | if (!p.PatchImage(i == 0)) { |
| 546 | LOG(ERROR) << "Failed to patch image file " << input_image_filename; |
| 547 | return false; |
| 548 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 549 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 550 | // Write the patched image spaces. |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 551 | if (output_image) { |
| 552 | std::string output_image_filename; |
| 553 | if (!GetDalvikCacheFilename(space->GetImageLocation().c_str(), |
| 554 | output_image_directory.c_str(), |
| 555 | &output_image_filename, |
| 556 | &error_msg)) { |
| 557 | LOG(ERROR) << "Failed to find relocated image file name: " << error_msg; |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | if (!CreateVdexAndOatSymlinks(input_image_filename, output_image_filename)) |
| 562 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 563 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 564 | t.NewTiming("Writing image"); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 565 | std::unique_ptr<File> output_image_file(CreateOrOpen(output_image_filename.c_str())); |
| 566 | if (output_image_file.get() == nullptr) { |
| 567 | LOG(ERROR) << "Failed to open output image file at " << output_image_filename; |
| 568 | return false; |
| 569 | } |
| 570 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 571 | bool success = p.WriteImage(output_image_file.get()); |
| 572 | success = FinishFile(output_image_file.get(), success); |
| 573 | if (!success) { |
| 574 | return false; |
| 575 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 578 | if (output_image_relocation) { |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 579 | t.NewTiming("Writing image relocation"); |
| 580 | std::string original_image_filename(space->GetImageLocation() + ".rel"); |
| 581 | std::string image_relocation_filename = |
| 582 | output_image_relocation_directory |
| 583 | + (android::base::StartsWith(original_image_filename, "/") ? "" : "/") |
| 584 | + original_image_filename.substr(original_image_filename.find_last_of("/")); |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 585 | int64_t input_image_size = input_image->GetLength(); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 586 | if (input_image_size < 0) { |
| 587 | LOG(ERROR) << "Error while getting input image size"; |
| 588 | return false; |
| 589 | } |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 590 | std::unique_ptr<MemMap> original(MemMap::MapFile(input_image_size, |
| 591 | PROT_READ, |
| 592 | MAP_PRIVATE, |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 593 | input_image->Fd(), |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 594 | 0, |
| 595 | /*low_4gb*/false, |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 596 | input_image->GetPath().c_str(), |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 597 | &error_msg)); |
| 598 | if (original.get() == nullptr) { |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 599 | LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg; |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 600 | return false; |
| 601 | } |
| 602 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 603 | const MemMap* relocated = p.image_; |
| 604 | |
| 605 | if (!WriteRelFile(*original, *relocated, image_relocation_filename, &error_msg)) { |
| 606 | LOG(ERROR) << "Failed to create image relocation file " << image_relocation_filename |
| 607 | << ": " << error_msg; |
| 608 | return false; |
| 609 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 610 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 611 | } |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 612 | |
| 613 | if (!kIsDebugBuild && !(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
| 614 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 615 | // in an orderly fashion. So release the following fields. |
| 616 | runtime.release(); |
| 617 | } |
| 618 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 619 | return true; |
| 620 | } |
| 621 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 622 | bool PatchOat::Verify(const std::string& image_location, |
| 623 | const std::string& output_image_directory, |
| 624 | InstructionSet isa, |
| 625 | TimingLogger* timings) { |
| 626 | if (image_location.empty()) { |
| 627 | LOG(ERROR) << "Original image file not provided"; |
| 628 | return false; |
| 629 | } |
| 630 | if (output_image_directory.empty()) { |
| 631 | LOG(ERROR) << "Relocated image directory not provided"; |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
| 636 | |
| 637 | CHECK_NE(isa, InstructionSet::kNone); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 638 | |
| 639 | // Set up the runtime |
Andreas Gampe | c7d2508 | 2018-03-09 12:06:45 -0800 | [diff] [blame] | 640 | PatchoatRuntimeOptionsHolder options_holder(image_location, isa); |
| 641 | if (!Runtime::Create(options_holder.GetRuntimeOptions(), false)) { |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 642 | LOG(ERROR) << "Unable to initialize runtime"; |
| 643 | return false; |
| 644 | } |
| 645 | std::unique_ptr<Runtime> runtime(Runtime::Current()); |
| 646 | |
| 647 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 648 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 649 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 650 | ScopedObjectAccess soa(Thread::Current()); |
| 651 | |
| 652 | t.NewTiming("Image Verification setup"); |
| 653 | std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 654 | |
| 655 | // TODO: Check that no other .rel files exist in the original dir |
| 656 | |
| 657 | bool success = true; |
| 658 | std::string image_location_dir = android::base::Dirname(image_location); |
| 659 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 660 | gc::space::ImageSpace* space = spaces[i]; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 661 | |
| 662 | std::string relocated_image_filename; |
| 663 | std::string error_msg; |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 664 | if (!GetDalvikCacheFilename(space->GetImageLocation().c_str(), |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 665 | output_image_directory.c_str(), &relocated_image_filename, &error_msg)) { |
| 666 | LOG(ERROR) << "Failed to find relocated image file name: " << error_msg; |
| 667 | success = false; |
| 668 | break; |
| 669 | } |
| 670 | // location: /system/framework/boot.art |
| 671 | // isa: arm64 |
| 672 | // basename: boot.art |
| 673 | // original: /system/framework/arm64/boot.art |
| 674 | // relocation: /system/framework/arm64/boot.art.rel |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 675 | std::string original_image_filename = |
| 676 | GetSystemImageFilename(space->GetImageLocation().c_str(), isa); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 677 | |
| 678 | if (!CheckImageIdenticalToOriginalExceptForRelocation( |
| 679 | relocated_image_filename, original_image_filename, &error_msg)) { |
| 680 | LOG(ERROR) << error_msg; |
| 681 | success = false; |
| 682 | break; |
| 683 | } |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 684 | |
| 685 | if (!VerifyVdexAndOatSymlinks(original_image_filename, relocated_image_filename)) { |
| 686 | LOG(ERROR) << "Verification of vdex and oat symlinks for " |
| 687 | << space->GetImageLocation() << " failed."; |
| 688 | success = false; |
| 689 | break; |
| 690 | } |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (!kIsDebugBuild && !(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
| 694 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 695 | // in an orderly fashion. So release the following fields. |
| 696 | runtime.release(); |
| 697 | } |
| 698 | |
| 699 | return success; |
| 700 | } |
| 701 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 702 | bool PatchOat::WriteImage(File* out) { |
Greg Kaiser | 4eb1779 | 2018-04-06 14:26:23 -0700 | [diff] [blame] | 703 | CHECK(out != nullptr); |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 704 | TimingLogger::ScopedTiming t("Writing image File", timings_); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 705 | std::string error_msg; |
| 706 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 707 | // No error checking here, this is best effort. The locking may or may not |
| 708 | // succeed and we don't really care either way. |
| 709 | ScopedFlock img_flock = LockedFile::DupOf(out->Fd(), out->GetPath(), |
| 710 | true /* read_only_mode */, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 711 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 712 | CHECK(image_ != nullptr); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 713 | size_t expect = image_->Size(); |
| 714 | if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) && |
| 715 | out->SetLength(expect) == 0) { |
| 716 | return true; |
| 717 | } else { |
| 718 | LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed."; |
| 719 | return false; |
| 720 | } |
| 721 | } |
| 722 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 723 | bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) { |
| 724 | if (!image_header.CompilePic()) { |
| 725 | if (kIsDebugBuild) { |
| 726 | LOG(INFO) << "image at location " << image_path << " was *not* compiled pic"; |
| 727 | } |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | if (kIsDebugBuild) { |
| 732 | LOG(INFO) << "image at location " << image_path << " was compiled PIC"; |
| 733 | } |
| 734 | |
| 735 | return true; |
| 736 | } |
| 737 | |
| 738 | PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) { |
| 739 | if (oat_in == nullptr) { |
| 740 | LOG(ERROR) << "No ELF input oat fie available"; |
| 741 | return ERROR_OAT_FILE; |
| 742 | } |
| 743 | |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 744 | const std::string& file_path = oat_in->GetFilePath(); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 745 | |
| 746 | const OatHeader* oat_header = GetOatHeader(oat_in); |
| 747 | if (oat_header == nullptr) { |
| 748 | LOG(ERROR) << "Failed to find oat header in oat file " << file_path; |
| 749 | return ERROR_OAT_FILE; |
| 750 | } |
| 751 | |
| 752 | if (!oat_header->IsValid()) { |
| 753 | LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header"; |
| 754 | return ERROR_OAT_FILE; |
| 755 | } |
| 756 | |
| 757 | bool is_pic = oat_header->IsPic(); |
| 758 | if (kIsDebugBuild) { |
| 759 | LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic"); |
| 760 | } |
| 761 | |
| 762 | return is_pic ? PIC : NOT_PIC; |
| 763 | } |
| 764 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 765 | class PatchOat::PatchOatArtFieldVisitor : public ArtFieldVisitor { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 766 | public: |
| 767 | explicit PatchOatArtFieldVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 768 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 769 | void Visit(ArtField* field) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 770 | ArtField* const dest = patch_oat_->RelocatedCopyOf(field); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 771 | dest->SetDeclaringClass( |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 772 | patch_oat_->RelocatedAddressOfPointer(field->GetDeclaringClass().Ptr())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 773 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 774 | |
| 775 | private: |
| 776 | PatchOat* const patch_oat_; |
| 777 | }; |
| 778 | |
| 779 | void PatchOat::PatchArtFields(const ImageHeader* image_header) { |
| 780 | PatchOatArtFieldVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 781 | image_header->VisitPackedArtFields(&visitor, heap_->Begin()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 784 | class PatchOat::PatchOatArtMethodVisitor : public ArtMethodVisitor { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 785 | public: |
| 786 | explicit PatchOatArtMethodVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 787 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 788 | void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 789 | ArtMethod* const dest = patch_oat_->RelocatedCopyOf(method); |
| 790 | patch_oat_->FixupMethod(method, dest); |
| 791 | } |
| 792 | |
| 793 | private: |
| 794 | PatchOat* const patch_oat_; |
| 795 | }; |
| 796 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 797 | void PatchOat::PatchArtMethods(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 798 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 799 | PatchOatArtMethodVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 800 | image_header->VisitPackedArtMethods(&visitor, heap_->Begin(), pointer_size); |
| 801 | } |
| 802 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 803 | void PatchOat::PatchImTables(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 804 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 805 | // We can safely walk target image since the conflict tables are independent. |
| 806 | image_header->VisitPackedImTables( |
| 807 | [this](ArtMethod* method) { |
| 808 | return RelocatedAddressOfPointer(method); |
| 809 | }, |
| 810 | image_->Begin(), |
| 811 | pointer_size); |
| 812 | } |
| 813 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 814 | void PatchOat::PatchImtConflictTables(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 815 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 816 | // We can safely walk target image since the conflict tables are independent. |
| 817 | image_header->VisitPackedImtConflictTables( |
| 818 | [this](ArtMethod* method) { |
| 819 | return RelocatedAddressOfPointer(method); |
| 820 | }, |
| 821 | image_->Begin(), |
| 822 | pointer_size); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 825 | class PatchOat::FixupRootVisitor : public RootVisitor { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 826 | public: |
| 827 | explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) { |
| 828 | } |
| 829 | |
| 830 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 831 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 832 | for (size_t i = 0; i < count; ++i) { |
| 833 | *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 838 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 839 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 840 | for (size_t i = 0; i < count; ++i) { |
| 841 | roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr())); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | private: |
| 846 | const PatchOat* const patch_oat_; |
| 847 | }; |
| 848 | |
| 849 | void PatchOat::PatchInternedStrings(const ImageHeader* image_header) { |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 850 | const auto& section = image_header->GetInternedStringsSection(); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 851 | if (section.Size() == 0) { |
| 852 | return; |
| 853 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 854 | InternTable temp_table; |
| 855 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 856 | // This also relies on visit roots not doing any verification which could fail after we update |
| 857 | // the roots to be the image addresses. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 858 | temp_table.AddTableFromMemory(image_->Begin() + section.Offset()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 859 | FixupRootVisitor visitor(this); |
| 860 | temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots); |
| 861 | } |
| 862 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 863 | void PatchOat::PatchClassTable(const ImageHeader* image_header) { |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 864 | const auto& section = image_header->GetClassTableSection(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 865 | if (section.Size() == 0) { |
| 866 | return; |
| 867 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 868 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 869 | // This also relies on visit roots not doing any verification which could fail after we update |
| 870 | // the roots to be the image addresses. |
| 871 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 872 | ClassTable temp_table; |
| 873 | temp_table.ReadFromMemory(image_->Begin() + section.Offset()); |
| 874 | FixupRootVisitor visitor(this); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 875 | temp_table.VisitRoots(UnbufferedRootVisitor(&visitor, RootInfo(kRootUnknown))); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 879 | class PatchOat::RelocatedPointerVisitor { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 880 | public: |
| 881 | explicit RelocatedPointerVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 882 | |
| 883 | template <typename T> |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 884 | T* operator()(T* ptr, void** dest_addr ATTRIBUTE_UNUSED = 0) const { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 885 | return patch_oat_->RelocatedAddressOfPointer(ptr); |
| 886 | } |
| 887 | |
| 888 | private: |
| 889 | PatchOat* const patch_oat_; |
| 890 | }; |
| 891 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 892 | void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) { |
| 893 | auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>( |
| 894 | img_roots->Get(ImageHeader::kDexCaches)); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 895 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 896 | for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 897 | auto* orig_dex_cache = dex_caches->GetWithoutChecks(i); |
| 898 | auto* copy_dex_cache = RelocatedCopyOf(orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 899 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 900 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 901 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 902 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 903 | mirror::StringDexCacheType* orig_strings = orig_dex_cache->GetStrings(); |
| 904 | mirror::StringDexCacheType* relocated_strings = RelocatedAddressOfPointer(orig_strings); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 905 | copy_dex_cache->SetField64<false>( |
| 906 | mirror::DexCache::StringsOffset(), |
| 907 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_strings))); |
| 908 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 909 | orig_dex_cache->FixupStrings(RelocatedCopyOf(orig_strings), RelocatedPointerVisitor(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 910 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 911 | mirror::TypeDexCacheType* orig_types = orig_dex_cache->GetResolvedTypes(); |
| 912 | mirror::TypeDexCacheType* relocated_types = RelocatedAddressOfPointer(orig_types); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 913 | copy_dex_cache->SetField64<false>( |
| 914 | mirror::DexCache::ResolvedTypesOffset(), |
| 915 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_types))); |
| 916 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 917 | orig_dex_cache->FixupResolvedTypes(RelocatedCopyOf(orig_types), |
| 918 | RelocatedPointerVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 919 | } |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 920 | mirror::MethodDexCacheType* orig_methods = orig_dex_cache->GetResolvedMethods(); |
| 921 | mirror::MethodDexCacheType* relocated_methods = RelocatedAddressOfPointer(orig_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 922 | copy_dex_cache->SetField64<false>( |
| 923 | mirror::DexCache::ResolvedMethodsOffset(), |
| 924 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_methods))); |
| 925 | if (orig_methods != nullptr) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 926 | mirror::MethodDexCacheType* copy_methods = RelocatedCopyOf(orig_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 927 | for (size_t j = 0, num = orig_dex_cache->NumResolvedMethods(); j != num; ++j) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 928 | mirror::MethodDexCachePair orig = |
| 929 | mirror::DexCache::GetNativePairPtrSize(orig_methods, j, pointer_size); |
| 930 | mirror::MethodDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index); |
| 931 | mirror::DexCache::SetNativePairPtrSize(copy_methods, j, copy, pointer_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 932 | } |
| 933 | } |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 934 | mirror::FieldDexCacheType* orig_fields = orig_dex_cache->GetResolvedFields(); |
| 935 | mirror::FieldDexCacheType* relocated_fields = RelocatedAddressOfPointer(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 936 | copy_dex_cache->SetField64<false>( |
| 937 | mirror::DexCache::ResolvedFieldsOffset(), |
| 938 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_fields))); |
| 939 | if (orig_fields != nullptr) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 940 | mirror::FieldDexCacheType* copy_fields = RelocatedCopyOf(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 941 | for (size_t j = 0, num = orig_dex_cache->NumResolvedFields(); j != num; ++j) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 942 | mirror::FieldDexCachePair orig = |
| 943 | mirror::DexCache::GetNativePairPtrSize(orig_fields, j, pointer_size); |
| 944 | mirror::FieldDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index); |
| 945 | mirror::DexCache::SetNativePairPtrSize(copy_fields, j, copy, pointer_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 946 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 947 | } |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 948 | mirror::MethodTypeDexCacheType* orig_method_types = orig_dex_cache->GetResolvedMethodTypes(); |
| 949 | mirror::MethodTypeDexCacheType* relocated_method_types = |
| 950 | RelocatedAddressOfPointer(orig_method_types); |
| 951 | copy_dex_cache->SetField64<false>( |
| 952 | mirror::DexCache::ResolvedMethodTypesOffset(), |
| 953 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_method_types))); |
| 954 | if (orig_method_types != nullptr) { |
| 955 | orig_dex_cache->FixupResolvedMethodTypes(RelocatedCopyOf(orig_method_types), |
| 956 | RelocatedPointerVisitor(this)); |
| 957 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 958 | |
| 959 | GcRoot<mirror::CallSite>* orig_call_sites = orig_dex_cache->GetResolvedCallSites(); |
| 960 | GcRoot<mirror::CallSite>* relocated_call_sites = RelocatedAddressOfPointer(orig_call_sites); |
| 961 | copy_dex_cache->SetField64<false>( |
| 962 | mirror::DexCache::ResolvedCallSitesOffset(), |
| 963 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_call_sites))); |
| 964 | if (orig_call_sites != nullptr) { |
| 965 | orig_dex_cache->FixupResolvedCallSites(RelocatedCopyOf(orig_call_sites), |
| 966 | RelocatedPointerVisitor(this)); |
| 967 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 971 | bool PatchOat::PatchImage(bool primary_image) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 972 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
| 973 | CHECK_GT(image_->Size(), sizeof(ImageHeader)); |
| 974 | // These are the roots from the original file. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 975 | auto* img_roots = image_header->GetImageRoots(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 976 | image_header->RelocateImage(delta_); |
| 977 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 978 | PatchArtFields(image_header); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 979 | PatchArtMethods(image_header); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 980 | PatchImTables(image_header); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 981 | PatchImtConflictTables(image_header); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 982 | PatchInternedStrings(image_header); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 983 | PatchClassTable(image_header); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 984 | // Patch dex file int/long arrays which point to ArtFields. |
| 985 | PatchDexFileArrays(img_roots); |
| 986 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 987 | if (primary_image) { |
| 988 | VisitObject(img_roots); |
| 989 | } |
| 990 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 991 | if (!image_header->IsValid()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 992 | LOG(ERROR) << "relocation renders image header invalid"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 993 | return false; |
| 994 | } |
| 995 | |
| 996 | { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 997 | TimingLogger::ScopedTiming t("Walk Bitmap", timings_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 998 | // Walk the bitmap. |
| 999 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1000 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1001 | VisitObject(obj); |
| 1002 | }; |
| 1003 | bitmap_->Walk(visitor); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1004 | } |
| 1005 | return true; |
| 1006 | } |
| 1007 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1008 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1009 | void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Object> obj, |
| 1010 | MemberOffset off, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1011 | bool is_static_unused ATTRIBUTE_UNUSED) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1012 | mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1013 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1014 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 1015 | } |
| 1016 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1017 | void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Class> cls ATTRIBUTE_UNUSED, |
| 1018 | ObjPtr<mirror::Reference> ref) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1019 | MemberOffset off = mirror::Reference::ReferentOffset(); |
| 1020 | mirror::Object* referent = ref->GetReferent(); |
Mathieu Chartier | a13abba | 2016-04-21 10:23:16 -0700 | [diff] [blame] | 1021 | DCHECK(referent == nullptr || |
| 1022 | Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(referent)) << referent; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1023 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1024 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 1025 | } |
| 1026 | |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1027 | // Called by PatchImage. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1028 | void PatchOat::VisitObject(mirror::Object* object) { |
| 1029 | mirror::Object* copy = RelocatedCopyOf(object); |
| 1030 | CHECK(copy != nullptr); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1031 | if (kUseBakerReadBarrier) { |
| 1032 | object->AssertReadBarrierState(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1033 | } |
| 1034 | PatchOat::PatchVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1035 | object->VisitReferences<kVerifyNone>(visitor, visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1036 | if (object->IsClass<kVerifyNone>()) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1037 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1038 | mirror::Class* klass = object->AsClass(); |
| 1039 | mirror::Class* copy_klass = down_cast<mirror::Class*>(copy); |
| 1040 | RelocatedPointerVisitor native_visitor(this); |
| 1041 | klass->FixupNativePointers(copy_klass, pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1042 | auto* vtable = klass->GetVTable(); |
| 1043 | if (vtable != nullptr) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1044 | vtable->Fixup(RelocatedCopyOfFollowImages(vtable), pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1045 | } |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 1046 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 1047 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1048 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1049 | auto* method_array = iftable->GetMethodArray(i); |
| 1050 | CHECK(method_array != nullptr); |
| 1051 | method_array->Fixup(RelocatedCopyOfFollowImages(method_array), |
| 1052 | pointer_size, |
| 1053 | native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1054 | } |
| 1055 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1056 | } else if (object->GetClass() == mirror::Method::StaticClass() || |
| 1057 | object->GetClass() == mirror::Constructor::StaticClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1058 | // Need to go update the ArtMethod. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 1059 | auto* dest = down_cast<mirror::Executable*>(copy); |
| 1060 | auto* src = down_cast<mirror::Executable*>(object); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1061 | dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod())); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1065 | void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1066 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1067 | copy->CopyFrom(object, pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1068 | // Just update the entry points if it looks like we should. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1069 | // TODO: sanity check all the pointers' values |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1070 | copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1071 | copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer( |
| 1072 | object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1073 | // No special handling for IMT conflict table since all pointers are moved by the same offset. |
Andreas Gampe | 75f0885 | 2016-07-19 08:06:07 -0700 | [diff] [blame] | 1074 | copy->SetDataPtrSize(RelocatedAddressOfPointer( |
| 1075 | object->GetDataPtrSize(pointer_size)), pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1078 | static int orig_argc; |
| 1079 | static char** orig_argv; |
| 1080 | |
| 1081 | static std::string CommandLine() { |
| 1082 | std::vector<std::string> command; |
| 1083 | for (int i = 0; i < orig_argc; ++i) { |
| 1084 | command.push_back(orig_argv[i]); |
| 1085 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 1086 | return android::base::Join(command, ' '); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 1090 | std::string error; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 1091 | android::base::StringAppendV(&error, fmt, ap); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1092 | LOG(ERROR) << error; |
| 1093 | } |
| 1094 | |
| 1095 | static void UsageError(const char* fmt, ...) { |
| 1096 | va_list ap; |
| 1097 | va_start(ap, fmt); |
| 1098 | UsageErrorV(fmt, ap); |
| 1099 | va_end(ap); |
| 1100 | } |
| 1101 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 1102 | NO_RETURN static void Usage(const char *fmt, ...) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1103 | va_list ap; |
| 1104 | va_start(ap, fmt); |
| 1105 | UsageErrorV(fmt, ap); |
| 1106 | va_end(ap); |
| 1107 | |
| 1108 | UsageError("Command: %s", CommandLine().c_str()); |
| 1109 | UsageError("Usage: patchoat [options]..."); |
| 1110 | UsageError(""); |
| 1111 | UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is"); |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 1112 | UsageError(" compiled for (required)."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1113 | UsageError(""); |
| 1114 | UsageError(" --input-image-location=<file.art>: Specifies the 'location' of the image file to"); |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 1115 | UsageError(" be patched."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1116 | UsageError(""); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1117 | UsageError(" --output-image-directory=<dir>: Specifies the directory to write the patched"); |
| 1118 | UsageError(" image file(s) to."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1119 | UsageError(""); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1120 | UsageError(" --output-image-relocation-directory=<dir>: Specifies the directory to write"); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 1121 | UsageError(" the image relocation information to."); |
| 1122 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1123 | UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by."); |
| 1124 | UsageError(" This value may be negative."); |
| 1125 | UsageError(""); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1126 | UsageError(" --verify: Verify an existing patched file instead of creating one."); |
| 1127 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1128 | UsageError(" --dump-timings: dump out patch timing information"); |
| 1129 | UsageError(""); |
| 1130 | UsageError(" --no-dump-timings: do not dump out patch timing information"); |
| 1131 | UsageError(""); |
| 1132 | |
| 1133 | exit(EXIT_FAILURE); |
| 1134 | } |
| 1135 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1136 | static int patchoat_patch_image(TimingLogger& timings, |
| 1137 | InstructionSet isa, |
| 1138 | const std::string& input_image_location, |
| 1139 | const std::string& output_image_directory, |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1140 | const std::string& output_image_relocation_directory, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1141 | off_t base_delta, |
| 1142 | bool base_delta_set, |
| 1143 | bool debug) { |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1144 | CHECK(!input_image_location.empty()); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1145 | if ((output_image_directory.empty()) && (output_image_relocation_directory.empty())) { |
| 1146 | Usage("Image patching requires --output-image-directory or --output-image-relocation-directory"); |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | if (!base_delta_set) { |
| 1150 | Usage("Must supply a desired new offset or delta."); |
| 1151 | } |
| 1152 | |
| 1153 | if (!IsAligned<kPageSize>(base_delta)) { |
| 1154 | Usage("Base offset/delta must be aligned to a pagesize (0x%08x) boundary.", kPageSize); |
| 1155 | } |
| 1156 | |
| 1157 | if (debug) { |
| 1158 | LOG(INFO) << "moving offset by " << base_delta |
| 1159 | << " (0x" << std::hex << base_delta << ") bytes or " |
| 1160 | << std::dec << (base_delta/kPageSize) << " pages."; |
| 1161 | } |
| 1162 | |
| 1163 | TimingLogger::ScopedTiming pt("patch image and oat", &timings); |
| 1164 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 1165 | bool ret = |
| 1166 | PatchOat::Patch( |
| 1167 | input_image_location, |
| 1168 | base_delta, |
| 1169 | output_image_directory, |
| 1170 | output_image_relocation_directory, |
| 1171 | isa, |
| 1172 | &timings); |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1173 | |
| 1174 | if (kIsDebugBuild) { |
| 1175 | LOG(INFO) << "Exiting with return ... " << ret; |
| 1176 | } |
| 1177 | return ret ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1178 | } |
| 1179 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1180 | static int patchoat_verify_image(TimingLogger& timings, |
| 1181 | InstructionSet isa, |
| 1182 | const std::string& input_image_location, |
| 1183 | const std::string& output_image_directory) { |
| 1184 | CHECK(!input_image_location.empty()); |
| 1185 | TimingLogger::ScopedTiming pt("verify image and oat", &timings); |
| 1186 | |
| 1187 | bool ret = |
| 1188 | PatchOat::Verify( |
| 1189 | input_image_location, |
| 1190 | output_image_directory, |
| 1191 | isa, |
| 1192 | &timings); |
| 1193 | |
| 1194 | if (kIsDebugBuild) { |
| 1195 | LOG(INFO) << "Exiting with return ... " << ret; |
| 1196 | } |
| 1197 | return ret ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1198 | } |
| 1199 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1200 | static int patchoat(int argc, char **argv) { |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1201 | Locks::Init(); |
Andreas Gampe | 51d80cc | 2017-06-21 21:05:13 -0700 | [diff] [blame] | 1202 | InitLogging(argv, Runtime::Abort); |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 1203 | MemMap::Init(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1204 | const bool debug = kIsDebugBuild; |
| 1205 | orig_argc = argc; |
| 1206 | orig_argv = argv; |
| 1207 | TimingLogger timings("patcher", false, false); |
| 1208 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1209 | // Skip over the command name. |
| 1210 | argv++; |
| 1211 | argc--; |
| 1212 | |
| 1213 | if (argc == 0) { |
| 1214 | Usage("No arguments specified"); |
| 1215 | } |
| 1216 | |
| 1217 | timings.StartTiming("Patchoat"); |
| 1218 | |
| 1219 | // cmd line args |
| 1220 | bool isa_set = false; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1221 | InstructionSet isa = InstructionSet::kNone; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1222 | std::string input_image_location; |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1223 | std::string output_image_directory; |
| 1224 | std::string output_image_relocation_directory; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1225 | off_t base_delta = 0; |
| 1226 | bool base_delta_set = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1227 | bool dump_timings = kIsDebugBuild; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1228 | bool verify = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1229 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1230 | for (int i = 0; i < argc; ++i) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1231 | const StringPiece option(argv[i]); |
| 1232 | const bool log_options = false; |
| 1233 | if (log_options) { |
| 1234 | LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i]; |
| 1235 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1236 | if (option.starts_with("--instruction-set=")) { |
| 1237 | isa_set = true; |
| 1238 | const char* isa_str = option.substr(strlen("--instruction-set=")).data(); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 1239 | isa = GetInstructionSetFromString(isa_str); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1240 | if (isa == InstructionSet::kNone) { |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 1241 | Usage("Unknown or invalid instruction set %s", isa_str); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1242 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1243 | } else if (option.starts_with("--input-image-location=")) { |
| 1244 | input_image_location = option.substr(strlen("--input-image-location=")).data(); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1245 | } else if (option.starts_with("--output-image-directory=")) { |
| 1246 | output_image_directory = option.substr(strlen("--output-image-directory=")).data(); |
| 1247 | } else if (option.starts_with("--output-image-relocation-directory=")) { |
| 1248 | output_image_relocation_directory = |
| 1249 | option.substr(strlen("--output-image-relocation-directory=")).data(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1250 | } else if (option.starts_with("--base-offset-delta=")) { |
| 1251 | const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data(); |
| 1252 | base_delta_set = true; |
| 1253 | if (!ParseInt(base_delta_str, &base_delta)) { |
| 1254 | Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str); |
| 1255 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1256 | } else if (option == "--dump-timings") { |
| 1257 | dump_timings = true; |
| 1258 | } else if (option == "--no-dump-timings") { |
| 1259 | dump_timings = false; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1260 | } else if (option == "--verify") { |
| 1261 | verify = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1262 | } else { |
| 1263 | Usage("Unknown argument %s", option.data()); |
| 1264 | } |
| 1265 | } |
| 1266 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1267 | // The instruction set is mandatory. This simplifies things... |
| 1268 | if (!isa_set) { |
| 1269 | Usage("Instruction set must be set."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1272 | int ret; |
| 1273 | if (verify) { |
| 1274 | ret = patchoat_verify_image(timings, |
| 1275 | isa, |
| 1276 | input_image_location, |
| 1277 | output_image_directory); |
| 1278 | } else { |
| 1279 | ret = patchoat_patch_image(timings, |
| 1280 | isa, |
| 1281 | input_image_location, |
| 1282 | output_image_directory, |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1283 | output_image_relocation_directory, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1284 | base_delta, |
| 1285 | base_delta_set, |
| 1286 | debug); |
| 1287 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1288 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1289 | timings.EndTiming(); |
| 1290 | if (dump_timings) { |
| 1291 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1294 | return ret; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | } // namespace art |
| 1298 | |
| 1299 | int main(int argc, char **argv) { |
| 1300 | return art::patchoat(argc, argv); |
| 1301 | } |