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