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 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 20 | #include <string> |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 21 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 22 | #include "base/unix_file/fd_file.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 23 | #include "elf_file_impl.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // Used for compile time and runtime for ElfFile access. Because of |
| 28 | // the need for use at runtime, cannot directly use LLVM classes such as |
| 29 | // ELFObjectFile. |
| 30 | class ElfFile { |
| 31 | public: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 32 | 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] | 33 | // Open with specific mmap flags, Always maps in the whole file, not just the |
| 34 | // program header sections. |
| 35 | 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] | 36 | ~ElfFile(); |
| 37 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 38 | const bool is_elf64_; |
| 39 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 40 | // Load segments into memory based on PT_LOAD program headers |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 41 | bool Load(bool executable, std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 42 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 43 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 44 | |
| 45 | size_t Size() const; |
| 46 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 47 | uint8_t* Begin() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 48 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 49 | uint8_t* End() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 50 | |
| 51 | const File& GetFile() const; |
| 52 | |
| 53 | bool GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size); |
| 54 | |
| 55 | uint64_t FindSymbolAddress(unsigned section_type, |
| 56 | const std::string& symbol_name, |
| 57 | bool build_map); |
| 58 | |
| 59 | size_t GetLoadedSize() const; |
| 60 | |
| 61 | // Strip an ELF file of unneeded debugging information. |
| 62 | // Returns true on success, false on failure. |
| 63 | static bool Strip(File* file, std::string* error_msg); |
| 64 | |
| 65 | // Fixup an ELF file so that that oat header will be loaded at oat_begin. |
| 66 | // Returns true on success, false on failure. |
| 67 | static bool Fixup(File* file, uintptr_t oat_data_begin); |
| 68 | |
| 69 | bool Fixup(uintptr_t base_address); |
| 70 | |
| 71 | ElfFileImpl32* GetImpl32() const; |
| 72 | ElfFileImpl64* GetImpl64() const; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 73 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 74 | private: |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 75 | explicit ElfFile(ElfFileImpl32* elf32); |
| 76 | explicit ElfFile(ElfFileImpl64* elf64); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 77 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 78 | union ElfFileContainer { |
| 79 | ElfFileImpl32* elf32_; |
| 80 | ElfFileImpl64* elf64_; |
| 81 | } elf_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace art |
| 85 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 86 | #endif // ART_RUNTIME_ELF_FILE_H_ |