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 | |
| 17 | #ifndef ART_SRC_ELF_FILE_H_ |
| 18 | #define ART_SRC_ELF_FILE_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <llvm/Support/ELF.h> |
| 23 | |
| 24 | #include "base/unix_file/fd_file.h" |
| 25 | #include "globals.h" |
| 26 | #include "mem_map.h" |
| 27 | #include "os.h" |
| 28 | #include "UniquePtr.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | // Used for compile time and runtime for ElfFile access. Because of |
| 33 | // the need for use at runtime, cannot directly use LLVM classes such as |
| 34 | // ELFObjectFile. |
| 35 | class ElfFile { |
| 36 | public: |
| 37 | static ElfFile* Open(File* file, bool writable, bool program_header_only); |
| 38 | ~ElfFile(); |
| 39 | |
| 40 | // Load segments into memory based on PT_LOAD program headers |
| 41 | |
| 42 | File& GetFile() const { |
| 43 | return *file_; |
| 44 | } |
| 45 | |
| 46 | byte* Begin() { |
| 47 | return map_->Begin(); |
| 48 | } |
| 49 | |
| 50 | byte* End() { |
| 51 | return map_->End(); |
| 52 | } |
| 53 | |
| 54 | size_t Size() const { |
| 55 | return map_->Size(); |
| 56 | } |
| 57 | |
| 58 | llvm::ELF::Elf32_Ehdr& GetHeader(); |
| 59 | |
| 60 | llvm::ELF::Elf32_Word GetProgramHeaderNum(); |
| 61 | llvm::ELF::Elf32_Phdr& GetProgramHeader(llvm::ELF::Elf32_Word); |
| 62 | llvm::ELF::Elf32_Phdr* FindProgamHeaderByType(llvm::ELF::Elf32_Word type); |
| 63 | |
| 64 | llvm::ELF::Elf32_Word GetSectionHeaderNum(); |
| 65 | llvm::ELF::Elf32_Shdr& GetSectionHeader(llvm::ELF::Elf32_Word); |
| 66 | llvm::ELF::Elf32_Shdr* FindSectionByType(llvm::ELF::Elf32_Word type); |
| 67 | |
| 68 | byte* FindDynamicSymbolAddress(const std::string& symbol_name); |
| 69 | |
| 70 | static bool IsSymbolSectionType(llvm::ELF::Elf32_Word section_type); |
| 71 | llvm::ELF::Elf32_Word GetSymbolNum(llvm::ELF::Elf32_Shdr&); |
| 72 | llvm::ELF::Elf32_Sym& GetSymbol(llvm::ELF::Elf32_Word section_type, llvm::ELF::Elf32_Word i); |
| 73 | llvm::ELF::Elf32_Sym* FindSymbolByName(llvm::ELF::Elf32_Word section_type, |
| 74 | const std::string& symbol_name); |
| 75 | llvm::ELF::Elf32_Addr FindSymbolAddress(llvm::ELF::Elf32_Word section_type, |
| 76 | const std::string& symbol_name); |
| 77 | |
| 78 | char* GetString(llvm::ELF::Elf32_Shdr&, llvm::ELF::Elf32_Word); |
| 79 | |
| 80 | llvm::ELF::Elf32_Word GetDynamicNum(); |
| 81 | llvm::ELF::Elf32_Dyn& GetDynamic(llvm::ELF::Elf32_Word); |
| 82 | |
| 83 | // Returns the expected size when the file is loaded at runtime |
| 84 | size_t GetLoadedSize(); |
| 85 | |
| 86 | // Load segments into memory based on PT_LOAD program headers |
| 87 | bool Load(); |
| 88 | |
| 89 | private: |
| 90 | ElfFile(); |
| 91 | |
| 92 | bool Setup(File* file, bool writable, bool program_header_only); |
| 93 | |
| 94 | bool SetMap(MemMap* map); |
| 95 | |
| 96 | byte* GetProgramHeadersStart(); |
| 97 | byte* GetSectionHeadersStart(); |
| 98 | llvm::ELF::Elf32_Phdr& GetDynamicProgramHeader(); |
| 99 | llvm::ELF::Elf32_Dyn* GetDynamicSectionStart(); |
| 100 | llvm::ELF::Elf32_Sym* GetSymbolSectionStart(llvm::ELF::Elf32_Word section_type); |
| 101 | char* GetSymbolStringSectionStart(llvm::ELF::Elf32_Word section_type); |
| 102 | llvm::ELF::Elf32_Word* GetHashSectionStart(); |
| 103 | llvm::ELF::Elf32_Word GetHashBucketNum(); |
| 104 | llvm::ELF::Elf32_Word GetHashChainNum(); |
| 105 | llvm::ELF::Elf32_Word GetHashBucket(size_t i); |
| 106 | llvm::ELF::Elf32_Word GetHashChain(size_t i); |
| 107 | |
| 108 | File* file_; |
| 109 | bool writable_; |
| 110 | bool program_header_only_; |
| 111 | UniquePtr<MemMap> map_; |
| 112 | llvm::ELF::Elf32_Ehdr* header_; |
| 113 | std::vector<MemMap*> segments_; |
| 114 | byte* base_address_; |
| 115 | |
| 116 | // The program header should always available but use GetProgramHeadersStart() to be sure. |
| 117 | byte* program_headers_start_; |
| 118 | |
| 119 | // Conditionally available values. Use accessors to ensure they exist if they are required. |
| 120 | byte* section_headers_start_; |
| 121 | llvm::ELF::Elf32_Phdr* dynamic_program_header_; |
| 122 | llvm::ELF::Elf32_Dyn* dynamic_section_start_; |
| 123 | llvm::ELF::Elf32_Sym* symtab_section_start_; |
| 124 | llvm::ELF::Elf32_Sym* dynsym_section_start_; |
| 125 | char* strtab_section_start_; |
| 126 | char* dynstr_section_start_; |
| 127 | llvm::ELF::Elf32_Word* hash_section_start_; |
| 128 | |
| 129 | }; |
| 130 | |
| 131 | } // namespace art |
| 132 | |
| 133 | #endif // ART_SRC_ELF_FILE_H_ |