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