Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2011 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_RUNTIME_IMAGE_H_
     18 #define ART_RUNTIME_IMAGE_H_
     19 
     20 #include <string.h>
     21 
     22 #include "globals.h"
     23 #include "mirror/object.h"
     24 #include "utils.h"
     25 
     26 namespace art {
     27 
     28 // header of image files written by ImageWriter, read and validated by Space.
     29 class PACKED(4) ImageHeader {
     30  public:
     31   ImageHeader() {}
     32 
     33   ImageHeader(uint32_t image_begin,
     34               uint32_t image_size_,
     35               uint32_t image_bitmap_offset,
     36               uint32_t image_bitmap_size,
     37               uint32_t image_roots,
     38               uint32_t oat_checksum,
     39               uint32_t oat_file_begin,
     40               uint32_t oat_data_begin,
     41               uint32_t oat_data_end,
     42               uint32_t oat_file_end);
     43 
     44   bool IsValid() const;
     45   const char* GetMagic() const;
     46 
     47   byte* GetImageBegin() const {
     48     return reinterpret_cast<byte*>(image_begin_);
     49   }
     50 
     51   size_t GetImageSize() const {
     52     return static_cast<uint32_t>(image_size_);
     53   }
     54 
     55   size_t GetImageBitmapOffset() const {
     56     return image_bitmap_offset_;
     57   }
     58 
     59   size_t GetImageBitmapSize() const {
     60     return image_bitmap_size_;
     61   }
     62 
     63   uint32_t GetOatChecksum() const {
     64     return oat_checksum_;
     65   }
     66 
     67   void SetOatChecksum(uint32_t oat_checksum) {
     68     oat_checksum_ = oat_checksum;
     69   }
     70 
     71   byte* GetOatFileBegin() const {
     72     return reinterpret_cast<byte*>(oat_file_begin_);
     73   }
     74 
     75   byte* GetOatDataBegin() const {
     76     return reinterpret_cast<byte*>(oat_data_begin_);
     77   }
     78 
     79   byte* GetOatDataEnd() const {
     80     return reinterpret_cast<byte*>(oat_data_end_);
     81   }
     82 
     83   byte* GetOatFileEnd() const {
     84     return reinterpret_cast<byte*>(oat_file_end_);
     85   }
     86 
     87   off_t GetPatchDelta() const {
     88     return patch_delta_;
     89   }
     90 
     91   size_t GetBitmapOffset() const {
     92     return RoundUp(image_size_, kPageSize);
     93   }
     94 
     95   static std::string GetOatLocationFromImageLocation(const std::string& image) {
     96     std::string oat_filename = image;
     97     if (oat_filename.length() <= 3) {
     98       oat_filename += ".oat";
     99     } else {
    100       oat_filename.replace(oat_filename.length() - 3, 3, "oat");
    101     }
    102     return oat_filename;
    103   }
    104 
    105   enum ImageRoot {
    106     kResolutionMethod,
    107     kImtConflictMethod,
    108     kDefaultImt,
    109     kCalleeSaveMethod,
    110     kRefsOnlySaveMethod,
    111     kRefsAndArgsSaveMethod,
    112     kDexCaches,
    113     kClassRoots,
    114     kImageRootsMax,
    115   };
    116 
    117   mirror::Object* GetImageRoot(ImageRoot image_root) const
    118       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    119   mirror::ObjectArray<mirror::Object>* GetImageRoots() const;
    120 
    121   void RelocateImage(off_t delta);
    122 
    123  private:
    124   static const byte kImageMagic[4];
    125   static const byte kImageVersion[4];
    126 
    127   byte magic_[4];
    128   byte version_[4];
    129 
    130   // Required base address for mapping the image.
    131   uint32_t image_begin_;
    132 
    133   // Image size, not page aligned.
    134   uint32_t image_size_;
    135 
    136   // Image bitmap offset in the file.
    137   uint32_t image_bitmap_offset_;
    138 
    139   // Size of the image bitmap.
    140   uint32_t image_bitmap_size_;
    141 
    142   // Checksum of the oat file we link to for load time sanity check.
    143   uint32_t oat_checksum_;
    144 
    145   // Start address for oat file. Will be before oat_data_begin_ for .so files.
    146   uint32_t oat_file_begin_;
    147 
    148   // Required oat address expected by image Method::GetCode() pointers.
    149   uint32_t oat_data_begin_;
    150 
    151   // End of oat data address range for this image file.
    152   uint32_t oat_data_end_;
    153 
    154   // End of oat file address range. will be after oat_data_end_ for
    155   // .so files. Used for positioning a following alloc spaces.
    156   uint32_t oat_file_end_;
    157 
    158   // The total delta that this image has been patched.
    159   int32_t patch_delta_;
    160 
    161   // Absolute address of an Object[] of objects needed to reinitialize from an image.
    162   uint32_t image_roots_;
    163 
    164   friend class ImageWriter;
    165 };
    166 
    167 }  // namespace art
    168 
    169 #endif  // ART_RUNTIME_IMAGE_H_
    170