Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2012 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_ELF_FILE_H_
     18 #define ART_RUNTIME_ELF_FILE_H_
     19 
     20 #include <map>
     21 #include <memory>
     22 #include <vector>
     23 
     24 #include "base/unix_file/fd_file.h"
     25 #include "globals.h"
     26 #include "elf_utils.h"
     27 #include "mem_map.h"
     28 #include "os.h"
     29 
     30 namespace art {
     31 
     32 // Interface to GDB JIT for backtrace information.
     33 extern "C" {
     34   struct JITCodeEntry;
     35 }
     36 
     37 
     38 // Used for compile time and runtime for ElfFile access. Because of
     39 // the need for use at runtime, cannot directly use LLVM classes such as
     40 // ELFObjectFile.
     41 class ElfFile {
     42  public:
     43   static ElfFile* Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
     44                        uint8_t* requested_base = nullptr);  // TODO: move arg to before error_msg.
     45   // Open with specific mmap flags, Always maps in the whole file, not just the
     46   // program header sections.
     47   static ElfFile* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg);
     48   ~ElfFile();
     49 
     50   // Load segments into memory based on PT_LOAD program headers
     51 
     52   const File& GetFile() const {
     53     return *file_;
     54   }
     55 
     56   // The start of the memory map address range for this ELF file.
     57   byte* Begin() const {
     58     return map_->Begin();
     59   }
     60 
     61   // The end of the memory map address range for this ELF file.
     62   byte* End() const {
     63     return map_->End();
     64   }
     65 
     66   size_t Size() const {
     67     return map_->Size();
     68   }
     69 
     70   Elf32_Ehdr& GetHeader() const;
     71 
     72   Elf32_Word GetProgramHeaderNum() const;
     73   Elf32_Phdr* GetProgramHeader(Elf32_Word) const;
     74 
     75   Elf32_Word GetSectionHeaderNum() const;
     76   Elf32_Shdr* GetSectionHeader(Elf32_Word) const;
     77   Elf32_Shdr* FindSectionByType(Elf32_Word type) const;
     78   Elf32_Shdr* FindSectionByName(const std::string& name) const;
     79 
     80   Elf32_Shdr* GetSectionNameStringSection() const;
     81 
     82   // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress.
     83   const byte* FindDynamicSymbolAddress(const std::string& symbol_name) const;
     84 
     85   Elf32_Word GetSymbolNum(Elf32_Shdr&) const;
     86   Elf32_Sym* GetSymbol(Elf32_Word section_type, Elf32_Word i) const;
     87 
     88   // Find address of symbol in specified table, returning 0 if it is
     89   // not found. See FindSymbolByName for an explanation of build_map.
     90   Elf32_Addr FindSymbolAddress(Elf32_Word section_type,
     91                                const std::string& symbol_name,
     92                                bool build_map);
     93 
     94   // Lookup a string given string section and offset. Returns nullptr for
     95   // special 0 offset.
     96   const char* GetString(Elf32_Shdr&, Elf32_Word) const;
     97 
     98   Elf32_Word GetDynamicNum() const;
     99   Elf32_Dyn& GetDynamic(Elf32_Word) const;
    100 
    101   Elf32_Word GetRelNum(Elf32_Shdr&) const;
    102   Elf32_Rel& GetRel(Elf32_Shdr&, Elf32_Word) const;
    103 
    104   Elf32_Word GetRelaNum(Elf32_Shdr&) const;
    105   Elf32_Rela& GetRela(Elf32_Shdr&, Elf32_Word) const;
    106 
    107   // Returns the expected size when the file is loaded at runtime
    108   size_t GetLoadedSize() const;
    109 
    110   // Load segments into memory based on PT_LOAD program headers.
    111   // executable is true at run time, false at compile time.
    112   bool Load(bool executable, std::string* error_msg);
    113 
    114  private:
    115   ElfFile(File* file, bool writable, bool program_header_only, uint8_t* requested_base);
    116 
    117   bool Setup(int prot, int flags, std::string* error_msg);
    118 
    119   bool SetMap(MemMap* map, std::string* error_msg);
    120 
    121   byte* GetProgramHeadersStart() const;
    122   byte* GetSectionHeadersStart() const;
    123   Elf32_Phdr& GetDynamicProgramHeader() const;
    124   Elf32_Dyn* GetDynamicSectionStart() const;
    125   Elf32_Sym* GetSymbolSectionStart(Elf32_Word section_type) const;
    126   const char* GetStringSectionStart(Elf32_Word section_type) const;
    127   Elf32_Rel* GetRelSectionStart(Elf32_Shdr&) const;
    128   Elf32_Rela* GetRelaSectionStart(Elf32_Shdr&) const;
    129   Elf32_Word* GetHashSectionStart() const;
    130   Elf32_Word GetHashBucketNum() const;
    131   Elf32_Word GetHashChainNum() const;
    132   Elf32_Word GetHashBucket(size_t i, bool* ok) const;
    133   Elf32_Word GetHashChain(size_t i, bool* ok) const;
    134 
    135   typedef std::map<std::string, Elf32_Sym*> SymbolTable;
    136   SymbolTable** GetSymbolTable(Elf32_Word section_type);
    137 
    138   bool ValidPointer(const byte* start) const;
    139 
    140   const Elf32_Sym* FindDynamicSymbol(const std::string& symbol_name) const;
    141 
    142   // Check that certain sections and their dependencies exist.
    143   bool CheckSectionsExist(std::string* error_msg) const;
    144 
    145   // Check that the link of the first section links to the second section.
    146   bool CheckSectionsLinked(const byte* source, const byte* target) const;
    147 
    148   // Check whether the offset is in range, and set to target to Begin() + offset if OK.
    149   bool CheckAndSet(Elf32_Off offset, const char* label, byte** target, std::string* error_msg);
    150 
    151   // Find symbol in specified table, returning nullptr if it is not found.
    152   //
    153   // If build_map is true, builds a map to speed repeated access. The
    154   // map does not included untyped symbol values (aka STT_NOTYPE)
    155   // since they can contain duplicates. If build_map is false, the map
    156   // will be used if it was already created. Typically build_map
    157   // should be set unless only a small number of symbols will be
    158   // looked up.
    159   Elf32_Sym* FindSymbolByName(Elf32_Word section_type,
    160                               const std::string& symbol_name,
    161                               bool build_map);
    162 
    163   Elf32_Phdr* FindProgamHeaderByType(Elf32_Word type) const;
    164 
    165   Elf32_Dyn* FindDynamicByType(Elf32_Sword type) const;
    166   Elf32_Word FindDynamicValueByType(Elf32_Sword type) const;
    167 
    168   // Lookup a string by section type. Returns nullptr for special 0 offset.
    169   const char* GetString(Elf32_Word section_type, Elf32_Word) const;
    170 
    171   const File* const file_;
    172   const bool writable_;
    173   const bool program_header_only_;
    174 
    175   // ELF header mapping. If program_header_only_ is false, will
    176   // actually point to the entire elf file.
    177   std::unique_ptr<MemMap> map_;
    178   Elf32_Ehdr* header_;
    179   std::vector<MemMap*> segments_;
    180 
    181   // Pointer to start of first PT_LOAD program segment after Load()
    182   // when program_header_only_ is true.
    183   byte* base_address_;
    184 
    185   // The program header should always available but use GetProgramHeadersStart() to be sure.
    186   byte* program_headers_start_;
    187 
    188   // Conditionally available values. Use accessors to ensure they exist if they are required.
    189   byte* section_headers_start_;
    190   Elf32_Phdr* dynamic_program_header_;
    191   Elf32_Dyn* dynamic_section_start_;
    192   Elf32_Sym* symtab_section_start_;
    193   Elf32_Sym* dynsym_section_start_;
    194   char* strtab_section_start_;
    195   char* dynstr_section_start_;
    196   Elf32_Word* hash_section_start_;
    197 
    198   SymbolTable* symtab_symbol_table_;
    199   SymbolTable* dynsym_symbol_table_;
    200 
    201   // Support for GDB JIT
    202   byte* jit_elf_image_;
    203   JITCodeEntry* jit_gdb_entry_;
    204   std::unique_ptr<ElfFile> gdb_file_mapping_;
    205   void GdbJITSupport();
    206 
    207   // When not-null, override the base vaddr we memory map LOAD segments into.
    208   uint8_t* requested_base_;
    209 };
    210 
    211 }  // namespace art
    212 
    213 #endif  // ART_RUNTIME_ELF_FILE_H_
    214