blob: 1d80bda258ff527b9a3e07018bdf1afe81ff7f60 [file] [log] [blame]
Alex Light53cb16b2014-06-12 11:26:29 -07001/*
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 Lighta59dd802014-07-02 16:28:08 -070020#include <sys/file.h>
Alex Light53cb16b2014-06-12 11:26:29 -070021#include <sys/stat.h>
Alex Lighta59dd802014-07-02 16:28:08 -070022#include <unistd.h>
Alex Light53cb16b2014-06-12 11:26:29 -070023
24#include <string>
25#include <vector>
26
Mathieu Chartierc7853442015-03-27 14:35:38 -070027#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "art_method-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070029#include "base/dumpable.h"
Alex Lighta59dd802014-07-02 16:28:08 -070030#include "base/scoped_flock.h"
Alex Light53cb16b2014-06-12 11:26:29 -070031#include "base/stringpiece.h"
32#include "base/stringprintf.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070033#include "base/unix_file/fd_file.h"
Alex Light53cb16b2014-06-12 11:26:29 -070034#include "elf_utils.h"
35#include "elf_file.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070036#include "elf_file_impl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "gc/space/image_space.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080038#include "image-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070039#include "mirror/abstract_method.h"
Alex Light53cb16b2014-06-12 11:26:29 -070040#include "mirror/object-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070041#include "mirror/method.h"
Alex Light53cb16b2014-06-12 11:26:29 -070042#include "mirror/reference.h"
43#include "noop_compiler_callbacks.h"
44#include "offsets.h"
45#include "os.h"
46#include "runtime.h"
47#include "scoped_thread_state_change.h"
48#include "thread.h"
49#include "utils.h"
50
51namespace art {
52
Alex Lightcf4bf382014-07-24 11:29:14 -070053static bool LocationToFilename(const std::string& location, InstructionSet isa,
54 std::string* filename) {
55 bool has_system = false;
56 bool has_cache = false;
57 // image_location = /system/framework/boot.art
Igor Murashkin46774762014-10-22 11:37:02 -070058 // system_image_filename = /system/framework/<image_isa>/boot.art
Alex Lightcf4bf382014-07-24 11:29:14 -070059 std::string system_filename(GetSystemImageFilename(location.c_str(), isa));
60 if (OS::FileExists(system_filename.c_str())) {
61 has_system = true;
62 }
63
64 bool have_android_data = false;
65 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -070066 bool is_global_cache = false;
Alex Lightcf4bf382014-07-24 11:29:14 -070067 std::string dalvik_cache;
68 GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -070069 &have_android_data, &dalvik_cache_exists, &is_global_cache);
Alex Lightcf4bf382014-07-24 11:29:14 -070070
71 std::string cache_filename;
72 if (have_android_data && dalvik_cache_exists) {
73 // Always set output location even if it does not exist,
74 // so that the caller knows where to create the image.
75 //
76 // image_location = /system/framework/boot.art
77 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
78 std::string error_msg;
79 if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(),
80 &cache_filename, &error_msg)) {
81 has_cache = true;
82 }
83 }
84 if (has_system) {
85 *filename = system_filename;
86 return true;
87 } else if (has_cache) {
88 *filename = cache_filename;
89 return true;
90 } else {
91 return false;
92 }
93}
94
Alex Light0eb76d22015-08-11 18:03:47 -070095static const OatHeader* GetOatHeader(const ElfFile* elf_file) {
96 uint64_t off = 0;
97 if (!elf_file->GetSectionOffsetAndSize(".rodata", &off, nullptr)) {
98 return nullptr;
99 }
100
101 OatHeader* oat_header = reinterpret_cast<OatHeader*>(elf_file->Begin() + off);
102 return oat_header;
103}
104
105// This function takes an elf file and reads the current patch delta value
106// encoded in its oat header value
107static bool ReadOatPatchDelta(const ElfFile* elf_file, off_t* delta, std::string* error_msg) {
108 const OatHeader* oat_header = GetOatHeader(elf_file);
109 if (oat_header == nullptr) {
110 *error_msg = "Unable to get oat header from elf file.";
111 return false;
112 }
113 if (!oat_header->IsValid()) {
114 *error_msg = "Elf file has an invalid oat header";
115 return false;
116 }
117 *delta = oat_header->GetImagePatchDelta();
118 return true;
119}
120
Jeff Haodcdc85b2015-12-04 14:06:18 -0800121static File* CreateOrOpen(const char* name, bool* created) {
122 if (OS::FileExists(name)) {
123 *created = false;
124 return OS::OpenFileReadWrite(name);
125 } else {
126 *created = true;
127 std::unique_ptr<File> f(OS::CreateEmptyFile(name));
128 if (f.get() != nullptr) {
129 if (fchmod(f->Fd(), 0644) != 0) {
130 PLOG(ERROR) << "Unable to make " << name << " world readable";
131 TEMP_FAILURE_RETRY(unlink(name));
132 return nullptr;
133 }
134 }
135 return f.release();
136 }
137}
138
139// Either try to close the file (close=true), or erase it.
140static bool FinishFile(File* file, bool close) {
141 if (close) {
142 if (file->FlushCloseOrErase() != 0) {
143 PLOG(ERROR) << "Failed to flush and close file.";
144 return false;
145 }
146 return true;
147 } else {
148 file->Erase();
149 return false;
150 }
151}
152
Andreas Gampe6eb6a392016-02-10 20:18:37 -0800153bool PatchOat::Patch(const std::string& image_location,
154 off_t delta,
155 const std::string& output_directory,
156 InstructionSet isa,
157 TimingLogger* timings) {
Alex Light53cb16b2014-06-12 11:26:29 -0700158 CHECK(Runtime::Current() == nullptr);
Alex Light53cb16b2014-06-12 11:26:29 -0700159 CHECK(!image_location.empty()) << "image file must have a filename.";
160
Alex Lighteefbe392014-07-08 09:53:18 -0700161 TimingLogger::ScopedTiming t("Runtime Setup", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700162
Andreas Gampe6eb6a392016-02-10 20:18:37 -0800163 CHECK_NE(isa, kNone);
Alex Light53cb16b2014-06-12 11:26:29 -0700164 const char* isa_name = GetInstructionSetString(isa);
Igor Murashkin46774762014-10-22 11:37:02 -0700165
Alex Light53cb16b2014-06-12 11:26:29 -0700166 // Set up the runtime
Ian Rogerse63db272014-07-15 15:36:11 -0700167 RuntimeOptions options;
Alex Light53cb16b2014-06-12 11:26:29 -0700168 NoopCompilerCallbacks callbacks;
169 options.push_back(std::make_pair("compilercallbacks", &callbacks));
170 std::string img = "-Ximage:" + image_location;
171 options.push_back(std::make_pair(img.c_str(), nullptr));
172 options.push_back(std::make_pair("imageinstructionset", reinterpret_cast<const void*>(isa_name)));
Calin Juravle01aaf6e2015-06-19 22:05:39 +0100173 options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
Alex Light53cb16b2014-06-12 11:26:29 -0700174 if (!Runtime::Create(options, false)) {
175 LOG(ERROR) << "Unable to initialize runtime";
176 return false;
177 }
178 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
179 // give it away now and then switch to a more manageable ScopedObjectAccess.
180 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
181 ScopedObjectAccess soa(Thread::Current());
182
183 t.NewTiming("Image and oat Patching setup");
Jeff Haodcdc85b2015-12-04 14:06:18 -0800184 std::vector<gc::space::ImageSpace*> spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
185 std::map<gc::space::ImageSpace*, std::unique_ptr<File>> space_to_file_map;
186 std::map<gc::space::ImageSpace*, std::unique_ptr<MemMap>> space_to_memmap_map;
187 std::map<gc::space::ImageSpace*, PatchOat> space_to_patchoat_map;
188 std::map<gc::space::ImageSpace*, bool> space_to_skip_patching_map;
Alex Light53cb16b2014-06-12 11:26:29 -0700189
Jeff Haodcdc85b2015-12-04 14:06:18 -0800190 for (size_t i = 0; i < spaces.size(); ++i) {
191 gc::space::ImageSpace* space = spaces[i];
192 std::string input_image_filename = space->GetImageFilename();
193 std::unique_ptr<File> input_image(OS::OpenFileForReading(input_image_filename.c_str()));
194 if (input_image.get() == nullptr) {
195 LOG(ERROR) << "Unable to open input image file at " << input_image_filename;
Igor Murashkin46774762014-10-22 11:37:02 -0700196 return false;
197 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800198
199 int64_t image_len = input_image->GetLength();
200 if (image_len < 0) {
201 LOG(ERROR) << "Error while getting image length";
202 return false;
203 }
204 ImageHeader image_header;
205 if (sizeof(image_header) != input_image->Read(reinterpret_cast<char*>(&image_header),
206 sizeof(image_header), 0)) {
207 LOG(ERROR) << "Unable to read image header from image file " << input_image->GetPath();
208 }
209
210 /*bool is_image_pic = */IsImagePic(image_header, input_image->GetPath());
211 // Nothing special to do right now since the image always needs to get patched.
212 // Perhaps in some far-off future we may have images with relative addresses that are true-PIC.
213
214 // Create the map where we will write the image patches to.
215 std::string error_msg;
216 std::unique_ptr<MemMap> image(MemMap::MapFile(image_len,
217 PROT_READ | PROT_WRITE,
218 MAP_PRIVATE,
219 input_image->Fd(),
220 0,
221 /*low_4gb*/false,
222 input_image->GetPath().c_str(),
223 &error_msg));
224 if (image.get() == nullptr) {
225 LOG(ERROR) << "Unable to map image file " << input_image->GetPath() << " : " << error_msg;
226 return false;
227 }
228 space_to_file_map.emplace(space, std::move(input_image));
229 space_to_memmap_map.emplace(space, std::move(image));
Igor Murashkin46774762014-10-22 11:37:02 -0700230 }
231
Jeff Haodcdc85b2015-12-04 14:06:18 -0800232 for (size_t i = 0; i < spaces.size(); ++i) {
233 gc::space::ImageSpace* space = spaces[i];
234 std::string input_image_filename = space->GetImageFilename();
235 std::string input_oat_filename =
236 ImageHeader::GetOatLocationFromImageLocation(input_image_filename);
237 std::unique_ptr<File> input_oat_file(OS::OpenFileForReading(input_oat_filename.c_str()));
238 if (input_oat_file.get() == nullptr) {
239 LOG(ERROR) << "Unable to open input oat file at " << input_oat_filename;
240 return false;
241 }
242 std::string error_msg;
243 std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat_file.get(),
244 PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
245 if (elf.get() == nullptr) {
246 LOG(ERROR) << "Unable to open oat file " << input_oat_file->GetPath() << " : " << error_msg;
247 return false;
248 }
249
250 bool skip_patching_oat = false;
251 MaybePic is_oat_pic = IsOatPic(elf.get());
252 if (is_oat_pic >= ERROR_FIRST) {
253 // Error logged by IsOatPic
254 return false;
255 } else if (is_oat_pic == PIC) {
256 // Do not need to do ELF-file patching. Create a symlink and skip the ELF patching.
257
258 std::string converted_image_filename = space->GetImageLocation();
259 std::replace(converted_image_filename.begin() + 1, converted_image_filename.end(), '/', '@');
260 std::string output_image_filename = output_directory +
261 (StartsWith(converted_image_filename, "/") ? "" : "/") +
262 converted_image_filename;
263 std::string output_oat_filename =
264 ImageHeader::GetOatLocationFromImageLocation(output_image_filename);
265
266 if (!ReplaceOatFileWithSymlink(input_oat_file->GetPath(),
267 output_oat_filename,
268 false,
269 true)) {
270 // Errors already logged by above call.
271 return false;
272 }
273 // Don't patch the OAT, since we just symlinked it. Image still needs patching.
274 skip_patching_oat = true;
275 } else {
276 CHECK(is_oat_pic == NOT_PIC);
277 }
278
279 PatchOat& p = space_to_patchoat_map.emplace(space,
280 PatchOat(
281 isa,
282 elf.release(),
283 space_to_memmap_map.find(space)->second.get(),
284 space->GetLiveBitmap(),
285 space->GetMemMap(),
286 delta,
287 &space_to_memmap_map,
288 timings)).first->second;
289
290 t.NewTiming("Patching files");
291 if (!skip_patching_oat && !p.PatchElf()) {
292 LOG(ERROR) << "Failed to patch oat file " << input_oat_file->GetPath();
293 return false;
294 }
295 if (!p.PatchImage(i == 0)) {
296 LOG(ERROR) << "Failed to patch image file " << input_image_filename;
297 return false;
298 }
299
300 space_to_skip_patching_map.emplace(space, skip_patching_oat);
Alex Light53cb16b2014-06-12 11:26:29 -0700301 }
302
Jeff Haodcdc85b2015-12-04 14:06:18 -0800303 for (size_t i = 0; i < spaces.size(); ++i) {
304 gc::space::ImageSpace* space = spaces[i];
305 std::string input_image_filename = space->GetImageFilename();
306
307 t.NewTiming("Writing files");
308 std::string converted_image_filename = space->GetImageLocation();
309 std::replace(converted_image_filename.begin() + 1, converted_image_filename.end(), '/', '@');
310 std::string output_image_filename = output_directory +
311 (StartsWith(converted_image_filename, "/") ? "" : "/") +
312 converted_image_filename;
Andreas Gampe6eb6a392016-02-10 20:18:37 -0800313 bool new_oat_out;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800314 std::unique_ptr<File>
315 output_image_file(CreateOrOpen(output_image_filename.c_str(), &new_oat_out));
316 if (output_image_file.get() == nullptr) {
317 LOG(ERROR) << "Failed to open output image file at " << output_image_filename;
318 return false;
319 }
320
321 PatchOat& p = space_to_patchoat_map.find(space)->second;
322
323 if (!p.WriteImage(output_image_file.get())) {
324 LOG(ERROR) << "Failed to write image file " << output_image_file->GetPath();
325 return false;
326 }
327 FinishFile(output_image_file.get(), true);
328
329 bool skip_patching_oat = space_to_skip_patching_map.find(space)->second;
330 if (!skip_patching_oat) {
331 std::string output_oat_filename =
332 ImageHeader::GetOatLocationFromImageLocation(output_image_filename);
333 std::unique_ptr<File>
334 output_oat_file(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out));
335 if (output_oat_file.get() == nullptr) {
336 LOG(ERROR) << "Failed to open output oat file at " << output_oat_filename;
337 return false;
338 }
339 if (!p.WriteElf(output_oat_file.get())) {
340 LOG(ERROR) << "Failed to write oat file " << output_oat_file->GetPath();
341 return false;
342 }
343 FinishFile(output_oat_file.get(), true);
344 }
Alex Light53cb16b2014-06-12 11:26:29 -0700345 }
346 return true;
347}
348
349bool PatchOat::WriteElf(File* out) {
Alex Lighteefbe392014-07-08 09:53:18 -0700350 TimingLogger::ScopedTiming t("Writing Elf File", timings_);
Alex Lighta59dd802014-07-02 16:28:08 -0700351
Alex Light53cb16b2014-06-12 11:26:29 -0700352 CHECK(oat_file_.get() != nullptr);
353 CHECK(out != nullptr);
354 size_t expect = oat_file_->Size();
355 if (out->WriteFully(reinterpret_cast<char*>(oat_file_->Begin()), expect) &&
356 out->SetLength(expect) == 0) {
357 return true;
358 } else {
359 LOG(ERROR) << "Writing to oat file " << out->GetPath() << " failed.";
360 return false;
361 }
362}
363
364bool PatchOat::WriteImage(File* out) {
Alex Lighteefbe392014-07-08 09:53:18 -0700365 TimingLogger::ScopedTiming t("Writing image File", timings_);
Alex Lighta59dd802014-07-02 16:28:08 -0700366 std::string error_msg;
367
Alex Lightcf4bf382014-07-24 11:29:14 -0700368 ScopedFlock img_flock;
369 img_flock.Init(out, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700370
Alex Light53cb16b2014-06-12 11:26:29 -0700371 CHECK(image_ != nullptr);
372 CHECK(out != nullptr);
373 size_t expect = image_->Size();
374 if (out->WriteFully(reinterpret_cast<char*>(image_->Begin()), expect) &&
375 out->SetLength(expect) == 0) {
376 return true;
377 } else {
378 LOG(ERROR) << "Writing to image file " << out->GetPath() << " failed.";
379 return false;
380 }
381}
382
Igor Murashkin46774762014-10-22 11:37:02 -0700383bool PatchOat::IsImagePic(const ImageHeader& image_header, const std::string& image_path) {
384 if (!image_header.CompilePic()) {
385 if (kIsDebugBuild) {
386 LOG(INFO) << "image at location " << image_path << " was *not* compiled pic";
387 }
388 return false;
389 }
390
391 if (kIsDebugBuild) {
392 LOG(INFO) << "image at location " << image_path << " was compiled PIC";
393 }
394
395 return true;
396}
397
398PatchOat::MaybePic PatchOat::IsOatPic(const ElfFile* oat_in) {
399 if (oat_in == nullptr) {
400 LOG(ERROR) << "No ELF input oat fie available";
401 return ERROR_OAT_FILE;
402 }
403
404 const std::string& file_path = oat_in->GetFile().GetPath();
405
406 const OatHeader* oat_header = GetOatHeader(oat_in);
407 if (oat_header == nullptr) {
408 LOG(ERROR) << "Failed to find oat header in oat file " << file_path;
409 return ERROR_OAT_FILE;
410 }
411
412 if (!oat_header->IsValid()) {
413 LOG(ERROR) << "Elf file " << file_path << " has an invalid oat header";
414 return ERROR_OAT_FILE;
415 }
416
417 bool is_pic = oat_header->IsPic();
418 if (kIsDebugBuild) {
419 LOG(INFO) << "Oat file at " << file_path << " is " << (is_pic ? "PIC" : "not pic");
420 }
421
422 return is_pic ? PIC : NOT_PIC;
423}
424
425bool PatchOat::ReplaceOatFileWithSymlink(const std::string& input_oat_filename,
426 const std::string& output_oat_filename,
427 bool output_oat_opened_from_fd,
428 bool new_oat_out) {
429 // Need a file when we are PIC, since we symlink over it. Refusing to symlink into FD.
430 if (output_oat_opened_from_fd) {
431 // TODO: installd uses --output-oat-fd. Should we change class linking logic for PIC?
432 LOG(ERROR) << "No output oat filename specified, needs filename for when we are PIC";
433 return false;
434 }
435
436 // Image was PIC. Create symlink where the oat is supposed to go.
437 if (!new_oat_out) {
438 LOG(ERROR) << "Oat file " << output_oat_filename << " already exists, refusing to overwrite";
439 return false;
440 }
441
442 // Delete the original file, since we won't need it.
443 TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str()));
444
445 // Create a symlink from the old oat to the new oat
446 if (symlink(input_oat_filename.c_str(), output_oat_filename.c_str()) < 0) {
447 int err = errno;
448 LOG(ERROR) << "Failed to create symlink at " << output_oat_filename
449 << " error(" << err << "): " << strerror(err);
450 return false;
451 }
452
453 if (kIsDebugBuild) {
454 LOG(INFO) << "Created symlink " << output_oat_filename << " -> " << input_oat_filename;
455 }
456
457 return true;
458}
459
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700460class PatchOatArtFieldVisitor : public ArtFieldVisitor {
461 public:
462 explicit PatchOatArtFieldVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
463
464 void Visit(ArtField* field) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
465 ArtField* const dest = patch_oat_->RelocatedCopyOf(field);
466 dest->SetDeclaringClass(patch_oat_->RelocatedAddressOfPointer(field->GetDeclaringClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700467 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700468
469 private:
470 PatchOat* const patch_oat_;
471};
472
473void PatchOat::PatchArtFields(const ImageHeader* image_header) {
474 PatchOatArtFieldVisitor visitor(this);
475 const auto& section = image_header->GetImageSection(ImageHeader::kSectionArtFields);
476 section.VisitPackedArtFields(&visitor, heap_->Begin());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700477}
478
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700479class PatchOatArtMethodVisitor : public ArtMethodVisitor {
480 public:
481 explicit PatchOatArtMethodVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
482
483 void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
484 ArtMethod* const dest = patch_oat_->RelocatedCopyOf(method);
485 patch_oat_->FixupMethod(method, dest);
486 }
487
488 private:
489 PatchOat* const patch_oat_;
490};
491
Mathieu Chartiere401d142015-04-22 13:56:20 -0700492void PatchOat::PatchArtMethods(const ImageHeader* image_header) {
493 const auto& section = image_header->GetMethodsSection();
494 const size_t pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700495 PatchOatArtMethodVisitor visitor(this);
Vladimir Markocf36d492015-08-12 19:27:26 +0100496 section.VisitPackedArtMethods(&visitor, heap_->Begin(), pointer_size);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700497}
498
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700499class FixupRootVisitor : public RootVisitor {
500 public:
501 explicit FixupRootVisitor(const PatchOat* patch_oat) : patch_oat_(patch_oat) {
502 }
503
504 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700505 OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700506 for (size_t i = 0; i < count; ++i) {
507 *roots[i] = patch_oat_->RelocatedAddressOfPointer(*roots[i]);
508 }
509 }
510
511 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
512 const RootInfo& info ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700513 OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700514 for (size_t i = 0; i < count; ++i) {
515 roots[i]->Assign(patch_oat_->RelocatedAddressOfPointer(roots[i]->AsMirrorPtr()));
516 }
517 }
518
519 private:
520 const PatchOat* const patch_oat_;
521};
522
523void PatchOat::PatchInternedStrings(const ImageHeader* image_header) {
524 const auto& section = image_header->GetImageSection(ImageHeader::kSectionInternedStrings);
525 InternTable temp_table;
526 // Note that we require that ReadFromMemory does not make an internal copy of the elements.
527 // This also relies on visit roots not doing any verification which could fail after we update
528 // the roots to be the image addresses.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800529 temp_table.AddTableFromMemory(image_->Begin() + section.Offset());
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700530 FixupRootVisitor visitor(this);
531 temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots);
532}
533
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800534void PatchOat::PatchClassTable(const ImageHeader* image_header) {
535 const auto& section = image_header->GetImageSection(ImageHeader::kSectionClassTable);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800536 if (section.Size() == 0) {
537 return;
538 }
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800539 // Note that we require that ReadFromMemory does not make an internal copy of the elements.
540 // This also relies on visit roots not doing any verification which could fail after we update
541 // the roots to be the image addresses.
542 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
543 ClassTable temp_table;
544 temp_table.ReadFromMemory(image_->Begin() + section.Offset());
545 FixupRootVisitor visitor(this);
546 BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(&visitor, RootInfo(kRootUnknown));
547 temp_table.VisitRoots(buffered_visitor);
548}
549
550
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800551class RelocatedPointerVisitor {
552 public:
553 explicit RelocatedPointerVisitor(PatchOat* patch_oat) : patch_oat_(patch_oat) {}
554
555 template <typename T>
556 T* operator()(T* ptr) const {
557 return patch_oat_->RelocatedAddressOfPointer(ptr);
558 }
559
560 private:
561 PatchOat* const patch_oat_;
562};
563
Mathieu Chartierc7853442015-03-27 14:35:38 -0700564void PatchOat::PatchDexFileArrays(mirror::ObjectArray<mirror::Object>* img_roots) {
565 auto* dex_caches = down_cast<mirror::ObjectArray<mirror::DexCache>*>(
566 img_roots->Get(ImageHeader::kDexCaches));
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800567 const size_t pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700568 for (size_t i = 0, count = dex_caches->GetLength(); i < count; ++i) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100569 auto* orig_dex_cache = dex_caches->GetWithoutChecks(i);
570 auto* copy_dex_cache = RelocatedCopyOf(orig_dex_cache);
Vladimir Marko05792b92015-08-03 11:56:49 +0100571 // Though the DexCache array fields are usually treated as native pointers, we set the full
572 // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is
573 // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e.
574 // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))).
575 GcRoot<mirror::String>* orig_strings = orig_dex_cache->GetStrings();
576 GcRoot<mirror::String>* relocated_strings = RelocatedAddressOfPointer(orig_strings);
577 copy_dex_cache->SetField64<false>(
578 mirror::DexCache::StringsOffset(),
579 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_strings)));
580 if (orig_strings != nullptr) {
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800581 orig_dex_cache->FixupStrings(RelocatedCopyOf(orig_strings), RelocatedPointerVisitor(this));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700582 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100583 GcRoot<mirror::Class>* orig_types = orig_dex_cache->GetResolvedTypes();
584 GcRoot<mirror::Class>* relocated_types = RelocatedAddressOfPointer(orig_types);
585 copy_dex_cache->SetField64<false>(
586 mirror::DexCache::ResolvedTypesOffset(),
587 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_types)));
588 if (orig_types != nullptr) {
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800589 orig_dex_cache->FixupResolvedTypes(RelocatedCopyOf(orig_types),
590 RelocatedPointerVisitor(this));
Vladimir Marko05792b92015-08-03 11:56:49 +0100591 }
592 ArtMethod** orig_methods = orig_dex_cache->GetResolvedMethods();
593 ArtMethod** relocated_methods = RelocatedAddressOfPointer(orig_methods);
594 copy_dex_cache->SetField64<false>(
595 mirror::DexCache::ResolvedMethodsOffset(),
596 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_methods)));
597 if (orig_methods != nullptr) {
598 ArtMethod** copy_methods = RelocatedCopyOf(orig_methods);
599 for (size_t j = 0, num = orig_dex_cache->NumResolvedMethods(); j != num; ++j) {
600 ArtMethod* orig = mirror::DexCache::GetElementPtrSize(orig_methods, j, pointer_size);
601 ArtMethod* copy = RelocatedAddressOfPointer(orig);
602 mirror::DexCache::SetElementPtrSize(copy_methods, j, copy, pointer_size);
603 }
604 }
605 ArtField** orig_fields = orig_dex_cache->GetResolvedFields();
606 ArtField** relocated_fields = RelocatedAddressOfPointer(orig_fields);
607 copy_dex_cache->SetField64<false>(
608 mirror::DexCache::ResolvedFieldsOffset(),
609 static_cast<int64_t>(reinterpret_cast<uintptr_t>(relocated_fields)));
610 if (orig_fields != nullptr) {
611 ArtField** copy_fields = RelocatedCopyOf(orig_fields);
612 for (size_t j = 0, num = orig_dex_cache->NumResolvedFields(); j != num; ++j) {
613 ArtField* orig = mirror::DexCache::GetElementPtrSize(orig_fields, j, pointer_size);
614 ArtField* copy = RelocatedAddressOfPointer(orig);
615 mirror::DexCache::SetElementPtrSize(copy_fields, j, copy, pointer_size);
616 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700617 }
618 }
619}
620
Jeff Haodcdc85b2015-12-04 14:06:18 -0800621bool PatchOat::PatchImage(bool primary_image) {
Alex Light53cb16b2014-06-12 11:26:29 -0700622 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
623 CHECK_GT(image_->Size(), sizeof(ImageHeader));
624 // These are the roots from the original file.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700625 auto* img_roots = image_header->GetImageRoots();
Alex Light53cb16b2014-06-12 11:26:29 -0700626 image_header->RelocateImage(delta_);
627
Mathieu Chartierc7853442015-03-27 14:35:38 -0700628 PatchArtFields(image_header);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700629 PatchArtMethods(image_header);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700630 PatchInternedStrings(image_header);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800631 PatchClassTable(image_header);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700632 // Patch dex file int/long arrays which point to ArtFields.
633 PatchDexFileArrays(img_roots);
634
Jeff Haodcdc85b2015-12-04 14:06:18 -0800635 if (primary_image) {
636 VisitObject(img_roots);
637 }
638
Alex Light53cb16b2014-06-12 11:26:29 -0700639 if (!image_header->IsValid()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800640 LOG(ERROR) << "relocation renders image header invalid";
Alex Light53cb16b2014-06-12 11:26:29 -0700641 return false;
642 }
643
644 {
Alex Lighteefbe392014-07-08 09:53:18 -0700645 TimingLogger::ScopedTiming t("Walk Bitmap", timings_);
Alex Light53cb16b2014-06-12 11:26:29 -0700646 // Walk the bitmap.
647 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
648 bitmap_->Walk(PatchOat::BitmapCallback, this);
649 }
650 return true;
651}
652
653bool PatchOat::InHeap(mirror::Object* o) {
654 uintptr_t begin = reinterpret_cast<uintptr_t>(heap_->Begin());
655 uintptr_t end = reinterpret_cast<uintptr_t>(heap_->End());
656 uintptr_t obj = reinterpret_cast<uintptr_t>(o);
657 return o == nullptr || (begin <= obj && obj < end);
658}
659
660void PatchOat::PatchVisitor::operator() (mirror::Object* obj, MemberOffset off,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700661 bool is_static_unused ATTRIBUTE_UNUSED) const {
Alex Light53cb16b2014-06-12 11:26:29 -0700662 mirror::Object* referent = obj->GetFieldObject<mirror::Object, kVerifyNone>(off);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700663 mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent);
Alex Light53cb16b2014-06-12 11:26:29 -0700664 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
665}
666
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700667void PatchOat::PatchVisitor::operator() (mirror::Class* cls ATTRIBUTE_UNUSED,
668 mirror::Reference* ref) const {
Alex Light53cb16b2014-06-12 11:26:29 -0700669 MemberOffset off = mirror::Reference::ReferentOffset();
670 mirror::Object* referent = ref->GetReferent();
Jeff Hao0d2af302016-01-04 17:38:06 -0800671 DCHECK(patcher_->InHeap(referent)) << "Referent is not in the heap.";
Mathieu Chartierc7853442015-03-27 14:35:38 -0700672 mirror::Object* moved_object = patcher_->RelocatedAddressOfPointer(referent);
Alex Light53cb16b2014-06-12 11:26:29 -0700673 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(off, moved_object);
674}
675
Alex Light53cb16b2014-06-12 11:26:29 -0700676// Called by BitmapCallback
677void PatchOat::VisitObject(mirror::Object* object) {
678 mirror::Object* copy = RelocatedCopyOf(object);
679 CHECK(copy != nullptr);
680 if (kUseBakerOrBrooksReadBarrier) {
681 object->AssertReadBarrierPointer();
682 if (kUseBrooksReadBarrier) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700683 mirror::Object* moved_to = RelocatedAddressOfPointer(object);
Alex Light53cb16b2014-06-12 11:26:29 -0700684 copy->SetReadBarrierPointer(moved_to);
685 DCHECK_EQ(copy->GetReadBarrierPointer(), moved_to);
686 }
687 }
688 PatchOat::PatchVisitor visitor(this, copy);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700689 object->VisitReferences<kVerifyNone>(visitor, visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700690 if (object->IsClass<kVerifyNone>()) {
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800691 const size_t pointer_size = InstructionSetPointerSize(isa_);
692 mirror::Class* klass = object->AsClass();
693 mirror::Class* copy_klass = down_cast<mirror::Class*>(copy);
694 RelocatedPointerVisitor native_visitor(this);
695 klass->FixupNativePointers(copy_klass, pointer_size, native_visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700696 auto* vtable = klass->GetVTable();
697 if (vtable != nullptr) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800698 vtable->Fixup(RelocatedCopyOfFollowImages(vtable), pointer_size, native_visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700699 }
700 auto* iftable = klass->GetIfTable();
701 if (iftable != nullptr) {
702 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
703 if (iftable->GetMethodArrayCount(i) > 0) {
704 auto* method_array = iftable->GetMethodArray(i);
705 CHECK(method_array != nullptr);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800706 method_array->Fixup(RelocatedCopyOfFollowImages(method_array),
707 pointer_size,
708 native_visitor);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700709 }
710 }
711 }
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800712 } else if (object->GetClass() == mirror::Method::StaticClass() ||
713 object->GetClass() == mirror::Constructor::StaticClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700714 // Need to go update the ArtMethod.
715 auto* dest = down_cast<mirror::AbstractMethod*>(copy);
716 auto* src = down_cast<mirror::AbstractMethod*>(object);
717 dest->SetArtMethod(RelocatedAddressOfPointer(src->GetArtMethod()));
Alex Light53cb16b2014-06-12 11:26:29 -0700718 }
719}
720
Mathieu Chartiere401d142015-04-22 13:56:20 -0700721void PatchOat::FixupMethod(ArtMethod* object, ArtMethod* copy) {
Mathieu Chartier2d721012014-11-10 11:08:06 -0800722 const size_t pointer_size = InstructionSetPointerSize(isa_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700723 copy->CopyFrom(object, pointer_size);
Alex Light53cb16b2014-06-12 11:26:29 -0700724 // Just update the entry points if it looks like we should.
Alex Lighteefbe392014-07-08 09:53:18 -0700725 // TODO: sanity check all the pointers' values
Mathieu Chartiere401d142015-04-22 13:56:20 -0700726 copy->SetDeclaringClass(RelocatedAddressOfPointer(object->GetDeclaringClass()));
Vladimir Marko05792b92015-08-03 11:56:49 +0100727 copy->SetDexCacheResolvedMethods(
728 RelocatedAddressOfPointer(object->GetDexCacheResolvedMethods(pointer_size)), pointer_size);
729 copy->SetDexCacheResolvedTypes(
730 RelocatedAddressOfPointer(object->GetDexCacheResolvedTypes(pointer_size)), pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700731 copy->SetEntryPointFromQuickCompiledCodePtrSize(RelocatedAddressOfPointer(
732 object->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)), pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700733 copy->SetEntryPointFromJniPtrSize(RelocatedAddressOfPointer(
734 object->GetEntryPointFromJniPtrSize(pointer_size)), pointer_size);
Alex Light53cb16b2014-06-12 11:26:29 -0700735}
736
Igor Murashkin46774762014-10-22 11:37:02 -0700737bool PatchOat::Patch(File* input_oat, off_t delta, File* output_oat, TimingLogger* timings,
738 bool output_oat_opened_from_fd, bool new_oat_out) {
Alex Light53cb16b2014-06-12 11:26:29 -0700739 CHECK(input_oat != nullptr);
740 CHECK(output_oat != nullptr);
741 CHECK_GE(input_oat->Fd(), 0);
742 CHECK_GE(output_oat->Fd(), 0);
Alex Lighteefbe392014-07-08 09:53:18 -0700743 TimingLogger::ScopedTiming t("Setup Oat File Patching", timings);
Alex Light53cb16b2014-06-12 11:26:29 -0700744
745 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -0700746 std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat,
Alex Light53cb16b2014-06-12 11:26:29 -0700747 PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
748 if (elf.get() == nullptr) {
749 LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
750 return false;
751 }
752
Igor Murashkin46774762014-10-22 11:37:02 -0700753 MaybePic is_oat_pic = IsOatPic(elf.get());
754 if (is_oat_pic >= ERROR_FIRST) {
755 // Error logged by IsOatPic
756 return false;
757 } else if (is_oat_pic == PIC) {
758 // Do not need to do ELF-file patching. Create a symlink and skip the rest.
759 // Any errors will be logged by the function call.
760 return ReplaceOatFileWithSymlink(input_oat->GetPath(),
761 output_oat->GetPath(),
762 output_oat_opened_from_fd,
763 new_oat_out);
764 } else {
765 CHECK(is_oat_pic == NOT_PIC);
766 }
767
Alex Light53cb16b2014-06-12 11:26:29 -0700768 PatchOat p(elf.release(), delta, timings);
769 t.NewTiming("Patch Oat file");
770 if (!p.PatchElf()) {
771 return false;
772 }
773
774 t.NewTiming("Writing oat file");
775 if (!p.WriteElf(output_oat)) {
776 return false;
777 }
778 return true;
779}
780
Tong Shen62d1ca32014-09-03 17:24:56 -0700781template <typename ElfFileImpl>
782bool PatchOat::PatchOatHeader(ElfFileImpl* oat_file) {
783 auto rodata_sec = oat_file->FindSectionByName(".rodata");
Alex Lighta59dd802014-07-02 16:28:08 -0700784 if (rodata_sec == nullptr) {
785 return false;
786 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700787 OatHeader* oat_header = reinterpret_cast<OatHeader*>(oat_file->Begin() + rodata_sec->sh_offset);
Alex Lighta59dd802014-07-02 16:28:08 -0700788 if (!oat_header->IsValid()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700789 LOG(ERROR) << "Elf file " << oat_file->GetFile().GetPath() << " has an invalid oat header";
Alex Lighta59dd802014-07-02 16:28:08 -0700790 return false;
791 }
792 oat_header->RelocateOat(delta_);
793 return true;
794}
795
Alex Light53cb16b2014-06-12 11:26:29 -0700796bool PatchOat::PatchElf() {
Ian Rogersd4c4d952014-10-16 20:31:53 -0700797 if (oat_file_->Is64Bit())
Tong Shen62d1ca32014-09-03 17:24:56 -0700798 return PatchElf<ElfFileImpl64>(oat_file_->GetImpl64());
799 else
800 return PatchElf<ElfFileImpl32>(oat_file_->GetImpl32());
801}
802
803template <typename ElfFileImpl>
804bool PatchOat::PatchElf(ElfFileImpl* oat_file) {
Alex Lighta59dd802014-07-02 16:28:08 -0700805 TimingLogger::ScopedTiming t("Fixup Elf Text Section", timings_);
Vladimir Marko3fc99032015-05-13 19:06:30 +0100806
807 // Fix up absolute references to locations within the boot image.
David Srbecky2f6cdb02015-04-11 00:17:53 +0100808 if (!oat_file->ApplyOatPatchesTo(".text", delta_)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700809 return false;
810 }
811
Vladimir Marko3fc99032015-05-13 19:06:30 +0100812 // Update the OatHeader fields referencing the boot image.
Tong Shen62d1ca32014-09-03 17:24:56 -0700813 if (!PatchOatHeader<ElfFileImpl>(oat_file)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700814 return false;
815 }
816
Vladimir Marko3fc99032015-05-13 19:06:30 +0100817 bool need_boot_oat_fixup = true;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700818 for (unsigned int i = 0; i < oat_file->GetProgramHeaderNum(); ++i) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700819 auto hdr = oat_file->GetProgramHeader(i);
Vladimir Marko3fc99032015-05-13 19:06:30 +0100820 if (hdr->p_type == PT_LOAD && hdr->p_vaddr == 0u) {
821 need_boot_oat_fixup = false;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700822 break;
Alex Light53cb16b2014-06-12 11:26:29 -0700823 }
824 }
Vladimir Marko3fc99032015-05-13 19:06:30 +0100825 if (!need_boot_oat_fixup) {
826 // This is an app oat file that can be loaded at an arbitrary address in memory.
827 // Boot image references were patched above and there's nothing else to do.
Alex Lighta59dd802014-07-02 16:28:08 -0700828 return true;
829 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700830
Vladimir Marko3fc99032015-05-13 19:06:30 +0100831 // This is a boot oat file that's loaded at a particular address and we need
832 // to patch all absolute addresses, starting with ELF program headers.
833
Tong Shen62d1ca32014-09-03 17:24:56 -0700834 t.NewTiming("Fixup Elf Headers");
835 // Fixup Phdr's
836 oat_file->FixupProgramHeaders(delta_);
837
Alex Lighta59dd802014-07-02 16:28:08 -0700838 t.NewTiming("Fixup Section Headers");
Tong Shen62d1ca32014-09-03 17:24:56 -0700839 // Fixup Shdr's
840 oat_file->FixupSectionHeaders(delta_);
Alex Light53cb16b2014-06-12 11:26:29 -0700841
Alex Lighta59dd802014-07-02 16:28:08 -0700842 t.NewTiming("Fixup Dynamics");
Tong Shen62d1ca32014-09-03 17:24:56 -0700843 oat_file->FixupDynamic(delta_);
Alex Light53cb16b2014-06-12 11:26:29 -0700844
845 t.NewTiming("Fixup Elf Symbols");
846 // Fixup dynsym
Tong Shen62d1ca32014-09-03 17:24:56 -0700847 if (!oat_file->FixupSymbols(delta_, true)) {
Alex Light53cb16b2014-06-12 11:26:29 -0700848 return false;
849 }
Alex Light53cb16b2014-06-12 11:26:29 -0700850 // Fixup symtab
Tong Shen62d1ca32014-09-03 17:24:56 -0700851 if (!oat_file->FixupSymbols(delta_, false)) {
852 return false;
Alex Light53cb16b2014-06-12 11:26:29 -0700853 }
854
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700855 t.NewTiming("Fixup Debug Sections");
Tong Shen62d1ca32014-09-03 17:24:56 -0700856 if (!oat_file->FixupDebugSections(delta_)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700857 return false;
858 }
859
Alex Light53cb16b2014-06-12 11:26:29 -0700860 return true;
861}
862
Alex Light53cb16b2014-06-12 11:26:29 -0700863static int orig_argc;
864static char** orig_argv;
865
866static std::string CommandLine() {
867 std::vector<std::string> command;
868 for (int i = 0; i < orig_argc; ++i) {
869 command.push_back(orig_argv[i]);
870 }
871 return Join(command, ' ');
872}
873
874static void UsageErrorV(const char* fmt, va_list ap) {
875 std::string error;
876 StringAppendV(&error, fmt, ap);
877 LOG(ERROR) << error;
878}
879
880static void UsageError(const char* fmt, ...) {
881 va_list ap;
882 va_start(ap, fmt);
883 UsageErrorV(fmt, ap);
884 va_end(ap);
885}
886
Andreas Gampe794ad762015-02-23 08:12:24 -0800887NO_RETURN static void Usage(const char *fmt, ...) {
Alex Light53cb16b2014-06-12 11:26:29 -0700888 va_list ap;
889 va_start(ap, fmt);
890 UsageErrorV(fmt, ap);
891 va_end(ap);
892
893 UsageError("Command: %s", CommandLine().c_str());
894 UsageError("Usage: patchoat [options]...");
895 UsageError("");
896 UsageError(" --instruction-set=<isa>: Specifies the instruction set the patched code is");
897 UsageError(" compiled for. Required if you use --input-oat-location");
898 UsageError("");
899 UsageError(" --input-oat-file=<file.oat>: Specifies the exact filename of the oat file to be");
900 UsageError(" patched.");
901 UsageError("");
902 UsageError(" --input-oat-fd=<file-descriptor>: Specifies the file-descriptor of the oat file");
903 UsageError(" to be patched.");
904 UsageError("");
905 UsageError(" --input-oat-location=<file.oat>: Specifies the 'location' to read the patched");
906 UsageError(" oat file from. If used one must also supply the --instruction-set");
907 UsageError("");
908 UsageError(" --input-image-location=<file.art>: Specifies the 'location' of the image file to");
909 UsageError(" be patched. If --instruction-set is not given it will use the instruction set");
910 UsageError(" extracted from the --input-oat-file.");
911 UsageError("");
912 UsageError(" --output-oat-file=<file.oat>: Specifies the exact file to write the patched oat");
913 UsageError(" file to.");
914 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700915 UsageError(" --output-oat-fd=<file-descriptor>: Specifies the file-descriptor to write the");
916 UsageError(" the patched oat file to.");
917 UsageError("");
918 UsageError(" --output-image-file=<file.art>: Specifies the exact file to write the patched");
919 UsageError(" image file to.");
920 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700921 UsageError(" --base-offset-delta=<delta>: Specify the amount to change the old base-offset by.");
922 UsageError(" This value may be negative.");
923 UsageError("");
Alex Light0eb76d22015-08-11 18:03:47 -0700924 UsageError(" --patched-image-location=<file.art>: Relocate the oat file to be the same as the");
925 UsageError(" image at the given location. If used one must also specify the");
Alex Lighta59dd802014-07-02 16:28:08 -0700926 UsageError(" --instruction-set flag. It will search for this image in the same way that");
927 UsageError(" is done when loading one.");
Alex Light53cb16b2014-06-12 11:26:29 -0700928 UsageError("");
Alex Lightcf4bf382014-07-24 11:29:14 -0700929 UsageError(" --lock-output: Obtain a flock on output oat file before starting.");
930 UsageError("");
931 UsageError(" --no-lock-output: Do not attempt to obtain a flock on output oat file.");
932 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700933 UsageError(" --dump-timings: dump out patch timing information");
934 UsageError("");
935 UsageError(" --no-dump-timings: do not dump out patch timing information");
936 UsageError("");
937
938 exit(EXIT_FAILURE);
939}
940
Alex Lighteefbe392014-07-08 09:53:18 -0700941static bool ReadBaseDelta(const char* name, off_t* delta, std::string* error_msg) {
Alex Light53cb16b2014-06-12 11:26:29 -0700942 CHECK(name != nullptr);
943 CHECK(delta != nullptr);
944 std::unique_ptr<File> file;
945 if (OS::FileExists(name)) {
946 file.reset(OS::OpenFileForReading(name));
947 if (file.get() == nullptr) {
Alex Lighteefbe392014-07-08 09:53:18 -0700948 *error_msg = "Failed to open file %s for reading";
Alex Light53cb16b2014-06-12 11:26:29 -0700949 return false;
950 }
951 } else {
Alex Lighteefbe392014-07-08 09:53:18 -0700952 *error_msg = "File %s does not exist";
Alex Light53cb16b2014-06-12 11:26:29 -0700953 return false;
954 }
955 CHECK(file.get() != nullptr);
956 ImageHeader hdr;
957 if (sizeof(hdr) != file->Read(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0)) {
Alex Lighteefbe392014-07-08 09:53:18 -0700958 *error_msg = "Failed to read file %s";
Alex Light53cb16b2014-06-12 11:26:29 -0700959 return false;
960 }
961 if (!hdr.IsValid()) {
Alex Lighteefbe392014-07-08 09:53:18 -0700962 *error_msg = "%s does not contain a valid image header.";
Alex Light53cb16b2014-06-12 11:26:29 -0700963 return false;
964 }
965 *delta = hdr.GetPatchDelta();
966 return true;
967}
968
Andreas Gampe6eb6a392016-02-10 20:18:37 -0800969static int patchoat_image(TimingLogger& timings,
970 InstructionSet isa,
971 const std::string& input_image_location,
972 const std::string& output_image_filename,
973 off_t base_delta,
974 bool base_delta_set,
975 bool debug) {
976 CHECK(!input_image_location.empty());
977 if (output_image_filename.empty()) {
978 Usage("Image patching requires --output-image-file");
979 }
980
981 if (!base_delta_set) {
982 Usage("Must supply a desired new offset or delta.");
983 }
984
985 if (!IsAligned<kPageSize>(base_delta)) {
986 Usage("Base offset/delta must be aligned to a pagesize (0x%08x) boundary.", kPageSize);
987 }
988
989 if (debug) {
990 LOG(INFO) << "moving offset by " << base_delta
991 << " (0x" << std::hex << base_delta << ") bytes or "
992 << std::dec << (base_delta/kPageSize) << " pages.";
993 }
994
995 TimingLogger::ScopedTiming pt("patch image and oat", &timings);
996
997 std::string output_directory =
998 output_image_filename.substr(0, output_image_filename.find_last_of("/"));
999 bool ret = PatchOat::Patch(input_image_location, base_delta, output_directory, isa, &timings);
1000
1001 if (kIsDebugBuild) {
1002 LOG(INFO) << "Exiting with return ... " << ret;
1003 }
1004 return ret ? EXIT_SUCCESS : EXIT_FAILURE;
1005}
1006
1007static int patchoat_oat(TimingLogger& timings,
1008 InstructionSet isa,
1009 const std::string& patched_image_location,
1010 off_t base_delta,
1011 bool base_delta_set,
1012 int input_oat_fd,
1013 const std::string& input_oat_location,
1014 std::string input_oat_filename,
1015 bool have_input_oat,
1016 int output_oat_fd,
1017 std::string output_oat_filename,
1018 bool have_output_oat,
1019 bool lock_output,
1020 bool debug) {
1021 {
1022 // Only 1 of these may be set.
1023 uint32_t cnt = 0;
1024 cnt += (base_delta_set) ? 1 : 0;
1025 cnt += (!patched_image_location.empty()) ? 1 : 0;
1026 if (cnt > 1) {
1027 Usage("Only one of --base-offset-delta or --patched-image-location may be used.");
1028 } else if (cnt == 0) {
1029 Usage("Must specify --base-offset-delta or --patched-image-location.");
1030 }
1031 }
1032
1033 if (!have_input_oat || !have_output_oat) {
1034 Usage("Both input and output oat must be supplied to patch an app odex.");
1035 }
1036
1037 if (!input_oat_location.empty()) {
1038 if (!LocationToFilename(input_oat_location, isa, &input_oat_filename)) {
1039 Usage("Unable to find filename for input oat location %s", input_oat_location.c_str());
1040 }
1041 if (debug) {
1042 LOG(INFO) << "Using input-oat-file " << input_oat_filename;
1043 }
1044 }
1045
1046 bool match_delta = false;
1047 if (!patched_image_location.empty()) {
1048 std::string system_filename;
1049 bool has_system = false;
1050 std::string cache_filename;
1051 bool has_cache = false;
1052 bool has_android_data_unused = false;
1053 bool is_global_cache = false;
1054 if (!gc::space::ImageSpace::FindImageFilename(patched_image_location.c_str(), isa,
1055 &system_filename, &has_system, &cache_filename,
1056 &has_android_data_unused, &has_cache,
1057 &is_global_cache)) {
1058 Usage("Unable to determine image file for location %s", patched_image_location.c_str());
1059 }
1060 std::string patched_image_filename;
1061 if (has_cache) {
1062 patched_image_filename = cache_filename;
1063 } else if (has_system) {
1064 LOG(WARNING) << "Only image file found was in /system for image location "
1065 << patched_image_location;
1066 patched_image_filename = system_filename;
1067 } else {
1068 Usage("Unable to determine image file for location %s", patched_image_location.c_str());
1069 }
1070 if (debug) {
1071 LOG(INFO) << "Using patched-image-file " << patched_image_filename;
1072 }
1073
1074 base_delta_set = true;
1075 match_delta = true;
1076 std::string error_msg;
1077 if (!ReadBaseDelta(patched_image_filename.c_str(), &base_delta, &error_msg)) {
1078 Usage(error_msg.c_str(), patched_image_filename.c_str());
1079 }
1080 }
1081
1082 if (!IsAligned<kPageSize>(base_delta)) {
1083 Usage("Base offset/delta must be alligned to a pagesize (0x%08x) boundary.", kPageSize);
1084 }
1085
1086 // Do we need to cleanup output files if we fail?
1087 bool new_oat_out = false;
1088
1089 std::unique_ptr<File> input_oat;
1090 std::unique_ptr<File> output_oat;
1091
1092 if (input_oat_fd != -1) {
1093 if (input_oat_filename.empty()) {
1094 input_oat_filename = "input-oat-file";
1095 }
1096 input_oat.reset(new File(input_oat_fd, input_oat_filename, false));
1097 if (input_oat_fd == output_oat_fd) {
1098 input_oat.get()->DisableAutoClose();
1099 }
1100 if (input_oat == nullptr) {
1101 // Unlikely, but ensure exhaustive logging in non-0 exit code case
1102 LOG(ERROR) << "Failed to open input oat file by its FD" << input_oat_fd;
1103 }
1104 } else {
1105 CHECK(!input_oat_filename.empty());
1106 input_oat.reset(OS::OpenFileForReading(input_oat_filename.c_str()));
1107 if (input_oat == nullptr) {
1108 int err = errno;
1109 LOG(ERROR) << "Failed to open input oat file " << input_oat_filename
1110 << ": " << strerror(err) << "(" << err << ")";
1111 }
1112 }
1113
1114 if (output_oat_fd != -1) {
1115 if (output_oat_filename.empty()) {
1116 output_oat_filename = "output-oat-file";
1117 }
1118 output_oat.reset(new File(output_oat_fd, output_oat_filename, true));
1119 if (output_oat == nullptr) {
1120 // Unlikely, but ensure exhaustive logging in non-0 exit code case
1121 LOG(ERROR) << "Failed to open output oat file by its FD" << output_oat_fd;
1122 }
1123 } else {
1124 CHECK(!output_oat_filename.empty());
1125 output_oat.reset(CreateOrOpen(output_oat_filename.c_str(), &new_oat_out));
1126 if (output_oat == nullptr) {
1127 int err = errno;
1128 LOG(ERROR) << "Failed to open output oat file " << output_oat_filename
1129 << ": " << strerror(err) << "(" << err << ")";
1130 }
1131 }
1132
1133 // TODO: get rid of this.
1134 auto cleanup = [&output_oat_filename, &new_oat_out](bool success) {
1135 if (!success) {
1136 if (new_oat_out) {
1137 CHECK(!output_oat_filename.empty());
1138 TEMP_FAILURE_RETRY(unlink(output_oat_filename.c_str()));
1139 }
1140 }
1141
1142 if (kIsDebugBuild) {
1143 LOG(INFO) << "Cleaning up.. success? " << success;
1144 }
1145 };
1146
1147 if (input_oat.get() == nullptr || output_oat.get() == nullptr) {
1148 LOG(ERROR) << "Failed to open input/output oat files";
1149 cleanup(false);
1150 return EXIT_FAILURE;
1151 }
1152
1153 if (match_delta) {
1154 std::string error_msg;
1155 // Figure out what the current delta is so we can match it to the desired delta.
1156 std::unique_ptr<ElfFile> elf(ElfFile::Open(input_oat.get(), PROT_READ, MAP_PRIVATE,
1157 &error_msg));
1158 off_t current_delta = 0;
1159 if (elf.get() == nullptr) {
1160 LOG(ERROR) << "unable to open oat file " << input_oat->GetPath() << " : " << error_msg;
1161 cleanup(false);
1162 return EXIT_FAILURE;
1163 } else if (!ReadOatPatchDelta(elf.get(), &current_delta, &error_msg)) {
1164 LOG(ERROR) << "Unable to get current delta: " << error_msg;
1165 cleanup(false);
1166 return EXIT_FAILURE;
1167 }
1168 // Before this line base_delta is the desired final delta. We need it to be the actual amount to
1169 // change everything by. We subtract the current delta from it to make it this.
1170 base_delta -= current_delta;
1171 if (!IsAligned<kPageSize>(base_delta)) {
1172 LOG(ERROR) << "Given image file was relocated by an illegal delta";
1173 cleanup(false);
1174 return false;
1175 }
1176 }
1177
1178 if (debug) {
1179 LOG(INFO) << "moving offset by " << base_delta
1180 << " (0x" << std::hex << base_delta << ") bytes or "
1181 << std::dec << (base_delta/kPageSize) << " pages.";
1182 }
1183
1184 ScopedFlock output_oat_lock;
1185 if (lock_output) {
1186 std::string error_msg;
1187 if (!output_oat_lock.Init(output_oat.get(), &error_msg)) {
1188 LOG(ERROR) << "Unable to lock output oat " << output_oat->GetPath() << ": " << error_msg;
1189 cleanup(false);
1190 return EXIT_FAILURE;
1191 }
1192 }
1193
1194 TimingLogger::ScopedTiming pt("patch oat", &timings);
1195 bool ret = PatchOat::Patch(input_oat.get(), base_delta, output_oat.get(), &timings,
1196 output_oat_fd >= 0, // was it opened from FD?
1197 new_oat_out);
1198 ret = FinishFile(output_oat.get(), ret);
1199
1200 if (kIsDebugBuild) {
1201 LOG(INFO) << "Exiting with return ... " << ret;
1202 }
1203 cleanup(ret);
1204 return ret ? EXIT_SUCCESS : EXIT_FAILURE;
1205}
1206
Alex Lighteefbe392014-07-08 09:53:18 -07001207static int patchoat(int argc, char **argv) {
Alex Light53cb16b2014-06-12 11:26:29 -07001208 InitLogging(argv);
Mathieu Chartier6e88ef62014-10-14 15:01:24 -07001209 MemMap::Init();
Alex Light53cb16b2014-06-12 11:26:29 -07001210 const bool debug = kIsDebugBuild;
1211 orig_argc = argc;
1212 orig_argv = argv;
1213 TimingLogger timings("patcher", false, false);
1214
1215 InitLogging(argv);
1216
1217 // Skip over the command name.
1218 argv++;
1219 argc--;
1220
1221 if (argc == 0) {
1222 Usage("No arguments specified");
1223 }
1224
1225 timings.StartTiming("Patchoat");
1226
1227 // cmd line args
1228 bool isa_set = false;
1229 InstructionSet isa = kNone;
1230 std::string input_oat_filename;
1231 std::string input_oat_location;
1232 int input_oat_fd = -1;
1233 bool have_input_oat = false;
1234 std::string input_image_location;
1235 std::string output_oat_filename;
Alex Light53cb16b2014-06-12 11:26:29 -07001236 int output_oat_fd = -1;
1237 bool have_output_oat = false;
1238 std::string output_image_filename;
Alex Light53cb16b2014-06-12 11:26:29 -07001239 off_t base_delta = 0;
1240 bool base_delta_set = false;
1241 std::string patched_image_filename;
1242 std::string patched_image_location;
1243 bool dump_timings = kIsDebugBuild;
Alex Lightcf4bf382014-07-24 11:29:14 -07001244 bool lock_output = true;
Alex Light53cb16b2014-06-12 11:26:29 -07001245
Ian Rogersd4c4d952014-10-16 20:31:53 -07001246 for (int i = 0; i < argc; ++i) {
Alex Light53cb16b2014-06-12 11:26:29 -07001247 const StringPiece option(argv[i]);
1248 const bool log_options = false;
1249 if (log_options) {
1250 LOG(INFO) << "patchoat: option[" << i << "]=" << argv[i];
1251 }
Alex Light53cb16b2014-06-12 11:26:29 -07001252 if (option.starts_with("--instruction-set=")) {
1253 isa_set = true;
1254 const char* isa_str = option.substr(strlen("--instruction-set=")).data();
Andreas Gampe20c89302014-08-19 17:28:06 -07001255 isa = GetInstructionSetFromString(isa_str);
1256 if (isa == kNone) {
1257 Usage("Unknown or invalid instruction set %s", isa_str);
Alex Light53cb16b2014-06-12 11:26:29 -07001258 }
1259 } else if (option.starts_with("--input-oat-location=")) {
1260 if (have_input_oat) {
1261 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
1262 }
1263 have_input_oat = true;
1264 input_oat_location = option.substr(strlen("--input-oat-location=")).data();
1265 } else if (option.starts_with("--input-oat-file=")) {
1266 if (have_input_oat) {
1267 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
1268 }
1269 have_input_oat = true;
1270 input_oat_filename = option.substr(strlen("--input-oat-file=")).data();
1271 } else if (option.starts_with("--input-oat-fd=")) {
1272 if (have_input_oat) {
1273 Usage("Only one of --input-oat-file, --input-oat-location and --input-oat-fd may be used.");
1274 }
1275 have_input_oat = true;
1276 const char* oat_fd_str = option.substr(strlen("--input-oat-fd=")).data();
1277 if (!ParseInt(oat_fd_str, &input_oat_fd)) {
1278 Usage("Failed to parse --input-oat-fd argument '%s' as an integer", oat_fd_str);
1279 }
1280 if (input_oat_fd < 0) {
1281 Usage("--input-oat-fd pass a negative value %d", input_oat_fd);
1282 }
1283 } else if (option.starts_with("--input-image-location=")) {
1284 input_image_location = option.substr(strlen("--input-image-location=")).data();
Alex Light53cb16b2014-06-12 11:26:29 -07001285 } else if (option.starts_with("--output-oat-file=")) {
1286 if (have_output_oat) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001287 Usage("Only one of --output-oat-file, and --output-oat-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -07001288 }
1289 have_output_oat = true;
1290 output_oat_filename = option.substr(strlen("--output-oat-file=")).data();
1291 } else if (option.starts_with("--output-oat-fd=")) {
1292 if (have_output_oat) {
Alex Lightcf4bf382014-07-24 11:29:14 -07001293 Usage("Only one of --output-oat-file, --output-oat-fd may be used.");
Alex Light53cb16b2014-06-12 11:26:29 -07001294 }
1295 have_output_oat = true;
1296 const char* oat_fd_str = option.substr(strlen("--output-oat-fd=")).data();
1297 if (!ParseInt(oat_fd_str, &output_oat_fd)) {
1298 Usage("Failed to parse --output-oat-fd argument '%s' as an integer", oat_fd_str);
1299 }
1300 if (output_oat_fd < 0) {
1301 Usage("--output-oat-fd pass a negative value %d", output_oat_fd);
1302 }
Alex Light53cb16b2014-06-12 11:26:29 -07001303 } else if (option.starts_with("--output-image-file=")) {
Alex Light53cb16b2014-06-12 11:26:29 -07001304 output_image_filename = option.substr(strlen("--output-image-file=")).data();
Alex Light53cb16b2014-06-12 11:26:29 -07001305 } else if (option.starts_with("--base-offset-delta=")) {
1306 const char* base_delta_str = option.substr(strlen("--base-offset-delta=")).data();
1307 base_delta_set = true;
1308 if (!ParseInt(base_delta_str, &base_delta)) {
1309 Usage("Failed to parse --base-offset-delta argument '%s' as an off_t", base_delta_str);
1310 }
1311 } else if (option.starts_with("--patched-image-location=")) {
1312 patched_image_location = option.substr(strlen("--patched-image-location=")).data();
Alex Lightcf4bf382014-07-24 11:29:14 -07001313 } else if (option == "--lock-output") {
1314 lock_output = true;
1315 } else if (option == "--no-lock-output") {
1316 lock_output = false;
Alex Light53cb16b2014-06-12 11:26:29 -07001317 } else if (option == "--dump-timings") {
1318 dump_timings = true;
1319 } else if (option == "--no-dump-timings") {
1320 dump_timings = false;
1321 } else {
1322 Usage("Unknown argument %s", option.data());
1323 }
1324 }
1325
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001326 // The instruction set is mandatory. This simplifies things...
1327 if (!isa_set) {
1328 Usage("Instruction set must be set.");
Alex Light53cb16b2014-06-12 11:26:29 -07001329 }
1330
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001331 int ret;
1332 if (!input_image_location.empty()) {
1333 ret = patchoat_image(timings,
1334 isa,
1335 input_image_location,
1336 output_image_filename,
1337 base_delta,
1338 base_delta_set,
1339 debug);
Alex Light53cb16b2014-06-12 11:26:29 -07001340 } else {
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001341 ret = patchoat_oat(timings,
1342 isa,
1343 patched_image_location,
1344 base_delta,
1345 base_delta_set,
1346 input_oat_fd,
1347 input_oat_location,
1348 input_oat_filename,
1349 have_input_oat,
1350 output_oat_fd,
1351 output_oat_filename,
1352 have_output_oat,
1353 lock_output,
1354 debug);
Alex Light53cb16b2014-06-12 11:26:29 -07001355 }
1356
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001357 timings.EndTiming();
1358 if (dump_timings) {
1359 LOG(INFO) << Dumpable<TimingLogger>(timings);
Alex Light53cb16b2014-06-12 11:26:29 -07001360 }
1361
Andreas Gampe6eb6a392016-02-10 20:18:37 -08001362 return ret;
Alex Light53cb16b2014-06-12 11:26:29 -07001363}
1364
1365} // namespace art
1366
1367int main(int argc, char **argv) {
1368 return art::patchoat(argc, argv);
1369}