Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 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_IMPL_H_ |
| 18 | #define ART_RUNTIME_ELF_FILE_IMPL_H_ |
| 19 | |
| 20 | #include <map> |
| 21 | #include <memory> |
| 22 | #include <vector> |
| 23 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 24 | // Explicitly include our own elf.h to avoid Linux and other dependencies. |
| 25 | #include "./elf.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 26 | #include "mem_map.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | extern "C" { |
| 31 | struct JITCodeEntry; |
| 32 | } |
| 33 | |
| 34 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 35 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 36 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 37 | class ElfFileImpl { |
| 38 | public: |
| 39 | static ElfFileImpl* Open(File* file, bool writable, bool program_header_only, std::string* error_msg); |
| 40 | static ElfFileImpl* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg); |
| 41 | ~ElfFileImpl(); |
| 42 | |
| 43 | const File& GetFile() const { |
| 44 | return *file_; |
| 45 | } |
| 46 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 47 | uint8_t* Begin() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 48 | return map_->Begin(); |
| 49 | } |
| 50 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 51 | uint8_t* End() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 52 | return map_->End(); |
| 53 | } |
| 54 | |
| 55 | size_t Size() const { |
| 56 | return map_->Size(); |
| 57 | } |
| 58 | |
| 59 | Elf_Ehdr& GetHeader() const; |
| 60 | |
| 61 | Elf_Word GetProgramHeaderNum() const; |
| 62 | Elf_Phdr* GetProgramHeader(Elf_Word) const; |
| 63 | |
| 64 | Elf_Word GetSectionHeaderNum() const; |
| 65 | Elf_Shdr* GetSectionHeader(Elf_Word) const; |
| 66 | Elf_Shdr* FindSectionByType(Elf_Word type) const; |
| 67 | Elf_Shdr* FindSectionByName(const std::string& name) const; |
| 68 | |
| 69 | Elf_Shdr* GetSectionNameStringSection() const; |
| 70 | |
| 71 | // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 72 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 73 | |
| 74 | static bool IsSymbolSectionType(Elf_Word section_type); |
| 75 | Elf_Word GetSymbolNum(Elf_Shdr&) const; |
| 76 | Elf_Sym* GetSymbol(Elf_Word section_type, Elf_Word i) const; |
| 77 | |
| 78 | // Find address of symbol in specified table, returning 0 if it is |
| 79 | // not found. See FindSymbolByName for an explanation of build_map. |
| 80 | Elf_Addr FindSymbolAddress(Elf_Word section_type, |
| 81 | const std::string& symbol_name, |
| 82 | bool build_map); |
| 83 | |
| 84 | // Lookup a string given string section and offset. Returns nullptr for |
| 85 | // special 0 offset. |
| 86 | const char* GetString(Elf_Shdr&, Elf_Word) const; |
| 87 | |
| 88 | Elf_Word GetDynamicNum() const; |
| 89 | Elf_Dyn& GetDynamic(Elf_Word) const; |
| 90 | |
| 91 | Elf_Word GetRelNum(Elf_Shdr&) const; |
| 92 | Elf_Rel& GetRel(Elf_Shdr&, Elf_Word) const; |
| 93 | |
| 94 | Elf_Word GetRelaNum(Elf_Shdr&) const; |
| 95 | Elf_Rela& GetRela(Elf_Shdr&, Elf_Word) const; |
| 96 | |
| 97 | // Returns the expected size when the file is loaded at runtime |
| 98 | size_t GetLoadedSize() const; |
| 99 | |
| 100 | // Load segments into memory based on PT_LOAD program headers. |
| 101 | // executable is true at run time, false at compile time. |
| 102 | bool Load(bool executable, std::string* error_msg); |
| 103 | |
| 104 | bool Fixup(uintptr_t base_address); |
| 105 | bool FixupDynamic(uintptr_t base_address); |
| 106 | bool FixupSectionHeaders(uintptr_t base_address); |
| 107 | bool FixupProgramHeaders(uintptr_t base_address); |
| 108 | bool FixupSymbols(uintptr_t base_address, bool dynamic); |
| 109 | bool FixupRelocations(uintptr_t base_address); |
| 110 | bool FixupDebugSections(off_t base_address_delta); |
| 111 | |
| 112 | bool Strip(std::string* error_msg); |
| 113 | |
| 114 | private: |
| 115 | ElfFileImpl(File* file, bool writable, bool program_header_only); |
| 116 | |
| 117 | bool Setup(int prot, int flags, std::string* error_msg); |
| 118 | |
| 119 | bool SetMap(MemMap* map, std::string* error_msg); |
| 120 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 121 | uint8_t* GetProgramHeadersStart() const; |
| 122 | uint8_t* GetSectionHeadersStart() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 123 | Elf_Phdr& GetDynamicProgramHeader() const; |
| 124 | Elf_Dyn* GetDynamicSectionStart() const; |
| 125 | Elf_Sym* GetSymbolSectionStart(Elf_Word section_type) const; |
| 126 | const char* GetStringSectionStart(Elf_Word section_type) const; |
| 127 | Elf_Rel* GetRelSectionStart(Elf_Shdr&) const; |
| 128 | Elf_Rela* GetRelaSectionStart(Elf_Shdr&) const; |
| 129 | Elf_Word* GetHashSectionStart() const; |
| 130 | Elf_Word GetHashBucketNum() const; |
| 131 | Elf_Word GetHashChainNum() const; |
| 132 | Elf_Word GetHashBucket(size_t i, bool* ok) const; |
| 133 | Elf_Word GetHashChain(size_t i, bool* ok) const; |
| 134 | |
| 135 | typedef std::map<std::string, Elf_Sym*> SymbolTable; |
| 136 | SymbolTable** GetSymbolTable(Elf_Word section_type); |
| 137 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 138 | bool ValidPointer(const uint8_t* start) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 139 | |
| 140 | const Elf_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. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 146 | bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 147 | |
| 148 | // Check whether the offset is in range, and set to target to Begin() + offset if OK. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 149 | bool CheckAndSet(Elf32_Off offset, const char* label, uint8_t** target, std::string* error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 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 | Elf_Sym* FindSymbolByName(Elf_Word section_type, |
| 160 | const std::string& symbol_name, |
| 161 | bool build_map); |
| 162 | |
| 163 | Elf_Phdr* FindProgamHeaderByType(Elf_Word type) const; |
| 164 | |
| 165 | Elf_Dyn* FindDynamicByType(Elf_Sword type) const; |
| 166 | Elf_Word FindDynamicValueByType(Elf_Sword type) const; |
| 167 | |
| 168 | // Lookup a string by section type. Returns nullptr for special 0 offset. |
| 169 | const char* GetString(Elf_Word section_type, Elf_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 | Elf_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. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 183 | uint8_t* base_address_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 184 | |
| 185 | // The program header should always available but use GetProgramHeadersStart() to be sure. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 186 | uint8_t* program_headers_start_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 187 | |
| 188 | // Conditionally available values. Use accessors to ensure they exist if they are required. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 189 | uint8_t* section_headers_start_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 190 | Elf_Phdr* dynamic_program_header_; |
| 191 | Elf_Dyn* dynamic_section_start_; |
| 192 | Elf_Sym* symtab_section_start_; |
| 193 | Elf_Sym* dynsym_section_start_; |
| 194 | char* strtab_section_start_; |
| 195 | char* dynstr_section_start_; |
| 196 | Elf_Word* hash_section_start_; |
| 197 | |
| 198 | SymbolTable* symtab_symbol_table_; |
| 199 | SymbolTable* dynsym_symbol_table_; |
| 200 | |
| 201 | // Support for GDB JIT |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 202 | uint8_t* jit_elf_image_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 203 | JITCodeEntry* jit_gdb_entry_; |
| 204 | std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 205 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, |
| 206 | Elf_Rela, Elf_Dyn, Elf_Off>> gdb_file_mapping_; |
| 207 | void GdbJITSupport(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 208 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 209 | DISALLOW_COPY_AND_ASSIGN(ElfFileImpl); |
| 210 | }; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 211 | |
| 212 | } // namespace art |
| 213 | |
| 214 | #endif // ART_RUNTIME_ELF_FILE_IMPL_H_ |