xref: /linux/tools/perf/util/llvm.c (revision bca753204e9eab3bd56907ec5f5ed151094d7742)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "llvm.h"
3 #include "annotate.h"
4 #include "debug.h"
5 #include "dso.h"
6 #include "map.h"
7 #include "namespaces.h"
8 #include "srcline.h"
9 #include "symbol.h"
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <linux/zalloc.h>
14 
15 #ifdef HAVE_LIBLLVM_SUPPORT
16 #include "llvm-c-helpers.h"
17 #include <llvm-c/Disassembler.h>
18 #include <llvm-c/Target.h>
19 #endif
20 
21 #ifdef HAVE_LIBLLVM_SUPPORT
22 static void free_llvm_inline_frames(struct llvm_a2l_frame *inline_frames,
23 				    int num_frames)
24 {
25 	if (inline_frames != NULL) {
26 		for (int i = 0; i < num_frames; ++i) {
27 			zfree(&inline_frames[i].filename);
28 			zfree(&inline_frames[i].funcname);
29 		}
30 		zfree(&inline_frames);
31 	}
32 }
33 #endif
34 
35 int llvm__addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_unused,
36 		     char **file __maybe_unused, unsigned int *line __maybe_unused,
37 		     struct dso *dso __maybe_unused, bool unwind_inlines __maybe_unused,
38 		     struct inline_node *node __maybe_unused, struct symbol *sym __maybe_unused)
39 {
40 #ifdef HAVE_LIBLLVM_SUPPORT
41 	struct llvm_a2l_frame *inline_frames = NULL;
42 	int num_frames = llvm_addr2line(dso_name, addr, file, line,
43 					node && unwind_inlines, &inline_frames);
44 
45 	if (num_frames == 0 || !inline_frames) {
46 		/* Error, or we didn't want inlines. */
47 		return num_frames;
48 	}
49 
50 	for (int i = 0; i < num_frames; ++i) {
51 		struct symbol *inline_sym =
52 			new_inline_sym(dso, sym, inline_frames[i].funcname);
53 		char *srcline = NULL;
54 
55 		if (inline_frames[i].filename) {
56 			srcline =
57 				srcline_from_fileline(inline_frames[i].filename,
58 						      inline_frames[i].line);
59 		}
60 		if (inline_list__append(inline_sym, srcline, node) != 0) {
61 			free_llvm_inline_frames(inline_frames, num_frames);
62 			return 0;
63 		}
64 	}
65 	free_llvm_inline_frames(inline_frames, num_frames);
66 
67 	return num_frames;
68 #else
69 	return -1;
70 #endif
71 }
72 
73 void dso__free_a2l_llvm(struct dso *dso __maybe_unused)
74 {
75 	/* Nothing to free. */
76 }
77 
78 #ifdef HAVE_LIBLLVM_SUPPORT
79 static void init_llvm(void)
80 {
81 	static bool init;
82 
83 	if (!init) {
84 		LLVMInitializeAllTargetInfos();
85 		LLVMInitializeAllTargetMCs();
86 		LLVMInitializeAllDisassemblers();
87 		init = true;
88 	}
89 }
90 
91 /*
92  * Whenever LLVM wants to resolve an address into a symbol, it calls this
93  * callback. We don't ever actually _return_ anything (in particular, because
94  * it puts quotation marks around what we return), but we use this as a hint
95  * that there is a branch or PC-relative address in the expression that we
96  * should add some textual annotation for after the instruction. The caller
97  * will use this information to add the actual annotation.
98  */
99 struct symbol_lookup_storage {
100 	u64 branch_addr;
101 	u64 pcrel_load_addr;
102 };
103 
104 static const char *
105 symbol_lookup_callback(void *disinfo, uint64_t value,
106 		       uint64_t *ref_type,
107 		       uint64_t address __maybe_unused,
108 		       const char **ref __maybe_unused)
109 {
110 	struct symbol_lookup_storage *storage = disinfo;
111 
112 	if (*ref_type == LLVMDisassembler_ReferenceType_In_Branch)
113 		storage->branch_addr = value;
114 	else if (*ref_type == LLVMDisassembler_ReferenceType_In_PCrel_Load)
115 		storage->pcrel_load_addr = value;
116 	*ref_type = LLVMDisassembler_ReferenceType_InOut_None;
117 	return NULL;
118 }
119 #endif
120 
121 int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
122 			     struct annotate_args *args __maybe_unused)
123 {
124 #ifdef HAVE_LIBLLVM_SUPPORT
125 	struct annotation *notes = symbol__annotation(sym);
126 	struct map *map = args->ms.map;
127 	struct dso *dso = map__dso(map);
128 	u64 start = map__rip_2objdump(map, sym->start);
129 	/* Malloc-ed buffer containing instructions read from disk. */
130 	u8 *code_buf = NULL;
131 	/* Pointer to code to be disassembled. */
132 	const u8 *buf;
133 	u64 buf_len;
134 	u64 pc;
135 	bool is_64bit;
136 	char disasm_buf[2048];
137 	size_t disasm_len;
138 	struct disasm_line *dl;
139 	LLVMDisasmContextRef disasm = NULL;
140 	struct symbol_lookup_storage storage;
141 	char *line_storage = NULL;
142 	size_t line_storage_len = 0;
143 	int ret = -1;
144 
145 	if (args->options->objdump_path)
146 		return -1;
147 
148 	buf = dso__read_symbol(dso, filename, map, sym,
149 			       &code_buf, &buf_len, &is_64bit);
150 	if (buf == NULL)
151 		return errno;
152 
153 	init_llvm();
154 	if (arch__is(args->arch, "x86")) {
155 		const char *triplet = is_64bit ? "x86_64-pc-linux" : "i686-pc-linux";
156 
157 		disasm = LLVMCreateDisasm(triplet, &storage, /*tag_type=*/0,
158 					  /*get_op_info=*/NULL, symbol_lookup_callback);
159 	} else {
160 		char triplet[64];
161 
162 		scnprintf(triplet, sizeof(triplet), "%s-linux-gnu",
163 			  args->arch->name);
164 		disasm = LLVMCreateDisasm(triplet, &storage, /*tag_type=*/0,
165 					  /*get_op_info=*/NULL, symbol_lookup_callback);
166 	}
167 
168 	if (disasm == NULL)
169 		goto err;
170 
171 	if (args->options->disassembler_style &&
172 	    !strcmp(args->options->disassembler_style, "intel"))
173 		LLVMSetDisasmOptions(disasm,
174 				     LLVMDisassembler_Option_AsmPrinterVariant);
175 
176 	/*
177 	 * This needs to be set after AsmPrinterVariant, due to a bug in LLVM;
178 	 * setting AsmPrinterVariant makes a new instruction printer, making it
179 	 * forget about the PrintImmHex flag (which is applied before if both
180 	 * are given to the same call).
181 	 */
182 	LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex);
183 
184 	/* add the function address and name */
185 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
186 		  start, sym->name);
187 
188 	args->offset = -1;
189 	args->line = disasm_buf;
190 	args->line_nr = 0;
191 	args->fileloc = NULL;
192 	args->ms.sym = sym;
193 
194 	dl = disasm_line__new(args);
195 	if (dl == NULL)
196 		goto err;
197 
198 	annotation_line__add(&dl->al, &notes->src->source);
199 
200 	pc = start;
201 	for (u64 offset = 0; offset < buf_len; ) {
202 		unsigned int ins_len;
203 
204 		storage.branch_addr = 0;
205 		storage.pcrel_load_addr = 0;
206 
207 		/*
208 		 * LLVM's API has the code be disassembled as non-const, cast
209 		 * here as we may be disassembling from mapped read-only memory.
210 		 */
211 		ins_len = LLVMDisasmInstruction(disasm, (u8 *)(buf + offset),
212 						buf_len - offset, pc,
213 						disasm_buf, sizeof(disasm_buf));
214 		if (ins_len == 0)
215 			goto err;
216 		disasm_len = strlen(disasm_buf);
217 
218 		if (storage.branch_addr != 0) {
219 			char *name = llvm_name_for_code(dso, filename,
220 							storage.branch_addr);
221 			if (name != NULL) {
222 				disasm_len += scnprintf(disasm_buf + disasm_len,
223 							sizeof(disasm_buf) -
224 								disasm_len,
225 							" <%s>", name);
226 				free(name);
227 			}
228 		}
229 		if (storage.pcrel_load_addr != 0) {
230 			char *name = llvm_name_for_data(dso, filename,
231 							storage.pcrel_load_addr);
232 			disasm_len += scnprintf(disasm_buf + disasm_len,
233 						sizeof(disasm_buf) - disasm_len,
234 						"  # %#"PRIx64,
235 						storage.pcrel_load_addr);
236 			if (name) {
237 				disasm_len += scnprintf(disasm_buf + disasm_len,
238 							sizeof(disasm_buf) -
239 							disasm_len,
240 							" <%s>", name);
241 				free(name);
242 			}
243 		}
244 
245 		args->offset = offset;
246 		args->line = expand_tabs(disasm_buf, &line_storage,
247 					 &line_storage_len);
248 		args->line_nr = 0;
249 		args->fileloc = NULL;
250 		args->ms.sym = sym;
251 
252 		llvm_addr2line(filename, pc, &args->fileloc,
253 			       (unsigned int *)&args->line_nr, false, NULL);
254 
255 		dl = disasm_line__new(args);
256 		if (dl == NULL)
257 			goto err;
258 
259 		annotation_line__add(&dl->al, &notes->src->source);
260 
261 		free(args->fileloc);
262 		pc += ins_len;
263 		offset += ins_len;
264 	}
265 
266 	ret = 0;
267 
268 err:
269 	LLVMDisasmDispose(disasm);
270 	free(code_buf);
271 	free(line_storage);
272 	return ret;
273 #else // HAVE_LIBLLVM_SUPPORT
274 	pr_debug("The LLVM disassembler isn't linked in for %s in %s\n",
275 		 sym->name, filename);
276 	return -1;
277 #endif
278 }
279