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" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 24 | #include "base/os.h" |
David Srbecky | 5092811 | 2019-03-22 17:06:28 +0000 | [diff] [blame] | 25 | #include "elf/elf_utils.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 26 | |
| 27 | namespace art { |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 28 | |
| 29 | class MemMap; |
| 30 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 31 | template <typename ElfTypes> |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 32 | class ElfFileImpl; |
| 33 | |
| 34 | // Explicitly instantiated in elf_file.cc |
Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 35 | using ElfFileImpl32 = ElfFileImpl<ElfTypes32>; |
| 36 | using ElfFileImpl64 = ElfFileImpl<ElfTypes64>; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 37 | |
| 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: |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 43 | static ElfFile* Open(File* file, |
| 44 | bool writable, |
| 45 | bool program_header_only, |
| 46 | bool low_4gb, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 47 | /*out*/std::string* error_msg); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 48 | // Open with specific mmap flags, Always maps in the whole file, not just the |
| 49 | // program header sections. |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 50 | static ElfFile* Open(File* file, |
| 51 | int mmap_prot, |
| 52 | int mmap_flags, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 53 | /*out*/std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 54 | ~ElfFile(); |
| 55 | |
| 56 | // Load segments into memory based on PT_LOAD program headers |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 57 | bool Load(File* file, |
| 58 | bool executable, |
| 59 | bool low_4gb, |
| 60 | /*inout*/MemMap* reservation, |
| 61 | /*out*/std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 62 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 63 | const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 64 | |
| 65 | size_t Size() const; |
| 66 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 67 | // The start of the memory map address range for this ELF file. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 68 | uint8_t* Begin() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 69 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 70 | // The end of the memory map address range for this ELF file. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 71 | uint8_t* End() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 72 | |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 73 | const std::string& GetFilePath() const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 74 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 75 | 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] | 76 | |
Jeff Hao | 24ec028 | 2016-03-17 21:32:45 -0700 | [diff] [blame] | 77 | bool HasSection(const std::string& name) const; |
| 78 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 79 | uint64_t FindSymbolAddress(unsigned section_type, |
| 80 | const std::string& symbol_name, |
| 81 | bool build_map); |
| 82 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 83 | bool GetLoadedSize(size_t* size, std::string* error_msg) const; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 84 | |
| 85 | // Strip an ELF file of unneeded debugging information. |
| 86 | // Returns true on success, false on failure. |
| 87 | static bool Strip(File* file, std::string* error_msg); |
| 88 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 89 | bool Is64Bit() const { |
| 90 | return elf64_.get() != nullptr; |
| 91 | } |
| 92 | |
| 93 | ElfFileImpl32* GetImpl32() const { |
| 94 | return elf32_.get(); |
| 95 | } |
| 96 | |
| 97 | ElfFileImpl64* GetImpl64() const { |
| 98 | return elf64_.get(); |
| 99 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 100 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 101 | private: |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 102 | explicit ElfFile(ElfFileImpl32* elf32); |
| 103 | explicit ElfFile(ElfFileImpl64* elf64); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 104 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 105 | const std::unique_ptr<ElfFileImpl32> elf32_; |
| 106 | const std::unique_ptr<ElfFileImpl64> elf64_; |
| 107 | |
| 108 | DISALLOW_COPY_AND_ASSIGN(ElfFile); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace art |
| 112 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 113 | #endif // ART_RUNTIME_ELF_FILE_H_ |