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 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 20 | #include <sys/file.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 23 | |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 27 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 28 | #include "art_method-inl.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 29 | #include "base/dumpable.h" |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 30 | #include "base/scoped_flock.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 31 | #include "base/stringpiece.h" |
| 32 | #include "base/stringprintf.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 33 | #include "base/unix_file/fd_file.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 34 | #include "elf_utils.h" |
| 35 | #include "elf_file.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 36 | #include "elf_file_impl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 37 | #include "gc/space/image_space.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 38 | #include "image.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | #include "mirror/abstract_method.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 40 | #include "mirror/object-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 41 | #include "mirror/method.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 42 | #include "mirror/reference.h" |
| 43 | #include "noop_compiler_callbacks.h" |
| 44 | #include "offsets.h" |
| 45 | #include "os.h" |
| 46 | #include "runtime.h" |
| 47 | #include "scoped_thread_state_change.h" |
| 48 | #include "thread.h" |
| 49 | #include "utils.h" |
| 50 | |
| 51 | namespace art { |
| 52 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 53 | static bool LocationToFilename(const std::string& location, InstructionSet isa, |
| 54 | std::string* filename) { |
| 55 | bool has_system = false; |
| 56 | bool has_cache = false; |
| 57 | // image_location = /system/framework/boot.art |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 58 | // system_image_filename = /system/framework/<image_isa>/boot.art |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 59 | std::string system_filename(GetSystemImageFilename(location.c_str(), isa)); |
| 60 | if (OS::FileExists(system_filename.c_str())) { |
| 61 | has_system = true; |
| 62 | } |
| 63 | |
| 64 | bool have_android_data = false; |
| 65 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 66 | bool is_global_cache = false; |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 67 | std::string dalvik_cache; |
| 68 | GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 69 | &have_android_data, &dalvik_cache_exists, &is_global_cache); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 70 | |
| 71 | std::string cache_filename; |
| 72 | if (have_android_data && dalvik_cache_exists) { |
| 73 | // Always set output location even if it does not exist, |
| 74 | // so that the caller knows where to create the image. |
| 75 | // |
| 76 | // image_location = /system/framework/boot.art |
| 77 | // *image_filename = /data/dalvik-cache/<image_isa>/boot.art |
| 78 | std::string error_msg; |
| 79 | if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(), |
| 80 | &cache_filename, &error_msg)) { |
| 81 | has_cache = true; |
| 82 | } |
| 83 | } |
| 84 | if (has_system) { |
| 85 | *filename = system_filename; |
| 86 | return true; |
| 87 | } else if (has_cache) { |
| 88 | *filename = cache_filename; |
| 89 | return true; |
| 90 | } else { |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 95 | static const OatHeader* GetOatHeader(const ElfFile* elf_file) { |
| 96 | uint64_t off = 0; |
| 97 | if (!elf_file->GetSectionOffsetAndSize(".rodata", &off, nullptr)) { |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf_file->Begin() + off); |
| 102 | return oat_header; |
| 103 | } |
| 104 | |
| 105 | // This function takes an elf file and reads the current patch delta value |
| 106 | // encoded in its oat header value |
| 107 | static bool ReadOatPatchDelta(const ElfFile* elf_file, off_t* delta, std::string* error_msg) { |
| 108 | const OatHeader* oat_header = GetOatHeader(elf_file); |
| 109 | if (oat_header == nullptr) { |
| 110 | *error_msg = "Unable to get oat header from elf file."; |
| 111 | return false; |
| 112 | } |
| 113 | if (!oat_header->IsValid()) { |
| 114 | *error_msg = "Elf file has an invalid oat header"; |
| 115 | return false; |
| 116 | } |
| 117 | *delta = oat_header->GetImagePatchDelta(); |
| 118 | return true; |
| 119 | } |
| 120 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 121 | bool PatchOat::Patch(const std::string& image_location, off_t delta, |
| 122 | File* output_image, InstructionSet isa, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 123 | TimingLogger* timings) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 124 | CHECK(Runtime::Current() == nullptr); |
| 125 | CHECK(output_image != nullptr); |
| 126 | CHECK_GE(output_image->Fd(), 0); |
| 127 | CHECK(!image_location.empty()) << "image file must have a filename."; |
| 128 | CHECK_NE(isa, kNone); |
| 129 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 130 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 131 | const char *isa_name = GetInstructionSetString(isa); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 132 | std::string image_filename; |
| 133 | if (!LocationToFilename(image_location, isa, &image_filename)) { |
| 134 | LOG(ERROR) << "Unable to find image at location " << image_location; |
| 135 | return false; |
| 136 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 137 | std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str())); |
| 138 | if (input_image.get() == nullptr) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 139 | LOG(ERROR) << "unable to open input image file at " << image_filename |
| 140 | << " for location " << image_location; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 143 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 144 | int64_t image_len = input_image->GetLength(); |
| 145 | if (image_len < 0) { |
| 146 | LOG(ERROR) << "Error while getting image length"; |
| 147 | return false; |
| 148 | } |
| 149 | ImageHeader image_header; |
| 150 | if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 151 | sizeof(image_header), 0)) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 152 | LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath(); |
| 153 | return false; |
| 154 | } |
| 155 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 156 | /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath()); |
| 157 | // Nothing special to do right now since the image always needs to get patched. |
| 158 | // Perhaps in some far-off future we may have images with relative addresses that are true-PIC. |
| 159 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 160 | // Set up the runtime |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 161 | RuntimeOptions options; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 162 | NoopCompilerCallbacks callbacks; |
| 163 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
| 164 | std::string img = "-Ximage:" + image_location; |
| 165 | options.push_back(std::make_pair(img.c_str(), nullptr)); |
| 166 | options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name))); |
Calin Juravle | 01aaf6e | 2015-06-19 22:05:39 +0100 | [diff] [blame] | 167 | options.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 168 | if (!Runtime::Create(options, false)) { |
| 169 | LOG(ERROR) << "Unable to initialize runtime"; |
| 170 | return false; |
| 171 | } |
| 172 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 173 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 174 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 175 | ScopedObjectAccess soa(Thread::Current()); |
| 176 | |
| 177 | t.NewTiming("Image and oat Patching setup"); |
| 178 | // Create the map where we will write the image patches to. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 179 | std::string error_msg; |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 180 | std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, |
| 181 | PROT_READ | PROT_WRITE, |
| 182 | MAP_PRIVATE, |
| 183 | input_image->Fd(), |
| 184 | 0, |
| 185 | /*low_4gb*/false, |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 186 | input_image->GetPath().c_str(), |
| 187 | &error_msg)); |
| 188 | if (image.get() == nullptr) { |
| 189 | LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg; |
| 190 | return false; |
| 191 | } |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 192 | gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetBootImageSpace(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 193 | |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 194 | PatchOat p(isa, image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(), delta, timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 195 | t.NewTiming("Patching files"); |
| 196 | if (!p.PatchImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 197 | LOG(ERROR) << "Failed to patch image file " << input_image->GetPath(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | |
| 201 | t.NewTiming("Writing files"); |
| 202 | if (!p.WriteImage(output_image)) { |
| 203 | return false; |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 208 | bool PatchOat::Patch(File* input_oat, const std::string& image_location, off_t delta, |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 209 | File* output_oat, File* output_image, InstructionSet isa, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 210 | TimingLogger* timings, |
| 211 | bool output_oat_opened_from_fd, |
| 212 | bool new_oat_out) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 213 | CHECK(Runtime::Current() == nullptr); |
| 214 | CHECK(output_image != nullptr); |
| 215 | CHECK_GE(output_image->Fd(), 0); |
| 216 | CHECK(input_oat != nullptr); |
| 217 | CHECK(output_oat != nullptr); |
| 218 | CHECK_GE(input_oat->Fd(), 0); |
| 219 | CHECK_GE(output_oat->Fd(), 0); |
| 220 | CHECK(!image_location.empty()) << "image file must have a filename."; |
| 221 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 222 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 223 | |
| 224 | if (isa == kNone) { |
| 225 | Elf32_Ehdr elf_hdr; |
| 226 | if (sizeof(elf_hdr) != input_oat->Read(reinterpret_cast<char*>(&elf_hdr), sizeof(elf_hdr), 0)) { |
| 227 | LOG(ERROR) << "unable to read elf header"; |
| 228 | return false; |
| 229 | } |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 230 | isa = GetInstructionSetFromELF(elf_hdr.e_machine, elf_hdr.e_flags); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 231 | } |
| 232 | const char* isa_name = GetInstructionSetString(isa); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 233 | std::string image_filename; |
| 234 | if (!LocationToFilename(image_location, isa, &image_filename)) { |
| 235 | LOG(ERROR) << "Unable to find image at location " << image_location; |
| 236 | return false; |
| 237 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 238 | std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str())); |
| 239 | if (input_image.get() == nullptr) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 240 | LOG(ERROR) << "unable to open input image file at " << image_filename |
| 241 | << " for location " << image_location; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | int64_t image_len = input_image->GetLength(); |
| 245 | if (image_len < 0) { |
| 246 | LOG(ERROR) << "Error while getting image length"; |
| 247 | return false; |
| 248 | } |
| 249 | ImageHeader image_header; |
| 250 | if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header), |
| 251 | sizeof(image_header), 0)) { |
| 252 | LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath(); |
| 253 | } |
| 254 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 255 | /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath()); |
| 256 | // Nothing special to do right now since the image always needs to get patched. |
| 257 | // Perhaps in some far-off future we may have images with relative addresses that are true-PIC. |
| 258 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 259 | // Set up the runtime |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 260 | RuntimeOptions options; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 261 | NoopCompilerCallbacks callbacks; |
| 262 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
| 263 | std::string img = "-Ximage:" + image_location; |
| 264 | options.push_back(std::make_pair(img.c_str(), nullptr)); |
| 265 | options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name))); |
Calin Juravle | 01aaf6e | 2015-06-19 22:05:39 +0100 | [diff] [blame] | 266 | options.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 267 | if (!Runtime::Create(options, false)) { |
| 268 | LOG(ERROR) << "Unable to initialize runtime"; |
| 269 | return false; |
| 270 | } |
| 271 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 272 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 273 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 274 | ScopedObjectAccess soa(Thread::Current()); |
| 275 | |
| 276 | t.NewTiming("Image and oat Patching setup"); |
| 277 | // Create the map where we will write the image patches to. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 278 | std::string error_msg; |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 279 | std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, |
| 280 | PROT_READ | PROT_WRITE, |
| 281 | MAP_PRIVATE, |
| 282 | input_image->Fd(), |
| 283 | 0, |
| 284 | /*low_4gb*/false, |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 285 | input_image->GetPath().c_str(), |
| 286 | &error_msg)); |
| 287 | if (image.get() == nullptr) { |
| 288 | LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg; |
| 289 | return false; |
| 290 | } |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 291 | gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetBootImageSpace(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 292 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 293 | std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat, |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 294 | PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg)); |
| 295 | if (elf.get() == nullptr) { |
| 296 | LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg; |
| 297 | return false; |
| 298 | } |
| 299 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 300 | bool skip_patching_oat = false; |
| 301 | MaybePic is_oat_pic = IsOatPic(elf.get()); |
| 302 | if (is_oat_pic >= ERROR_FIRST) { |
| 303 | // Error logged by IsOatPic |
| 304 | return false; |
| 305 | } else if (is_oat_pic == PIC) { |
| 306 | // Do not need to do ELF-file patching. Create a symlink and skip the ELF patching. |
| 307 | if (!ReplaceOatFileWithSymlink(input_oat->GetPath(), |
| 308 | output_oat->GetPath(), |
| 309 | output_oat_opened_from_fd, |
| 310 | new_oat_out)) { |
| 311 | // Errors already logged by above call. |
| 312 | return false; |
| 313 | } |
| 314 | // Don't patch the OAT, since we just symlinked it. Image still needs patching. |
| 315 | skip_patching_oat = true; |
| 316 | } else { |
| 317 | CHECK(is_oat_pic == NOT_PIC); |
| 318 | } |
| 319 | |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 320 | PatchOat p(isa, elf.release(), image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(), |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 321 | delta, timings); |
| 322 | t.NewTiming("Patching files"); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 323 | if (!skip_patching_oat && !p.PatchElf()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 324 | LOG(ERROR) << "Failed to patch oat file " << input_oat->GetPath(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | if (!p.PatchImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 328 | LOG(ERROR) << "Failed to patch image file " << input_image->GetPath(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | |
| 332 | t.NewTiming("Writing files"); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 333 | if (!skip_patching_oat && !p.WriteElf(output_oat)) { |
| 334 | LOG(ERROR) << "Failed to write oat file " << input_oat->GetPath(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | if (!p.WriteImage(output_image)) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 338 | LOG(ERROR) << "Failed to write image file " << input_image->GetPath(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 339 | return false; |
| 340 | } |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | bool PatchOat::WriteElf(File* out) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 345 | TimingLogger::ScopedTiming t("Writing Elf File", timings_); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 346 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 347 | CHECK(oat_file_.get() != nullptr); |
| 348 | CHECK(out != nullptr); |
| 349 | size_t expect = oat_file_->Size(); |
| 350 | if (out->WriteFully(reinterpret_cast<char*>(oat_file_->Begin()), expect) && |
| 351 | out->SetLength(expect) == 0) { |
| 352 | return true; |
| 353 | } else { |
| 354 | LOG(ERROR) << "Writing to oat file " << out->GetPath() << " failed."; |
| 355 | return false; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | bool PatchOat::WriteImage(File* out) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 360 | TimingLogger::ScopedTiming t("Writing image File", timings_); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 361 | std::string error_msg; |
| 362 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 363 | ScopedFlock img_flock; |
| 364 | img_flock.Init(out, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 365 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 366 | CHECK(image_ != nullptr); |
| 367 | CHECK(out != nullptr); |
| 368 | size_t expect = image_->Size(); |
| 369 | if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) && |
| 370 | out->SetLength(expect) == 0) { |
| 371 | return true; |
| 372 | } else { |
| 373 | LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed."; |
| 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 378 | bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) { |
| 379 | if (!image_header.CompilePic()) { |
| 380 | if (kIsDebugBuild) { |
| 381 | LOG(INFO) << "image at location " << image_path << " was *not* compiled pic"; |
| 382 | } |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | if (kIsDebugBuild) { |
| 387 | LOG(INFO) << "image at location " << image_path << " was compiled PIC"; |
| 388 | } |
| 389 | |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) { |
| 394 | if (oat_in == nullptr) { |
| 395 | LOG(ERROR) << "No ELF input oat fie available"; |
| 396 | return ERROR_OAT_FILE; |
| 397 | } |
| 398 | |
| 399 | const std::string& file_path = oat_in->GetFile().GetPath(); |
| 400 | |
| 401 | const OatHeader* oat_header = GetOatHeader(oat_in); |
| 402 | if (oat_header == nullptr) { |
| 403 | LOG(ERROR) << "Failed to find oat header in oat file " << file_path; |
| 404 | return ERROR_OAT_FILE; |
| 405 | } |
| 406 | |
| 407 | if (!oat_header->IsValid()) { |
| 408 | LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header"; |
| 409 | return ERROR_OAT_FILE; |
| 410 | } |
| 411 | |
| 412 | bool is_pic = oat_header->IsPic(); |
| 413 | if (kIsDebugBuild) { |
| 414 | LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic"); |
| 415 | } |
| 416 | |
| 417 | return is_pic ? PIC : NOT_PIC; |
| 418 | } |
| 419 | |
| 420 | bool PatchOat::ReplaceOatFileWithSymlink(const std::string& input_oat_filename, |
| 421 | const std::string& output_oat_filename, |
| 422 | bool output_oat_opened_from_fd, |
| 423 | bool new_oat_out) { |
| 424 | // Need a file when we are PIC, since we symlink over it. Refusing to symlink into FD. |
| 425 | if (output_oat_opened_from_fd) { |
| 426 | // TODO: installd uses --output-oat-fd. Should we change class linking logic for PIC? |
| 427 | LOG(ERROR) << "No output oat filename specified, needs filename for when we are PIC"; |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | // Image was PIC. Create symlink where the oat is supposed to go. |
| 432 | if (!new_oat_out) { |
| 433 | LOG(ERROR) << "Oat file " << output_oat_filename << " already exists, refusing to overwrite"; |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | // Delete the original file, since we won't need it. |
| 438 | TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str())); |
| 439 | |
| 440 | // Create a symlink from the old oat to the new oat |
| 441 | if (symlink(input_oat_filename.c_str(), output_oat_filename.c_str()) < 0) { |
| 442 | int err = errno; |
| 443 | LOG(ERROR) << "Failed to create symlink at " << output_oat_filename |
| 444 | << " error(" << err << "): " << strerror(err); |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | if (kIsDebugBuild) { |
| 449 | LOG(INFO) << "Created symlink " << output_oat_filename << " -> " << input_oat_filename; |
| 450 | } |
| 451 | |
| 452 | return true; |
| 453 | } |
| 454 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 455 | class PatchOatArtFieldVisitor : public ArtFieldVisitor { |
| 456 | public: |
| 457 | explicit PatchOatArtFieldVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 458 | |
| 459 | void Visit(ArtField* field) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 460 | ArtField* const dest = patch_oat_->RelocatedCopyOf(field); |
| 461 | dest->SetDeclaringClass(patch_oat_->RelocatedAddressOfPointer(field->GetDeclaringClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 462 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 463 | |
| 464 | private: |
| 465 | PatchOat* const patch_oat_; |
| 466 | }; |
| 467 | |
| 468 | void PatchOat::PatchArtFields(const ImageHeader* image_header) { |
| 469 | PatchOatArtFieldVisitor visitor(this); |
| 470 | const auto& section = image_header->GetImageSection(ImageHeader::kSectionArtFields); |
| 471 | section.VisitPackedArtFields(&visitor, heap_->Begin()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 474 | class PatchOatArtMethodVisitor : public ArtMethodVisitor { |
| 475 | public: |
| 476 | explicit PatchOatArtMethodVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 477 | |
| 478 | void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 479 | ArtMethod* const dest = patch_oat_->RelocatedCopyOf(method); |
| 480 | patch_oat_->FixupMethod(method, dest); |
| 481 | } |
| 482 | |
| 483 | private: |
| 484 | PatchOat* const patch_oat_; |
| 485 | }; |
| 486 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 487 | void PatchOat::PatchArtMethods(const ImageHeader* image_header) { |
| 488 | const auto& section = image_header->GetMethodsSection(); |
| 489 | const size_t pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 490 | PatchOatArtMethodVisitor visitor(this); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 491 | section.VisitPackedArtMethods(&visitor, heap_->Begin(), pointer_size); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 494 | class FixupRootVisitor : public RootVisitor { |
| 495 | public: |
| 496 | explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) { |
| 497 | } |
| 498 | |
| 499 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 500 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 501 | for (size_t i = 0; i < count; ++i) { |
| 502 | *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 507 | const RootInfo& info ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 508 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 509 | for (size_t i = 0; i < count; ++i) { |
| 510 | roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr())); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private: |
| 515 | const PatchOat* const patch_oat_; |
| 516 | }; |
| 517 | |
| 518 | void PatchOat::PatchInternedStrings(const ImageHeader* image_header) { |
| 519 | const auto& section = image_header->GetImageSection(ImageHeader::kSectionInternedStrings); |
| 520 | InternTable temp_table; |
| 521 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 522 | // This also relies on visit roots not doing any verification which could fail after we update |
| 523 | // the roots to be the image addresses. |
| 524 | temp_table.ReadFromMemory(image_->Begin() + section.Offset()); |
| 525 | FixupRootVisitor visitor(this); |
| 526 | temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots); |
| 527 | } |
| 528 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 529 | class RelocatedPointerVisitor { |
| 530 | public: |
| 531 | explicit RelocatedPointerVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {} |
| 532 | |
| 533 | template <typename T> |
| 534 | T* operator()(T* ptr) const { |
| 535 | return patch_oat_->RelocatedAddressOfPointer(ptr); |
| 536 | } |
| 537 | |
| 538 | private: |
| 539 | PatchOat* const patch_oat_; |
| 540 | }; |
| 541 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 542 | void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) { |
| 543 | auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>( |
| 544 | img_roots->Get(ImageHeader::kDexCaches)); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 545 | const size_t pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 546 | for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 547 | auto* orig_dex_cache = dex_caches->GetWithoutChecks(i); |
| 548 | auto* copy_dex_cache = RelocatedCopyOf(orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 549 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 550 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 551 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 552 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
| 553 | GcRoot<mirror::String>* orig_strings = orig_dex_cache->GetStrings(); |
| 554 | GcRoot<mirror::String>* relocated_strings = RelocatedAddressOfPointer(orig_strings); |
| 555 | copy_dex_cache->SetField64<false>( |
| 556 | mirror::DexCache::StringsOffset(), |
| 557 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_strings))); |
| 558 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 559 | orig_dex_cache->FixupStrings(RelocatedCopyOf(orig_strings), RelocatedPointerVisitor(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 560 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 561 | GcRoot<mirror::Class>* orig_types = orig_dex_cache->GetResolvedTypes(); |
| 562 | GcRoot<mirror::Class>* relocated_types = RelocatedAddressOfPointer(orig_types); |
| 563 | copy_dex_cache->SetField64<false>( |
| 564 | mirror::DexCache::ResolvedTypesOffset(), |
| 565 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_types))); |
| 566 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 567 | orig_dex_cache->FixupResolvedTypes(RelocatedCopyOf(orig_types), |
| 568 | RelocatedPointerVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 569 | } |
| 570 | ArtMethod** orig_methods = orig_dex_cache->GetResolvedMethods(); |
| 571 | ArtMethod** relocated_methods = RelocatedAddressOfPointer(orig_methods); |
| 572 | copy_dex_cache->SetField64<false>( |
| 573 | mirror::DexCache::ResolvedMethodsOffset(), |
| 574 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_methods))); |
| 575 | if (orig_methods != nullptr) { |
| 576 | ArtMethod** copy_methods = RelocatedCopyOf(orig_methods); |
| 577 | for (size_t j = 0, num = orig_dex_cache->NumResolvedMethods(); j != num; ++j) { |
| 578 | ArtMethod* orig = mirror::DexCache::GetElementPtrSize(orig_methods, j, pointer_size); |
| 579 | ArtMethod* copy = RelocatedAddressOfPointer(orig); |
| 580 | mirror::DexCache::SetElementPtrSize(copy_methods, j, copy, pointer_size); |
| 581 | } |
| 582 | } |
| 583 | ArtField** orig_fields = orig_dex_cache->GetResolvedFields(); |
| 584 | ArtField** relocated_fields = RelocatedAddressOfPointer(orig_fields); |
| 585 | copy_dex_cache->SetField64<false>( |
| 586 | mirror::DexCache::ResolvedFieldsOffset(), |
| 587 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_fields))); |
| 588 | if (orig_fields != nullptr) { |
| 589 | ArtField** copy_fields = RelocatedCopyOf(orig_fields); |
| 590 | for (size_t j = 0, num = orig_dex_cache->NumResolvedFields(); j != num; ++j) { |
| 591 | ArtField* orig = mirror::DexCache::GetElementPtrSize(orig_fields, j, pointer_size); |
| 592 | ArtField* copy = RelocatedAddressOfPointer(orig); |
| 593 | mirror::DexCache::SetElementPtrSize(copy_fields, j, copy, pointer_size); |
| 594 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 599 | bool PatchOat::PatchImage() { |
| 600 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
| 601 | CHECK_GT(image_->Size(), sizeof(ImageHeader)); |
| 602 | // These are the roots from the original file. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 603 | auto* img_roots = image_header->GetImageRoots(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 604 | image_header->RelocateImage(delta_); |
| 605 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 606 | PatchArtFields(image_header); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 607 | PatchArtMethods(image_header); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 608 | PatchInternedStrings(image_header); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 609 | // Patch dex file int/long arrays which point to ArtFields. |
| 610 | PatchDexFileArrays(img_roots); |
| 611 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 612 | VisitObject(img_roots); |
| 613 | if (!image_header->IsValid()) { |
| 614 | LOG(ERROR) << "reloction renders image header invalid"; |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 619 | TimingLogger::ScopedTiming t("Walk Bitmap", timings_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 620 | // Walk the bitmap. |
| 621 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 622 | bitmap_->Walk(PatchOat::BitmapCallback, this); |
| 623 | } |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | bool PatchOat::InHeap(mirror::Object* o) { |
| 628 | uintptr_t begin = reinterpret_cast<uintptr_t>(heap_->Begin()); |
| 629 | uintptr_t end = reinterpret_cast<uintptr_t>(heap_->End()); |
| 630 | uintptr_t obj = reinterpret_cast<uintptr_t>(o); |
| 631 | return o == nullptr || (begin <= obj && obj < end); |
| 632 | } |
| 633 | |
| 634 | void PatchOat::PatchVisitor::operator() (mirror::Object* obj, MemberOffset off, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 635 | bool is_static_unused ATTRIBUTE_UNUSED) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 636 | mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off); |
| 637 | DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap."; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 638 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 639 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 640 | } |
| 641 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 642 | void PatchOat::PatchVisitor::operator() (mirror::Class* cls ATTRIBUTE_UNUSED, |
| 643 | mirror::Reference* ref) const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 644 | MemberOffset off = mirror::Reference::ReferentOffset(); |
| 645 | mirror::Object* referent = ref->GetReferent(); |
| 646 | DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap."; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 647 | mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 648 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 649 | } |
| 650 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 651 | // Called by BitmapCallback |
| 652 | void PatchOat::VisitObject(mirror::Object* object) { |
| 653 | mirror::Object* copy = RelocatedCopyOf(object); |
| 654 | CHECK(copy != nullptr); |
| 655 | if (kUseBakerOrBrooksReadBarrier) { |
| 656 | object->AssertReadBarrierPointer(); |
| 657 | if (kUseBrooksReadBarrier) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 658 | mirror::Object* moved_to = RelocatedAddressOfPointer(object); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 659 | copy->SetReadBarrierPointer(moved_to); |
| 660 | DCHECK_EQ(copy->GetReadBarrierPointer(), moved_to); |
| 661 | } |
| 662 | } |
| 663 | PatchOat::PatchVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 664 | object->VisitReferences<kVerifyNone>(visitor, visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 665 | if (object->IsClass<kVerifyNone>()) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 666 | const size_t pointer_size = InstructionSetPointerSize(isa_); |
| 667 | mirror::Class* klass = object->AsClass(); |
| 668 | mirror::Class* copy_klass = down_cast<mirror::Class*>(copy); |
| 669 | RelocatedPointerVisitor native_visitor(this); |
| 670 | klass->FixupNativePointers(copy_klass, pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 671 | auto* vtable = klass->GetVTable(); |
| 672 | if (vtable != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 673 | vtable->Fixup(RelocatedCopyOf(vtable), pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 674 | } |
| 675 | auto* iftable = klass->GetIfTable(); |
| 676 | if (iftable != nullptr) { |
| 677 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 678 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 679 | auto* method_array = iftable->GetMethodArray(i); |
| 680 | CHECK(method_array != nullptr); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 681 | method_array->Fixup(RelocatedCopyOf(method_array), pointer_size, native_visitor); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 685 | } else if (object->GetClass() == mirror::Method::StaticClass() || |
| 686 | object->GetClass() == mirror::Constructor::StaticClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 687 | // Need to go update the ArtMethod. |
| 688 | auto* dest = down_cast<mirror::AbstractMethod*>(copy); |
| 689 | auto* src = down_cast<mirror::AbstractMethod*>(object); |
| 690 | dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod())); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 694 | void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 695 | const size_t pointer_size = InstructionSetPointerSize(isa_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 696 | copy->CopyFrom(object, pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 697 | // Just update the entry points if it looks like we should. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 698 | // TODO: sanity check all the pointers' values |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 699 | copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass())); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 700 | copy->SetDexCacheResolvedMethods( |
| 701 | RelocatedAddressOfPointer(object->GetDexCacheResolvedMethods(pointer_size)), pointer_size); |
| 702 | copy->SetDexCacheResolvedTypes( |
| 703 | RelocatedAddressOfPointer(object->GetDexCacheResolvedTypes(pointer_size)), pointer_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 704 | copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer( |
| 705 | object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 706 | copy->SetEntryPointFromJniPtrSize(RelocatedAddressOfPointer( |
| 707 | object->GetEntryPointFromJniPtrSize(pointer_size)), pointer_size); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 710 | bool PatchOat::Patch(File* input_oat, off_t delta, File* output_oat, TimingLogger* timings, |
| 711 | bool output_oat_opened_from_fd, bool new_oat_out) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 712 | CHECK(input_oat != nullptr); |
| 713 | CHECK(output_oat != nullptr); |
| 714 | CHECK_GE(input_oat->Fd(), 0); |
| 715 | CHECK_GE(output_oat->Fd(), 0); |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 716 | TimingLogger::ScopedTiming t("Setup Oat File Patching", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 717 | |
| 718 | std::string error_msg; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 719 | std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat, |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 720 | PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg)); |
| 721 | if (elf.get() == nullptr) { |
| 722 | LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg; |
| 723 | return false; |
| 724 | } |
| 725 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 726 | MaybePic is_oat_pic = IsOatPic(elf.get()); |
| 727 | if (is_oat_pic >= ERROR_FIRST) { |
| 728 | // Error logged by IsOatPic |
| 729 | return false; |
| 730 | } else if (is_oat_pic == PIC) { |
| 731 | // Do not need to do ELF-file patching. Create a symlink and skip the rest. |
| 732 | // Any errors will be logged by the function call. |
| 733 | return ReplaceOatFileWithSymlink(input_oat->GetPath(), |
| 734 | output_oat->GetPath(), |
| 735 | output_oat_opened_from_fd, |
| 736 | new_oat_out); |
| 737 | } else { |
| 738 | CHECK(is_oat_pic == NOT_PIC); |
| 739 | } |
| 740 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 741 | PatchOat p(elf.release(), delta, timings); |
| 742 | t.NewTiming("Patch Oat file"); |
| 743 | if (!p.PatchElf()) { |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | t.NewTiming("Writing oat file"); |
| 748 | if (!p.WriteElf(output_oat)) { |
| 749 | return false; |
| 750 | } |
| 751 | return true; |
| 752 | } |
| 753 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 754 | template <typename ElfFileImpl> |
| 755 | bool PatchOat::PatchOatHeader(ElfFileImpl* oat_file) { |
| 756 | auto rodata_sec = oat_file->FindSectionByName(".rodata"); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 757 | if (rodata_sec == nullptr) { |
| 758 | return false; |
| 759 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 760 | OatHeader* oat_header = reinterpret_cast<OatHeader*>(oat_file->Begin() + rodata_sec->sh_offset); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 761 | if (!oat_header->IsValid()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 762 | LOG(ERROR) << "Elf file " << oat_file->GetFile().GetPath() << " has an invalid oat header"; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 763 | return false; |
| 764 | } |
| 765 | oat_header->RelocateOat(delta_); |
| 766 | return true; |
| 767 | } |
| 768 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 769 | bool PatchOat::PatchElf() { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 770 | if (oat_file_->Is64Bit()) |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 771 | return PatchElf<ElfFileImpl64>(oat_file_->GetImpl64()); |
| 772 | else |
| 773 | return PatchElf<ElfFileImpl32>(oat_file_->GetImpl32()); |
| 774 | } |
| 775 | |
| 776 | template <typename ElfFileImpl> |
| 777 | bool PatchOat::PatchElf(ElfFileImpl* oat_file) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 778 | TimingLogger::ScopedTiming t("Fixup Elf Text Section", timings_); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 779 | |
| 780 | // Fix up absolute references to locations within the boot image. |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 781 | if (!oat_file->ApplyOatPatchesTo(".text", delta_)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 785 | // Update the OatHeader fields referencing the boot image. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 786 | if (!PatchOatHeader<ElfFileImpl>(oat_file)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 787 | return false; |
| 788 | } |
| 789 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 790 | bool need_boot_oat_fixup = true; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 791 | for (unsigned int i = 0; i < oat_file->GetProgramHeaderNum(); ++i) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 792 | auto hdr = oat_file->GetProgramHeader(i); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 793 | if (hdr->p_type == PT_LOAD && hdr->p_vaddr == 0u) { |
| 794 | need_boot_oat_fixup = false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 795 | break; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 796 | } |
| 797 | } |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 798 | if (!need_boot_oat_fixup) { |
| 799 | // This is an app oat file that can be loaded at an arbitrary address in memory. |
| 800 | // Boot image references were patched above and there's nothing else to do. |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 801 | return true; |
| 802 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 803 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 804 | // This is a boot oat file that's loaded at a particular address and we need |
| 805 | // to patch all absolute addresses, starting with ELF program headers. |
| 806 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 807 | t.NewTiming("Fixup Elf Headers"); |
| 808 | // Fixup Phdr's |
| 809 | oat_file->FixupProgramHeaders(delta_); |
| 810 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 811 | t.NewTiming("Fixup Section Headers"); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 812 | // Fixup Shdr's |
| 813 | oat_file->FixupSectionHeaders(delta_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 814 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 815 | t.NewTiming("Fixup Dynamics"); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 816 | oat_file->FixupDynamic(delta_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 817 | |
| 818 | t.NewTiming("Fixup Elf Symbols"); |
| 819 | // Fixup dynsym |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 820 | if (!oat_file->FixupSymbols(delta_, true)) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 821 | return false; |
| 822 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 823 | // Fixup symtab |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 824 | if (!oat_file->FixupSymbols(delta_, false)) { |
| 825 | return false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 828 | t.NewTiming("Fixup Debug Sections"); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 829 | if (!oat_file->FixupDebugSections(delta_)) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 830 | return false; |
| 831 | } |
| 832 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 833 | return true; |
| 834 | } |
| 835 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 836 | static int orig_argc; |
| 837 | static char** orig_argv; |
| 838 | |
| 839 | static std::string CommandLine() { |
| 840 | std::vector<std::string> command; |
| 841 | for (int i = 0; i < orig_argc; ++i) { |
| 842 | command.push_back(orig_argv[i]); |
| 843 | } |
| 844 | return Join(command, ' '); |
| 845 | } |
| 846 | |
| 847 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 848 | std::string error; |
| 849 | StringAppendV(&error, fmt, ap); |
| 850 | LOG(ERROR) << error; |
| 851 | } |
| 852 | |
| 853 | static void UsageError(const char* fmt, ...) { |
| 854 | va_list ap; |
| 855 | va_start(ap, fmt); |
| 856 | UsageErrorV(fmt, ap); |
| 857 | va_end(ap); |
| 858 | } |
| 859 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 860 | NO_RETURN static void Usage(const char *fmt, ...) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 861 | va_list ap; |
| 862 | va_start(ap, fmt); |
| 863 | UsageErrorV(fmt, ap); |
| 864 | va_end(ap); |
| 865 | |
| 866 | UsageError("Command: %s", CommandLine().c_str()); |
| 867 | UsageError("Usage: patchoat [options]..."); |
| 868 | UsageError(""); |
| 869 | UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is"); |
| 870 | UsageError(" compiled for. Required if you use --input-oat-location"); |
| 871 | UsageError(""); |
| 872 | UsageError(" --input-oat-file=<file.oat>: Specifies the exact filename of the oat file to be"); |
| 873 | UsageError(" patched."); |
| 874 | UsageError(""); |
| 875 | UsageError(" --input-oat-fd=<file-descriptor>: Specifies the file-descriptor of the oat file"); |
| 876 | UsageError(" to be patched."); |
| 877 | UsageError(""); |
| 878 | UsageError(" --input-oat-location=<file.oat>: Specifies the 'location' to read the patched"); |
| 879 | UsageError(" oat file from. If used one must also supply the --instruction-set"); |
| 880 | UsageError(""); |
| 881 | UsageError(" --input-image-location=<file.art>: Specifies the 'location' of the image file to"); |
| 882 | UsageError(" be patched. If --instruction-set is not given it will use the instruction set"); |
| 883 | UsageError(" extracted from the --input-oat-file."); |
| 884 | UsageError(""); |
| 885 | UsageError(" --output-oat-file=<file.oat>: Specifies the exact file to write the patched oat"); |
| 886 | UsageError(" file to."); |
| 887 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 888 | UsageError(" --output-oat-fd=<file-descriptor>: Specifies the file-descriptor to write the"); |
| 889 | UsageError(" the patched oat file to."); |
| 890 | UsageError(""); |
| 891 | UsageError(" --output-image-file=<file.art>: Specifies the exact file to write the patched"); |
| 892 | UsageError(" image file to."); |
| 893 | UsageError(""); |
| 894 | UsageError(" --output-image-fd=<file-descriptor>: Specifies the file-descriptor to write the"); |
| 895 | UsageError(" the patched image file to."); |
| 896 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 897 | UsageError(" --orig-base-offset=<original-base-offset>: Specify the base offset the input file"); |
| 898 | UsageError(" was compiled with. This is needed if one is specifying a --base-offset"); |
| 899 | UsageError(""); |
| 900 | UsageError(" --base-offset=<new-base-offset>: Specify the base offset we will repatch the"); |
| 901 | UsageError(" given files to use. This requires that --orig-base-offset is also given."); |
| 902 | UsageError(""); |
| 903 | UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by."); |
| 904 | UsageError(" This value may be negative."); |
| 905 | UsageError(""); |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 906 | UsageError(" --patched-image-file=<file.art>: Relocate the oat file to be the same as the"); |
| 907 | UsageError(" given image file."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 908 | UsageError(""); |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 909 | UsageError(" --patched-image-location=<file.art>: Relocate the oat file to be the same as the"); |
| 910 | UsageError(" image at the given location. If used one must also specify the"); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 911 | UsageError(" --instruction-set flag. It will search for this image in the same way that"); |
| 912 | UsageError(" is done when loading one."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 913 | UsageError(""); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 914 | UsageError(" --lock-output: Obtain a flock on output oat file before starting."); |
| 915 | UsageError(""); |
| 916 | UsageError(" --no-lock-output: Do not attempt to obtain a flock on output oat file."); |
| 917 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 918 | UsageError(" --dump-timings: dump out patch timing information"); |
| 919 | UsageError(""); |
| 920 | UsageError(" --no-dump-timings: do not dump out patch timing information"); |
| 921 | UsageError(""); |
| 922 | |
| 923 | exit(EXIT_FAILURE); |
| 924 | } |
| 925 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 926 | static bool ReadBaseDelta(const char* name, off_t* delta, std::string* error_msg) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 927 | CHECK(name != nullptr); |
| 928 | CHECK(delta != nullptr); |
| 929 | std::unique_ptr<File> file; |
| 930 | if (OS::FileExists(name)) { |
| 931 | file.reset(OS::OpenFileForReading(name)); |
| 932 | if (file.get() == nullptr) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 933 | *error_msg = "Failed to open file %s for reading"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 934 | return false; |
| 935 | } |
| 936 | } else { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 937 | *error_msg = "File %s does not exist"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 938 | return false; |
| 939 | } |
| 940 | CHECK(file.get() != nullptr); |
| 941 | ImageHeader hdr; |
| 942 | if (sizeof(hdr) != file->Read(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0)) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 943 | *error_msg = "Failed to read file %s"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 944 | return false; |
| 945 | } |
| 946 | if (!hdr.IsValid()) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 947 | *error_msg = "%s does not contain a valid image header."; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 948 | return false; |
| 949 | } |
| 950 | *delta = hdr.GetPatchDelta(); |
| 951 | return true; |
| 952 | } |
| 953 | |
| 954 | static File* CreateOrOpen(const char* name, bool* created) { |
| 955 | if (OS::FileExists(name)) { |
| 956 | *created = false; |
| 957 | return OS::OpenFileReadWrite(name); |
| 958 | } else { |
| 959 | *created = true; |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 960 | std::unique_ptr<File> f(OS::CreateEmptyFile(name)); |
| 961 | if (f.get() != nullptr) { |
| 962 | if (fchmod(f->Fd(), 0644) != 0) { |
| 963 | PLOG(ERROR) << "Unable to make " << name << " world readable"; |
Brian Carlstrom | 8c52a3f | 2014-09-30 16:18:01 -0700 | [diff] [blame] | 964 | TEMP_FAILURE_RETRY(unlink(name)); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 965 | return nullptr; |
| 966 | } |
| 967 | } |
| 968 | return f.release(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 972 | // Either try to close the file (close=true), or erase it. |
| 973 | static bool FinishFile(File* file, bool close) { |
| 974 | if (close) { |
| 975 | if (file->FlushCloseOrErase() != 0) { |
| 976 | PLOG(ERROR) << "Failed to flush and close file."; |
| 977 | return false; |
| 978 | } |
| 979 | return true; |
| 980 | } else { |
| 981 | file->Erase(); |
| 982 | return false; |
| 983 | } |
| 984 | } |
| 985 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 986 | static int patchoat(int argc, char **argv) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 987 | InitLogging(argv); |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 988 | MemMap::Init(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 989 | const bool debug = kIsDebugBuild; |
| 990 | orig_argc = argc; |
| 991 | orig_argv = argv; |
| 992 | TimingLogger timings("patcher", false, false); |
| 993 | |
| 994 | InitLogging(argv); |
| 995 | |
| 996 | // Skip over the command name. |
| 997 | argv++; |
| 998 | argc--; |
| 999 | |
| 1000 | if (argc == 0) { |
| 1001 | Usage("No arguments specified"); |
| 1002 | } |
| 1003 | |
| 1004 | timings.StartTiming("Patchoat"); |
| 1005 | |
| 1006 | // cmd line args |
| 1007 | bool isa_set = false; |
| 1008 | InstructionSet isa = kNone; |
| 1009 | std::string input_oat_filename; |
| 1010 | std::string input_oat_location; |
| 1011 | int input_oat_fd = -1; |
| 1012 | bool have_input_oat = false; |
| 1013 | std::string input_image_location; |
| 1014 | std::string output_oat_filename; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1015 | int output_oat_fd = -1; |
| 1016 | bool have_output_oat = false; |
| 1017 | std::string output_image_filename; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1018 | int output_image_fd = -1; |
| 1019 | bool have_output_image = false; |
| 1020 | uintptr_t base_offset = 0; |
| 1021 | bool base_offset_set = false; |
| 1022 | uintptr_t orig_base_offset = 0; |
| 1023 | bool orig_base_offset_set = false; |
| 1024 | off_t base_delta = 0; |
| 1025 | bool base_delta_set = false; |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1026 | bool match_delta = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1027 | std::string patched_image_filename; |
| 1028 | std::string patched_image_location; |
| 1029 | bool dump_timings = kIsDebugBuild; |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1030 | bool lock_output = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1031 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1032 | for (int i = 0; i < argc; ++i) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1033 | const StringPiece option(argv[i]); |
| 1034 | const bool log_options = false; |
| 1035 | if (log_options) { |
| 1036 | LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i]; |
| 1037 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1038 | if (option.starts_with("--instruction-set=")) { |
| 1039 | isa_set = true; |
| 1040 | const char* isa_str = option.substr(strlen("--instruction-set=")).data(); |
Andreas Gampe | 20c8930 | 2014-08-19 17:28:06 -0700 | [diff] [blame] | 1041 | isa = GetInstructionSetFromString(isa_str); |
| 1042 | if (isa == kNone) { |
| 1043 | Usage("Unknown or invalid instruction set %s", isa_str); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } else if (option.starts_with("--input-oat-location=")) { |
| 1046 | if (have_input_oat) { |
| 1047 | Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used."); |
| 1048 | } |
| 1049 | have_input_oat = true; |
| 1050 | input_oat_location = option.substr(strlen("--input-oat-location=")).data(); |
| 1051 | } else if (option.starts_with("--input-oat-file=")) { |
| 1052 | if (have_input_oat) { |
| 1053 | Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used."); |
| 1054 | } |
| 1055 | have_input_oat = true; |
| 1056 | input_oat_filename = option.substr(strlen("--input-oat-file=")).data(); |
| 1057 | } else if (option.starts_with("--input-oat-fd=")) { |
| 1058 | if (have_input_oat) { |
| 1059 | Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used."); |
| 1060 | } |
| 1061 | have_input_oat = true; |
| 1062 | const char* oat_fd_str = option.substr(strlen("--input-oat-fd=")).data(); |
| 1063 | if (!ParseInt(oat_fd_str, &input_oat_fd)) { |
| 1064 | Usage("Failed to parse --input-oat-fd argument '%s' as an integer", oat_fd_str); |
| 1065 | } |
| 1066 | if (input_oat_fd < 0) { |
| 1067 | Usage("--input-oat-fd pass a negative value %d", input_oat_fd); |
| 1068 | } |
| 1069 | } else if (option.starts_with("--input-image-location=")) { |
| 1070 | input_image_location = option.substr(strlen("--input-image-location=")).data(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1071 | } else if (option.starts_with("--output-oat-file=")) { |
| 1072 | if (have_output_oat) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1073 | Usage("Only one of --output-oat-file, and --output-oat-fd may be used."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1074 | } |
| 1075 | have_output_oat = true; |
| 1076 | output_oat_filename = option.substr(strlen("--output-oat-file=")).data(); |
| 1077 | } else if (option.starts_with("--output-oat-fd=")) { |
| 1078 | if (have_output_oat) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1079 | Usage("Only one of --output-oat-file, --output-oat-fd may be used."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1080 | } |
| 1081 | have_output_oat = true; |
| 1082 | const char* oat_fd_str = option.substr(strlen("--output-oat-fd=")).data(); |
| 1083 | if (!ParseInt(oat_fd_str, &output_oat_fd)) { |
| 1084 | Usage("Failed to parse --output-oat-fd argument '%s' as an integer", oat_fd_str); |
| 1085 | } |
| 1086 | if (output_oat_fd < 0) { |
| 1087 | Usage("--output-oat-fd pass a negative value %d", output_oat_fd); |
| 1088 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1089 | } else if (option.starts_with("--output-image-file=")) { |
| 1090 | if (have_output_image) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1091 | Usage("Only one of --output-image-file, and --output-image-fd may be used."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1092 | } |
| 1093 | have_output_image = true; |
| 1094 | output_image_filename = option.substr(strlen("--output-image-file=")).data(); |
| 1095 | } else if (option.starts_with("--output-image-fd=")) { |
| 1096 | if (have_output_image) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1097 | Usage("Only one of --output-image-file, and --output-image-fd may be used."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1098 | } |
| 1099 | have_output_image = true; |
| 1100 | const char* image_fd_str = option.substr(strlen("--output-image-fd=")).data(); |
| 1101 | if (!ParseInt(image_fd_str, &output_image_fd)) { |
| 1102 | Usage("Failed to parse --output-image-fd argument '%s' as an integer", image_fd_str); |
| 1103 | } |
| 1104 | if (output_image_fd < 0) { |
| 1105 | Usage("--output-image-fd pass a negative value %d", output_image_fd); |
| 1106 | } |
| 1107 | } else if (option.starts_with("--orig-base-offset=")) { |
| 1108 | const char* orig_base_offset_str = option.substr(strlen("--orig-base-offset=")).data(); |
| 1109 | orig_base_offset_set = true; |
| 1110 | if (!ParseUint(orig_base_offset_str, &orig_base_offset)) { |
| 1111 | Usage("Failed to parse --orig-base-offset argument '%s' as an uintptr_t", |
| 1112 | orig_base_offset_str); |
| 1113 | } |
| 1114 | } else if (option.starts_with("--base-offset=")) { |
| 1115 | const char* base_offset_str = option.substr(strlen("--base-offset=")).data(); |
| 1116 | base_offset_set = true; |
| 1117 | if (!ParseUint(base_offset_str, &base_offset)) { |
| 1118 | Usage("Failed to parse --base-offset argument '%s' as an uintptr_t", base_offset_str); |
| 1119 | } |
| 1120 | } else if (option.starts_with("--base-offset-delta=")) { |
| 1121 | const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data(); |
| 1122 | base_delta_set = true; |
| 1123 | if (!ParseInt(base_delta_str, &base_delta)) { |
| 1124 | Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str); |
| 1125 | } |
| 1126 | } else if (option.starts_with("--patched-image-location=")) { |
| 1127 | patched_image_location = option.substr(strlen("--patched-image-location=")).data(); |
| 1128 | } else if (option.starts_with("--patched-image-file=")) { |
| 1129 | patched_image_filename = option.substr(strlen("--patched-image-file=")).data(); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1130 | } else if (option == "--lock-output") { |
| 1131 | lock_output = true; |
| 1132 | } else if (option == "--no-lock-output") { |
| 1133 | lock_output = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1134 | } else if (option == "--dump-timings") { |
| 1135 | dump_timings = true; |
| 1136 | } else if (option == "--no-dump-timings") { |
| 1137 | dump_timings = false; |
| 1138 | } else { |
| 1139 | Usage("Unknown argument %s", option.data()); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | { |
| 1144 | // Only 1 of these may be set. |
| 1145 | uint32_t cnt = 0; |
| 1146 | cnt += (base_delta_set) ? 1 : 0; |
| 1147 | cnt += (base_offset_set && orig_base_offset_set) ? 1 : 0; |
| 1148 | cnt += (!patched_image_filename.empty()) ? 1 : 0; |
| 1149 | cnt += (!patched_image_location.empty()) ? 1 : 0; |
| 1150 | if (cnt > 1) { |
| 1151 | Usage("Only one of --base-offset/--orig-base-offset, --base-offset-delta, " |
| 1152 | "--patched-image-filename or --patched-image-location may be used."); |
| 1153 | } else if (cnt == 0) { |
| 1154 | Usage("Must specify --base-offset-delta, --base-offset and --orig-base-offset, " |
| 1155 | "--patched-image-location or --patched-image-file"); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | if (have_input_oat != have_output_oat) { |
| 1160 | Usage("Either both input and output oat must be supplied or niether must be."); |
| 1161 | } |
| 1162 | |
| 1163 | if ((!input_image_location.empty()) != have_output_image) { |
| 1164 | Usage("Either both input and output image must be supplied or niether must be."); |
| 1165 | } |
| 1166 | |
| 1167 | // We know we have both the input and output so rename for clarity. |
| 1168 | bool have_image_files = have_output_image; |
| 1169 | bool have_oat_files = have_output_oat; |
| 1170 | |
| 1171 | if (!have_oat_files && !have_image_files) { |
| 1172 | Usage("Must be patching either an oat or an image file or both."); |
| 1173 | } |
| 1174 | |
| 1175 | if (!have_oat_files && !isa_set) { |
| 1176 | Usage("Must include ISA if patching an image file without an oat file."); |
| 1177 | } |
| 1178 | |
| 1179 | if (!input_oat_location.empty()) { |
| 1180 | if (!isa_set) { |
| 1181 | Usage("specifying a location requires specifying an instruction set"); |
| 1182 | } |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1183 | if (!LocationToFilename(input_oat_location, isa, &input_oat_filename)) { |
| 1184 | Usage("Unable to find filename for input oat location %s", input_oat_location.c_str()); |
| 1185 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1186 | if (debug) { |
| 1187 | LOG(INFO) << "Using input-oat-file " << input_oat_filename; |
| 1188 | } |
| 1189 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1190 | if (!patched_image_location.empty()) { |
| 1191 | if (!isa_set) { |
| 1192 | Usage("specifying a location requires specifying an instruction set"); |
| 1193 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1194 | std::string system_filename; |
| 1195 | bool has_system = false; |
| 1196 | std::string cache_filename; |
| 1197 | bool has_cache = false; |
| 1198 | bool has_android_data_unused = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1199 | bool is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1200 | if (!gc::space::ImageSpace::FindImageFilename(patched_image_location.c_str(), isa, |
| 1201 | &system_filename, &has_system, &cache_filename, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1202 | &has_android_data_unused, &has_cache, |
| 1203 | &is_global_cache)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1204 | Usage("Unable to determine image file for location %s", patched_image_location.c_str()); |
| 1205 | } |
| 1206 | if (has_cache) { |
| 1207 | patched_image_filename = cache_filename; |
| 1208 | } else if (has_system) { |
| 1209 | LOG(WARNING) << "Only image file found was in /system for image location " |
| 1210 | << patched_image_location; |
| 1211 | patched_image_filename = system_filename; |
| 1212 | } else { |
| 1213 | Usage("Unable to determine image file for location %s", patched_image_location.c_str()); |
| 1214 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1215 | if (debug) { |
| 1216 | LOG(INFO) << "Using patched-image-file " << patched_image_filename; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | if (!base_delta_set) { |
| 1221 | if (orig_base_offset_set && base_offset_set) { |
| 1222 | base_delta_set = true; |
| 1223 | base_delta = base_offset - orig_base_offset; |
| 1224 | } else if (!patched_image_filename.empty()) { |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1225 | if (have_image_files) { |
| 1226 | Usage("--patched-image-location should not be used when patching other images"); |
| 1227 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1228 | base_delta_set = true; |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1229 | match_delta = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1230 | std::string error_msg; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1231 | if (!ReadBaseDelta(patched_image_filename.c_str(), &base_delta, &error_msg)) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1232 | Usage(error_msg.c_str(), patched_image_filename.c_str()); |
| 1233 | } |
| 1234 | } else { |
| 1235 | if (base_offset_set) { |
| 1236 | Usage("Unable to determine original base offset."); |
| 1237 | } else { |
| 1238 | Usage("Must supply a desired new offset or delta."); |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | if (!IsAligned<kPageSize>(base_delta)) { |
| 1244 | Usage("Base offset/delta must be alligned to a pagesize (0x%08x) boundary.", kPageSize); |
| 1245 | } |
| 1246 | |
| 1247 | // Do we need to cleanup output files if we fail? |
| 1248 | bool new_image_out = false; |
| 1249 | bool new_oat_out = false; |
| 1250 | |
| 1251 | std::unique_ptr<File> input_oat; |
| 1252 | std::unique_ptr<File> output_oat; |
| 1253 | std::unique_ptr<File> output_image; |
| 1254 | |
| 1255 | if (have_image_files) { |
| 1256 | CHECK(!input_image_location.empty()); |
| 1257 | |
| 1258 | if (output_image_fd != -1) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1259 | if (output_image_filename.empty()) { |
| 1260 | output_image_filename = "output-image-file"; |
| 1261 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1262 | output_image.reset(new File(output_image_fd, output_image_filename, true)); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1263 | } else { |
| 1264 | CHECK(!output_image_filename.empty()); |
| 1265 | output_image.reset(CreateOrOpen(output_image_filename.c_str(), &new_image_out)); |
| 1266 | } |
| 1267 | } else { |
| 1268 | CHECK(output_image_filename.empty() && output_image_fd == -1 && input_image_location.empty()); |
| 1269 | } |
| 1270 | |
| 1271 | if (have_oat_files) { |
| 1272 | if (input_oat_fd != -1) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1273 | if (input_oat_filename.empty()) { |
| 1274 | input_oat_filename = "input-oat-file"; |
| 1275 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1276 | input_oat.reset(new File(input_oat_fd, input_oat_filename, false)); |
Julien Delayen | a473f51 | 2015-03-05 16:37:52 +0100 | [diff] [blame] | 1277 | if (input_oat_fd == output_oat_fd) { |
| 1278 | input_oat.get()->DisableAutoClose(); |
| 1279 | } |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1280 | if (input_oat == nullptr) { |
| 1281 | // Unlikely, but ensure exhaustive logging in non-0 exit code case |
| 1282 | LOG(ERROR) << "Failed to open input oat file by its FD" << input_oat_fd; |
| 1283 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1284 | } else { |
| 1285 | CHECK(!input_oat_filename.empty()); |
| 1286 | input_oat.reset(OS::OpenFileForReading(input_oat_filename.c_str())); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1287 | if (input_oat == nullptr) { |
| 1288 | int err = errno; |
| 1289 | LOG(ERROR) << "Failed to open input oat file " << input_oat_filename |
| 1290 | << ": " << strerror(err) << "(" << err << ")"; |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 1291 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | if (output_oat_fd != -1) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1295 | if (output_oat_filename.empty()) { |
| 1296 | output_oat_filename = "output-oat-file"; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1297 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1298 | output_oat.reset(new File(output_oat_fd, output_oat_filename, true)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1299 | if (output_oat == nullptr) { |
| 1300 | // Unlikely, but ensure exhaustive logging in non-0 exit code case |
| 1301 | LOG(ERROR) << "Failed to open output oat file by its FD" << output_oat_fd; |
| 1302 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1303 | } else { |
| 1304 | CHECK(!output_oat_filename.empty()); |
| 1305 | output_oat.reset(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1306 | if (output_oat == nullptr) { |
| 1307 | int err = errno; |
| 1308 | LOG(ERROR) << "Failed to open output oat file " << output_oat_filename |
| 1309 | << ": " << strerror(err) << "(" << err << ")"; |
| 1310 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1314 | // TODO: get rid of this. |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1315 | auto cleanup = [&output_image_filename, &output_oat_filename, |
| 1316 | &new_oat_out, &new_image_out, &timings, &dump_timings](bool success) { |
| 1317 | timings.EndTiming(); |
| 1318 | if (!success) { |
| 1319 | if (new_oat_out) { |
| 1320 | CHECK(!output_oat_filename.empty()); |
Brian Carlstrom | 8c52a3f | 2014-09-30 16:18:01 -0700 | [diff] [blame] | 1321 | TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str())); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1322 | } |
| 1323 | if (new_image_out) { |
| 1324 | CHECK(!output_image_filename.empty()); |
Brian Carlstrom | 8c52a3f | 2014-09-30 16:18:01 -0700 | [diff] [blame] | 1325 | TEMP_FAILURE_RETRY(unlink(output_image_filename.c_str())); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | if (dump_timings) { |
| 1329 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
| 1330 | } |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1331 | |
| 1332 | if (kIsDebugBuild) { |
| 1333 | LOG(INFO) << "Cleaning up.. success? " << success; |
| 1334 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1335 | }; |
| 1336 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1337 | if (have_oat_files && (input_oat.get() == nullptr || output_oat.get() == nullptr)) { |
| 1338 | LOG(ERROR) << "Failed to open input/output oat files"; |
| 1339 | cleanup(false); |
| 1340 | return EXIT_FAILURE; |
| 1341 | } else if (have_image_files && output_image.get() == nullptr) { |
| 1342 | LOG(ERROR) << "Failed to open output image file"; |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1343 | cleanup(false); |
| 1344 | return EXIT_FAILURE; |
| 1345 | } |
| 1346 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1347 | if (match_delta) { |
| 1348 | CHECK(!have_image_files); // We will not do this with images. |
| 1349 | std::string error_msg; |
| 1350 | // Figure out what the current delta is so we can match it to the desired delta. |
| 1351 | std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat.get(), PROT_READ, MAP_PRIVATE, |
| 1352 | &error_msg)); |
| 1353 | off_t current_delta = 0; |
| 1354 | if (elf.get() == nullptr) { |
| 1355 | LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg; |
| 1356 | cleanup(false); |
| 1357 | return EXIT_FAILURE; |
| 1358 | } else if (!ReadOatPatchDelta(elf.get(), ¤t_delta, &error_msg)) { |
| 1359 | LOG(ERROR) << "Unable to get current delta: " << error_msg; |
| 1360 | cleanup(false); |
| 1361 | return EXIT_FAILURE; |
| 1362 | } |
| 1363 | // Before this line base_delta is the desired final delta. We need it to be the actual amount to |
| 1364 | // change everything by. We subtract the current delta from it to make it this. |
| 1365 | base_delta -= current_delta; |
| 1366 | if (!IsAligned<kPageSize>(base_delta)) { |
| 1367 | LOG(ERROR) << "Given image file was relocated by an illegal delta"; |
| 1368 | cleanup(false); |
| 1369 | return false; |
| 1370 | } |
| 1371 | } |
| 1372 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1373 | if (debug) { |
| 1374 | LOG(INFO) << "moving offset by " << base_delta |
| 1375 | << " (0x" << std::hex << base_delta << ") bytes or " |
| 1376 | << std::dec << (base_delta/kPageSize) << " pages."; |
| 1377 | } |
| 1378 | |
| 1379 | // TODO: is it going to be promatic to unlink a file that was flock-ed? |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1380 | ScopedFlock output_oat_lock; |
| 1381 | if (lock_output) { |
| 1382 | std::string error_msg; |
| 1383 | if (have_oat_files && !output_oat_lock.Init(output_oat.get(), &error_msg)) { |
| 1384 | LOG(ERROR) << "Unable to lock output oat " << output_image->GetPath() << ": " << error_msg; |
| 1385 | cleanup(false); |
| 1386 | return EXIT_FAILURE; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1390 | bool ret; |
| 1391 | if (have_image_files && have_oat_files) { |
| 1392 | TimingLogger::ScopedTiming pt("patch image and oat", &timings); |
| 1393 | ret = PatchOat::Patch(input_oat.get(), input_image_location, base_delta, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1394 | output_oat.get(), output_image.get(), isa, &timings, |
| 1395 | output_oat_fd >= 0, // was it opened from FD? |
| 1396 | new_oat_out); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1397 | // The order here doesn't matter. If the first one is successfully saved and the second one |
| 1398 | // erased, ImageSpace will still detect a problem and not use the files. |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1399 | ret = FinishFile(output_image.get(), ret); |
| 1400 | ret = FinishFile(output_oat.get(), ret); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1401 | } else if (have_oat_files) { |
| 1402 | TimingLogger::ScopedTiming pt("patch oat", &timings); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1403 | ret = PatchOat::Patch(input_oat.get(), base_delta, output_oat.get(), &timings, |
| 1404 | output_oat_fd >= 0, // was it opened from FD? |
| 1405 | new_oat_out); |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1406 | ret = FinishFile(output_oat.get(), ret); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1407 | } else if (have_image_files) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1408 | TimingLogger::ScopedTiming pt("patch image", &timings); |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1409 | ret = PatchOat::Patch(input_image_location, base_delta, output_image.get(), isa, &timings); |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1410 | ret = FinishFile(output_image.get(), ret); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1411 | } else { |
| 1412 | CHECK(false); |
| 1413 | ret = true; |
| 1414 | } |
| 1415 | |
| 1416 | if (kIsDebugBuild) { |
| 1417 | LOG(INFO) << "Exiting with return ... " << ret; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1418 | } |
| 1419 | cleanup(ret); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1420 | return (ret) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1421 | } |
| 1422 | |
| 1423 | } // namespace art |
| 1424 | |
| 1425 | int main(int argc, char **argv) { |
| 1426 | return art::patchoat(argc, argv); |
| 1427 | } |