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