Home | History | Annotate | Download | only in patchoat
      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 
     17 #ifndef ART_PATCHOAT_PATCHOAT_H_
     18 #define ART_PATCHOAT_PATCHOAT_H_
     19 
     20 #include "base/macros.h"
     21 #include "base/mutex.h"
     22 #include "instruction_set.h"
     23 #include "os.h"
     24 #include "elf_file.h"
     25 #include "elf_utils.h"
     26 #include "gc/accounting/space_bitmap.h"
     27 #include "gc/heap.h"
     28 #include "utils.h"
     29 
     30 namespace art {
     31 
     32 class ImageHeader;
     33 
     34 namespace mirror {
     35 class Object;
     36 class Reference;
     37 class Class;
     38 class ArtMethod;
     39 };  // namespace mirror
     40 
     41 class PatchOat {
     42  public:
     43   static bool Patch(File* oat_in, off_t delta, File* oat_out, TimingLogger* timings);
     44 
     45   static bool Patch(const std::string& art_location, off_t delta, File* art_out, InstructionSet isa,
     46                     TimingLogger* timings);
     47 
     48   static bool Patch(const File* oat_in, const std::string& art_location,
     49                     off_t delta, File* oat_out, File* art_out, InstructionSet isa,
     50                     TimingLogger* timings);
     51 
     52  private:
     53   // Takes ownership only of the ElfFile. All other pointers are only borrowed.
     54   PatchOat(ElfFile* oat_file, off_t delta, TimingLogger* timings)
     55       : oat_file_(oat_file), delta_(delta), timings_(timings) {}
     56   PatchOat(MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
     57            MemMap* heap, off_t delta, TimingLogger* timings)
     58       : image_(image), bitmap_(bitmap), heap_(heap),
     59         delta_(delta), timings_(timings) {}
     60   PatchOat(ElfFile* oat_file, MemMap* image, gc::accounting::ContinuousSpaceBitmap* bitmap,
     61            MemMap* heap, off_t delta, TimingLogger* timings)
     62       : oat_file_(oat_file), image_(image), bitmap_(bitmap), heap_(heap),
     63         delta_(delta), timings_(timings) {}
     64   ~PatchOat() {}
     65 
     66   static void BitmapCallback(mirror::Object* obj, void* arg)
     67       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     68     reinterpret_cast<PatchOat*>(arg)->VisitObject(obj);
     69   }
     70 
     71   void VisitObject(mirror::Object* obj)
     72       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     73   void FixupMethod(mirror::ArtMethod* object, mirror::ArtMethod* copy)
     74       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     75   bool InHeap(mirror::Object*);
     76 
     77   bool CheckOatFile();
     78 
     79   // Patches oat in place, modifying the oat_file given to the constructor.
     80   bool PatchElf();
     81   bool PatchTextSection();
     82   bool PatchOatHeader();
     83   bool PatchSymbols(Elf32_Shdr* section);
     84 
     85   bool PatchImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     86 
     87   bool WriteElf(File* out);
     88   bool WriteImage(File* out);
     89 
     90   mirror::Object* RelocatedCopyOf(mirror::Object*);
     91   mirror::Object* RelocatedAddressOf(mirror::Object* obj);
     92 
     93   // Walks through the old image and patches the mmap'd copy of it to the new offset. It does not
     94   // change the heap.
     95   class PatchVisitor {
     96   public:
     97     PatchVisitor(PatchOat* patcher, mirror::Object* copy) : patcher_(patcher), copy_(copy) {}
     98     ~PatchVisitor() {}
     99     void operator() (mirror::Object* obj, MemberOffset off, bool b) const
    100       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
    101     // For reference classes.
    102     void operator() (mirror::Class* cls, mirror::Reference* ref) const
    103       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
    104   private:
    105     PatchOat* patcher_;
    106     mirror::Object* copy_;
    107   };
    108 
    109   // The elf file we are patching.
    110   std::unique_ptr<ElfFile> oat_file_;
    111   // A mmap of the image we are patching. This is modified.
    112   const MemMap* image_;
    113   // The heap we are patching. This is not modified.
    114   gc::accounting::ContinuousSpaceBitmap* bitmap_;
    115   // The heap we are patching. This is not modified.
    116   const MemMap* heap_;
    117   // The amount we are changing the offset by.
    118   off_t delta_;
    119   TimingLogger* timings_;
    120 
    121   DISALLOW_IMPLICIT_CONSTRUCTORS(PatchOat);
    122 };
    123 
    124 }  // namespace art
    125 #endif  // ART_PATCHOAT_PATCHOAT_H_
    126