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 | |
| 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 | extern "C" { |
| 33 | struct JITCodeEntry; |
| 34 | } |
| 35 | |
| 36 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 37 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 38 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 39 | class ElfFileImpl { |
| 40 | public: |
| 41 | static ElfFileImpl* Open(File* file, bool writable, bool program_header_only, std::string* error_msg); |
| 42 | static ElfFileImpl* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg); |
| 43 | ~ElfFileImpl(); |
| 44 | |
| 45 | const File& GetFile() const { |
| 46 | return *file_; |
| 47 | } |
| 48 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 49 | uint8_t* Begin() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 50 | return map_->Begin(); |
| 51 | } |
| 52 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 53 | uint8_t* End() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 54 | return map_->End(); |
| 55 | } |
| 56 | |
| 57 | size_t Size() const { |
| 58 | return map_->Size(); |
| 59 | } |
| 60 | |
| 61 | Elf_Ehdr& GetHeader() const; |
| 62 | |
| 63 | Elf_Word GetProgramHeaderNum() const; |
| 64 | Elf_Phdr* GetProgramHeader(Elf_Word) const; |
| 65 | |
| 66 | Elf_Word GetSectionHeaderNum() const; |
| 67 | Elf_Shdr* GetSectionHeader(Elf_Word) const; |
| 68 | Elf_Shdr* FindSectionByType(Elf_Word type) const; |
| 69 | Elf_Shdr* FindSectionByName(const std::string& name) const; |
| 70 | |
| 71 | Elf_Shdr* GetSectionNameStringSection() const; |
| 72 | |
| 73 | // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 74 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 75 | |
| 76 | static bool IsSymbolSectionType(Elf_Word section_type); |
| 77 | Elf_Word GetSymbolNum(Elf_Shdr&) const; |
| 78 | Elf_Sym* GetSymbol(Elf_Word section_type, Elf_Word i) const; |
| 79 | |
| 80 | // Find address of symbol in specified table, returning 0 if it is |
| 81 | // not found. See FindSymbolByName for an explanation of build_map. |
| 82 | Elf_Addr FindSymbolAddress(Elf_Word section_type, |
| 83 | const std::string& symbol_name, |
| 84 | bool build_map); |
| 85 | |
| 86 | // Lookup a string given string section and offset. Returns nullptr for |
| 87 | // special 0 offset. |
| 88 | const char* GetString(Elf_Shdr&, Elf_Word) const; |
| 89 | |
| 90 | Elf_Word GetDynamicNum() const; |
| 91 | Elf_Dyn& GetDynamic(Elf_Word) const; |
| 92 | |
| 93 | Elf_Word GetRelNum(Elf_Shdr&) const; |
| 94 | Elf_Rel& GetRel(Elf_Shdr&, Elf_Word) const; |
| 95 | |
| 96 | Elf_Word GetRelaNum(Elf_Shdr&) const; |
| 97 | Elf_Rela& GetRela(Elf_Shdr&, Elf_Word) const; |
| 98 | |
| 99 | // Returns the expected size when the file is loaded at runtime |
| 100 | size_t GetLoadedSize() const; |
| 101 | |
| 102 | // Load segments into memory based on PT_LOAD program headers. |
| 103 | // executable is true at run time, false at compile time. |
| 104 | bool Load(bool executable, std::string* error_msg); |
| 105 | |
| 106 | bool Fixup(uintptr_t base_address); |
| 107 | bool FixupDynamic(uintptr_t base_address); |
| 108 | bool FixupSectionHeaders(uintptr_t base_address); |
| 109 | bool FixupProgramHeaders(uintptr_t base_address); |
| 110 | bool FixupSymbols(uintptr_t base_address, bool dynamic); |
| 111 | bool FixupRelocations(uintptr_t base_address); |
| 112 | bool FixupDebugSections(off_t base_address_delta); |
| 113 | |
| 114 | bool Strip(std::string* error_msg); |
| 115 | |
| 116 | private: |
| 117 | ElfFileImpl(File* file, bool writable, bool program_header_only); |
| 118 | |
| 119 | bool Setup(int prot, int flags, std::string* error_msg); |
| 120 | |
| 121 | bool SetMap(MemMap* map, std::string* error_msg); |
| 122 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 123 | uint8_t* GetProgramHeadersStart() const; |
| 124 | uint8_t* GetSectionHeadersStart() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 125 | Elf_Phdr& GetDynamicProgramHeader() const; |
| 126 | Elf_Dyn* GetDynamicSectionStart() const; |
| 127 | Elf_Sym* GetSymbolSectionStart(Elf_Word section_type) const; |
| 128 | const char* GetStringSectionStart(Elf_Word section_type) const; |
| 129 | Elf_Rel* GetRelSectionStart(Elf_Shdr&) const; |
| 130 | Elf_Rela* GetRelaSectionStart(Elf_Shdr&) const; |
| 131 | Elf_Word* GetHashSectionStart() const; |
| 132 | Elf_Word GetHashBucketNum() const; |
| 133 | Elf_Word GetHashChainNum() const; |
| 134 | Elf_Word GetHashBucket(size_t i, bool* ok) const; |
| 135 | Elf_Word GetHashChain(size_t i, bool* ok) const; |
| 136 | |
| 137 | typedef std::map<std::string, Elf_Sym*> SymbolTable; |
| 138 | SymbolTable** GetSymbolTable(Elf_Word section_type); |
| 139 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 140 | bool ValidPointer(const uint8_t* start) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 141 | |
| 142 | const Elf_Sym* FindDynamicSymbol(const std::string& symbol_name) const; |
| 143 | |
| 144 | // Check that certain sections and their dependencies exist. |
| 145 | bool CheckSectionsExist(std::string* error_msg) const; |
| 146 | |
| 147 | // 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^] | 148 | bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 149 | |
| 150 | // 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^] | 151 | 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] | 152 | |
| 153 | // Find symbol in specified table, returning nullptr if it is not found. |
| 154 | // |
| 155 | // If build_map is true, builds a map to speed repeated access. The |
| 156 | // map does not included untyped symbol values (aka STT_NOTYPE) |
| 157 | // since they can contain duplicates. If build_map is false, the map |
| 158 | // will be used if it was already created. Typically build_map |
| 159 | // should be set unless only a small number of symbols will be |
| 160 | // looked up. |
| 161 | Elf_Sym* FindSymbolByName(Elf_Word section_type, |
| 162 | const std::string& symbol_name, |
| 163 | bool build_map); |
| 164 | |
| 165 | Elf_Phdr* FindProgamHeaderByType(Elf_Word type) const; |
| 166 | |
| 167 | Elf_Dyn* FindDynamicByType(Elf_Sword type) const; |
| 168 | Elf_Word FindDynamicValueByType(Elf_Sword type) const; |
| 169 | |
| 170 | // Lookup a string by section type. Returns nullptr for special 0 offset. |
| 171 | const char* GetString(Elf_Word section_type, Elf_Word) const; |
| 172 | |
| 173 | const File* const file_; |
| 174 | const bool writable_; |
| 175 | const bool program_header_only_; |
| 176 | |
| 177 | // ELF header mapping. If program_header_only_ is false, will |
| 178 | // actually point to the entire elf file. |
| 179 | std::unique_ptr<MemMap> map_; |
| 180 | Elf_Ehdr* header_; |
| 181 | std::vector<MemMap*> segments_; |
| 182 | |
| 183 | // Pointer to start of first PT_LOAD program segment after Load() |
| 184 | // when program_header_only_ is true. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 185 | uint8_t* base_address_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 186 | |
| 187 | // The program header should always available but use GetProgramHeadersStart() to be sure. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 188 | uint8_t* program_headers_start_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 189 | |
| 190 | // 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^] | 191 | uint8_t* section_headers_start_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 192 | Elf_Phdr* dynamic_program_header_; |
| 193 | Elf_Dyn* dynamic_section_start_; |
| 194 | Elf_Sym* symtab_section_start_; |
| 195 | Elf_Sym* dynsym_section_start_; |
| 196 | char* strtab_section_start_; |
| 197 | char* dynstr_section_start_; |
| 198 | Elf_Word* hash_section_start_; |
| 199 | |
| 200 | SymbolTable* symtab_symbol_table_; |
| 201 | SymbolTable* dynsym_symbol_table_; |
| 202 | |
| 203 | // Support for GDB JIT |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 204 | uint8_t* jit_elf_image_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 205 | JITCodeEntry* jit_gdb_entry_; |
| 206 | std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 207 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, |
| 208 | Elf_Rela, Elf_Dyn, Elf_Off>> gdb_file_mapping_; |
| 209 | void GdbJITSupport(); |
| 210 | }; |
| 211 | |
| 212 | // Explicitly instantiated in elf_file.cc |
| 213 | typedef ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word, Elf32_Sword, |
| 214 | Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off> ElfFileImpl32; |
| 215 | typedef ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word, Elf64_Sword, |
| 216 | Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off> ElfFileImpl64; |
| 217 | |
| 218 | } // namespace art |
| 219 | |
| 220 | #endif // ART_RUNTIME_ELF_FILE_IMPL_H_ |