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