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