summaryrefslogtreecommitdiff
path: root/tools/perf/util/llvm.c
blob: ddc737194692f507219ed15885f1939dae33cd84 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SPDX-License-Identifier: GPL-2.0
#include "llvm.h"
#include "annotate.h"
#include "debug.h"
#include "dso.h"
#include "map.h"
#include "namespaces.h"
#include "srcline.h"
#include "symbol.h"
#include <fcntl.h>
#include <unistd.h>
#include <linux/zalloc.h>

#ifdef HAVE_LIBLLVM_SUPPORT
#include "llvm-c-helpers.h"
#include <llvm-c/Disassembler.h>
#include <llvm-c/Target.h>
#endif

#ifdef HAVE_LIBLLVM_SUPPORT
static void free_llvm_inline_frames(struct llvm_a2l_frame *inline_frames,
				    int num_frames)
{
	if (inline_frames != NULL) {
		for (int i = 0; i < num_frames; ++i) {
			zfree(&inline_frames[i].filename);
			zfree(&inline_frames[i].funcname);
		}
		zfree(&inline_frames);
	}
}
#endif

int llvm__addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_unused,
		     char **file __maybe_unused, unsigned int *line __maybe_unused,
		     struct dso *dso __maybe_unused, bool unwind_inlines __maybe_unused,
		     struct inline_node *node __maybe_unused, struct symbol *sym __maybe_unused)
{
#ifdef HAVE_LIBLLVM_SUPPORT
	struct llvm_a2l_frame *inline_frames = NULL;
	int num_frames = llvm_addr2line(dso_name, addr, file, line,
					node && unwind_inlines, &inline_frames);

	if (num_frames == 0 || !inline_frames) {
		/* Error, or we didn't want inlines. */
		return num_frames;
	}

	for (int i = 0; i < num_frames; ++i) {
		struct symbol *inline_sym =
			new_inline_sym(dso, sym, inline_frames[i].funcname);
		char *srcline = NULL;

		if (inline_frames[i].filename) {
			srcline =
				srcline_from_fileline(inline_frames[i].filename,
						      inline_frames[i].line);
		}
		if (inline_list__append(inline_sym, srcline, node) != 0) {
			free_llvm_inline_frames(inline_frames, num_frames);
			return 0;
		}
	}
	free_llvm_inline_frames(inline_frames, num_frames);

	return num_frames;
#else
	return -1;
#endif
}

void dso__free_a2l_llvm(struct dso *dso __maybe_unused)
{
	/* Nothing to free. */
}


#if defined(HAVE_LIBLLVM_SUPPORT)
struct find_file_offset_data {
	u64 ip;
	u64 offset;
};

/* This will be called for each PHDR in an ELF binary */
static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
{
	struct find_file_offset_data *data = arg;

	if (start <= data->ip && data->ip < start + len) {
		data->offset = pgoff + data->ip - start;
		return 1;
	}
	return 0;
}

static u8 *
read_symbol(const char *filename, struct map *map, struct symbol *sym,
	    u64 *len, bool *is_64bit)
{
	struct dso *dso = map__dso(map);
	struct nscookie nsc;
	u64 start = map__rip_2objdump(map, sym->start);
	u64 end = map__rip_2objdump(map, sym->end);
	int fd, count;
	u8 *buf = NULL;
	struct find_file_offset_data data = {
		.ip = start,
	};

	*is_64bit = false;

	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
	fd = open(filename, O_RDONLY);
	nsinfo__mountns_exit(&nsc);
	if (fd < 0)
		return NULL;

	if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
			    is_64bit) == 0)
		goto err;

	*len = end - start;
	buf = malloc(*len);
	if (buf == NULL)
		goto err;

	count = pread(fd, buf, *len, data.offset);
	close(fd);
	fd = -1;

	if ((u64)count != *len)
		goto err;

	return buf;

err:
	if (fd >= 0)
		close(fd);
	free(buf);
	return NULL;
}
#endif

/*
 * Whenever LLVM wants to resolve an address into a symbol, it calls this
 * callback. We don't ever actually _return_ anything (in particular, because
 * it puts quotation marks around what we return), but we use this as a hint
 * that there is a branch or PC-relative address in the expression that we
 * should add some textual annotation for after the instruction. The caller
 * will use this information to add the actual annotation.
 */
#ifdef HAVE_LIBLLVM_SUPPORT
struct symbol_lookup_storage {
	u64 branch_addr;
	u64 pcrel_load_addr;
};

static const char *
symbol_lookup_callback(void *disinfo, uint64_t value,
		       uint64_t *ref_type,
		       uint64_t address __maybe_unused,
		       const char **ref __maybe_unused)
{
	struct symbol_lookup_storage *storage = disinfo;

	if (*ref_type == LLVMDisassembler_ReferenceType_In_Branch)
		storage->branch_addr = value;
	else if (*ref_type == LLVMDisassembler_ReferenceType_In_PCrel_Load)
		storage->pcrel_load_addr = value;
	*ref_type = LLVMDisassembler_ReferenceType_InOut_None;
	return NULL;
}
#endif

