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 | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 25 | // Read ELF functions are called in different situations, so it is hard to |
| 26 | // decide whether to report error or not. So read ELF functions don't report |
| 27 | // error when something wrong happens, instead they return ElfStatus, which |
| 28 | // identifies different errors met while reading elf file. |
| 29 | enum ElfStatus { |
| 30 | NO_ERROR, |
| 31 | FILE_NOT_FOUND, |
| 32 | READ_FAILED, |
| 33 | FILE_MALFORMED, |
| 34 | NO_SYMBOL_TABLE, |
| 35 | NO_BUILD_ID, |
| 36 | BUILD_ID_MISMATCH, |
| 37 | SECTION_NOT_FOUND, |
| 38 | }; |
| 39 | |
| 40 | std::ostream& operator<<(std::ostream& os, const ElfStatus& status); |
| 41 | |
| 42 | ElfStatus GetBuildIdFromNoteFile(const std::string& filename, BuildId* build_id); |
| 43 | ElfStatus GetBuildIdFromElfFile(const std::string& filename, BuildId* build_id); |
| 44 | ElfStatus GetBuildIdFromEmbeddedElfFile(const std::string& filename, uint64_t file_offset, |
| 45 | uint32_t file_size, BuildId* build_id); |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 46 | |
Yabin Cui | 2c17e0d | 2015-06-11 14:57:21 -0700 | [diff] [blame] | 47 | // The symbol prefix used to indicate that the symbol belongs to android linker. |
| 48 | static const std::string linker_prefix = "__dl_"; |
| 49 | |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 50 | struct ElfFileSymbol { |
Yabin Cui | 39d3cae | 2015-07-13 16:23:13 -0700 | [diff] [blame] | 51 | uint64_t vaddr; |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 52 | uint64_t len; |
| 53 | bool is_func; |
Yabin Cui | b378355 | 2015-06-11 11:15:42 -0700 | [diff] [blame] | 54 | bool is_label; |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 55 | bool is_in_text_section; |
| 56 | std::string name; |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 57 | |
| 58 | ElfFileSymbol() : vaddr(0), len(0), is_func(false), is_label(false), is_in_text_section(false) { |
| 59 | } |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 62 | ElfStatus ParseSymbolsFromElfFile(const std::string& filename, |
| 63 | const BuildId& expected_build_id, |
| 64 | const std::function<void(const ElfFileSymbol&)>& callback); |
| 65 | ElfStatus ParseSymbolsFromEmbeddedElfFile(const std::string& filename, uint64_t file_offset, |
| 66 | uint32_t file_size, const BuildId& expected_build_id, |
| 67 | const std::function<void(const ElfFileSymbol&)>& callback); |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 68 | ElfStatus ParseSymbolsFromElfFileInMemory(const char* data, size_t size, |
| 69 | const std::function<void(const ElfFileSymbol&)>& callback); |
| 70 | ElfStatus ParseDynamicSymbolsFromElfFile(const std::string& filename, |
| 71 | const std::function<void(const ElfFileSymbol&)>& callback); |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 72 | |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 73 | ElfStatus ReadMinExecutableVirtualAddressFromElfFile(const std::string& filename, |
| 74 | const BuildId& expected_build_id, |
| 75 | uint64_t* min_addr); |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 76 | ElfStatus ReadMinExecutableVirtualAddressFromEmbeddedElfFile(const std::string& filename, |
| 77 | uint64_t file_offset, |
| 78 | uint32_t file_size, |
| 79 | const BuildId& expected_build_id, |
| 80 | uint64_t* min_vaddr); |
Yabin Cui | 547c60e | 2015-10-12 16:56:05 -0700 | [diff] [blame] | 81 | |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 82 | ElfStatus ReadSectionFromElfFile(const std::string& filename, const std::string& section_name, |
| 83 | std::string* content); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 84 | |
Yabin Cui | a5bdf0a | 2015-06-22 19:41:39 -0700 | [diff] [blame] | 85 | // Expose the following functions for unit tests. |
| 86 | bool IsArmMappingSymbol(const char* name); |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 87 | ElfStatus IsValidElfFile(int fd); |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 88 | bool IsValidElfFileMagic(const char* buf, size_t buf_size); |
Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 89 | ElfStatus IsValidElfPath(const std::string& filename); |
Yabin Cui | f79a008 | 2016-11-14 11:23:14 -0800 | [diff] [blame] | 90 | bool GetBuildIdFromNoteSection(const char* section, size_t section_size, BuildId* build_id); |
Yabin Cui | a5bdf0a | 2015-06-22 19:41:39 -0700 | [diff] [blame] | 91 | |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 92 | #endif // SIMPLE_PERF_READ_ELF_H_ |