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