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 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 35 | template <typename ElfTypes> |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 36 | class ElfFileImpl { |
| 37 | public: |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 38 | using Elf_Addr = typename ElfTypes::Addr; |
| 39 | using Elf_Off = typename ElfTypes::Off; |
| 40 | using Elf_Half = typename ElfTypes::Half; |
| 41 | using Elf_Word = typename ElfTypes::Word; |
| 42 | using Elf_Sword = typename ElfTypes::Sword; |
| 43 | using Elf_Ehdr = typename ElfTypes::Ehdr; |
| 44 | using Elf_Shdr = typename ElfTypes::Shdr; |
| 45 | using Elf_Sym = typename ElfTypes::Sym; |
| 46 | using Elf_Rel = typename ElfTypes::Rel; |
| 47 | using Elf_Rela = typename ElfTypes::Rela; |
| 48 | using Elf_Phdr = typename ElfTypes::Phdr; |
| 49 | using Elf_Dyn = typename ElfTypes::Dyn; |
| 50 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 51 | static ElfFileImpl* Open(File* file, bool writable, bool program_header_only, |
| 52 | std::string* error_msg, uint8_t* requested_base = nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 53 | static ElfFileImpl* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg); |
| 54 | ~ElfFileImpl(); |
| 55 | |
| 56 | const File& GetFile() const { |
| 57 | return *file_; |
| 58 | } |
| 59 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 60 | uint8_t* Begin() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 61 | return map_->Begin(); |
| 62 | } |
| 63 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 64 | uint8_t* End() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 65 | return map_->End(); |
| 66 | } |
| 67 | |
| 68 | size_t Size() const { |
| 69 | return map_->Size(); |
| 70 | } |
| 71 | |
| 72 | Elf_Ehdr& GetHeader() const; |
| 73 | |
| 74 | Elf_Word GetProgramHeaderNum() const; |
| 75 | Elf_Phdr* GetProgramHeader(Elf_Word) const; |
| 76 | |
| 77 | Elf_Word GetSectionHeaderNum() const; |
| 78 | Elf_Shdr* GetSectionHeader(Elf_Word) const; |
| 79 | Elf_Shdr* FindSectionByType(Elf_Word type) const; |
| 80 | Elf_Shdr* FindSectionByName(const std::string& name) const; |
| 81 | |
| 82 | Elf_Shdr* GetSectionNameStringSection() const; |
| 83 | |
| 84 | // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 85 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 86 | |
| 87 | static bool IsSymbolSectionType(Elf_Word section_type); |
| 88 | Elf_Word GetSymbolNum(Elf_Shdr&) const; |
| 89 | Elf_Sym* GetSymbol(Elf_Word section_type, Elf_Word i) const; |
| 90 | |
| 91 | // Find address of symbol in specified table, returning 0 if it is |
| 92 | // not found. See FindSymbolByName for an explanation of build_map. |
| 93 | Elf_Addr FindSymbolAddress(Elf_Word section_type, |
| 94 | const std::string& symbol_name, |
| 95 | bool build_map); |
| 96 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 97 | // Lookup a string given string section and offset. Returns null for special 0 offset. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 98 | const char* GetString(Elf_Shdr&, Elf_Word) const; |
| 99 | |
| 100 | Elf_Word GetDynamicNum() const; |
| 101 | Elf_Dyn& GetDynamic(Elf_Word) const; |
| 102 | |
| 103 | Elf_Word GetRelNum(Elf_Shdr&) const; |
| 104 | Elf_Rel& GetRel(Elf_Shdr&, Elf_Word) const; |
| 105 | |
| 106 | Elf_Word GetRelaNum(Elf_Shdr&) const; |
| 107 | Elf_Rela& GetRela(Elf_Shdr&, Elf_Word) const; |
| 108 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 109 | // Retrieves the expected size when the file is loaded at runtime. Returns true if successful. |
| 110 | bool GetLoadedSize(size_t* size, std::string* error_msg) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 111 | |
| 112 | // Load segments into memory based on PT_LOAD program headers. |
| 113 | // executable is true at run time, false at compile time. |
| 114 | bool Load(bool executable, std::string* error_msg); |
| 115 | |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 116 | bool Fixup(Elf_Addr base_address); |
| 117 | bool FixupDynamic(Elf_Addr base_address); |
| 118 | bool FixupSectionHeaders(Elf_Addr base_address); |
| 119 | bool FixupProgramHeaders(Elf_Addr base_address); |
| 120 | bool FixupSymbols(Elf_Addr base_address, bool dynamic); |
| 121 | bool FixupRelocations(Elf_Addr base_address); |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 122 | bool FixupDebugSections(Elf_Addr base_address_delta); |
| 123 | bool ApplyOatPatchesTo(const char* target_section_name, Elf_Addr base_address_delta); |
| 124 | static void ApplyOatPatches(const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta, |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 125 | uint8_t* to_patch, const uint8_t* to_patch_end); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 126 | |
| 127 | bool Strip(std::string* error_msg); |
| 128 | |
| 129 | private: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 130 | 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] | 131 | |
| 132 | bool Setup(int prot, int flags, std::string* error_msg); |
| 133 | |
| 134 | bool SetMap(MemMap* map, std::string* error_msg); |
| 135 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 136 | uint8_t* GetProgramHeadersStart() const; |
| 137 | uint8_t* GetSectionHeadersStart() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 138 | Elf_Phdr& GetDynamicProgramHeader() const; |
| 139 | Elf_Dyn* GetDynamicSectionStart() const; |
| 140 | Elf_Sym* GetSymbolSectionStart(Elf_Word section_type) const; |
| 141 | const char* GetStringSectionStart(Elf_Word section_type) const; |
| 142 | Elf_Rel* GetRelSectionStart(Elf_Shdr&) const; |
| 143 | Elf_Rela* GetRelaSectionStart(Elf_Shdr&) const; |
| 144 | Elf_Word* GetHashSectionStart() const; |
| 145 | Elf_Word GetHashBucketNum() const; |
| 146 | Elf_Word GetHashChainNum() const; |
| 147 | Elf_Word GetHashBucket(size_t i, bool* ok) const; |
| 148 | Elf_Word GetHashChain(size_t i, bool* ok) const; |
| 149 | |
| 150 | typedef std::map<std::string, Elf_Sym*> SymbolTable; |
| 151 | SymbolTable** GetSymbolTable(Elf_Word section_type); |
| 152 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 153 | bool ValidPointer(const uint8_t* start) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 154 | |
| 155 | const Elf_Sym* FindDynamicSymbol(const std::string& symbol_name) const; |
| 156 | |
| 157 | // Check that certain sections and their dependencies exist. |
| 158 | bool CheckSectionsExist(std::string* error_msg) const; |
| 159 | |
| 160 | // 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] | 161 | bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 162 | |
| 163 | // 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] | 164 | 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] | 165 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 166 | // Find symbol in specified table, returning null if it is not found. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 167 | // |
| 168 | // If build_map is true, builds a map to speed repeated access. The |
| 169 | // map does not included untyped symbol values (aka STT_NOTYPE) |
| 170 | // since they can contain duplicates. If build_map is false, the map |
| 171 | // will be used if it was already created. Typically build_map |
| 172 | // should be set unless only a small number of symbols will be |
| 173 | // looked up. |
| 174 | Elf_Sym* FindSymbolByName(Elf_Word section_type, |
| 175 | const std::string& symbol_name, |
| 176 | bool build_map); |
| 177 | |
| 178 | Elf_Phdr* FindProgamHeaderByType(Elf_Word type) const; |
| 179 | |
| 180 | Elf_Dyn* FindDynamicByType(Elf_Sword type) const; |
| 181 | Elf_Word FindDynamicValueByType(Elf_Sword type) const; |
| 182 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 183 | // Lookup a string by section type. Returns null for special 0 offset. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 184 | const char* GetString(Elf_Word section_type, Elf_Word) const; |
| 185 | |
| 186 | const File* const file_; |
| 187 | const bool writable_; |
| 188 | const bool program_header_only_; |
| 189 | |
| 190 | // ELF header mapping. If program_header_only_ is false, will |
| 191 | // actually point to the entire elf file. |
| 192 | std::unique_ptr<MemMap> map_; |
| 193 | Elf_Ehdr* header_; |
| 194 | std::vector<MemMap*> segments_; |
| 195 | |
| 196 | // Pointer to start of first PT_LOAD program segment after Load() |
| 197 | // when program_header_only_ is true. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 198 | uint8_t* base_address_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 199 | |
| 200 | // The program header should always available but use GetProgramHeadersStart() to be sure. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 201 | uint8_t* program_headers_start_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 202 | |
| 203 | // 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] | 204 | uint8_t* section_headers_start_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 205 | Elf_Phdr* dynamic_program_header_; |
| 206 | Elf_Dyn* dynamic_section_start_; |
| 207 | Elf_Sym* symtab_section_start_; |
| 208 | Elf_Sym* dynsym_section_start_; |
| 209 | char* strtab_section_start_; |
| 210 | char* dynstr_section_start_; |
| 211 | Elf_Word* hash_section_start_; |
| 212 | |
| 213 | SymbolTable* symtab_symbol_table_; |
| 214 | SymbolTable* dynsym_symbol_table_; |
| 215 | |
| 216 | // Support for GDB JIT |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 217 | uint8_t* jit_elf_image_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 218 | JITCodeEntry* jit_gdb_entry_; |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 219 | std::unique_ptr<ElfFileImpl<ElfTypes>> gdb_file_mapping_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 220 | void GdbJITSupport(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 221 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 222 | // Override the 'base' p_vaddr in the first LOAD segment with this value (if non-null). |
| 223 | uint8_t* requested_base_; |
| 224 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 225 | DISALLOW_COPY_AND_ASSIGN(ElfFileImpl); |
| 226 | }; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 227 | |
| 228 | } // namespace art |
| 229 | |
| 230 | #endif // ART_RUNTIME_ELF_FILE_IMPL_H_ |