Repository navigation
Expand file tree
/
Copy pathbfd-disasm.cpp
More file actions
138 lines (109 loc) · 2.68 KB
/
Copy pathbfd-disasm.cpp
File metadata and controls
138 lines (109 loc) · 2.68 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
131
132
133
134
135
136
137
138
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#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 "bcc_syms.h"
#include "bcc_elf.h"
#include "bfd-disasm.h"
namespace bpftrace {
BfdDisasm::BfdDisasm(std::string &path) : size_(0)
{
fd_ = open(path.c_str(), O_RDONLY);
if (fd_ >= 0) {
struct stat st;
if (fstat(fd_, &st) == 0)
size_ = st.st_size;
}
}
BfdDisasm::~BfdDisasm()
{
if (fd_ >= 0)
close(fd_);
}
static void get_exec_path(char *tpath, size_t size)
{
const char *path = "/proc/self/exe";
ssize_t len;
len = readlink(path, tpath, size - 1);
if (len < 0)
len = 0;
tpath[len] = 0;
}
static int fprintf_nop(void *out __attribute__((unused)), const char *fmt __attribute__((unused)), ...)
{
return 0;
}
static AlignState is_aligned_buf(void *buf, uint64_t size, uint64_t offset)
{
disassembler_ftype disassemble;
struct disassemble_info info;
char tpath[4096];
bfd *bfdf;
get_exec_path(tpath, sizeof(tpath));
bfdf = bfd_openr(tpath, NULL);
if (bfdf == NULL)
return AlignState::Fail;
if (!bfd_check_format(bfdf, bfd_object))
{
bfd_close(bfdf);
return AlignState::Fail;
}
init_disassemble_info(&info, stdout, fprintf_nop);
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);
#ifdef LIBBFD_DISASM_FOUR_ARGS_SIGNATURE
disassemble = disassembler(info.arch,
bfd_big_endian(bfdf),
info.mach,
bfdf);
#else
disassemble = disassembler(bfdf);
#endif
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_);
void *buf;
if (fd_ < 0)
return aligned;
buf = malloc(size);
if (!buf) {
perror("malloc failed");
return aligned;
}
uint64_t sz;
sz = pread(fd_, buf, size, offset);
if (sz == size)
aligned = is_aligned_buf(buf, size, pc);
else
perror("pread failed");
free(buf);
return aligned;
}
} // namespace bpftrace