Repository navigation
Expand file tree
/
Copy pathbfd-disasm.cpp
More file actions
130 lines (104 loc) · 3.06 KB
/
Copy pathbfd-disasm.cpp
File metadata and controls
130 lines (104 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <fcntl.h>
#include <memory>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
// bfd.h assumes everyone is using autotools and will error out unless
// PACKAGE is defined. Some distros patch this check out.
#define PACKAGE "bpftrace"
#include <bfd.h>
#include <dis-asm.h>
#include <filesystem>
#include "bfd-disasm.h"
#include "util/system.h"
namespace bpftrace {
BfdDisasm::BfdDisasm(std::string &path)
{
fd_ = open(path.c_str(), O_RDONLY);
if (fd_ >= 0) {
std::error_code ec;
std::filesystem::path fs_path{ path };
std::uintmax_t file_size = std::filesystem::file_size(fs_path, ec);
if (!ec) {
size_ = file_size;
}
}
}
BfdDisasm::~BfdDisasm()
{
if (fd_ >= 0)
close(fd_);
}
// NOLINTNEXTLINE(modernize-avoid-variadic-functions): required by libbfd API
static int fprintf_nop(void *out __attribute__((unused)),
const char *fmt __attribute__((unused)),
...)
{
return 0;
}
#ifdef LIBBFD_INIT_DISASM_INFO_FOUR_ARGS_SIGNATURE
// NOLINTNEXTLINE(modernize-avoid-variadic-functions): required by libbfd API
static int fprintf_styled_nop(void *out __attribute__((unused)),
enum disassembler_style s __attribute__((unused)),
const char *fmt __attribute__((unused)),
...)
{
return 0;
}
#endif
static AlignState is_aligned_buf(void *buf, uint64_t size, uint64_t offset)
{
disassembler_ftype disassemble;
struct disassemble_info info;
auto tpath = util::get_pid_exe("self");
if (!tpath) {
return AlignState::Fail;
}
bfd *bfdf;
bfdf = bfd_openr(tpath->c_str(), nullptr);
if (bfdf == nullptr)
return AlignState::Fail;
if (!bfd_check_format(bfdf, bfd_object)) {
bfd_close(bfdf);
return AlignState::Fail;
}
#ifdef LIBBFD_INIT_DISASM_INFO_FOUR_ARGS_SIGNATURE
init_disassemble_info(&info, stdout, fprintf_nop, fprintf_styled_nop);
#else
init_disassemble_info(&info, stdout, fprintf_nop);
#endif
info.arch = bfd_get_arch(bfdf);
info.mach = bfd_get_mach(bfdf);
info.buffer = static_cast<bfd_byte *>(buf);
info.buffer_length = size;
disassemble_init_for_target(&info);
disassemble = disassembler(info.arch, bfd_big_endian(bfdf), info.mach, bfdf);
uint64_t pc = 0;
int count;
do {
count = disassemble(pc, &info);
pc += static_cast<uint64_t>(count);
if (pc == offset) {
bfd_close(bfdf);
return AlignState::Ok;
}
} while (static_cast<uint64_t>(count) > 0 && pc < size && pc < offset);
bfd_close(bfdf);
return AlignState::NotAlign;
}
AlignState BfdDisasm::is_aligned(uint64_t offset, uint64_t pc)
{
AlignState aligned = AlignState::Fail;
// 100 bytes should be enough to cover next instruction behind pc
uint64_t size = std::min(pc + 100, size_);
auto buf = std::make_unique<char[]>(size);
if (fd_ < 0)
return aligned;
uint64_t sz = pread(fd_, buf.get(), size, offset);
if (sz == size)
aligned = is_aligned_buf(buf.get(), size, pc);
else
perror("pread failed");
return aligned;
}
} // namespace bpftrace