Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 SIMPLE_PERF_READ_ELF_H_ |
| 18 | #define SIMPLE_PERF_READ_ELF_H_ |
| 19 | |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 20 | #include <functional> |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 21 | #include <ostream> |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | #include "build_id.h" |
| 24 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 25 | namespace llvm { |
| 26 | class MemoryBuffer; |
| 27 | } |
| 28 | |
| 29 | namespace simpleperf { |
| 30 | |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 31 | // Read ELF functions are called in different situations, so it is hard to |
| 32 | // decide whether to report error or not. So read ELF functions don't report |
| 33 | // error when something wrong happens, instead they return ElfStatus, which |
| 34 | // identifies different errors met while reading elf file. |
Yabin Cui | ea105c8 | 2020-07-22 13:01:31 -0700 | [diff] [blame] | 35 | enum class ElfStatus { |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 36 | NO_ERROR, |
| 37 | FILE_NOT_FOUND, |
| 38 | READ_FAILED, |
| 39 | FILE_MALFORMED, |
| 40 | NO_SYMBOL_TABLE, |
| 41 | NO_BUILD_ID, |
| 42 | BUILD_ID_MISMATCH, |
| 43 | SECTION_NOT_FOUND, |
| 44 | }; |
| 45 | |
| 46 | std::ostream& operator<<(std::ostream& os, const ElfStatus& status); |
| 47 | |
| 48 | ElfStatus GetBuildIdFromNoteFile(const std::string& filename, BuildId* build_id); |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 49 | |
Yabin Cui | 2c17e0d | 2015-06-11 14:57:21 -0700 | [diff] [blame] | 50 | // The symbol prefix used to indicate that the symbol belongs to android linker. |
| 51 | static const std::string linker_prefix = "__dl_"; |
| 52 | |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 53 | struct ElfFileSymbol { |
Yabin Cui | 39d3cae | 2015-07-13 16:23:13 -0700 | [diff] [blame] | 54 | uint64_t vaddr; |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 55 | uint64_t len; |
| 56 | bool is_func; |
Yabin Cui | b378355 | 2015-06-11 11:15:42 -0700 | [diff] [blame] | 57 | bool is_label; |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 58 | bool is_in_text_section; |
| 59 | std::string name; |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 60 | |
ThiƩbaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 61 | ElfFileSymbol() : vaddr(0), len(0), is_func(false), is_label(false), is_in_text_section(false) {} |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Yabin Cui | 4ad10fb | 2020-04-01 15:45:48 -0700 | [diff] [blame] | 64 | struct ElfSegment { |
| 65 | uint64_t vaddr = 0; |
| 66 | uint64_t file_offset = 0; |
| 67 | uint64_t file_size = 0; |
| 68 | bool is_executable = false; |
Yabin Cui | 4e4cbbc | 2020-12-14 15:26:19 -0800 | [diff] [blame] | 69 | bool is_load = false; |
Yabin Cui | 4ad10fb | 2020-04-01 15:45:48 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 72 | struct ElfSection { |
| 73 | std::string name; |
| 74 | uint64_t vaddr = 0; |
| 75 | uint64_t file_offset = 0; |
Yabin Cui | 16f41ff | 2021-03-23 14:58:25 -0700 | [diff] [blame] | 76 | uint64_t size = 0; |
Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
Yabin Cui | 02e2033 | 2020-03-16 19:38:23 -0700 | [diff] [blame] | 79 | class ElfFile { |
| 80 | public: |
Yabin Cui | b60b33c | 2020-07-15 11:58:41 -0700 | [diff] [blame] | 81 | // Report error instead of returning status. |
| 82 | static std::unique_ptr<ElfFile> Open(const std::string& filename); |
Yabin Cui | 0194703 | 2020-06-30 14:36:46 -0700 | [diff] [blame] | 83 | static std::unique_ptr<ElfFile> Open(const std::string& filename, ElfStatus* status) { |
| 84 | return Open(filename, nullptr, status); |
| 85 | } |
| 86 | |
| 87 | static std::unique_ptr<ElfFile> Open(const std::string& filename, |
| 88 | const BuildId* expected_build_id, ElfStatus* status); |
| 89 | static std::unique_ptr<ElfFile> Open(const char* data, size_t size, ElfStatus* status); |
Yabin Cui | 02e2033 | 2020-03-16 19:38:23 -0700 | [diff] [blame] | 90 | virtual ~ElfFile() {} |
| 91 | |
Yabin Cui | 4ad10fb | 2020-04-01 15:45:48 -0700 | [diff] [blame] | 92 | virtual bool Is64Bit() = 0; |
Yabin Cui | 02e2033 | 2020-03-16 19:38:23 -0700 | [diff] [blame] | 93 | virtual llvm::MemoryBuffer* GetMemoryBuffer() = 0; |
Yabin Cui | 4ad10fb | 2020-04-01 15:45:48 -0700 | [diff] [blame] | 94 | virtual std::vector<ElfSegment> GetProgramHeader() = 0; |
Yabin Cui | 7078c67 | 2020-11-10 16:24:12 -0800 | [diff] [blame] | 95 | virtual std::vector<ElfSection> GetSectionHeader() = 0; |
Yabin Cui | 3a88045 | 2020-06-29 16:37:31 -0700 | [diff] [blame] | 96 | virtual ElfStatus GetBuildId(BuildId* build_id) = 0; |
Yabin Cui | 02e2033 | 2020-03-16 19:38:23 -0700 | [diff] [blame] | 97 | |
Yabin Cui | 0194703 | 2020-06-30 14:36:46 -0700 | [diff] [blame] | 98 | using ParseSymbolCallback = std::function<void(const ElfFileSymbol&)>; |
| 99 | virtual ElfStatus ParseSymbols(const ParseSymbolCallback& callback) = 0; |
| 100 | virtual void ParseDynamicSymbols(const ParseSymbolCallback& callback) = 0; |
| 101 | |
| 102 | virtual ElfStatus ReadSection(const std::string& section_name, std::string* content) = 0; |
Yabin Cui | 90c3b30 | 2020-07-01 10:09:16 -0700 | [diff] [blame] | 103 | virtual uint64_t ReadMinExecutableVaddr(uint64_t* file_offset_of_min_vaddr) = 0; |
Yabin Cui | b60b33c | 2020-07-15 11:58:41 -0700 | [diff] [blame] | 104 | virtual bool VaddrToOff(uint64_t vaddr, uint64_t* file_offset) = 0; |
Yabin Cui | 0194703 | 2020-06-30 14:36:46 -0700 | [diff] [blame] | 105 | |
Yabin Cui | 02e2033 | 2020-03-16 19:38:23 -0700 | [diff] [blame] | 106 | protected: |
| 107 | ElfFile() {} |
| 108 | }; |
| 109 | |
Yabin Cui | a5bdf0a | 2015-06-22 19:41:39 -0700 | [diff] [blame] | 110 | bool IsArmMappingSymbol(const char* name); |
Yabin Cui | 02e2033 | 2020-03-16 19:38:23 -0700 | [diff] [blame] | 111 | ElfStatus IsValidElfFile(int fd, uint64_t file_offset = 0); |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 112 | bool IsValidElfFileMagic(const char* buf, size_t buf_size); |
Yabin Cui | f79a008 | 2016-11-14 11:23:14 -0800 | [diff] [blame] | 113 | bool GetBuildIdFromNoteSection(const char* section, size_t section_size, BuildId* build_id); |
Yabin Cui | a5bdf0a | 2015-06-22 19:41:39 -0700 | [diff] [blame] | 114 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 115 | } // namespace simpleperf |
| 116 | |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 117 | #endif // SIMPLE_PERF_READ_ELF_H_ |