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" |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 46 | #include "class_root.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 47 | #include "elf_file.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 48 | #include "elf_file_impl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 49 | #include "elf_utils.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 50 | #include "gc/space/image_space.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 51 | #include "image-inl.h" |
Andreas Gampe | b2d18fa | 2017-06-06 20:46:10 -0700 | [diff] [blame] | 52 | #include "intern_table.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 53 | #include "mirror/dex_cache.h" |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 54 | #include "mirror/executable.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 55 | #include "mirror/method.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 56 | #include "mirror/object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 57 | #include "mirror/object-refvisitor-inl.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 58 | #include "mirror/reference.h" |
| 59 | #include "noop_compiler_callbacks.h" |
| 60 | #include "offsets.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 61 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 62 | #include "scoped_thread_state_change-inl.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 63 | #include "thread.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 64 | |
| 65 | namespace art { |
| 66 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 67 | using android::base::StringPrintf; |
| 68 | |
Andreas Gampe | c7d2508 | 2018-03-09 12:06:45 -0800 | [diff] [blame] | 69 | namespace { |
| 70 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 71 | static 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 Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 81 | static File* CreateOrOpen(const char* name) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 82 | if (OS::FileExists(name)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 83 | return OS::OpenFileReadWrite(name); |
| 84 | } else { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 85 | 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 Ivanov | 7a1c014 | 2016-03-17 15:59:38 -0700 | [diff] [blame] | 89 | unlink(name); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 90 | return nullptr; |
| 91 | } |
| 92 | } |
| 93 | return f.release(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Either try to close the file (close=true), or erase it. |
| 98 | static 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 Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 111 | static 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 Gampe | c7d2508 | 2018-03-09 12:06:45 -0800 | [diff] [blame] | 133 | // Holder class for runtime options and related objects. |
| 134 | class 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 Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 165 | bool 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 | |
| 249 | static 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 Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 276 | static 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 Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 315 | "Relocated image file %s size not multiple of 4: %" PRId64, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 316 | relocated_filename.c_str(), image_size); |
| 317 | return false; |
| 318 | } |
Orion Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 319 | if (image_size > std::numeric_limits<uint32_t>::max()) { |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 320 | *error_msg = |
| 321 | StringPrintf( |
Orion Hodson | b348b3b | 2018-01-26 09:02:49 +0000 | [diff] [blame] | 322 | "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] | 323 | 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; |
David Srbecky | b16e4a3 | 2018-06-05 13:20:48 +0100 | [diff] [blame] | 366 | if (static_cast<int64_t>(offset) + static_cast<int64_t>(sizeof(uint32_t)) > image_size) { |
| 367 | *error_msg = StringPrintf("Relocation out of bounds in %s", relocated_filename.c_str()); |
| 368 | return false; |
| 369 | } |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 370 | uint32_t *image_value = reinterpret_cast<uint32_t*>(image_start + offset); |
| 371 | *image_value -= expected_diff; |
| 372 | } else { |
| 373 | *error_msg = |
| 374 | StringPrintf( |
| 375 | "Malformed image relocation file %s: " |
| 376 | "last byte has it's most significant bit set", |
| 377 | rel_filename.c_str()); |
| 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // Image in memory is now supposed to be identical to the original. We |
| 383 | // confirm this by comparing the digest of the in-memory image to the expected |
| 384 | // digest from relocation file. |
| 385 | uint8_t image_digest[SHA256_DIGEST_LENGTH]; |
| 386 | SHA256(image.get(), image_size, image_digest); |
| 387 | if (memcmp(image_digest, original_image_digest, SHA256_DIGEST_LENGTH) != 0) { |
| 388 | *error_msg = |
| 389 | StringPrintf( |
| 390 | "Relocated image %s does not match the original %s after unrelocation", |
| 391 | relocated_filename.c_str(), |
| 392 | original_filename.c_str()); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | // Relocated image is identical to the original, once relocations are taken into account |
| 397 | return true; |
| 398 | } |
| 399 | |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 400 | static bool VerifySymlink(const std::string& intended_target, const std::string& link_name) { |
| 401 | std::string actual_target; |
| 402 | if (!android::base::Readlink(link_name, &actual_target)) { |
| 403 | PLOG(ERROR) << "Readlink on " << link_name << " failed."; |
| 404 | return false; |
| 405 | } |
| 406 | return actual_target == intended_target; |
| 407 | } |
| 408 | |
| 409 | static bool VerifyVdexAndOatSymlinks(const std::string& input_image_filename, |
| 410 | const std::string& output_image_filename) { |
| 411 | return VerifySymlink(ImageHeader::GetVdexLocationFromImageLocation(input_image_filename), |
| 412 | ImageHeader::GetVdexLocationFromImageLocation(output_image_filename)) |
| 413 | && VerifySymlink(ImageHeader::GetOatLocationFromImageLocation(input_image_filename), |
| 414 | ImageHeader::GetOatLocationFromImageLocation(output_image_filename)); |
| 415 | } |
| 416 | |
| 417 | bool PatchOat::CreateVdexAndOatSymlinks(const std::string& input_image_filename, |
| 418 | const std::string& output_image_filename) { |
| 419 | std::string input_vdex_filename = |
| 420 | ImageHeader::GetVdexLocationFromImageLocation(input_image_filename); |
| 421 | std::string input_oat_filename = |
| 422 | ImageHeader::GetOatLocationFromImageLocation(input_image_filename); |
| 423 | |
| 424 | std::unique_ptr<File> input_oat_file(OS::OpenFileForReading(input_oat_filename.c_str())); |
| 425 | if (input_oat_file.get() == nullptr) { |
| 426 | LOG(ERROR) << "Unable to open input oat file at " << input_oat_filename; |
| 427 | return false; |
| 428 | } |
| 429 | std::string error_msg; |
| 430 | std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat_file.get(), |
| 431 | PROT_READ | PROT_WRITE, |
| 432 | MAP_PRIVATE, |
| 433 | &error_msg)); |
| 434 | if (elf.get() == nullptr) { |
| 435 | LOG(ERROR) << "Unable to open oat file " << input_oat_filename << " : " << error_msg; |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | MaybePic is_oat_pic = IsOatPic(elf.get()); |
| 440 | if (is_oat_pic >= ERROR_FIRST) { |
| 441 | // Error logged by IsOatPic |
| 442 | return false; |
| 443 | } else if (is_oat_pic == NOT_PIC) { |
| 444 | LOG(ERROR) << "patchoat cannot be used on non-PIC oat file: " << input_oat_filename; |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | CHECK(is_oat_pic == PIC); |
| 449 | |
| 450 | std::string output_vdex_filename = |
| 451 | ImageHeader::GetVdexLocationFromImageLocation(output_image_filename); |
| 452 | std::string output_oat_filename = |
| 453 | ImageHeader::GetOatLocationFromImageLocation(output_image_filename); |
| 454 | |
| 455 | return SymlinkFile(input_oat_filename, output_oat_filename) && |
| 456 | SymlinkFile(input_vdex_filename, output_vdex_filename); |
| 457 | } |
| 458 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 459 | bool PatchOat::Patch(const std::string& image_location, |
| 460 | off_t delta, |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 461 | const std::string& output_image_directory, |
| 462 | const std::string& output_image_relocation_directory, |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 463 | InstructionSet isa, |
| 464 | TimingLogger* timings) { |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 465 | bool output_image = !output_image_directory.empty(); |
| 466 | bool output_image_relocation = !output_image_relocation_directory.empty(); |
| 467 | if ((!output_image) && (!output_image_relocation)) { |
| 468 | // Nothing to do |
| 469 | return true; |
| 470 | } |
| 471 | if ((output_image_relocation) && (delta == 0)) { |
| 472 | LOG(ERROR) << "Cannot output image relocation information when requested relocation delta is 0"; |
| 473 | return false; |
| 474 | } |
| 475 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 476 | CHECK(Runtime::Current() == nullptr); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 477 | CHECK(!image_location.empty()) << "image file must have a filename."; |
| 478 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 479 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 480 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 481 | CHECK_NE(isa, InstructionSet::kNone); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 482 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 483 | // Set up the runtime |
Andreas Gampe | c7d2508 | 2018-03-09 12:06:45 -0800 | [diff] [blame] | 484 | PatchoatRuntimeOptionsHolder options_holder(image_location, isa); |
| 485 | if (!Runtime::Create(options_holder.GetRuntimeOptions(), false)) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 486 | LOG(ERROR) << "Unable to initialize runtime"; |
| 487 | return false; |
| 488 | } |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 489 | std::unique_ptr<Runtime> runtime(Runtime::Current()); |
| 490 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 491 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 492 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 493 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 494 | ScopedObjectAccess soa(Thread::Current()); |
| 495 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 496 | std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 497 | 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] | 498 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 499 | for (size_t i = 0; i < spaces.size(); ++i) { |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 500 | t.NewTiming("Image Patching setup"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 501 | gc::space::ImageSpace* space = spaces[i]; |
| 502 | std::string input_image_filename = space->GetImageFilename(); |
| 503 | std::unique_ptr<File> input_image(OS::OpenFileForReading(input_image_filename.c_str())); |
| 504 | if (input_image.get() == nullptr) { |
| 505 | LOG(ERROR) << "Unable to open input image file at " << input_image_filename; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 506 | return false; |
| 507 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 508 | |
| 509 | int64_t image_len = input_image->GetLength(); |
| 510 | if (image_len < 0) { |
| 511 | LOG(ERROR) << "Error while getting image length"; |
| 512 | return false; |
| 513 | } |
| 514 | ImageHeader image_header; |
| 515 | if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header), |
| 516 | sizeof(image_header), 0)) { |
| 517 | LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath(); |
| 518 | } |
| 519 | |
| 520 | /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath()); |
| 521 | // Nothing special to do right now since the image always needs to get patched. |
| 522 | // Perhaps in some far-off future we may have images with relative addresses that are true-PIC. |
| 523 | |
| 524 | // Create the map where we will write the image patches to. |
| 525 | std::string error_msg; |
| 526 | std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, |
| 527 | PROT_READ | PROT_WRITE, |
| 528 | MAP_PRIVATE, |
| 529 | input_image->Fd(), |
| 530 | 0, |
| 531 | /*low_4gb*/false, |
| 532 | input_image->GetPath().c_str(), |
| 533 | &error_msg)); |
| 534 | if (image.get() == nullptr) { |
| 535 | LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg; |
| 536 | return false; |
| 537 | } |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 538 | |
| 539 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 540 | space_to_memmap_map.emplace(space, std::move(image)); |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 541 | PatchOat p = PatchOat(isa, |
Vladimir Marko | 35d5b8a | 2018-07-03 09:18:32 +0100 | [diff] [blame] | 542 | space_to_memmap_map[space].get(), |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 543 | space->GetLiveBitmap(), |
| 544 | space->GetMemMap(), |
| 545 | delta, |
| 546 | &space_to_memmap_map, |
| 547 | timings); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 548 | |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 549 | t.NewTiming("Patching image"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 550 | if (!p.PatchImage(i == 0)) { |
| 551 | LOG(ERROR) << "Failed to patch image file " << input_image_filename; |
| 552 | return false; |
| 553 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 554 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 555 | // Write the patched image spaces. |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 556 | if (output_image) { |
| 557 | std::string output_image_filename; |
| 558 | if (!GetDalvikCacheFilename(space->GetImageLocation().c_str(), |
| 559 | output_image_directory.c_str(), |
| 560 | &output_image_filename, |
| 561 | &error_msg)) { |
| 562 | LOG(ERROR) << "Failed to find relocated image file name: " << error_msg; |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | if (!CreateVdexAndOatSymlinks(input_image_filename, output_image_filename)) |
| 567 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 568 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 569 | t.NewTiming("Writing image"); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 570 | std::unique_ptr<File> output_image_file(CreateOrOpen(output_image_filename.c_str())); |
| 571 | if (output_image_file.get() == nullptr) { |
| 572 | LOG(ERROR) << "Failed to open output image file at " << output_image_filename; |
| 573 | return false; |
| 574 | } |
| 575 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 576 | bool success = p.WriteImage(output_image_file.get()); |
| 577 | success = FinishFile(output_image_file.get(), success); |
| 578 | if (!success) { |
| 579 | return false; |
| 580 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 583 | if (output_image_relocation) { |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 584 | t.NewTiming("Writing image relocation"); |
| 585 | std::string original_image_filename(space->GetImageLocation() + ".rel"); |
| 586 | std::string image_relocation_filename = |
| 587 | output_image_relocation_directory |
| 588 | + (android::base::StartsWith(original_image_filename, "/") ? "" : "/") |
| 589 | + original_image_filename.substr(original_image_filename.find_last_of("/")); |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 590 | int64_t input_image_size = input_image->GetLength(); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 591 | if (input_image_size < 0) { |
| 592 | LOG(ERROR) << "Error while getting input image size"; |
| 593 | return false; |
| 594 | } |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 595 | std::unique_ptr<MemMap> original(MemMap::MapFile(input_image_size, |
| 596 | PROT_READ, |
| 597 | MAP_PRIVATE, |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 598 | input_image->Fd(), |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 599 | 0, |
| 600 | /*low_4gb*/false, |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 601 | input_image->GetPath().c_str(), |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 602 | &error_msg)); |
| 603 | if (original.get() == nullptr) { |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 604 | LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg; |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 605 | return false; |
| 606 | } |
| 607 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 608 | const MemMap* relocated = p.image_; |
| 609 | |
| 610 | if (!WriteRelFile(*original, *relocated, image_relocation_filename, &error_msg)) { |
| 611 | LOG(ERROR) << "Failed to create image relocation file " << image_relocation_filename |
| 612 | << ": " << error_msg; |
| 613 | return false; |
| 614 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 615 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 616 | } |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 617 | |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 618 | if (!kIsDebugBuild && !(kRunningOnMemoryTool && kMemoryToolDetectsLeaks)) { |
Andreas Gampe | b8cc175 | 2017-04-26 21:28:50 -0700 | [diff] [blame] | 619 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 620 | // in an orderly fashion. So release the following fields. |
| 621 | runtime.release(); |
| 622 | } |
| 623 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 624 | return true; |
| 625 | } |
| 626 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 627 | bool PatchOat::Verify(const std::string& image_location, |
| 628 | const std::string& output_image_directory, |
| 629 | InstructionSet isa, |
| 630 | TimingLogger* timings) { |
| 631 | if (image_location.empty()) { |
| 632 | LOG(ERROR) << "Original image file not provided"; |
| 633 | return false; |
| 634 | } |
| 635 | if (output_image_directory.empty()) { |
| 636 | LOG(ERROR) << "Relocated image directory not provided"; |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
| 641 | |
| 642 | CHECK_NE(isa, InstructionSet::kNone); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 643 | |
| 644 | // Set up the runtime |
Andreas Gampe | c7d2508 | 2018-03-09 12:06:45 -0800 | [diff] [blame] | 645 | PatchoatRuntimeOptionsHolder options_holder(image_location, isa); |
| 646 | if (!Runtime::Create(options_holder.GetRuntimeOptions(), false)) { |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 647 | LOG(ERROR) << "Unable to initialize runtime"; |
| 648 | return false; |
| 649 | } |
| 650 | std::unique_ptr<Runtime> runtime(Runtime::Current()); |
| 651 | |
| 652 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 653 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 654 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 655 | ScopedObjectAccess soa(Thread::Current()); |
| 656 | |
| 657 | t.NewTiming("Image Verification setup"); |
| 658 | std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 659 | |
| 660 | // TODO: Check that no other .rel files exist in the original dir |
| 661 | |
| 662 | bool success = true; |
| 663 | std::string image_location_dir = android::base::Dirname(image_location); |
| 664 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 665 | gc::space::ImageSpace* space = spaces[i]; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 666 | |
| 667 | std::string relocated_image_filename; |
| 668 | std::string error_msg; |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 669 | if (!GetDalvikCacheFilename(space->GetImageLocation().c_str(), |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 670 | output_image_directory.c_str(), &relocated_image_filename, &error_msg)) { |
| 671 | LOG(ERROR) << "Failed to find relocated image file name: " << error_msg; |
| 672 | success = false; |
| 673 | break; |
| 674 | } |
| 675 | // location: /system/framework/boot.art |
| 676 | // isa: arm64 |
| 677 | // basename: boot.art |
| 678 | // original: /system/framework/arm64/boot.art |
| 679 | // relocation: /system/framework/arm64/boot.art.rel |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 680 | std::string original_image_filename = |
| 681 | GetSystemImageFilename(space->GetImageLocation().c_str(), isa); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 682 | |
| 683 | if (!CheckImageIdenticalToOriginalExceptForRelocation( |
| 684 | relocated_image_filename, original_image_filename, &error_msg)) { |
| 685 | LOG(ERROR) << error_msg; |
| 686 | success = false; |
| 687 | break; |
| 688 | } |
Chris Morin | ae6832f | 2018-02-09 19:12:35 -0800 | [diff] [blame] | 689 | |
| 690 | if (!VerifyVdexAndOatSymlinks(original_image_filename, relocated_image_filename)) { |
| 691 | LOG(ERROR) << "Verification of vdex and oat symlinks for " |
| 692 | << space->GetImageLocation() << " failed."; |
| 693 | success = false; |
| 694 | break; |
| 695 | } |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 698 | if (!kIsDebugBuild && !(kRunningOnMemoryTool && kMemoryToolDetectsLeaks)) { |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 699 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 700 | // in an orderly fashion. So release the following fields. |
| 701 | runtime.release(); |
| 702 | } |
| 703 | |
| 704 | return success; |
| 705 | } |
| 706 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 707 | bool PatchOat::WriteImage(File* out) { |
Greg Kaiser | 4eb1779 | 2018-04-06 14:26:23 -0700 | [diff] [blame] | 708 | CHECK(out != nullptr); |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 709 | TimingLogger::ScopedTiming t("Writing image File", timings_); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 710 | std::string error_msg; |
| 711 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 712 | // No error checking here, this is best effort. The locking may or may not |
| 713 | // succeed and we don't really care either way. |
| 714 | ScopedFlock img_flock = LockedFile::DupOf(out->Fd(), out->GetPath(), |
| 715 | true /* read_only_mode */, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 716 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 717 | CHECK(image_ != nullptr); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 718 | size_t expect = image_->Size(); |
| 719 | if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) && |
| 720 | out->SetLength(expect) == 0) { |
| 721 | return true; |
| 722 | } else { |
| 723 | LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed."; |
| 724 | return false; |
| 725 | } |
| 726 | } |
| 727 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 728 | bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) { |
| 729 | if (!image_header.CompilePic()) { |
| 730 | if (kIsDebugBuild) { |
| 731 | LOG(INFO) << "image at location " << image_path << " was *not* compiled pic"; |
| 732 | } |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | if (kIsDebugBuild) { |
| 737 | LOG(INFO) << "image at location " << image_path << " was compiled PIC"; |
| 738 | } |
| 739 | |
| 740 | return true; |
| 741 | } |
| 742 | |
| 743 | PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) { |
| 744 | if (oat_in == nullptr) { |
| 745 | LOG(ERROR) << "No ELF input oat fie available"; |
| 746 | return ERROR_OAT_FILE; |
| 747 | } |
| 748 | |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 749 | const std::string& file_path = oat_in->GetFilePath(); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 750 | |
| 751 | const OatHeader* oat_header = GetOatHeader(oat_in); |
| 752 | if (oat_header == nullptr) { |
| 753 | LOG(ERROR) << "Failed to find oat header in oat file " << file_path; |
| 754 | return ERROR_OAT_FILE; |
| 755 | } |
| 756 | |
| 757 | if (!oat_header->IsValid()) { |
| 758 | LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header"; |
| 759 | return ERROR_OAT_FILE; |
| 760 | } |
| 761 | |
| 762 | bool is_pic = oat_header->IsPic(); |
| 763 | if (kIsDebugBuild) { |
| 764 | LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic"); |
| 765 | } |
| 766 | |
| 767 | return is_pic ? PIC : NOT_PIC; |
| 768 | } |
| 769 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 770 | class PatchOat::PatchOatArtFieldVisitor : public ArtFieldVisitor { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 771 | public: |
| 772 | explicit PatchOatArtFieldVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 773 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 774 | void Visit(ArtField* field) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 775 | ArtField* const dest = patch_oat_->RelocatedCopyOf(field); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 776 | dest->SetDeclaringClass( |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 777 | patch_oat_->RelocatedAddressOfPointer(field->GetDeclaringClass().Ptr())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 778 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 779 | |
| 780 | private: |
| 781 | PatchOat* const patch_oat_; |
| 782 | }; |
| 783 | |
| 784 | void PatchOat::PatchArtFields(const ImageHeader* image_header) { |
| 785 | PatchOatArtFieldVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 786 | image_header->VisitPackedArtFields(&visitor, heap_->Begin()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 789 | class PatchOat::PatchOatArtMethodVisitor : public ArtMethodVisitor { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 790 | public: |
| 791 | explicit PatchOatArtMethodVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 792 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 793 | void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 794 | ArtMethod* const dest = patch_oat_->RelocatedCopyOf(method); |
| 795 | patch_oat_->FixupMethod(method, dest); |
| 796 | } |
| 797 | |
| 798 | private: |
| 799 | PatchOat* const patch_oat_; |
| 800 | }; |
| 801 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 802 | void PatchOat::PatchArtMethods(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 803 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 804 | PatchOatArtMethodVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 805 | image_header->VisitPackedArtMethods(&visitor, heap_->Begin(), pointer_size); |
| 806 | } |
| 807 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 808 | void PatchOat::PatchImTables(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 809 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 810 | // We can safely walk target image since the conflict tables are independent. |
| 811 | image_header->VisitPackedImTables( |
| 812 | [this](ArtMethod* method) { |
| 813 | return RelocatedAddressOfPointer(method); |
| 814 | }, |
| 815 | image_->Begin(), |
| 816 | pointer_size); |
| 817 | } |
| 818 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 819 | void PatchOat::PatchImtConflictTables(const ImageHeader* image_header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 820 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 821 | // We can safely walk target image since the conflict tables are independent. |
| 822 | image_header->VisitPackedImtConflictTables( |
| 823 | [this](ArtMethod* method) { |
| 824 | return RelocatedAddressOfPointer(method); |
| 825 | }, |
| 826 | image_->Begin(), |
| 827 | pointer_size); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 830 | class PatchOat::FixupRootVisitor : public RootVisitor { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 831 | public: |
| 832 | explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) { |
| 833 | } |
| 834 | |
| 835 | 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] | 836 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 837 | for (size_t i = 0; i < count; ++i) { |
| 838 | *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 843 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 844 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 845 | for (size_t i = 0; i < count; ++i) { |
| 846 | roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr())); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | private: |
| 851 | const PatchOat* const patch_oat_; |
| 852 | }; |
| 853 | |
| 854 | void PatchOat::PatchInternedStrings(const ImageHeader* image_header) { |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 855 | const auto& section = image_header->GetInternedStringsSection(); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 856 | if (section.Size() == 0) { |
| 857 | return; |
| 858 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 859 | InternTable temp_table; |
| 860 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 861 | // This also relies on visit roots not doing any verification which could fail after we update |
| 862 | // the roots to be the image addresses. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 863 | temp_table.AddTableFromMemory(image_->Begin() + section.Offset()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 864 | FixupRootVisitor visitor(this); |
| 865 | temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots); |
| 866 | } |
| 867 | |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 868 | void PatchOat::PatchClassTable(const ImageHeader* image_header) { |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 869 | const auto& section = image_header->GetClassTableSection(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 870 | if (section.Size() == 0) { |
| 871 | return; |
| 872 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 873 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 874 | // This also relies on visit roots not doing any verification which could fail after we update |
| 875 | // the roots to be the image addresses. |
| 876 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 877 | ClassTable temp_table; |
| 878 | temp_table.ReadFromMemory(image_->Begin() + section.Offset()); |
| 879 | FixupRootVisitor visitor(this); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 880 | temp_table.VisitRoots(UnbufferedRootVisitor(&visitor, RootInfo(kRootUnknown))); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 884 | class PatchOat::RelocatedPointerVisitor { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 885 | public: |
| 886 | explicit RelocatedPointerVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 887 | |
| 888 | template <typename T> |
Vladimir Marko | ca8de0a | 2018-07-04 11:56:08 +0100 | [diff] [blame] | 889 | T* operator()(T* ptr, void** dest_addr ATTRIBUTE_UNUSED = nullptr) const { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 890 | return patch_oat_->RelocatedAddressOfPointer(ptr); |
| 891 | } |
| 892 | |
| 893 | private: |
| 894 | PatchOat* const patch_oat_; |
| 895 | }; |
| 896 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 897 | void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) { |
| 898 | auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>( |
| 899 | img_roots->Get(ImageHeader::kDexCaches)); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 900 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 901 | for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 902 | auto* orig_dex_cache = dex_caches->GetWithoutChecks(i); |
| 903 | auto* copy_dex_cache = RelocatedCopyOf(orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 904 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 905 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 906 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 907 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 908 | mirror::StringDexCacheType* orig_strings = orig_dex_cache->GetStrings(); |
| 909 | mirror::StringDexCacheType* relocated_strings = RelocatedAddressOfPointer(orig_strings); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 910 | copy_dex_cache->SetField64<false>( |
| 911 | mirror::DexCache::StringsOffset(), |
| 912 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_strings))); |
| 913 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 914 | orig_dex_cache->FixupStrings(RelocatedCopyOf(orig_strings), RelocatedPointerVisitor(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 915 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 916 | mirror::TypeDexCacheType* orig_types = orig_dex_cache->GetResolvedTypes(); |
| 917 | mirror::TypeDexCacheType* relocated_types = RelocatedAddressOfPointer(orig_types); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 918 | copy_dex_cache->SetField64<false>( |
| 919 | mirror::DexCache::ResolvedTypesOffset(), |
| 920 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_types))); |
| 921 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 922 | orig_dex_cache->FixupResolvedTypes(RelocatedCopyOf(orig_types), |
| 923 | RelocatedPointerVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 924 | } |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 925 | mirror::MethodDexCacheType* orig_methods = orig_dex_cache->GetResolvedMethods(); |
| 926 | mirror::MethodDexCacheType* relocated_methods = RelocatedAddressOfPointer(orig_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 927 | copy_dex_cache->SetField64<false>( |
| 928 | mirror::DexCache::ResolvedMethodsOffset(), |
| 929 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_methods))); |
| 930 | if (orig_methods != nullptr) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 931 | mirror::MethodDexCacheType* copy_methods = RelocatedCopyOf(orig_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 932 | 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] | 933 | mirror::MethodDexCachePair orig = |
| 934 | mirror::DexCache::GetNativePairPtrSize(orig_methods, j, pointer_size); |
| 935 | mirror::MethodDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index); |
| 936 | mirror::DexCache::SetNativePairPtrSize(copy_methods, j, copy, pointer_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 937 | } |
| 938 | } |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 939 | mirror::FieldDexCacheType* orig_fields = orig_dex_cache->GetResolvedFields(); |
| 940 | mirror::FieldDexCacheType* relocated_fields = RelocatedAddressOfPointer(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 941 | copy_dex_cache->SetField64<false>( |
| 942 | mirror::DexCache::ResolvedFieldsOffset(), |
| 943 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_fields))); |
| 944 | if (orig_fields != nullptr) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 945 | mirror::FieldDexCacheType* copy_fields = RelocatedCopyOf(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 946 | 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] | 947 | mirror::FieldDexCachePair orig = |
| 948 | mirror::DexCache::GetNativePairPtrSize(orig_fields, j, pointer_size); |
| 949 | mirror::FieldDexCachePair copy(RelocatedAddressOfPointer(orig.object), orig.index); |
| 950 | mirror::DexCache::SetNativePairPtrSize(copy_fields, j, copy, pointer_size); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 951 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 952 | } |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 953 | mirror::MethodTypeDexCacheType* orig_method_types = orig_dex_cache->GetResolvedMethodTypes(); |
| 954 | mirror::MethodTypeDexCacheType* relocated_method_types = |
| 955 | RelocatedAddressOfPointer(orig_method_types); |
| 956 | copy_dex_cache->SetField64<false>( |
| 957 | mirror::DexCache::ResolvedMethodTypesOffset(), |
| 958 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_method_types))); |
| 959 | if (orig_method_types != nullptr) { |
| 960 | orig_dex_cache->FixupResolvedMethodTypes(RelocatedCopyOf(orig_method_types), |
| 961 | RelocatedPointerVisitor(this)); |
| 962 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 963 | |
| 964 | GcRoot<mirror::CallSite>* orig_call_sites = orig_dex_cache->GetResolvedCallSites(); |
| 965 | GcRoot<mirror::CallSite>* relocated_call_sites = RelocatedAddressOfPointer(orig_call_sites); |
| 966 | copy_dex_cache->SetField64<false>( |
| 967 | mirror::DexCache::ResolvedCallSitesOffset(), |
| 968 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_call_sites))); |
| 969 | if (orig_call_sites != nullptr) { |
| 970 | orig_dex_cache->FixupResolvedCallSites(RelocatedCopyOf(orig_call_sites), |
| 971 | RelocatedPointerVisitor(this)); |
| 972 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 976 | bool PatchOat::PatchImage(bool primary_image) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 977 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
| 978 | CHECK_GT(image_->Size(), sizeof(ImageHeader)); |
| 979 | // These are the roots from the original file. |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 980 | mirror::ObjectArray<mirror::Object>* img_roots = image_header->GetImageRoots().Ptr(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 981 | image_header->RelocateImage(delta_); |
| 982 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 983 | PatchArtFields(image_header); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 984 | PatchArtMethods(image_header); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 985 | PatchImTables(image_header); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 986 | PatchImtConflictTables(image_header); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 987 | PatchInternedStrings(image_header); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 988 | PatchClassTable(image_header); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 989 | // Patch dex file int/long arrays which point to ArtFields. |
| 990 | PatchDexFileArrays(img_roots); |
| 991 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 992 | if (primary_image) { |
| 993 | VisitObject(img_roots); |
| 994 | } |
| 995 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 996 | if (!image_header->IsValid()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 997 | LOG(ERROR) << "relocation renders image header invalid"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 998 | return false; |
| 999 | } |
| 1000 | |
| 1001 | { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1002 | TimingLogger::ScopedTiming t("Walk Bitmap", timings_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1003 | // Walk the bitmap. |
| 1004 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1005 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1006 | VisitObject(obj); |
| 1007 | }; |
| 1008 | bitmap_->Walk(visitor); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1009 | } |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1013 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1014 | void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Object> obj, |
| 1015 | MemberOffset off, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1016 | bool is_static_unused ATTRIBUTE_UNUSED) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1017 | mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1018 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1019 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 1020 | } |
| 1021 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1022 | void PatchOat::PatchVisitor::operator() (ObjPtr<mirror::Class> cls ATTRIBUTE_UNUSED, |
| 1023 | ObjPtr<mirror::Reference> ref) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1024 | MemberOffset off = mirror::Reference::ReferentOffset(); |
| 1025 | mirror::Object* referent = ref->GetReferent(); |
Mathieu Chartier | a13abba | 2016-04-21 10:23:16 -0700 | [diff] [blame] | 1026 | DCHECK(referent == nullptr || |
| 1027 | Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(referent)) << referent; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1028 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1029 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 1030 | } |
| 1031 | |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1032 | // Called by PatchImage. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1033 | void PatchOat::VisitObject(mirror::Object* object) { |
| 1034 | mirror::Object* copy = RelocatedCopyOf(object); |
| 1035 | CHECK(copy != nullptr); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1036 | if (kUseBakerReadBarrier) { |
| 1037 | object->AssertReadBarrierState(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1038 | } |
| 1039 | PatchOat::PatchVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1040 | object->VisitReferences<kVerifyNone>(visitor, visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1041 | if (object->IsClass<kVerifyNone>()) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1042 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1043 | mirror::Class* klass = object->AsClass(); |
| 1044 | mirror::Class* copy_klass = down_cast<mirror::Class*>(copy); |
| 1045 | RelocatedPointerVisitor native_visitor(this); |
| 1046 | klass->FixupNativePointers(copy_klass, pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1047 | auto* vtable = klass->GetVTable(); |
| 1048 | if (vtable != nullptr) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1049 | vtable->Fixup(RelocatedCopyOfFollowImages(vtable), pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1050 | } |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 1051 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 1052 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1053 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1054 | auto* method_array = iftable->GetMethodArray(i); |
| 1055 | CHECK(method_array != nullptr); |
| 1056 | method_array->Fixup(RelocatedCopyOfFollowImages(method_array), |
| 1057 | pointer_size, |
| 1058 | native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1059 | } |
| 1060 | } |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 1061 | } else if (object->GetClass() == GetClassRoot<mirror::Method>() || |
| 1062 | object->GetClass() == GetClassRoot<mirror::Constructor>()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1063 | // Need to go update the ArtMethod. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 1064 | auto* dest = down_cast<mirror::Executable*>(copy); |
| 1065 | auto* src = down_cast<mirror::Executable*>(object); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1066 | dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod())); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1070 | void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1071 | const PointerSize pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1072 | copy->CopyFrom(object, pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1073 | // Just update the entry points if it looks like we should. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1074 | // TODO: sanity check all the pointers' values |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1075 | copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass().Ptr())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1076 | copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer( |
| 1077 | object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1078 | // 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] | 1079 | copy->SetDataPtrSize(RelocatedAddressOfPointer( |
| 1080 | object->GetDataPtrSize(pointer_size)), pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1083 | static int orig_argc; |
| 1084 | static char** orig_argv; |
| 1085 | |
| 1086 | static std::string CommandLine() { |
| 1087 | std::vector<std::string> command; |
| 1088 | for (int i = 0; i < orig_argc; ++i) { |
| 1089 | command.push_back(orig_argv[i]); |
| 1090 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 1091 | return android::base::Join(command, ' '); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 1095 | std::string error; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 1096 | android::base::StringAppendV(&error, fmt, ap); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1097 | LOG(ERROR) << error; |
| 1098 | } |
| 1099 | |
| 1100 | static void UsageError(const char* fmt, ...) { |
| 1101 | va_list ap; |
| 1102 | va_start(ap, fmt); |
| 1103 | UsageErrorV(fmt, ap); |
| 1104 | va_end(ap); |
| 1105 | } |
| 1106 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 1107 | NO_RETURN static void Usage(const char *fmt, ...) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1108 | va_list ap; |
| 1109 | va_start(ap, fmt); |
| 1110 | UsageErrorV(fmt, ap); |
| 1111 | va_end(ap); |
| 1112 | |
| 1113 | UsageError("Command: %s", CommandLine().c_str()); |
| 1114 | UsageError("Usage: patchoat [options]..."); |
| 1115 | UsageError(""); |
| 1116 | UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is"); |
Richard Uhler | 4bc11d0 | 2017-02-01 09:53:54 +0000 | [diff] [blame] | 1117 | UsageError(" compiled for (required)."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1118 | UsageError(""); |
| 1119 | 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] | 1120 | UsageError(" be patched."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1121 | UsageError(""); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1122 | UsageError(" --output-image-directory=<dir>: Specifies the directory to write the patched"); |
| 1123 | UsageError(" image file(s) to."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1124 | UsageError(""); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1125 | UsageError(" --output-image-relocation-directory=<dir>: Specifies the directory to write"); |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 1126 | UsageError(" the image relocation information to."); |
| 1127 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1128 | UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by."); |
| 1129 | UsageError(" This value may be negative."); |
| 1130 | UsageError(""); |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1131 | UsageError(" --verify: Verify an existing patched file instead of creating one."); |
| 1132 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1133 | UsageError(" --dump-timings: dump out patch timing information"); |
| 1134 | UsageError(""); |
| 1135 | UsageError(" --no-dump-timings: do not dump out patch timing information"); |
| 1136 | UsageError(""); |
| 1137 | |
| 1138 | exit(EXIT_FAILURE); |
| 1139 | } |
| 1140 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1141 | static int patchoat_patch_image(TimingLogger& timings, |
| 1142 | InstructionSet isa, |
| 1143 | const std::string& input_image_location, |
| 1144 | const std::string& output_image_directory, |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1145 | const std::string& output_image_relocation_directory, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1146 | off_t base_delta, |
| 1147 | bool base_delta_set, |
| 1148 | bool debug) { |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1149 | CHECK(!input_image_location.empty()); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1150 | if ((output_image_directory.empty()) && (output_image_relocation_directory.empty())) { |
| 1151 | Usage("Image patching requires --output-image-directory or --output-image-relocation-directory"); |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | if (!base_delta_set) { |
| 1155 | Usage("Must supply a desired new offset or delta."); |
| 1156 | } |
| 1157 | |
| 1158 | if (!IsAligned<kPageSize>(base_delta)) { |
| 1159 | Usage("Base offset/delta must be aligned to a pagesize (0x%08x) boundary.", kPageSize); |
| 1160 | } |
| 1161 | |
| 1162 | if (debug) { |
| 1163 | LOG(INFO) << "moving offset by " << base_delta |
| 1164 | << " (0x" << std::hex << base_delta << ") bytes or " |
| 1165 | << std::dec << (base_delta/kPageSize) << " pages."; |
| 1166 | } |
| 1167 | |
| 1168 | TimingLogger::ScopedTiming pt("patch image and oat", &timings); |
| 1169 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 1170 | bool ret = |
| 1171 | PatchOat::Patch( |
| 1172 | input_image_location, |
| 1173 | base_delta, |
| 1174 | output_image_directory, |
| 1175 | output_image_relocation_directory, |
| 1176 | isa, |
| 1177 | &timings); |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1178 | |
| 1179 | if (kIsDebugBuild) { |
| 1180 | LOG(INFO) << "Exiting with return ... " << ret; |
| 1181 | } |
| 1182 | return ret ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1183 | } |
| 1184 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1185 | static int patchoat_verify_image(TimingLogger& timings, |
| 1186 | InstructionSet isa, |
| 1187 | const std::string& input_image_location, |
| 1188 | const std::string& output_image_directory) { |
| 1189 | CHECK(!input_image_location.empty()); |
| 1190 | TimingLogger::ScopedTiming pt("verify image and oat", &timings); |
| 1191 | |
| 1192 | bool ret = |
| 1193 | PatchOat::Verify( |
| 1194 | input_image_location, |
| 1195 | output_image_directory, |
| 1196 | isa, |
| 1197 | &timings); |
| 1198 | |
| 1199 | if (kIsDebugBuild) { |
| 1200 | LOG(INFO) << "Exiting with return ... " << ret; |
| 1201 | } |
| 1202 | return ret ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1203 | } |
| 1204 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1205 | static int patchoat(int argc, char **argv) { |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1206 | Locks::Init(); |
Andreas Gampe | 51d80cc | 2017-06-21 21:05:13 -0700 | [diff] [blame] | 1207 | InitLogging(argv, Runtime::Abort); |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 1208 | MemMap::Init(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1209 | const bool debug = kIsDebugBuild; |
| 1210 | orig_argc = argc; |
| 1211 | orig_argv = argv; |
| 1212 | TimingLogger timings("patcher", false, false); |
| 1213 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1214 | // Skip over the command name. |
| 1215 | argv++; |
| 1216 | argc--; |
| 1217 | |
| 1218 | if (argc == 0) { |
| 1219 | Usage("No arguments specified"); |
| 1220 | } |
| 1221 | |
| 1222 | timings.StartTiming("Patchoat"); |
| 1223 | |
| 1224 | // cmd line args |
| 1225 | bool isa_set = false; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1226 | InstructionSet isa = InstructionSet::kNone; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1227 | std::string input_image_location; |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1228 | std::string output_image_directory; |
| 1229 | std::string output_image_relocation_directory; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1230 | off_t base_delta = 0; |
| 1231 | bool base_delta_set = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1232 | bool dump_timings = kIsDebugBuild; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1233 | bool verify = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1234 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1235 | for (int i = 0; i < argc; ++i) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1236 | const StringPiece option(argv[i]); |
| 1237 | const bool log_options = false; |
| 1238 | if (log_options) { |
| 1239 | LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i]; |
| 1240 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1241 | if (option.starts_with("--instruction-set=")) { |
| 1242 | isa_set = true; |
| 1243 | const char* isa_str = option.substr(strlen("--instruction-set=")).data(); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 1244 | isa = GetInstructionSetFromString(isa_str); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1245 | if (isa == InstructionSet::kNone) { |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 1246 | Usage("Unknown or invalid instruction set %s", isa_str); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1247 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1248 | } else if (option.starts_with("--input-image-location=")) { |
| 1249 | input_image_location = option.substr(strlen("--input-image-location=")).data(); |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1250 | } else if (option.starts_with("--output-image-directory=")) { |
| 1251 | output_image_directory = option.substr(strlen("--output-image-directory=")).data(); |
| 1252 | } else if (option.starts_with("--output-image-relocation-directory=")) { |
| 1253 | output_image_relocation_directory = |
| 1254 | option.substr(strlen("--output-image-relocation-directory=")).data(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1255 | } else if (option.starts_with("--base-offset-delta=")) { |
| 1256 | const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data(); |
| 1257 | base_delta_set = true; |
| 1258 | if (!ParseInt(base_delta_str, &base_delta)) { |
| 1259 | Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str); |
| 1260 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1261 | } else if (option == "--dump-timings") { |
| 1262 | dump_timings = true; |
| 1263 | } else if (option == "--no-dump-timings") { |
| 1264 | dump_timings = false; |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1265 | } else if (option == "--verify") { |
| 1266 | verify = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1267 | } else { |
| 1268 | Usage("Unknown argument %s", option.data()); |
| 1269 | } |
| 1270 | } |
| 1271 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1272 | // The instruction set is mandatory. This simplifies things... |
| 1273 | if (!isa_set) { |
| 1274 | Usage("Instruction set must be set."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1277 | int ret; |
| 1278 | if (verify) { |
| 1279 | ret = patchoat_verify_image(timings, |
| 1280 | isa, |
| 1281 | input_image_location, |
| 1282 | output_image_directory); |
| 1283 | } else { |
| 1284 | ret = patchoat_patch_image(timings, |
| 1285 | isa, |
| 1286 | input_image_location, |
| 1287 | output_image_directory, |
Chris Morin | 88c6d26 | 2018-02-13 15:26:21 -0800 | [diff] [blame] | 1288 | output_image_relocation_directory, |
Chris Morin | 754b757 | 2018-01-19 18:04:46 -0800 | [diff] [blame] | 1289 | base_delta, |
| 1290 | base_delta_set, |
| 1291 | debug); |
| 1292 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1293 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1294 | timings.EndTiming(); |
| 1295 | if (dump_timings) { |
| 1296 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Andreas Gampe | 6eb6a39 | 2016-02-10 20:18:37 -0800 | [diff] [blame] | 1299 | return ret; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | } // namespace art |
| 1303 | |
| 1304 | int main(int argc, char **argv) { |
| 1305 | return art::patchoat(argc, argv); |
| 1306 | } |