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