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 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 20 | #include <memory> |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 21 | #include <string> |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 22 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 23 | #include "base/macros.h" |
| 24 | // Explicitly include our own elf.h to avoid Linux and other dependencies. |
| 25 | #include "./elf.h" |
| 26 | #include "os.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 27 | |
| 28 | namespace art { |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 29 | template <typename ElfTypes> |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 30 | class ElfFileImpl; |
| 31 | |
| 32 | // Explicitly instantiated in elf_file.cc |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 33 | typedef ElfFileImpl<ElfTypes32> ElfFileImpl32; |
| 34 | typedef ElfFileImpl<ElfTypes64> ElfFileImpl64; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 35 | |
| 36 | // Used for compile time and runtime for ElfFile access. Because of |
| 37 | // the need for use at runtime, cannot directly use LLVM classes such as |
| 38 | // ELFObjectFile. |
| 39 | class ElfFile { |
| 40 | public: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 41 | static ElfFile* Open(File* file, bool writable, bool program_header_only, std::string* error_msg, |
| 42 | uint8_t* requested_base = nullptr); // TODO: move arg to before error_msg. |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 43 | // Open with specific mmap flags, Always maps in the whole file, not just the |
| 44 | // program header sections. |
| 45 | 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] | 46 | ~ElfFile(); |
| 47 | |
| 48 | // Load segments into memory based on PT_LOAD program headers |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 49 | bool Load(bool executable, std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 50 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 51 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 52 | |
| 53 | size_t Size() const; |
| 54 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 55 | // The start of the memory map address range for this ELF file. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 56 | uint8_t* Begin() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 57 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 58 | // The end of the memory map address range for this ELF file. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 59 | uint8_t* End() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 60 | |
| 61 | const File& GetFile() const; |
| 62 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 63 | bool GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 64 | |
| 65 | uint64_t FindSymbolAddress(unsigned section_type, |
| 66 | const std::string& symbol_name, |
| 67 | bool build_map); |
| 68 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 69 | bool GetLoadedSize(size_t* size, std::string* error_msg) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 70 | |
| 71 | // Strip an ELF file of unneeded debugging information. |
| 72 | // Returns true on success, false on failure. |
| 73 | static bool Strip(File* file, std::string* error_msg); |
| 74 | |
| 75 | // Fixup an ELF file so that that oat header will be loaded at oat_begin. |
| 76 | // Returns true on success, false on failure. |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 77 | static bool Fixup(File* file, uint64_t oat_data_begin); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 78 | |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 79 | bool Fixup(uint64_t base_address); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 80 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 81 | bool Is64Bit() const { |
| 82 | return elf64_.get() != nullptr; |
| 83 | } |
| 84 | |
| 85 | ElfFileImpl32* GetImpl32() const { |
| 86 | return elf32_.get(); |
| 87 | } |
| 88 | |
| 89 | ElfFileImpl64* GetImpl64() const { |
| 90 | return elf64_.get(); |
| 91 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 92 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 93 | private: |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 94 | explicit ElfFile(ElfFileImpl32* elf32); |
| 95 | explicit ElfFile(ElfFileImpl64* elf64); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 96 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 97 | const std::unique_ptr<ElfFileImpl32> elf32_; |
| 98 | const std::unique_ptr<ElfFileImpl64> elf64_; |
| 99 | |
| 100 | DISALLOW_COPY_AND_ASSIGN(ElfFile); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // namespace art |
| 104 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 105 | #endif // ART_RUNTIME_ELF_FILE_H_ |