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 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 27 | #include "base/scoped_flock.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 28 | #include "base/stringpiece.h" |
| 29 | #include "base/stringprintf.h" |
| 30 | #include "elf_utils.h" |
| 31 | #include "elf_file.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 32 | #include "gc/space/image_space.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 33 | #include "image.h" |
| 34 | #include "instruction_set.h" |
| 35 | #include "mirror/art_field.h" |
| 36 | #include "mirror/art_field-inl.h" |
| 37 | #include "mirror/art_method.h" |
| 38 | #include "mirror/art_method-inl.h" |
| 39 | #include "mirror/object.h" |
| 40 | #include "mirror/object-inl.h" |
| 41 | #include "mirror/reference.h" |
| 42 | #include "noop_compiler_callbacks.h" |
| 43 | #include "offsets.h" |
| 44 | #include "os.h" |
| 45 | #include "runtime.h" |
| 46 | #include "scoped_thread_state_change.h" |
| 47 | #include "thread.h" |
| 48 | #include "utils.h" |
| 49 | |
| 50 | namespace art { |
| 51 | |
| 52 | static InstructionSet ElfISAToInstructionSet(Elf32_Word isa) { |
| 53 | switch (isa) { |
| 54 | case EM_ARM: |
| 55 | return kArm; |
| 56 | case EM_AARCH64: |
| 57 | return kArm64; |
| 58 | case EM_386: |
| 59 | return kX86; |
| 60 | case EM_X86_64: |
| 61 | return kX86_64; |
| 62 | case EM_MIPS: |
| 63 | return kMips; |
| 64 | default: |
| 65 | return kNone; |
| 66 | } |
| 67 | } |
| 68 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 69 | static bool LocationToFilename(const std::string& location, InstructionSet isa, |
| 70 | std::string* filename) { |
| 71 | bool has_system = false; |
| 72 | bool has_cache = false; |
| 73 | // image_location = /system/framework/boot.art |
| 74 | // system_image_location = /system/framework/<image_isa>/boot.art |
| 75 | std::string system_filename(GetSystemImageFilename(location.c_str(), isa)); |
| 76 | if (OS::FileExists(system_filename.c_str())) { |
| 77 | has_system = true; |
| 78 | } |
| 79 | |
| 80 | bool have_android_data = false; |
| 81 | bool dalvik_cache_exists = false; |
| 82 | std::string dalvik_cache; |
| 83 | GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache, |
| 84 | &have_android_data, &dalvik_cache_exists); |
| 85 | |
| 86 | std::string cache_filename; |
| 87 | if (have_android_data && dalvik_cache_exists) { |
| 88 | // Always set output location even if it does not exist, |
| 89 | // so that the caller knows where to create the image. |
| 90 | // |
| 91 | // image_location = /system/framework/boot.art |
| 92 | // *image_filename = /data/dalvik-cache/<image_isa>/boot.art |
| 93 | std::string error_msg; |
| 94 | if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(), |
| 95 | &cache_filename, &error_msg)) { |
| 96 | has_cache = true; |
| 97 | } |
| 98 | } |
| 99 | if (has_system) { |
| 100 | *filename = system_filename; |
| 101 | return true; |
| 102 | } else if (has_cache) { |
| 103 | *filename = cache_filename; |
| 104 | return true; |
| 105 | } else { |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 110 | bool PatchOat::Patch(const std::string& image_location, off_t delta, |
| 111 | File* output_image, InstructionSet isa, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 112 | TimingLogger* timings) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 113 | CHECK(Runtime::Current() == nullptr); |
| 114 | CHECK(output_image != nullptr); |
| 115 | CHECK_GE(output_image->Fd(), 0); |
| 116 | CHECK(!image_location.empty()) << "image file must have a filename."; |
| 117 | CHECK_NE(isa, kNone); |
| 118 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 119 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 120 | const char *isa_name = GetInstructionSetString(isa); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 121 | std::string image_filename; |
| 122 | if (!LocationToFilename(image_location, isa, &image_filename)) { |
| 123 | LOG(ERROR) << "Unable to find image at location " << image_location; |
| 124 | return false; |
| 125 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 126 | std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str())); |
| 127 | if (input_image.get() == nullptr) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 128 | LOG(ERROR) << "unable to open input image file at " << image_filename |
| 129 | << " for location " << image_location; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | int64_t image_len = input_image->GetLength(); |
| 133 | if (image_len < 0) { |
| 134 | LOG(ERROR) << "Error while getting image length"; |
| 135 | return false; |
| 136 | } |
| 137 | ImageHeader image_header; |
| 138 | if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header), |
| 139 | sizeof(image_header), 0)) { |
| 140 | LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath(); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | // Set up the runtime |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 145 | RuntimeOptions options; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 146 | NoopCompilerCallbacks callbacks; |
| 147 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
| 148 | std::string img = "-Ximage:" + image_location; |
| 149 | options.push_back(std::make_pair(img.c_str(), nullptr)); |
| 150 | options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name))); |
| 151 | if (!Runtime::Create(options, false)) { |
| 152 | LOG(ERROR) << "Unable to initialize runtime"; |
| 153 | return false; |
| 154 | } |
| 155 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 156 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 157 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 158 | ScopedObjectAccess soa(Thread::Current()); |
| 159 | |
| 160 | t.NewTiming("Image and oat Patching setup"); |
| 161 | // Create the map where we will write the image patches to. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 162 | std::string error_msg; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 163 | std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE, |
| 164 | input_image->Fd(), 0, |
| 165 | input_image->GetPath().c_str(), |
| 166 | &error_msg)); |
| 167 | if (image.get() == nullptr) { |
| 168 | LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg; |
| 169 | return false; |
| 170 | } |
| 171 | gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 172 | |
| 173 | PatchOat p(image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(), |
| 174 | delta, timings); |
| 175 | t.NewTiming("Patching files"); |
| 176 | if (!p.PatchImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 177 | LOG(ERROR) << "Failed to patch image file " << input_image->GetPath(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
| 181 | t.NewTiming("Writing files"); |
| 182 | if (!p.WriteImage(output_image)) { |
| 183 | return false; |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | bool PatchOat::Patch(const File* input_oat, const std::string& image_location, off_t delta, |
| 189 | File* output_oat, File* output_image, InstructionSet isa, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 190 | TimingLogger* timings) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 191 | CHECK(Runtime::Current() == nullptr); |
| 192 | CHECK(output_image != nullptr); |
| 193 | CHECK_GE(output_image->Fd(), 0); |
| 194 | CHECK(input_oat != nullptr); |
| 195 | CHECK(output_oat != nullptr); |
| 196 | CHECK_GE(input_oat->Fd(), 0); |
| 197 | CHECK_GE(output_oat->Fd(), 0); |
| 198 | CHECK(!image_location.empty()) << "image file must have a filename."; |
| 199 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 200 | TimingLogger::ScopedTiming t("Runtime Setup", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 201 | |
| 202 | if (isa == kNone) { |
| 203 | Elf32_Ehdr elf_hdr; |
| 204 | if (sizeof(elf_hdr) != input_oat->Read(reinterpret_cast<char*>(&elf_hdr), sizeof(elf_hdr), 0)) { |
| 205 | LOG(ERROR) << "unable to read elf header"; |
| 206 | return false; |
| 207 | } |
| 208 | isa = ElfISAToInstructionSet(elf_hdr.e_machine); |
| 209 | } |
| 210 | const char* isa_name = GetInstructionSetString(isa); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 211 | std::string image_filename; |
| 212 | if (!LocationToFilename(image_location, isa, &image_filename)) { |
| 213 | LOG(ERROR) << "Unable to find image at location " << image_location; |
| 214 | return false; |
| 215 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 216 | std::unique_ptr<File> input_image(OS::OpenFileForReading(image_filename.c_str())); |
| 217 | if (input_image.get() == nullptr) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 218 | LOG(ERROR) << "unable to open input image file at " << image_filename |
| 219 | << " for location " << image_location; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | int64_t image_len = input_image->GetLength(); |
| 223 | if (image_len < 0) { |
| 224 | LOG(ERROR) << "Error while getting image length"; |
| 225 | return false; |
| 226 | } |
| 227 | ImageHeader image_header; |
| 228 | if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header), |
| 229 | sizeof(image_header), 0)) { |
| 230 | LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath(); |
| 231 | } |
| 232 | |
| 233 | // Set up the runtime |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 234 | RuntimeOptions options; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 235 | NoopCompilerCallbacks callbacks; |
| 236 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
| 237 | std::string img = "-Ximage:" + image_location; |
| 238 | options.push_back(std::make_pair(img.c_str(), nullptr)); |
| 239 | options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name))); |
| 240 | if (!Runtime::Create(options, false)) { |
| 241 | LOG(ERROR) << "Unable to initialize runtime"; |
| 242 | return false; |
| 243 | } |
| 244 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 245 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
| 246 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 247 | ScopedObjectAccess soa(Thread::Current()); |
| 248 | |
| 249 | t.NewTiming("Image and oat Patching setup"); |
| 250 | // Create the map where we will write the image patches to. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 251 | std::string error_msg; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 252 | std::unique_ptr<MemMap> image(MemMap::MapFile(image_len, PROT_READ | PROT_WRITE, MAP_PRIVATE, |
| 253 | input_image->Fd(), 0, |
| 254 | input_image->GetPath().c_str(), |
| 255 | &error_msg)); |
| 256 | if (image.get() == nullptr) { |
| 257 | LOG(ERROR) << "unable to map image file " << input_image->GetPath() << " : " << error_msg; |
| 258 | return false; |
| 259 | } |
| 260 | gc::space::ImageSpace* ispc = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 261 | |
| 262 | std::unique_ptr<ElfFile> elf(ElfFile::Open(const_cast<File*>(input_oat), |
| 263 | PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg)); |
| 264 | if (elf.get() == nullptr) { |
| 265 | LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg; |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | PatchOat p(elf.release(), image.release(), ispc->GetLiveBitmap(), ispc->GetMemMap(), |
| 270 | delta, timings); |
| 271 | t.NewTiming("Patching files"); |
| 272 | if (!p.PatchElf()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 273 | LOG(ERROR) << "Failed to patch oat file " << input_oat->GetPath(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | if (!p.PatchImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 277 | LOG(ERROR) << "Failed to patch image file " << input_image->GetPath(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
| 281 | t.NewTiming("Writing files"); |
| 282 | if (!p.WriteElf(output_oat)) { |
| 283 | return false; |
| 284 | } |
| 285 | if (!p.WriteImage(output_image)) { |
| 286 | return false; |
| 287 | } |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | bool PatchOat::WriteElf(File* out) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 292 | TimingLogger::ScopedTiming t("Writing Elf File", timings_); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 293 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 294 | CHECK(oat_file_.get() != nullptr); |
| 295 | CHECK(out != nullptr); |
| 296 | size_t expect = oat_file_->Size(); |
| 297 | if (out->WriteFully(reinterpret_cast<char*>(oat_file_->Begin()), expect) && |
| 298 | out->SetLength(expect) == 0) { |
| 299 | return true; |
| 300 | } else { |
| 301 | LOG(ERROR) << "Writing to oat file " << out->GetPath() << " failed."; |
| 302 | return false; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | bool PatchOat::WriteImage(File* out) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 307 | TimingLogger::ScopedTiming t("Writing image File", timings_); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 308 | std::string error_msg; |
| 309 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 310 | ScopedFlock img_flock; |
| 311 | img_flock.Init(out, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 312 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 313 | CHECK(image_ != nullptr); |
| 314 | CHECK(out != nullptr); |
| 315 | size_t expect = image_->Size(); |
| 316 | if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) && |
| 317 | out->SetLength(expect) == 0) { |
| 318 | return true; |
| 319 | } else { |
| 320 | LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed."; |
| 321 | return false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | bool PatchOat::PatchImage() { |
| 326 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
| 327 | CHECK_GT(image_->Size(), sizeof(ImageHeader)); |
| 328 | // These are the roots from the original file. |
| 329 | mirror::Object* img_roots = image_header->GetImageRoots(); |
| 330 | image_header->RelocateImage(delta_); |
| 331 | |
| 332 | VisitObject(img_roots); |
| 333 | if (!image_header->IsValid()) { |
| 334 | LOG(ERROR) << "reloction renders image header invalid"; |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 339 | TimingLogger::ScopedTiming t("Walk Bitmap", timings_); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 340 | // Walk the bitmap. |
| 341 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 342 | bitmap_->Walk(PatchOat::BitmapCallback, this); |
| 343 | } |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | bool PatchOat::InHeap(mirror::Object* o) { |
| 348 | uintptr_t begin = reinterpret_cast<uintptr_t>(heap_->Begin()); |
| 349 | uintptr_t end = reinterpret_cast<uintptr_t>(heap_->End()); |
| 350 | uintptr_t obj = reinterpret_cast<uintptr_t>(o); |
| 351 | return o == nullptr || (begin <= obj && obj < end); |
| 352 | } |
| 353 | |
| 354 | void PatchOat::PatchVisitor::operator() (mirror::Object* obj, MemberOffset off, |
| 355 | bool is_static_unused) const { |
| 356 | mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off); |
| 357 | DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap."; |
| 358 | mirror::Object* moved_object = patcher_->RelocatedAddressOf(referent); |
| 359 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 360 | } |
| 361 | |
| 362 | void PatchOat::PatchVisitor::operator() (mirror::Class* cls, mirror::Reference* ref) const { |
| 363 | MemberOffset off = mirror::Reference::ReferentOffset(); |
| 364 | mirror::Object* referent = ref->GetReferent(); |
| 365 | DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap."; |
| 366 | mirror::Object* moved_object = patcher_->RelocatedAddressOf(referent); |
| 367 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object); |
| 368 | } |
| 369 | |
| 370 | mirror::Object* PatchOat::RelocatedCopyOf(mirror::Object* obj) { |
| 371 | if (obj == nullptr) { |
| 372 | return nullptr; |
| 373 | } |
| 374 | DCHECK_GT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->Begin())); |
| 375 | DCHECK_LT(reinterpret_cast<uintptr_t>(obj), reinterpret_cast<uintptr_t>(heap_->End())); |
| 376 | uintptr_t heap_off = |
| 377 | reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(heap_->Begin()); |
| 378 | DCHECK_LT(heap_off, image_->Size()); |
| 379 | return reinterpret_cast<mirror::Object*>(image_->Begin() + heap_off); |
| 380 | } |
| 381 | |
| 382 | mirror::Object* PatchOat::RelocatedAddressOf(mirror::Object* obj) { |
| 383 | if (obj == nullptr) { |
| 384 | return nullptr; |
| 385 | } else { |
| 386 | return reinterpret_cast<mirror::Object*>(reinterpret_cast<byte*>(obj) + delta_); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // Called by BitmapCallback |
| 391 | void PatchOat::VisitObject(mirror::Object* object) { |
| 392 | mirror::Object* copy = RelocatedCopyOf(object); |
| 393 | CHECK(copy != nullptr); |
| 394 | if (kUseBakerOrBrooksReadBarrier) { |
| 395 | object->AssertReadBarrierPointer(); |
| 396 | if (kUseBrooksReadBarrier) { |
| 397 | mirror::Object* moved_to = RelocatedAddressOf(object); |
| 398 | copy->SetReadBarrierPointer(moved_to); |
| 399 | DCHECK_EQ(copy->GetReadBarrierPointer(), moved_to); |
| 400 | } |
| 401 | } |
| 402 | PatchOat::PatchVisitor visitor(this, copy); |
| 403 | object->VisitReferences<true, kVerifyNone>(visitor, visitor); |
| 404 | if (object->IsArtMethod<kVerifyNone>()) { |
| 405 | FixupMethod(static_cast<mirror::ArtMethod*>(object), |
| 406 | static_cast<mirror::ArtMethod*>(copy)); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void PatchOat::FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy) { |
| 411 | // Just update the entry points if it looks like we should. |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 412 | // TODO: sanity check all the pointers' values |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 413 | uintptr_t portable = reinterpret_cast<uintptr_t>( |
| 414 | object->GetEntryPointFromPortableCompiledCode<kVerifyNone>()); |
| 415 | if (portable != 0) { |
| 416 | copy->SetEntryPointFromPortableCompiledCode(reinterpret_cast<void*>(portable + delta_)); |
| 417 | } |
| 418 | uintptr_t quick= reinterpret_cast<uintptr_t>( |
| 419 | object->GetEntryPointFromQuickCompiledCode<kVerifyNone>()); |
| 420 | if (quick != 0) { |
| 421 | copy->SetEntryPointFromQuickCompiledCode(reinterpret_cast<void*>(quick + delta_)); |
| 422 | } |
| 423 | uintptr_t interpreter = reinterpret_cast<uintptr_t>( |
| 424 | object->GetEntryPointFromInterpreter<kVerifyNone>()); |
| 425 | if (interpreter != 0) { |
| 426 | copy->SetEntryPointFromInterpreter( |
| 427 | reinterpret_cast<mirror::EntryPointFromInterpreter*>(interpreter + delta_)); |
| 428 | } |
| 429 | |
| 430 | uintptr_t native_method = reinterpret_cast<uintptr_t>(object->GetNativeMethod()); |
| 431 | if (native_method != 0) { |
| 432 | copy->SetNativeMethod(reinterpret_cast<void*>(native_method + delta_)); |
| 433 | } |
| 434 | |
| 435 | uintptr_t native_gc_map = reinterpret_cast<uintptr_t>(object->GetNativeGcMap()); |
| 436 | if (native_gc_map != 0) { |
| 437 | copy->SetNativeGcMap(reinterpret_cast<uint8_t*>(native_gc_map + delta_)); |
| 438 | } |
| 439 | } |
| 440 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 441 | bool PatchOat::Patch(File* input_oat, off_t delta, File* output_oat, TimingLogger* timings) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 442 | CHECK(input_oat != nullptr); |
| 443 | CHECK(output_oat != nullptr); |
| 444 | CHECK_GE(input_oat->Fd(), 0); |
| 445 | CHECK_GE(output_oat->Fd(), 0); |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 446 | TimingLogger::ScopedTiming t("Setup Oat File Patching", timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 447 | |
| 448 | std::string error_msg; |
| 449 | std::unique_ptr<ElfFile> elf(ElfFile::Open(const_cast<File*>(input_oat), |
| 450 | PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg)); |
| 451 | if (elf.get() == nullptr) { |
| 452 | LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg; |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | PatchOat p(elf.release(), delta, timings); |
| 457 | t.NewTiming("Patch Oat file"); |
| 458 | if (!p.PatchElf()) { |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | t.NewTiming("Writing oat file"); |
| 463 | if (!p.WriteElf(output_oat)) { |
| 464 | return false; |
| 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | |
Alex Light | 4b0d2d9 | 2014-08-06 13:37:23 -0700 | [diff] [blame] | 469 | template <typename ptr_t> |
| 470 | bool PatchOat::CheckOatFile(const Elf32_Shdr& patches_sec) { |
| 471 | if (patches_sec.sh_type != SHT_OAT_PATCH) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 472 | return false; |
| 473 | } |
Alex Light | 4b0d2d9 | 2014-08-06 13:37:23 -0700 | [diff] [blame] | 474 | ptr_t* patches = reinterpret_cast<ptr_t*>(oat_file_->Begin() + patches_sec.sh_offset); |
| 475 | ptr_t* patches_end = patches + (patches_sec.sh_size / sizeof(ptr_t)); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 476 | Elf32_Shdr* oat_data_sec = oat_file_->FindSectionByName(".rodata"); |
| 477 | Elf32_Shdr* oat_text_sec = oat_file_->FindSectionByName(".text"); |
| 478 | if (oat_data_sec == nullptr) { |
| 479 | return false; |
| 480 | } |
| 481 | if (oat_text_sec == nullptr) { |
| 482 | return false; |
| 483 | } |
| 484 | if (oat_text_sec->sh_offset <= oat_data_sec->sh_offset) { |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | for (; patches < patches_end; patches++) { |
| 489 | if (oat_text_sec->sh_size <= *patches) { |
| 490 | return false; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 497 | bool PatchOat::PatchOatHeader() { |
| 498 | Elf32_Shdr *rodata_sec = oat_file_->FindSectionByName(".rodata"); |
| 499 | if (rodata_sec == nullptr) { |
| 500 | return false; |
| 501 | } |
| 502 | OatHeader* oat_header = reinterpret_cast<OatHeader*>(oat_file_->Begin() + rodata_sec->sh_offset); |
| 503 | if (!oat_header->IsValid()) { |
| 504 | LOG(ERROR) << "Elf file " << oat_file_->GetFile().GetPath() << " has an invalid oat header"; |
| 505 | return false; |
| 506 | } |
| 507 | oat_header->RelocateOat(delta_); |
| 508 | return true; |
| 509 | } |
| 510 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 511 | bool PatchOat::PatchElf() { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 512 | TimingLogger::ScopedTiming t("Fixup Elf Text Section", timings_); |
| 513 | if (!PatchTextSection()) { |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | if (!PatchOatHeader()) { |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | bool need_fixup = false; |
| 522 | t.NewTiming("Fixup Elf Headers"); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 523 | // Fixup Phdr's |
| 524 | for (unsigned int i = 0; i < oat_file_->GetProgramHeaderNum(); i++) { |
| 525 | Elf32_Phdr& hdr = oat_file_->GetProgramHeader(i); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 526 | if (hdr.p_vaddr != 0 && hdr.p_vaddr != hdr.p_offset) { |
| 527 | need_fixup = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 528 | hdr.p_vaddr += delta_; |
| 529 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 530 | if (hdr.p_paddr != 0 && hdr.p_paddr != hdr.p_offset) { |
| 531 | need_fixup = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 532 | hdr.p_paddr += delta_; |
| 533 | } |
| 534 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 535 | if (!need_fixup) { |
| 536 | // This was never passed through ElfFixup so all headers/symbols just have their offset as |
| 537 | // their addr. Therefore we do not need to update these parts. |
| 538 | return true; |
| 539 | } |
| 540 | t.NewTiming("Fixup Section Headers"); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 541 | for (unsigned int i = 0; i < oat_file_->GetSectionHeaderNum(); i++) { |
| 542 | Elf32_Shdr& hdr = oat_file_->GetSectionHeader(i); |
| 543 | if (hdr.sh_addr != 0) { |
| 544 | hdr.sh_addr += delta_; |
| 545 | } |
| 546 | } |
| 547 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 548 | t.NewTiming("Fixup Dynamics"); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 549 | for (Elf32_Word i = 0; i < oat_file_->GetDynamicNum(); i++) { |
| 550 | Elf32_Dyn& dyn = oat_file_->GetDynamic(i); |
| 551 | if (IsDynamicSectionPointer(dyn.d_tag, oat_file_->GetHeader().e_machine)) { |
| 552 | dyn.d_un.d_ptr += delta_; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | t.NewTiming("Fixup Elf Symbols"); |
| 557 | // Fixup dynsym |
| 558 | Elf32_Shdr* dynsym_sec = oat_file_->FindSectionByName(".dynsym"); |
| 559 | CHECK(dynsym_sec != nullptr); |
| 560 | if (!PatchSymbols(dynsym_sec)) { |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | // Fixup symtab |
| 565 | Elf32_Shdr* symtab_sec = oat_file_->FindSectionByName(".symtab"); |
| 566 | if (symtab_sec != nullptr) { |
| 567 | if (!PatchSymbols(symtab_sec)) { |
| 568 | return false; |
| 569 | } |
| 570 | } |
| 571 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame^] | 572 | t.NewTiming("Fixup Debug Sections"); |
| 573 | if (!oat_file_->FixupDebugSections(delta_)) { |
| 574 | return false; |
| 575 | } |
| 576 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 577 | return true; |
| 578 | } |
| 579 | |
| 580 | bool PatchOat::PatchSymbols(Elf32_Shdr* section) { |
| 581 | Elf32_Sym* syms = reinterpret_cast<Elf32_Sym*>(oat_file_->Begin() + section->sh_offset); |
| 582 | const Elf32_Sym* last_sym = |
| 583 | reinterpret_cast<Elf32_Sym*>(oat_file_->Begin() + section->sh_offset + section->sh_size); |
| 584 | CHECK_EQ(section->sh_size % sizeof(Elf32_Sym), 0u) |
| 585 | << "Symtab section size is not multiple of symbol size"; |
| 586 | for (; syms < last_sym; syms++) { |
| 587 | uint8_t sttype = ELF32_ST_TYPE(syms->st_info); |
| 588 | Elf32_Word shndx = syms->st_shndx; |
| 589 | if (shndx != SHN_ABS && shndx != SHN_COMMON && shndx != SHN_UNDEF && |
| 590 | (sttype == STT_FUNC || sttype == STT_OBJECT)) { |
| 591 | CHECK_NE(syms->st_value, 0u); |
| 592 | syms->st_value += delta_; |
| 593 | } |
| 594 | } |
| 595 | return true; |
| 596 | } |
| 597 | |
| 598 | bool PatchOat::PatchTextSection() { |
| 599 | Elf32_Shdr* patches_sec = oat_file_->FindSectionByName(".oat_patches"); |
| 600 | if (patches_sec == nullptr) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 601 | LOG(ERROR) << ".oat_patches section not found. Aborting patch"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 602 | return false; |
| 603 | } |
Alex Light | 4b0d2d9 | 2014-08-06 13:37:23 -0700 | [diff] [blame] | 604 | if (patches_sec->sh_type != SHT_OAT_PATCH) { |
| 605 | LOG(ERROR) << "Unexpected type of .oat_patches"; |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | switch (patches_sec->sh_entsize) { |
| 610 | case sizeof(uint32_t): |
| 611 | return PatchTextSection<uint32_t>(*patches_sec); |
| 612 | case sizeof(uint64_t): |
| 613 | return PatchTextSection<uint64_t>(*patches_sec); |
| 614 | default: |
| 615 | LOG(ERROR) << ".oat_patches Entsize of " << patches_sec->sh_entsize << "bits " |
| 616 | << "is not valid"; |
| 617 | return false; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | template <typename ptr_t> |
| 622 | bool PatchOat::PatchTextSection(const Elf32_Shdr& patches_sec) { |
| 623 | DCHECK(CheckOatFile<ptr_t>(patches_sec)) << "Oat file invalid"; |
| 624 | ptr_t* patches = reinterpret_cast<ptr_t*>(oat_file_->Begin() + patches_sec.sh_offset); |
| 625 | ptr_t* patches_end = patches + (patches_sec.sh_size / sizeof(ptr_t)); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 626 | Elf32_Shdr* oat_text_sec = oat_file_->FindSectionByName(".text"); |
| 627 | CHECK(oat_text_sec != nullptr); |
| 628 | byte* to_patch = oat_file_->Begin() + oat_text_sec->sh_offset; |
| 629 | uintptr_t to_patch_end = reinterpret_cast<uintptr_t>(to_patch) + oat_text_sec->sh_size; |
| 630 | |
| 631 | for (; patches < patches_end; patches++) { |
| 632 | CHECK_LT(*patches, oat_text_sec->sh_size) << "Bad Patch"; |
| 633 | uint32_t* patch_loc = reinterpret_cast<uint32_t*>(to_patch + *patches); |
| 634 | CHECK_LT(reinterpret_cast<uintptr_t>(patch_loc), to_patch_end); |
| 635 | *patch_loc += delta_; |
| 636 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 637 | return true; |
| 638 | } |
| 639 | |
| 640 | static int orig_argc; |
| 641 | static char** orig_argv; |
| 642 | |
| 643 | static std::string CommandLine() { |
| 644 | std::vector<std::string> command; |
| 645 | for (int i = 0; i < orig_argc; ++i) { |
| 646 | command.push_back(orig_argv[i]); |
| 647 | } |
| 648 | return Join(command, ' '); |
| 649 | } |
| 650 | |
| 651 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 652 | std::string error; |
| 653 | StringAppendV(&error, fmt, ap); |
| 654 | LOG(ERROR) << error; |
| 655 | } |
| 656 | |
| 657 | static void UsageError(const char* fmt, ...) { |
| 658 | va_list ap; |
| 659 | va_start(ap, fmt); |
| 660 | UsageErrorV(fmt, ap); |
| 661 | va_end(ap); |
| 662 | } |
| 663 | |
| 664 | static void Usage(const char *fmt, ...) { |
| 665 | va_list ap; |
| 666 | va_start(ap, fmt); |
| 667 | UsageErrorV(fmt, ap); |
| 668 | va_end(ap); |
| 669 | |
| 670 | UsageError("Command: %s", CommandLine().c_str()); |
| 671 | UsageError("Usage: patchoat [options]..."); |
| 672 | UsageError(""); |
| 673 | UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is"); |
| 674 | UsageError(" compiled for. Required if you use --input-oat-location"); |
| 675 | UsageError(""); |
| 676 | UsageError(" --input-oat-file=<file.oat>: Specifies the exact filename of the oat file to be"); |
| 677 | UsageError(" patched."); |
| 678 | UsageError(""); |
| 679 | UsageError(" --input-oat-fd=<file-descriptor>: Specifies the file-descriptor of the oat file"); |
| 680 | UsageError(" to be patched."); |
| 681 | UsageError(""); |
| 682 | UsageError(" --input-oat-location=<file.oat>: Specifies the 'location' to read the patched"); |
| 683 | UsageError(" oat file from. If used one must also supply the --instruction-set"); |
| 684 | UsageError(""); |
| 685 | UsageError(" --input-image-location=<file.art>: Specifies the 'location' of the image file to"); |
| 686 | UsageError(" be patched. If --instruction-set is not given it will use the instruction set"); |
| 687 | UsageError(" extracted from the --input-oat-file."); |
| 688 | UsageError(""); |
| 689 | UsageError(" --output-oat-file=<file.oat>: Specifies the exact file to write the patched oat"); |
| 690 | UsageError(" file to."); |
| 691 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 692 | UsageError(" --output-oat-fd=<file-descriptor>: Specifies the file-descriptor to write the"); |
| 693 | UsageError(" the patched oat file to."); |
| 694 | UsageError(""); |
| 695 | UsageError(" --output-image-file=<file.art>: Specifies the exact file to write the patched"); |
| 696 | UsageError(" image file to."); |
| 697 | UsageError(""); |
| 698 | UsageError(" --output-image-fd=<file-descriptor>: Specifies the file-descriptor to write the"); |
| 699 | UsageError(" the patched image file to."); |
| 700 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 701 | UsageError(" --orig-base-offset=<original-base-offset>: Specify the base offset the input file"); |
| 702 | UsageError(" was compiled with. This is needed if one is specifying a --base-offset"); |
| 703 | UsageError(""); |
| 704 | UsageError(" --base-offset=<new-base-offset>: Specify the base offset we will repatch the"); |
| 705 | UsageError(" given files to use. This requires that --orig-base-offset is also given."); |
| 706 | UsageError(""); |
| 707 | UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by."); |
| 708 | UsageError(" This value may be negative."); |
| 709 | UsageError(""); |
| 710 | UsageError(" --patched-image-file=<file.art>: Use the same patch delta as was used to patch"); |
| 711 | UsageError(" the given image file."); |
| 712 | UsageError(""); |
| 713 | UsageError(" --patched-image-location=<file.art>: Use the same patch delta as was used to"); |
| 714 | UsageError(" patch the given image location. If used one must also specify the"); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 715 | UsageError(" --instruction-set flag. It will search for this image in the same way that"); |
| 716 | UsageError(" is done when loading one."); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 717 | UsageError(""); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 718 | UsageError(" --lock-output: Obtain a flock on output oat file before starting."); |
| 719 | UsageError(""); |
| 720 | UsageError(" --no-lock-output: Do not attempt to obtain a flock on output oat file."); |
| 721 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 722 | UsageError(" --dump-timings: dump out patch timing information"); |
| 723 | UsageError(""); |
| 724 | UsageError(" --no-dump-timings: do not dump out patch timing information"); |
| 725 | UsageError(""); |
| 726 | |
| 727 | exit(EXIT_FAILURE); |
| 728 | } |
| 729 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 730 | 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] | 731 | CHECK(name != nullptr); |
| 732 | CHECK(delta != nullptr); |
| 733 | std::unique_ptr<File> file; |
| 734 | if (OS::FileExists(name)) { |
| 735 | file.reset(OS::OpenFileForReading(name)); |
| 736 | if (file.get() == nullptr) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 737 | *error_msg = "Failed to open file %s for reading"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 738 | return false; |
| 739 | } |
| 740 | } else { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 741 | *error_msg = "File %s does not exist"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 742 | return false; |
| 743 | } |
| 744 | CHECK(file.get() != nullptr); |
| 745 | ImageHeader hdr; |
| 746 | if (sizeof(hdr) != file->Read(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0)) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 747 | *error_msg = "Failed to read file %s"; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 748 | return false; |
| 749 | } |
| 750 | if (!hdr.IsValid()) { |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 751 | *error_msg = "%s does not contain a valid image header."; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | *delta = hdr.GetPatchDelta(); |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | static File* CreateOrOpen(const char* name, bool* created) { |
| 759 | if (OS::FileExists(name)) { |
| 760 | *created = false; |
| 761 | return OS::OpenFileReadWrite(name); |
| 762 | } else { |
| 763 | *created = true; |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 764 | std::unique_ptr<File> f(OS::CreateEmptyFile(name)); |
| 765 | if (f.get() != nullptr) { |
| 766 | if (fchmod(f->Fd(), 0644) != 0) { |
| 767 | PLOG(ERROR) << "Unable to make " << name << " world readable"; |
| 768 | unlink(name); |
| 769 | return nullptr; |
| 770 | } |
| 771 | } |
| 772 | return f.release(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 776 | static int patchoat(int argc, char **argv) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 777 | InitLogging(argv); |
| 778 | const bool debug = kIsDebugBuild; |
| 779 | orig_argc = argc; |
| 780 | orig_argv = argv; |
| 781 | TimingLogger timings("patcher", false, false); |
| 782 | |
| 783 | InitLogging(argv); |
| 784 | |
| 785 | // Skip over the command name. |
| 786 | argv++; |
| 787 | argc--; |
| 788 | |
| 789 | if (argc == 0) { |
| 790 | Usage("No arguments specified"); |
| 791 | } |
| 792 | |
| 793 | timings.StartTiming("Patchoat"); |
| 794 | |
| 795 | // cmd line args |
| 796 | bool isa_set = false; |
| 797 | InstructionSet isa = kNone; |
| 798 | std::string input_oat_filename; |
| 799 | std::string input_oat_location; |
| 800 | int input_oat_fd = -1; |
| 801 | bool have_input_oat = false; |
| 802 | std::string input_image_location; |
| 803 | std::string output_oat_filename; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 804 | int output_oat_fd = -1; |
| 805 | bool have_output_oat = false; |
| 806 | std::string output_image_filename; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 807 | int output_image_fd = -1; |
| 808 | bool have_output_image = false; |
| 809 | uintptr_t base_offset = 0; |
| 810 | bool base_offset_set = false; |
| 811 | uintptr_t orig_base_offset = 0; |
| 812 | bool orig_base_offset_set = false; |
| 813 | off_t base_delta = 0; |
| 814 | bool base_delta_set = false; |
| 815 | std::string patched_image_filename; |
| 816 | std::string patched_image_location; |
| 817 | bool dump_timings = kIsDebugBuild; |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 818 | bool lock_output = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 819 | |
| 820 | for (int i = 0; i < argc; i++) { |
| 821 | const StringPiece option(argv[i]); |
| 822 | const bool log_options = false; |
| 823 | if (log_options) { |
| 824 | LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i]; |
| 825 | } |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 826 | // TODO: GetInstructionSetFromString shouldn't LOG(FATAL). |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 827 | if (option.starts_with("--instruction-set=")) { |
| 828 | isa_set = true; |
| 829 | const char* isa_str = option.substr(strlen("--instruction-set=")).data(); |
| 830 | if (!strcmp("arm", isa_str)) { |
| 831 | isa = kArm; |
| 832 | } else if (!strcmp("arm64", isa_str)) { |
| 833 | isa = kArm64; |
| 834 | } else if (!strcmp("x86", isa_str)) { |
| 835 | isa = kX86; |
| 836 | } else if (!strcmp("x86_64", isa_str)) { |
| 837 | isa = kX86_64; |
| 838 | } else if (!strcmp("mips", isa_str)) { |
| 839 | isa = kMips; |
| 840 | } else { |
| 841 | Usage("Unknown instruction set %s", isa_str); |
| 842 | } |
| 843 | } else if (option.starts_with("--input-oat-location=")) { |
| 844 | if (have_input_oat) { |
| 845 | Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used."); |
| 846 | } |
| 847 | have_input_oat = true; |
| 848 | input_oat_location = option.substr(strlen("--input-oat-location=")).data(); |
| 849 | } else if (option.starts_with("--input-oat-file=")) { |
| 850 | if (have_input_oat) { |
| 851 | Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used."); |
| 852 | } |
| 853 | have_input_oat = true; |
| 854 | input_oat_filename = option.substr(strlen("--input-oat-file=")).data(); |
| 855 | } else if (option.starts_with("--input-oat-fd=")) { |
| 856 | if (have_input_oat) { |
| 857 | Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used."); |
| 858 | } |
| 859 | have_input_oat = true; |
| 860 | const char* oat_fd_str = option.substr(strlen("--input-oat-fd=")).data(); |
| 861 | if (!ParseInt(oat_fd_str, &input_oat_fd)) { |
| 862 | Usage("Failed to parse --input-oat-fd argument '%s' as an integer", oat_fd_str); |
| 863 | } |
| 864 | if (input_oat_fd < 0) { |
| 865 | Usage("--input-oat-fd pass a negative value %d", input_oat_fd); |
| 866 | } |
| 867 | } else if (option.starts_with("--input-image-location=")) { |
| 868 | input_image_location = option.substr(strlen("--input-image-location=")).data(); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 869 | } else if (option.starts_with("--output-oat-file=")) { |
| 870 | if (have_output_oat) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 871 | 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] | 872 | } |
| 873 | have_output_oat = true; |
| 874 | output_oat_filename = option.substr(strlen("--output-oat-file=")).data(); |
| 875 | } else if (option.starts_with("--output-oat-fd=")) { |
| 876 | if (have_output_oat) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 877 | 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] | 878 | } |
| 879 | have_output_oat = true; |
| 880 | const char* oat_fd_str = option.substr(strlen("--output-oat-fd=")).data(); |
| 881 | if (!ParseInt(oat_fd_str, &output_oat_fd)) { |
| 882 | Usage("Failed to parse --output-oat-fd argument '%s' as an integer", oat_fd_str); |
| 883 | } |
| 884 | if (output_oat_fd < 0) { |
| 885 | Usage("--output-oat-fd pass a negative value %d", output_oat_fd); |
| 886 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 887 | } else if (option.starts_with("--output-image-file=")) { |
| 888 | if (have_output_image) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 889 | 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] | 890 | } |
| 891 | have_output_image = true; |
| 892 | output_image_filename = option.substr(strlen("--output-image-file=")).data(); |
| 893 | } else if (option.starts_with("--output-image-fd=")) { |
| 894 | if (have_output_image) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 895 | 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] | 896 | } |
| 897 | have_output_image = true; |
| 898 | const char* image_fd_str = option.substr(strlen("--output-image-fd=")).data(); |
| 899 | if (!ParseInt(image_fd_str, &output_image_fd)) { |
| 900 | Usage("Failed to parse --output-image-fd argument '%s' as an integer", image_fd_str); |
| 901 | } |
| 902 | if (output_image_fd < 0) { |
| 903 | Usage("--output-image-fd pass a negative value %d", output_image_fd); |
| 904 | } |
| 905 | } else if (option.starts_with("--orig-base-offset=")) { |
| 906 | const char* orig_base_offset_str = option.substr(strlen("--orig-base-offset=")).data(); |
| 907 | orig_base_offset_set = true; |
| 908 | if (!ParseUint(orig_base_offset_str, &orig_base_offset)) { |
| 909 | Usage("Failed to parse --orig-base-offset argument '%s' as an uintptr_t", |
| 910 | orig_base_offset_str); |
| 911 | } |
| 912 | } else if (option.starts_with("--base-offset=")) { |
| 913 | const char* base_offset_str = option.substr(strlen("--base-offset=")).data(); |
| 914 | base_offset_set = true; |
| 915 | if (!ParseUint(base_offset_str, &base_offset)) { |
| 916 | Usage("Failed to parse --base-offset argument '%s' as an uintptr_t", base_offset_str); |
| 917 | } |
| 918 | } else if (option.starts_with("--base-offset-delta=")) { |
| 919 | const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data(); |
| 920 | base_delta_set = true; |
| 921 | if (!ParseInt(base_delta_str, &base_delta)) { |
| 922 | Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str); |
| 923 | } |
| 924 | } else if (option.starts_with("--patched-image-location=")) { |
| 925 | patched_image_location = option.substr(strlen("--patched-image-location=")).data(); |
| 926 | } else if (option.starts_with("--patched-image-file=")) { |
| 927 | patched_image_filename = option.substr(strlen("--patched-image-file=")).data(); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 928 | } else if (option == "--lock-output") { |
| 929 | lock_output = true; |
| 930 | } else if (option == "--no-lock-output") { |
| 931 | lock_output = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 932 | } else if (option == "--dump-timings") { |
| 933 | dump_timings = true; |
| 934 | } else if (option == "--no-dump-timings") { |
| 935 | dump_timings = false; |
| 936 | } else { |
| 937 | Usage("Unknown argument %s", option.data()); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | { |
| 942 | // Only 1 of these may be set. |
| 943 | uint32_t cnt = 0; |
| 944 | cnt += (base_delta_set) ? 1 : 0; |
| 945 | cnt += (base_offset_set && orig_base_offset_set) ? 1 : 0; |
| 946 | cnt += (!patched_image_filename.empty()) ? 1 : 0; |
| 947 | cnt += (!patched_image_location.empty()) ? 1 : 0; |
| 948 | if (cnt > 1) { |
| 949 | Usage("Only one of --base-offset/--orig-base-offset, --base-offset-delta, " |
| 950 | "--patched-image-filename or --patched-image-location may be used."); |
| 951 | } else if (cnt == 0) { |
| 952 | Usage("Must specify --base-offset-delta, --base-offset and --orig-base-offset, " |
| 953 | "--patched-image-location or --patched-image-file"); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | if (have_input_oat != have_output_oat) { |
| 958 | Usage("Either both input and output oat must be supplied or niether must be."); |
| 959 | } |
| 960 | |
| 961 | if ((!input_image_location.empty()) != have_output_image) { |
| 962 | Usage("Either both input and output image must be supplied or niether must be."); |
| 963 | } |
| 964 | |
| 965 | // We know we have both the input and output so rename for clarity. |
| 966 | bool have_image_files = have_output_image; |
| 967 | bool have_oat_files = have_output_oat; |
| 968 | |
| 969 | if (!have_oat_files && !have_image_files) { |
| 970 | Usage("Must be patching either an oat or an image file or both."); |
| 971 | } |
| 972 | |
| 973 | if (!have_oat_files && !isa_set) { |
| 974 | Usage("Must include ISA if patching an image file without an oat file."); |
| 975 | } |
| 976 | |
| 977 | if (!input_oat_location.empty()) { |
| 978 | if (!isa_set) { |
| 979 | Usage("specifying a location requires specifying an instruction set"); |
| 980 | } |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 981 | if (!LocationToFilename(input_oat_location, isa, &input_oat_filename)) { |
| 982 | Usage("Unable to find filename for input oat location %s", input_oat_location.c_str()); |
| 983 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 984 | if (debug) { |
| 985 | LOG(INFO) << "Using input-oat-file " << input_oat_filename; |
| 986 | } |
| 987 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 988 | if (!patched_image_location.empty()) { |
| 989 | if (!isa_set) { |
| 990 | Usage("specifying a location requires specifying an instruction set"); |
| 991 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 992 | std::string system_filename; |
| 993 | bool has_system = false; |
| 994 | std::string cache_filename; |
| 995 | bool has_cache = false; |
| 996 | bool has_android_data_unused = false; |
| 997 | if (!gc::space::ImageSpace::FindImageFilename(patched_image_location.c_str(), isa, |
| 998 | &system_filename, &has_system, &cache_filename, |
| 999 | &has_android_data_unused, &has_cache)) { |
| 1000 | Usage("Unable to determine image file for location %s", patched_image_location.c_str()); |
| 1001 | } |
| 1002 | if (has_cache) { |
| 1003 | patched_image_filename = cache_filename; |
| 1004 | } else if (has_system) { |
| 1005 | LOG(WARNING) << "Only image file found was in /system for image location " |
| 1006 | << patched_image_location; |
| 1007 | patched_image_filename = system_filename; |
| 1008 | } else { |
| 1009 | Usage("Unable to determine image file for location %s", patched_image_location.c_str()); |
| 1010 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1011 | if (debug) { |
| 1012 | LOG(INFO) << "Using patched-image-file " << patched_image_filename; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | if (!base_delta_set) { |
| 1017 | if (orig_base_offset_set && base_offset_set) { |
| 1018 | base_delta_set = true; |
| 1019 | base_delta = base_offset - orig_base_offset; |
| 1020 | } else if (!patched_image_filename.empty()) { |
| 1021 | base_delta_set = true; |
| 1022 | std::string error_msg; |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1023 | if (!ReadBaseDelta(patched_image_filename.c_str(), &base_delta, &error_msg)) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1024 | Usage(error_msg.c_str(), patched_image_filename.c_str()); |
| 1025 | } |
| 1026 | } else { |
| 1027 | if (base_offset_set) { |
| 1028 | Usage("Unable to determine original base offset."); |
| 1029 | } else { |
| 1030 | Usage("Must supply a desired new offset or delta."); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | if (!IsAligned<kPageSize>(base_delta)) { |
| 1036 | Usage("Base offset/delta must be alligned to a pagesize (0x%08x) boundary.", kPageSize); |
| 1037 | } |
| 1038 | |
| 1039 | // Do we need to cleanup output files if we fail? |
| 1040 | bool new_image_out = false; |
| 1041 | bool new_oat_out = false; |
| 1042 | |
| 1043 | std::unique_ptr<File> input_oat; |
| 1044 | std::unique_ptr<File> output_oat; |
| 1045 | std::unique_ptr<File> output_image; |
| 1046 | |
| 1047 | if (have_image_files) { |
| 1048 | CHECK(!input_image_location.empty()); |
| 1049 | |
| 1050 | if (output_image_fd != -1) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1051 | if (output_image_filename.empty()) { |
| 1052 | output_image_filename = "output-image-file"; |
| 1053 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1054 | output_image.reset(new File(output_image_fd, output_image_filename)); |
| 1055 | } else { |
| 1056 | CHECK(!output_image_filename.empty()); |
| 1057 | output_image.reset(CreateOrOpen(output_image_filename.c_str(), &new_image_out)); |
| 1058 | } |
| 1059 | } else { |
| 1060 | CHECK(output_image_filename.empty() && output_image_fd == -1 && input_image_location.empty()); |
| 1061 | } |
| 1062 | |
| 1063 | if (have_oat_files) { |
| 1064 | if (input_oat_fd != -1) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1065 | if (input_oat_filename.empty()) { |
| 1066 | input_oat_filename = "input-oat-file"; |
| 1067 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1068 | input_oat.reset(new File(input_oat_fd, input_oat_filename)); |
| 1069 | } else { |
| 1070 | CHECK(!input_oat_filename.empty()); |
| 1071 | input_oat.reset(OS::OpenFileForReading(input_oat_filename.c_str())); |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 1072 | if (input_oat.get() == nullptr) { |
| 1073 | LOG(ERROR) << "Could not open input oat file: " << strerror(errno); |
| 1074 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | if (output_oat_fd != -1) { |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1078 | if (output_oat_filename.empty()) { |
| 1079 | output_oat_filename = "output-oat-file"; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1080 | } |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1081 | output_oat.reset(new File(output_oat_fd, output_oat_filename)); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1082 | } else { |
| 1083 | CHECK(!output_oat_filename.empty()); |
| 1084 | output_oat.reset(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out)); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | auto cleanup = [&output_image_filename, &output_oat_filename, |
| 1089 | &new_oat_out, &new_image_out, &timings, &dump_timings](bool success) { |
| 1090 | timings.EndTiming(); |
| 1091 | if (!success) { |
| 1092 | if (new_oat_out) { |
| 1093 | CHECK(!output_oat_filename.empty()); |
| 1094 | unlink(output_oat_filename.c_str()); |
| 1095 | } |
| 1096 | if (new_image_out) { |
| 1097 | CHECK(!output_image_filename.empty()); |
| 1098 | unlink(output_image_filename.c_str()); |
| 1099 | } |
| 1100 | } |
| 1101 | if (dump_timings) { |
| 1102 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
| 1103 | } |
| 1104 | }; |
| 1105 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 1106 | if ((have_oat_files && (input_oat.get() == nullptr || output_oat.get() == nullptr)) || |
| 1107 | (have_image_files && output_image.get() == nullptr)) { |
| 1108 | cleanup(false); |
| 1109 | return EXIT_FAILURE; |
| 1110 | } |
| 1111 | |
| 1112 | ScopedFlock output_oat_lock; |
| 1113 | if (lock_output) { |
| 1114 | std::string error_msg; |
| 1115 | if (have_oat_files && !output_oat_lock.Init(output_oat.get(), &error_msg)) { |
| 1116 | LOG(ERROR) << "Unable to lock output oat " << output_image->GetPath() << ": " << error_msg; |
| 1117 | cleanup(false); |
| 1118 | return EXIT_FAILURE; |
| 1119 | } |
| 1120 | } |
| 1121 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1122 | if (debug) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1123 | LOG(INFO) << "moving offset by " << base_delta |
| 1124 | << " (0x" << std::hex << base_delta << ") bytes or " |
| 1125 | << std::dec << (base_delta/kPageSize) << " pages."; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | bool ret; |
| 1129 | if (have_image_files && have_oat_files) { |
| 1130 | TimingLogger::ScopedTiming pt("patch image and oat", &timings); |
| 1131 | ret = PatchOat::Patch(input_oat.get(), input_image_location, base_delta, |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1132 | output_oat.get(), output_image.get(), isa, &timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1133 | } else if (have_oat_files) { |
| 1134 | TimingLogger::ScopedTiming pt("patch oat", &timings); |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1135 | ret = PatchOat::Patch(input_oat.get(), base_delta, output_oat.get(), &timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1136 | } else { |
| 1137 | TimingLogger::ScopedTiming pt("patch image", &timings); |
| 1138 | CHECK(have_image_files); |
Alex Light | eefbe39 | 2014-07-08 09:53:18 -0700 | [diff] [blame] | 1139 | ret = PatchOat::Patch(input_image_location, base_delta, output_image.get(), isa, &timings); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1140 | } |
| 1141 | cleanup(ret); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1142 | return (ret) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1143 | } |
| 1144 | |
| 1145 | } // namespace art |
| 1146 | |
| 1147 | int main(int argc, char **argv) { |
| 1148 | return art::patchoat(argc, argv); |
| 1149 | } |