Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ELF_FILE_H_ |
| 18 | #define ART_RUNTIME_ELF_FILE_H_ |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 19 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 20 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 24 | #include "base/unix_file/fd_file.h" |
| 25 | #include "globals.h" |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 26 | #include "elf_utils.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 27 | #include "mem_map.h" |
| 28 | #include "os.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 32 | // Interface to GDB JIT for backtrace information. |
| 33 | extern "C" { |
| 34 | struct JITCodeEntry; |
| 35 | } |
| 36 | |
| 37 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 38 | // Used for compile time and runtime for ElfFile access. Because of |
| 39 | // the need for use at runtime, cannot directly use LLVM classes such as |
| 40 | // ELFObjectFile. |
| 41 | class ElfFile { |
| 42 | public: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 43 | static ElfFile* Open(File* file, bool writable, bool program_header_only, std::string* error_msg); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 44 | // Open with specific mmap flags, Always maps in the whole file, not just the |
| 45 | // program header sections. |
| 46 | static ElfFile* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 47 | ~ElfFile(); |
| 48 | |
| 49 | // Load segments into memory based on PT_LOAD program headers |
| 50 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 51 | const File& GetFile() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 52 | return *file_; |
| 53 | } |
| 54 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 55 | byte* Begin() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 56 | return map_->Begin(); |
| 57 | } |
| 58 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 59 | byte* End() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 60 | return map_->End(); |
| 61 | } |
| 62 | |
| 63 | size_t Size() const { |
| 64 | return map_->Size(); |
| 65 | } |
| 66 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 67 | Elf32_Ehdr& GetHeader() const; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 68 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 69 | Elf32_Word GetProgramHeaderNum() const; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 70 | Elf32_Phdr* GetProgramHeader(Elf32_Word) const; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 71 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 72 | Elf32_Word GetSectionHeaderNum() const; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 73 | Elf32_Shdr* GetSectionHeader(Elf32_Word) const; |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 74 | Elf32_Shdr* FindSectionByType(Elf32_Word type) const; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 75 | Elf32_Shdr* FindSectionByName(const std::string& name) const; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 76 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 77 | Elf32_Shdr* GetSectionNameStringSection() const; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 78 | |
| 79 | // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress. |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 80 | const byte* FindDynamicSymbolAddress(const std::string& symbol_name) const; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 81 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 82 | Elf32_Word GetSymbolNum(Elf32_Shdr&) const; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 83 | Elf32_Sym* GetSymbol(Elf32_Word section_type, Elf32_Word i) const; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 84 | |
| 85 | // Find address of symbol in specified table, returning 0 if it is |
| 86 | // not found. See FindSymbolByName for an explanation of build_map. |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 87 | Elf32_Addr FindSymbolAddress(Elf32_Word section_type, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 88 | const std::string& symbol_name, |
| 89 | bool build_map); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 90 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 91 | // Lookup a string given string section and offset. Returns nullptr for |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 92 | // special 0 offset. |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 93 | const char* GetString(Elf32_Shdr&, Elf32_Word) const; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 94 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 95 | Elf32_Word GetDynamicNum() const; |
| 96 | Elf32_Dyn& GetDynamic(Elf32_Word) const; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 97 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 98 | Elf32_Word GetRelNum(Elf32_Shdr&) const; |
| 99 | Elf32_Rel& GetRel(Elf32_Shdr&, Elf32_Word) const; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 100 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 101 | Elf32_Word GetRelaNum(Elf32_Shdr&) const; |
| 102 | Elf32_Rela& GetRela(Elf32_Shdr&, Elf32_Word) const; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 103 | |
| 104 | // Returns the expected size when the file is loaded at runtime |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 105 | size_t GetLoadedSize() const; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 106 | |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 107 | // Load segments into memory based on PT_LOAD program headers. |
| 108 | // executable is true at run time, false at compile time. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 109 | bool Load(bool executable, std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 110 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 111 | bool FixupDebugSections(off_t base_address_delta); |
| 112 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 113 | private: |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 114 | ElfFile(File* file, bool writable, bool program_header_only); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 115 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 116 | bool Setup(int prot, int flags, std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 117 | |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 118 | bool SetMap(MemMap* map, std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 119 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 120 | byte* GetProgramHeadersStart() const; |
| 121 | byte* GetSectionHeadersStart() const; |
| 122 | Elf32_Phdr& GetDynamicProgramHeader() const; |
| 123 | Elf32_Dyn* GetDynamicSectionStart() const; |
| 124 | Elf32_Sym* GetSymbolSectionStart(Elf32_Word section_type) const; |
| 125 | const char* GetStringSectionStart(Elf32_Word section_type) const; |
| 126 | Elf32_Rel* GetRelSectionStart(Elf32_Shdr&) const; |
| 127 | Elf32_Rela* GetRelaSectionStart(Elf32_Shdr&) const; |
| 128 | Elf32_Word* GetHashSectionStart() const; |
| 129 | Elf32_Word GetHashBucketNum() const; |
| 130 | Elf32_Word GetHashChainNum() const; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 131 | Elf32_Word GetHashBucket(size_t i, bool* ok) const; |
| 132 | Elf32_Word GetHashChain(size_t i, bool* ok) const; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 133 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 134 | typedef std::map<std::string, Elf32_Sym*> SymbolTable; |
| 135 | SymbolTable** GetSymbolTable(Elf32_Word section_type); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 136 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 137 | bool ValidPointer(const byte* start) const; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 138 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 139 | const Elf32_Sym* FindDynamicSymbol(const std::string& symbol_name) const; |
| 140 | |
| 141 | // Check that certain sections and their dependencies exist. |
| 142 | bool CheckSectionsExist(std::string* error_msg) const; |
| 143 | |
| 144 | // Check that the link of the first section links to the second section. |
| 145 | bool CheckSectionsLinked(const byte* source, const byte* target) const; |
| 146 | |
| 147 | // Check whether the offset is in range, and set to target to Begin() + offset if OK. |
| 148 | bool CheckAndSet(Elf32_Off offset, const char* label, byte** target, std::string* error_msg); |
| 149 | |
| 150 | // Find symbol in specified table, returning nullptr if it is not found. |
| 151 | // |
| 152 | // If build_map is true, builds a map to speed repeated access. The |
| 153 | // map does not included untyped symbol values (aka STT_NOTYPE) |
| 154 | // since they can contain duplicates. If build_map is false, the map |
| 155 | // will be used if it was already created. Typically build_map |
| 156 | // should be set unless only a small number of symbols will be |
| 157 | // looked up. |
| 158 | Elf32_Sym* FindSymbolByName(Elf32_Word section_type, |
| 159 | const std::string& symbol_name, |
| 160 | bool build_map); |
| 161 | |
| 162 | Elf32_Phdr* FindProgamHeaderByType(Elf32_Word type) const; |
| 163 | |
| 164 | Elf32_Dyn* FindDynamicByType(Elf32_Sword type) const; |
| 165 | Elf32_Word FindDynamicValueByType(Elf32_Sword type) const; |
| 166 | |
| 167 | // Lookup a string by section type. Returns nullptr for special 0 offset. |
| 168 | const char* GetString(Elf32_Word section_type, Elf32_Word) const; |
| 169 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 170 | const File* const file_; |
| 171 | const bool writable_; |
| 172 | const bool program_header_only_; |
| 173 | |
| 174 | // ELF header mapping. If program_header_only_ is false, will |
| 175 | // actually point to the entire elf file. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 176 | std::unique_ptr<MemMap> map_; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 177 | Elf32_Ehdr* header_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 178 | std::vector<MemMap*> segments_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 179 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 180 | // Pointer to start of first PT_LOAD program segment after Load() |
| 181 | // when program_header_only_ is true. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 182 | byte* base_address_; |
| 183 | |
| 184 | // The program header should always available but use GetProgramHeadersStart() to be sure. |
| 185 | byte* program_headers_start_; |
| 186 | |
| 187 | // Conditionally available values. Use accessors to ensure they exist if they are required. |
| 188 | byte* section_headers_start_; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 189 | Elf32_Phdr* dynamic_program_header_; |
| 190 | Elf32_Dyn* dynamic_section_start_; |
| 191 | Elf32_Sym* symtab_section_start_; |
| 192 | Elf32_Sym* dynsym_section_start_; |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 193 | char* strtab_section_start_; |
| 194 | char* dynstr_section_start_; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 195 | Elf32_Word* hash_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 196 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 197 | SymbolTable* symtab_symbol_table_; |
| 198 | SymbolTable* dynsym_symbol_table_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 199 | |
| 200 | // Support for GDB JIT |
| 201 | byte* jit_elf_image_; |
| 202 | JITCodeEntry* jit_gdb_entry_; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 203 | std::unique_ptr<ElfFile> gdb_file_mapping_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 204 | void GdbJITSupport(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | } // namespace art |
| 208 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 209 | #endif // ART_RUNTIME_ELF_FILE_H_ |