int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
			     struct annotate_args *args __maybe_unused)
{
#ifdef HAVE_LIBLLVM_SUPPORT
	struct annotation *notes = symbol__annotation(sym);
	struct map *map = args->ms.map;
	struct dso *dso = map__dso(map);
	u64 start = map__rip_2objdump(map, sym->start);
	u8 *buf;
	u64 len;
	u64 pc;
	bool is_64bit;
	char triplet[64];
	char disasm_buf[2048];
	size_t disasm_len;
	struct disasm_line *dl;
	LLVMDisasmContextRef disasm = NULL;
	struct symbol_lookup_storage storage;
	char *line_storage = NULL;
	size_t line_storage_len = 0;
	int ret = -1;

	if (args->options->objdump_path)
		return -1;

	LLVMInitializeAllTargetInfos();
	LLVMInitializeAllTargetMCs();
	LLVMInitializeAllDisassemblers();

	buf = read_symbol(filename, map, sym, &len, &is_64bit);
	if (buf == NULL)
		return -1;

	if (arch__is(args->arch, "x86")) {
		if (is_64bit)
			scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux");
		else
			scnprintf(triplet, sizeof(triplet), "i686-pc-linux");
	} else {
		scnprintf(triplet, sizeof(triplet), "%s-linux-gnu",
			  args->arch->name);
	}

	disasm = LLVMCreateDisasm(triplet, &storage, 0, NULL,
				  symbol_lookup_callback);
	if (disasm == NULL)
		goto err;

	if (args->options->disassembler_style &&
	    !strcmp(args->options->disassembler_style, "intel"))
		LLVMSetDisasmOptions(disasm,
				     LLVMDisassembler_Option_AsmPrinterVariant);

	/*
	 * This needs to be set after AsmPrinterVariant, due to a bug in LLVM;
	 * setting AsmPrinterVariant makes a new instruction printer, making it
	 * forget about the PrintImmHex flag (which is applied before if both
	 * are given to the same call).
	 */
	LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex);

	/* add the function address and name */
	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
		  start, sym->name);

	args->offset = -1;
	args->line = disasm_buf;
	args->line_nr = 0;
	args->fileloc = NULL;
	args->ms.sym = sym;

	dl = disasm_line__new(args);
	if (dl == NULL)
		goto err;

	annotation_line__add(&dl->al, &notes->src->source);

	pc = start;
	for (u64 offset = 0; offset < len; ) {
		unsigned int ins_len;

		storage.branch_addr = 0;
		storage.pcrel_load_addr = 0;

		ins_len = LLVMDisasmInstruction(disasm, buf + offset,
						len - offset, pc,
						disasm_buf, sizeof(disasm_buf));
		if (ins_len == 0)
			goto err;
		disasm_len = strlen(disasm_buf);

		if (storage.branch_addr != 0) {
			char *name = llvm_name_for_code(dso, filename,
							storage.branch_addr);
			if (name != NULL) {
				disasm_len += scnprintf(disasm_buf + disasm_len,
							sizeof(disasm_buf) -
								disasm_len,
							" <%s>", name);
				free(name);
			}
		}
		if (storage.pcrel_load_addr != 0) {
			char *name = llvm_name_for_data(dso, filename,
							storage.pcrel_load_addr);
			disasm_len += scnprintf(disasm_buf + disasm_len,
						sizeof(disasm_buf) - disasm_len,
						"  # %#"PRIx64,
						storage.pcrel_load_addr);
			if (name) {
				disasm_len += scnprintf(disasm_buf + disasm_len,
							sizeof(disasm_buf) -
							disasm_len,
							" <%s>", name);
				free(name);
			}
		}

		args->offset = offset;
		args->line = expand_tabs(disasm_buf, &line_storage,
					 &line_storage_len);
		args->line_nr = 0;
		args->fileloc = NULL;
		args->ms.sym = sym;

		llvm_addr2line(filename, pc, &args->fileloc,
			       (unsigned int *)&args->line_nr, false, NULL);

		dl = disasm_line__new(args);
		if (dl == NULL)
			goto err;

		annotation_line__add(&dl->al, &notes->src->source);

		free(args->fileloc);
		pc += ins_len;
		offset += ins_len;
	}

	ret = 0;

err:
	LLVMDisasmDispose(disasm);
	free(buf);
	free(line_storage);
	return ret;
#else // HAVE_LIBLLVM_SUPPORT
	pr_debug("The LLVM disassembler isn't linked in for %s in %s\n",
		 sym->name, filename);
	return -1;
#endif
}