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> |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 22 | #include <type_traits> |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 25 | // Explicitly include our own elf.h to avoid Linux and other dependencies. |
| 26 | #include "./elf.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 27 | #include "mem_map.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | extern "C" { |
| 32 | struct JITCodeEntry; |
| 33 | } |
| 34 | |
| 35 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 36 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 37 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 38 | class ElfFileImpl { |
| 39 | public: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 40 | static ElfFileImpl* Open(File* file, bool writable, bool program_header_only, |
| 41 | std::string* error_msg, uint8_t* requested_base = nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 106 | bool Fixup(Elf_Addr base_address); |
| 107 | bool FixupDynamic(Elf_Addr base_address); |
| 108 | bool FixupSectionHeaders(Elf_Addr base_address); |
| 109 | bool FixupProgramHeaders(Elf_Addr base_address); |
| 110 | bool FixupSymbols(Elf_Addr base_address, bool dynamic); |
| 111 | bool FixupRelocations(Elf_Addr base_address); |
| 112 | bool FixupDebugSections(typename std::make_signed<Elf_Off>::type base_address_delta); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame^] | 113 | bool ApplyOatPatchesTo(const char* target_section_name, |
| 114 | typename std::make_signed<Elf_Off>::type base_address_delta); |
| 115 | static bool ApplyOatPatches(const uint8_t* patches, const uint8_t* patches_end, |
| 116 | const char* target_section_name, |
| 117 | typename std::make_signed<Elf_Off>::type delta, |
| 118 | uint8_t* to_patch, const uint8_t* to_patch_end); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 119 | |
| 120 | bool Strip(std::string* error_msg); |
| 121 | |
| 122 | private: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 123 | ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 124 | |
| 125 | bool Setup(int prot, int flags, std::string* error_msg); |
| 126 | |
| 127 | bool SetMap(MemMap* map, std::string* error_msg); |
| 128 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 129 | uint8_t* GetProgramHeadersStart() const; |
| 130 | uint8_t* GetSectionHeadersStart() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 131 | Elf_Phdr& GetDynamicProgramHeader() const; |
| 132 | Elf_Dyn* GetDynamicSectionStart() const; |
| 133 | Elf_Sym* GetSymbolSectionStart(Elf_Word section_type) const; |
| 134 | const char* GetStringSectionStart(Elf_Word section_type) const; |
| 135 | Elf_Rel* GetRelSectionStart(Elf_Shdr&) const; |
| 136 | Elf_Rela* GetRelaSectionStart(Elf_Shdr&) const; |
| 137 | Elf_Word* GetHashSectionStart() const; |
| 138 | Elf_Word GetHashBucketNum() const; |
| 139 | Elf_Word GetHashChainNum() const; |
| 140 | Elf_Word GetHashBucket(size_t i, bool* ok) const; |
| 141 | Elf_Word GetHashChain(size_t i, bool* ok) const; |
| 142 | |
| 143 | typedef std::map<std::string, Elf_Sym*> SymbolTable; |
| 144 | SymbolTable** GetSymbolTable(Elf_Word section_type); |
| 145 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 146 | bool ValidPointer(const uint8_t* start) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 147 | |
| 148 | const Elf_Sym* FindDynamicSymbol(const std::string& symbol_name) const; |
| 149 | |
| 150 | // Check that certain sections and their dependencies exist. |
| 151 | bool CheckSectionsExist(std::string* error_msg) const; |
| 152 | |
| 153 | // 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] | 154 | bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 155 | |
| 156 | // 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] | 157 | 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] | 158 | |
| 159 | // Find symbol in specified table, returning nullptr if it is not found. |
| 160 | // |
| 161 | // If build_map is true, builds a map to speed repeated access. The |
| 162 | // map does not included untyped symbol values (aka STT_NOTYPE) |
| 163 | // since they can contain duplicates. If build_map is false, the map |
| 164 | // will be used if it was already created. Typically build_map |
| 165 | // should be set unless only a small number of symbols will be |
| 166 | // looked up. |
| 167 | Elf_Sym* FindSymbolByName(Elf_Word section_type, |
| 168 | const std::string& symbol_name, |
| 169 | bool build_map); |
| 170 | |
| 171 | Elf_Phdr* FindProgamHeaderByType(Elf_Word type) const; |
| 172 | |
| 173 | Elf_Dyn* FindDynamicByType(Elf_Sword type) const; |
| 174 | Elf_Word FindDynamicValueByType(Elf_Sword type) const; |
| 175 | |
| 176 | // Lookup a string by section type. Returns nullptr for special 0 offset. |
| 177 | const char* GetString(Elf_Word section_type, Elf_Word) const; |
| 178 | |
| 179 | const File* const file_; |
| 180 | const bool writable_; |
| 181 | const bool program_header_only_; |
| 182 | |
| 183 | // ELF header mapping. If program_header_only_ is false, will |
| 184 | // actually point to the entire elf file. |
| 185 | std::unique_ptr<MemMap> map_; |
| 186 | Elf_Ehdr* header_; |
| 187 | std::vector<MemMap*> segments_; |
| 188 | |
| 189 | // Pointer to start of first PT_LOAD program segment after Load() |
| 190 | // when program_header_only_ is true. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 191 | uint8_t* base_address_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 192 | |
| 193 | // The program header should always available but use GetProgramHeadersStart() to be sure. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 194 | uint8_t* program_headers_start_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 195 | |
| 196 | // 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] | 197 | uint8_t* section_headers_start_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 198 | Elf_Phdr* dynamic_program_header_; |
| 199 | Elf_Dyn* dynamic_section_start_; |
| 200 | Elf_Sym* symtab_section_start_; |
| 201 | Elf_Sym* dynsym_section_start_; |
| 202 | char* strtab_section_start_; |
| 203 | char* dynstr_section_start_; |
| 204 | Elf_Word* hash_section_start_; |
| 205 | |
| 206 | SymbolTable* symtab_symbol_table_; |
| 207 | SymbolTable* dynsym_symbol_table_; |
| 208 | |
| 209 | // Support for GDB JIT |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 210 | uint8_t* jit_elf_image_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 211 | JITCodeEntry* jit_gdb_entry_; |
| 212 | std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 213 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, |
| 214 | Elf_Rela, Elf_Dyn, Elf_Off>> gdb_file_mapping_; |
| 215 | void GdbJITSupport(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 216 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 217 | // Override the 'base' p_vaddr in the first LOAD segment with this value (if non-null). |
| 218 | uint8_t* requested_base_; |
| 219 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 220 | DISALLOW_COPY_AND_ASSIGN(ElfFileImpl); |
| 221 | }; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 222 | |
| 223 | } // namespace art |
| 224 | |
| 225 | #endif // ART_RUNTIME_ELF_FILE_IMPL_H_ |