blob: 3ade27faebb2751ac99c4f014895bea02f9211d5 [file] [log] [blame]
Yabin Cui8f622512015-05-05 19:58:07 -07001/*
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 Cuiec12ed92015-06-08 10:38:10 -070020#include <functional>
Yabin Cuidec43c12016-07-29 16:40:40 -070021#include <ostream>
Yabin Cui8f622512015-05-05 19:58:07 -070022#include <string>
23#include "build_id.h"
24
Yabin Cuifaa7b922021-01-11 17:35:57 -080025namespace llvm {
26class MemoryBuffer;
27}
28
29namespace simpleperf {
30
Yabin Cuidec43c12016-07-29 16:40:40 -070031// 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 Cuiea105c82020-07-22 13:01:31 -070035enum class ElfStatus {
Yabin Cuidec43c12016-07-29 16:40:40 -070036 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
46std::ostream& operator<<(std::ostream& os, const ElfStatus& status);
47
48ElfStatus GetBuildIdFromNoteFile(const std::string& filename, BuildId* build_id);
Yabin Cui8f622512015-05-05 19:58:07 -070049
Yabin Cui2c17e0d2015-06-11 14:57:21 -070050// The symbol prefix used to indicate that the symbol belongs to android linker.
51static const std::string linker_prefix = "__dl_";
52
Yabin Cuiec12ed92015-06-08 10:38:10 -070053struct ElfFileSymbol {
Yabin Cui39d3cae2015-07-13 16:23:13 -070054 uint64_t vaddr;
Yabin Cuiec12ed92015-06-08 10:38:10 -070055 uint64_t len;
56 bool is_func;
Yabin Cuib3783552015-06-11 11:15:42 -070057 bool is_label;
Yabin Cuiec12ed92015-06-08 10:38:10 -070058 bool is_in_text_section;
59 std::string name;
Yabin Cuiffaa9122016-01-15 15:25:48 -080060
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020061 ElfFileSymbol() : vaddr(0), len(0), is_func(false), is_label(false), is_in_text_section(false) {}
Yabin Cuiec12ed92015-06-08 10:38:10 -070062};
63
Yabin Cui4ad10fb2020-04-01 15:45:48 -070064struct ElfSegment {
65 uint64_t vaddr = 0;
66 uint64_t file_offset = 0;
67 uint64_t file_size = 0;
68 bool is_executable = false;
Yabin Cui4e4cbbc2020-12-14 15:26:19 -080069 bool is_load = false;
Yabin Cui4ad10fb2020-04-01 15:45:48 -070070};
71
Yabin Cui7078c672020-11-10 16:24:12 -080072struct ElfSection {
73 std::string name;
74 uint64_t vaddr = 0;
75 uint64_t file_offset = 0;
Yabin Cui16f41ff2021-03-23 14:58:25 -070076 uint64_t size = 0;
Yabin Cui7078c672020-11-10 16:24:12 -080077};
78
Yabin Cui02e20332020-03-16 19:38:23 -070079class ElfFile {
80 public:
Yabin Cuib60b33c2020-07-15 11:58:41 -070081 // Report error instead of returning status.
82 static std::unique_ptr<ElfFile> Open(const std::string& filename);
Yabin Cui01947032020-06-30 14:36:46 -070083 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 Cui02e20332020-03-16 19:38:23 -070090 virtual ~ElfFile() {}
91
Yabin Cui4ad10fb2020-04-01 15:45:48 -070092 virtual bool Is64Bit() = 0;
Yabin Cui02e20332020-03-16 19:38:23 -070093 virtual llvm::MemoryBuffer* GetMemoryBuffer() = 0;
Yabin Cui4ad10fb2020-04-01 15:45:48 -070094 virtual std::vector<ElfSegment> GetProgramHeader() = 0;
Yabin Cui7078c672020-11-10 16:24:12 -080095 virtual std::vector<ElfSection> GetSectionHeader() = 0;
Yabin Cui3a880452020-06-29 16:37:31 -070096 virtual ElfStatus GetBuildId(BuildId* build_id) = 0;
Yabin Cui02e20332020-03-16 19:38:23 -070097
Yabin Cui01947032020-06-30 14:36:46 -070098 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 Cui90c3b302020-07-01 10:09:16 -0700103 virtual uint64_t ReadMinExecutableVaddr(uint64_t* file_offset_of_min_vaddr) = 0;
Yabin Cuib60b33c2020-07-15 11:58:41 -0700104 virtual bool VaddrToOff(uint64_t vaddr, uint64_t* file_offset) = 0;
Yabin Cui01947032020-06-30 14:36:46 -0700105
Yabin Cui02e20332020-03-16 19:38:23 -0700106 protected:
107 ElfFile() {}
108};
109
Yabin Cuia5bdf0a2015-06-22 19:41:39 -0700110bool IsArmMappingSymbol(const char* name);
Yabin Cui02e20332020-03-16 19:38:23 -0700111ElfStatus IsValidElfFile(int fd, uint64_t file_offset = 0);
Yabin Cui9e43e9f2018-03-09 15:57:13 -0800112bool IsValidElfFileMagic(const char* buf, size_t buf_size);
Yabin Cuif79a0082016-11-14 11:23:14 -0800113bool GetBuildIdFromNoteSection(const char* section, size_t section_size, BuildId* build_id);
Yabin Cuia5bdf0a2015-06-22 19:41:39 -0700114
Yabin Cuifaa7b922021-01-11 17:35:57 -0800115} // namespace simpleperf
116
Yabin Cui8f622512015-05-05 19:58:07 -0700117#endif // SIMPLE_PERF_READ_ELF_H_