xref: /linux/tools/perf/util/llvm.c (revision 257046a36750a6db6e0bab4612d7c8f9774b6b83)
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 #ifdef HAVE_LIBLLVM_SUPPORT
74 static void init_llvm(void)
75 {
76 	static bool init;
77 
78 	if (!init) {
79 		LLVMInitializeAllTargetInfos();
80 		LLVMInitializeAllTargetMCs();
81 		LLVMInitializeAllDisassemblers();
82 		init = true;
83 	}
84 }
85 
86 /*
87  * Whenever LLVM wants to resolve an address into a symbol, it calls this
88  * callback. We don't ever actually _return_ anything (in particular, because
89  * it puts quotation marks around what we return), but we use this as a hint
90  * that there is a branch or PC-relative address in the expression that we
91  * should add some textual annotation for after the instruction. The caller
92  * will use this information to add the actual annotation.
93  */
94 struct symbol_lookup_storage {
95 	u64 branch_addr;
96 	u64 pcrel_load_addr;
97 };
98 
99 static const char *
100 symbol_lookup_callback(void *disinfo, uint64_t value,
101 		       uint64_t *ref_type,
102 		       uint64_t address __maybe_unused,
103 		       const char **ref __maybe_unused)
104 {
105 	struct symbol_lookup_storage *storage = disinfo;
106 
107 	if (*ref_type == LLVMDisassembler_ReferenceType_In_Branch)
108 		storage->branch_addr = value;
109 	else if (*ref_type == LLVMDisassembler_ReferenceType_In_PCrel_Load)
110 		storage->pcrel_load_addr = value;
111 	*ref_type = LLVMDisassembler_ReferenceType_InOut_None;
112 	return NULL;
113 }
114 #endif
115 
116 int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
117 			     struct annotate_args *args __maybe_unused)
118 {
119 #ifdef HAVE_LIBLLVM_SUPPORT
120 	struct annotation *notes = symbol__annotation(sym);
121 	struct map *map = args->ms.map;
122 	struct dso *dso = map__dso(map);
123 	u64 start = map__rip_2objdump(map, sym->start);
124 	/* Malloc-ed buffer containing instructions read from disk. */
125 	u8 *code_buf = NULL;
126 	/* Pointer to code to be disassembled. */
127 	const u8 *buf;
128 	u64 buf_len;
129 	u64 pc;
130 	bool is_64bit;
131 	char disasm_buf[2048];
132 	size_t disasm_len;
133 	struct disasm_line *dl;
134 	LLVMDisasmContextRef disasm = NULL;
135 	struct symbol_lookup_storage storage;
136 	char *line_storage = NULL;
137 	size_t line_storage_len = 0;
138 	int ret = -1;
139 
140 	if (args->options->objdump_path)
141 		return -1;
142 
143 	buf = dso__read_symbol(dso, filename, map, sym,
144 			       &code_buf, &buf_len, &is_64bit);
145 	if (buf == NULL)
146 		return errno;
147 
148 	init_llvm();
149 	if (arch__is(args->arch, "x86")) {
150 		const char *triplet = is_64bit ? "x86_64-pc-linux" : "i686-pc-linux";
151 
152 		disasm = LLVMCreateDisasm(triplet, &storage, /*tag_type=*/0,
153 					  /*get_op_info=*/NULL, symbol_lookup_callback);
154 	} else {
155 		char triplet[64];
156 
157 		scnprintf(triplet, sizeof(triplet), "%s-linux-gnu",
158 			  args->arch->name);
159 		disasm = LLVMCreateDisasm(triplet, &storage, /*tag_type=*/0,
160 					  /*get_op_info=*/NULL, symbol_lookup_callback);
161 	}
162 
163 	if (disasm == NULL)
164 		goto err;
165 
166 	if (args->options->disassembler_style &&
167 	    !strcmp(args->options->disassembler_style, "intel"))
168 		LLVMSetDisasmOptions(disasm,
169 				     LLVMDisassembler_Option_AsmPrinterVariant);
170 
171 	/*
172 	 * This needs to be set after AsmPrinterVariant, due to a bug in LLVM;
173 	 * setting AsmPrinterVariant makes a new instruction printer, making it
174 	 * forget about the PrintImmHex flag (which is applied before if both
175 	 * are given to the same call).
176 	 */
177 	LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex);
178 
179 	/* add the function address and name */
180 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
181 		  start, sym->name);
182 
183 	args->offset = -1;
184 	args->line = disasm_buf;
185 	args->line_nr = 0;
186 	args->fileloc = NULL;
187 	args->ms.sym = sym;
188 
189 	dl = disasm_line__new(args);
190 	if (dl == NULL)
191 		goto err;
192 
193 	annotation_line__add(&dl->al, &notes->src->source);
194 
195 	pc = start;
196 	for (u64 offset = 0; offset < buf_len; ) {
197 		unsigned int ins_len;
198 
199 		storage.branch_addr = 0;
200 		storage.pcrel_load_addr = 0;
201 
202 		/*
203 		 * LLVM's API has the code be disassembled as non-const, cast
204 		 * here as we may be disassembling from mapped read-only memory.
205 		 */
206 		ins_len = LLVMDisasmInstruction(disasm, (u8 *)(buf + offset),
207 						buf_len - offset, pc,
208 						disasm_buf, sizeof(disasm_buf));
209 		if (ins_len == 0)
210 			goto err;
211 		disasm_len = strlen(disasm_buf);
212 
213 		if (storage.branch_addr != 0) {
214 			char *name = llvm_name_for_code(dso, filename,
215 							storage.branch_addr);
216 			if (name != NULL) {
217 				disasm_len += scnprintf(disasm_buf + disasm_len,
218 							sizeof(disasm_buf) -
219 								disasm_len,
220 							" <%s>", name);
221 				free(name);
222 			}
223 		}
224 		if (storage.pcrel_load_addr != 0) {
225 			char *name = llvm_name_for_data(dso, filename,
226 							storage.pcrel_load_addr);
227 			disasm_len += scnprintf(disasm_buf + disasm_len,
228 						sizeof(disasm_buf) - disasm_len,
229 						"  # %#"PRIx64,
230 						storage.pcrel_load_addr);
231 			if (name) {
232 				disasm_len += scnprintf(disasm_buf + disasm_len,
233 							sizeof(disasm_buf) -
234 							disasm_len,
235 							" <%s>", name);
236 				free(name);
237 			}
238 		}
239 
240 		args->offset = offset;
241 		args->line = expand_tabs(disasm_buf, &line_storage,
242 					 &line_storage_len);
243 		args->line_nr = 0;
244 		args->fileloc = NULL;
245 		args->ms.sym = sym;
246 
247 		llvm_addr2line(filename, pc, &args->fileloc,
248 			       (unsigned int *)&args->line_nr, false, NULL);
249 
250 		dl = disasm_line__new(args);
251 		if (dl == NULL)
252 			goto err;
253 
254 		annotation_line__add(&dl->al, &notes->src->source);
255 
256 		free(args->fileloc);
257 		pc += ins_len;
258 		offset += ins_len;
259 	}
260 
261 	ret = 0;
262 
263 err:
264 	LLVMDisasmDispose(disasm);
265 	free(code_buf);
266 	free(line_storage);
267 	return ret;
268 #else // HAVE_LIBLLVM_SUPPORT
269 	pr_debug("The LLVM disassembler isn't linked in for %s in %s\n",
270 		 sym->name, filename);
271 	return -1;
272 #endif
273 }
274