xref: /linux/tools/perf/util/disasm.c (revision 782959ac248ac3cbac80f7476d4e0410662ff400)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <ctype.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <inttypes.h>
6 #include <libgen.h>
7 #include <regex.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 
11 #include <linux/string.h>
12 #include <subcmd/run-command.h>
13 
14 #include "annotate.h"
15 #include "annotate-data.h"
16 #include "build-id.h"
17 #include "debug.h"
18 #include "disasm.h"
19 #include "dso.h"
20 #include "env.h"
21 #include "evsel.h"
22 #include "map.h"
23 #include "maps.h"
24 #include "namespaces.h"
25 #include "srcline.h"
26 #include "symbol.h"
27 #include "util.h"
28 
29 static regex_t	 file_lineno;
30 
31 /* These can be referred from the arch-dependent code */
32 static struct ins_ops call_ops;
33 static struct ins_ops dec_ops;
34 static struct ins_ops jump_ops;
35 static struct ins_ops mov_ops;
36 static struct ins_ops nop_ops;
37 static struct ins_ops lock_ops;
38 static struct ins_ops ret_ops;
39 
40 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
41 			   struct ins_operands *ops, int max_ins_name);
42 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
43 			   struct ins_operands *ops, int max_ins_name);
44 
45 static void ins__sort(struct arch *arch);
46 static int disasm_line__parse(char *line, const char **namep, char **rawp);
47 
48 static __attribute__((constructor)) void symbol__init_regexpr(void)
49 {
50 	regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
51 }
52 
53 static int arch__grow_instructions(struct arch *arch)
54 {
55 	struct ins *new_instructions;
56 	size_t new_nr_allocated;
57 
58 	if (arch->nr_instructions_allocated == 0 && arch->instructions)
59 		goto grow_from_non_allocated_table;
60 
61 	new_nr_allocated = arch->nr_instructions_allocated + 128;
62 	new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
63 	if (new_instructions == NULL)
64 		return -1;
65 
66 out_update_instructions:
67 	arch->instructions = new_instructions;
68 	arch->nr_instructions_allocated = new_nr_allocated;
69 	return 0;
70 
71 grow_from_non_allocated_table:
72 	new_nr_allocated = arch->nr_instructions + 128;
73 	new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
74 	if (new_instructions == NULL)
75 		return -1;
76 
77 	memcpy(new_instructions, arch->instructions, arch->nr_instructions);
78 	goto out_update_instructions;
79 }
80 
81 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
82 {
83 	struct ins *ins;
84 
85 	if (arch->nr_instructions == arch->nr_instructions_allocated &&
86 	    arch__grow_instructions(arch))
87 		return -1;
88 
89 	ins = &arch->instructions[arch->nr_instructions];
90 	ins->name = strdup(name);
91 	if (!ins->name)
92 		return -1;
93 
94 	ins->ops  = ops;
95 	arch->nr_instructions++;
96 
97 	ins__sort(arch);
98 	return 0;
99 }
100 
101 #include "arch/arc/annotate/instructions.c"
102 #include "arch/arm/annotate/instructions.c"
103 #include "arch/arm64/annotate/instructions.c"
104 #include "arch/csky/annotate/instructions.c"
105 #include "arch/loongarch/annotate/instructions.c"
106 #include "arch/mips/annotate/instructions.c"
107 #include "arch/x86/annotate/instructions.c"
108 #include "arch/powerpc/annotate/instructions.c"
109 #include "arch/riscv64/annotate/instructions.c"
110 #include "arch/s390/annotate/instructions.c"
111 #include "arch/sparc/annotate/instructions.c"
112 
113 static struct arch architectures[] = {
114 	{
115 		.name = "arc",
116 		.init = arc__annotate_init,
117 	},
118 	{
119 		.name = "arm",
120 		.init = arm__annotate_init,
121 	},
122 	{
123 		.name = "arm64",
124 		.init = arm64__annotate_init,
125 	},
126 	{
127 		.name = "csky",
128 		.init = csky__annotate_init,
129 	},
130 	{
131 		.name = "mips",
132 		.init = mips__annotate_init,
133 		.objdump = {
134 			.comment_char = '#',
135 		},
136 	},
137 	{
138 		.name = "x86",
139 		.init = x86__annotate_init,
140 		.instructions = x86__instructions,
141 		.nr_instructions = ARRAY_SIZE(x86__instructions),
142 		.insn_suffix = "bwlq",
143 		.objdump =  {
144 			.comment_char = '#',
145 			.register_char = '%',
146 			.memory_ref_char = '(',
147 			.imm_char = '$',
148 		},
149 #ifdef HAVE_DWARF_SUPPORT
150 		.update_insn_state = update_insn_state_x86,
151 #endif
152 	},
153 	{
154 		.name = "powerpc",
155 		.init = powerpc__annotate_init,
156 	},
157 	{
158 		.name = "riscv64",
159 		.init = riscv64__annotate_init,
160 	},
161 	{
162 		.name = "s390",
163 		.init = s390__annotate_init,
164 		.objdump =  {
165 			.comment_char = '#',
166 		},
167 	},
168 	{
169 		.name = "sparc",
170 		.init = sparc__annotate_init,
171 		.objdump = {
172 			.comment_char = '#',
173 		},
174 	},
175 	{
176 		.name = "loongarch",
177 		.init = loongarch__annotate_init,
178 		.objdump = {
179 			.comment_char = '#',
180 		},
181 	},
182 };
183 
184 static int arch__key_cmp(const void *name, const void *archp)
185 {
186 	const struct arch *arch = archp;
187 
188 	return strcmp(name, arch->name);
189 }
190 
191 static int arch__cmp(const void *a, const void *b)
192 {
193 	const struct arch *aa = a;
194 	const struct arch *ab = b;
195 
196 	return strcmp(aa->name, ab->name);
197 }
198 
199 static void arch__sort(void)
200 {
201 	const int nmemb = ARRAY_SIZE(architectures);
202 
203 	qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
204 }
205 
206 struct arch *arch__find(const char *name)
207 {
208 	const int nmemb = ARRAY_SIZE(architectures);
209 	static bool sorted;
210 
211 	if (!sorted) {
212 		arch__sort();
213 		sorted = true;
214 	}
215 
216 	return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
217 }
218 
219 bool arch__is(struct arch *arch, const char *name)
220 {
221 	return !strcmp(arch->name, name);
222 }
223 
224 static void ins_ops__delete(struct ins_operands *ops)
225 {
226 	if (ops == NULL)
227 		return;
228 	zfree(&ops->source.raw);
229 	zfree(&ops->source.name);
230 	zfree(&ops->target.raw);
231 	zfree(&ops->target.name);
232 }
233 
234 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
235 			      struct ins_operands *ops, int max_ins_name)
236 {
237 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
238 }
239 
240 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
241 		   struct ins_operands *ops, int max_ins_name)
242 {
243 	if (ins->ops->scnprintf)
244 		return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
245 
246 	return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
247 }
248 
249 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
250 {
251 	if (!arch || !arch->ins_is_fused)
252 		return false;
253 
254 	return arch->ins_is_fused(arch, ins1, ins2);
255 }
256 
257 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
258 {
259 	char *endptr, *tok, *name;
260 	struct map *map = ms->map;
261 	struct addr_map_symbol target = {
262 		.ms = { .map = map, },
263 	};
264 
265 	ops->target.addr = strtoull(ops->raw, &endptr, 16);
266 
267 	name = strchr(endptr, '<');
268 	if (name == NULL)
269 		goto indirect_call;
270 
271 	name++;
272 
273 	if (arch->objdump.skip_functions_char &&
274 	    strchr(name, arch->objdump.skip_functions_char))
275 		return -1;
276 
277 	tok = strchr(name, '>');
278 	if (tok == NULL)
279 		return -1;
280 
281 	*tok = '\0';
282 	ops->target.name = strdup(name);
283 	*tok = '>';
284 
285 	if (ops->target.name == NULL)
286 		return -1;
287 find_target:
288 	target.addr = map__objdump_2mem(map, ops->target.addr);
289 
290 	if (maps__find_ams(ms->maps, &target) == 0 &&
291 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
292 		ops->target.sym = target.ms.sym;
293 
294 	return 0;
295 
296 indirect_call:
297 	tok = strchr(endptr, '*');
298 	if (tok != NULL) {
299 		endptr++;
300 
301 		/* Indirect call can use a non-rip register and offset: callq  *0x8(%rbx).
302 		 * Do not parse such instruction.  */
303 		if (strstr(endptr, "(%r") == NULL)
304 			ops->target.addr = strtoull(endptr, NULL, 16);
305 	}
306 	goto find_target;
307 }
308 
309 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
310 			   struct ins_operands *ops, int max_ins_name)
311 {
312 	if (ops->target.sym)
313 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
314 
315 	if (ops->target.addr == 0)
316 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
317 
318 	if (ops->target.name)
319 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
320 
321 	return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
322 }
323 
324 static struct ins_ops call_ops = {
325 	.parse	   = call__parse,
326 	.scnprintf = call__scnprintf,
327 };
328 
329 bool ins__is_call(const struct ins *ins)
330 {
331 	return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops;
332 }
333 
334 /*
335  * Prevents from matching commas in the comment section, e.g.:
336  * ffff200008446e70:       b.cs    ffff2000084470f4 <generic_exec_single+0x314>  // b.hs, b.nlast
337  *
338  * and skip comma as part of function arguments, e.g.:
339  * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
340  */
341 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
342 {
343 	if (ops->jump.raw_comment && c > ops->jump.raw_comment)
344 		return NULL;
345 
346 	if (ops->jump.raw_func_start && c > ops->jump.raw_func_start)
347 		return NULL;
348 
349 	return c;
350 }
351 
352 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
353 {
354 	struct map *map = ms->map;
355 	struct symbol *sym = ms->sym;
356 	struct addr_map_symbol target = {
357 		.ms = { .map = map, },
358 	};
359 	const char *c = strchr(ops->raw, ',');
360 	u64 start, end;
361 
362 	ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char);
363 	ops->jump.raw_func_start = strchr(ops->raw, '<');
364 
365 	c = validate_comma(c, ops);
366 
367 	/*
368 	 * Examples of lines to parse for the _cpp_lex_token@@Base
369 	 * function:
370 	 *
371 	 * 1159e6c: jne    115aa32 <_cpp_lex_token@@Base+0xf92>
372 	 * 1159e8b: jne    c469be <cpp_named_operator2name@@Base+0xa72>
373 	 *
374 	 * The first is a jump to an offset inside the same function,
375 	 * the second is to another function, i.e. that 0xa72 is an
376 	 * offset in the cpp_named_operator2name@@base function.
377 	 */
378 	/*
379 	 * skip over possible up to 2 operands to get to address, e.g.:
380 	 * tbnz	 w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
381 	 */
382 	if (c++ != NULL) {
383 		ops->target.addr = strtoull(c, NULL, 16);
384 		if (!ops->target.addr) {
385 			c = strchr(c, ',');
386 			c = validate_comma(c, ops);
387 			if (c++ != NULL)
388 				ops->target.addr = strtoull(c, NULL, 16);
389 		}
390 	} else {
391 		ops->target.addr = strtoull(ops->raw, NULL, 16);
392 	}
393 
394 	target.addr = map__objdump_2mem(map, ops->target.addr);
395 	start = map__unmap_ip(map, sym->start);
396 	end = map__unmap_ip(map, sym->end);
397 
398 	ops->target.outside = target.addr < start || target.addr > end;
399 
400 	/*
401 	 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
402 
403 		cpp_named_operator2name@@Base+0xa72
404 
405 	 * Point to a place that is after the cpp_named_operator2name
406 	 * boundaries, i.e.  in the ELF symbol table for cc1
407 	 * cpp_named_operator2name is marked as being 32-bytes long, but it in
408 	 * fact is much larger than that, so we seem to need a symbols__find()
409 	 * routine that looks for >= current->start and  < next_symbol->start,
410 	 * possibly just for C++ objects?
411 	 *
412 	 * For now lets just make some progress by marking jumps to outside the
413 	 * current function as call like.
414 	 *
415 	 * Actual navigation will come next, with further understanding of how
416 	 * the symbol searching and disassembly should be done.
417 	 */
418 	if (maps__find_ams(ms->maps, &target) == 0 &&
419 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
420 		ops->target.sym = target.ms.sym;
421 
422 	if (!ops->target.outside) {
423 		ops->target.offset = target.addr - start;
424 		ops->target.offset_avail = true;
425 	} else {
426 		ops->target.offset_avail = false;
427 	}
428 
429 	return 0;
430 }
431 
432 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
433 			   struct ins_operands *ops, int max_ins_name)
434 {
435 	const char *c;
436 
437 	if (!ops->target.addr || ops->target.offset < 0)
438 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
439 
440 	if (ops->target.outside && ops->target.sym != NULL)
441 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
442 
443 	c = strchr(ops->raw, ',');
444 	c = validate_comma(c, ops);
445 
446 	if (c != NULL) {
447 		const char *c2 = strchr(c + 1, ',');
448 
449 		c2 = validate_comma(c2, ops);
450 		/* check for 3-op insn */
451 		if (c2 != NULL)
452 			c = c2;
453 		c++;
454 
455 		/* mirror arch objdump's space-after-comma style */
456 		if (*c == ' ')
457 			c++;
458 	}
459 
460 	return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
461 			 ins->name, c ? c - ops->raw : 0, ops->raw,
462 			 ops->target.offset);
463 }
464 
465 static void jump__delete(struct ins_operands *ops __maybe_unused)
466 {
467 	/*
468 	 * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the
469 	 * raw string, don't free them.
470 	 */
471 }
472 
473 static struct ins_ops jump_ops = {
474 	.free	   = jump__delete,
475 	.parse	   = jump__parse,
476 	.scnprintf = jump__scnprintf,
477 };
478 
479 bool ins__is_jump(const struct ins *ins)
480 {
481 	return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops;
482 }
483 
484 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
485 {
486 	char *endptr, *name, *t;
487 
488 	if (strstr(raw, "(%rip)") == NULL)
489 		return 0;
490 
491 	*addrp = strtoull(comment, &endptr, 16);
492 	if (endptr == comment)
493 		return 0;
494 	name = strchr(endptr, '<');
495 	if (name == NULL)
496 		return -1;
497 
498 	name++;
499 
500 	t = strchr(name, '>');
501 	if (t == NULL)
502 		return 0;
503 
504 	*t = '\0';
505 	*namep = strdup(name);
506 	*t = '>';
507 
508 	return 0;
509 }
510 
511 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
512 {
513 	ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
514 	if (ops->locked.ops == NULL)
515 		return 0;
516 
517 	if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
518 		goto out_free_ops;
519 
520 	ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
521 
522 	if (ops->locked.ins.ops == NULL)
523 		goto out_free_ops;
524 
525 	if (ops->locked.ins.ops->parse &&
526 	    ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
527 		goto out_free_ops;
528 
529 	return 0;
530 
531 out_free_ops:
532 	zfree(&ops->locked.ops);
533 	return 0;
534 }
535 
536 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
537 			   struct ins_operands *ops, int max_ins_name)
538 {
539 	int printed;
540 
541 	if (ops->locked.ins.ops == NULL)
542 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
543 
544 	printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
545 	return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
546 					size - printed, ops->locked.ops, max_ins_name);
547 }
548 
549 static void lock__delete(struct ins_operands *ops)
550 {
551 	struct ins *ins = &ops->locked.ins;
552 
553 	if (ins->ops && ins->ops->free)
554 		ins->ops->free(ops->locked.ops);
555 	else
556 		ins_ops__delete(ops->locked.ops);
557 
558 	zfree(&ops->locked.ops);
559 	zfree(&ops->target.raw);
560 	zfree(&ops->target.name);
561 }
562 
563 static struct ins_ops lock_ops = {
564 	.free	   = lock__delete,
565 	.parse	   = lock__parse,
566 	.scnprintf = lock__scnprintf,
567 };
568 
569 /*
570  * Check if the operand has more than one registers like x86 SIB addressing:
571  *   0x1234(%rax, %rbx, 8)
572  *
573  * But it doesn't care segment selectors like %gs:0x5678(%rcx), so just check
574  * the input string after 'memory_ref_char' if exists.
575  */
576 static bool check_multi_regs(struct arch *arch, const char *op)
577 {
578 	int count = 0;
579 
580 	if (arch->objdump.register_char == 0)
581 		return false;
582 
583 	if (arch->objdump.memory_ref_char) {
584 		op = strchr(op, arch->objdump.memory_ref_char);
585 		if (op == NULL)
586 			return false;
587 	}
588 
589 	while ((op = strchr(op, arch->objdump.register_char)) != NULL) {
590 		count++;
591 		op++;
592 	}
593 
594 	return count > 1;
595 }
596 
597 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
598 {
599 	char *s = strchr(ops->raw, ','), *target, *comment, prev;
600 
601 	if (s == NULL)
602 		return -1;
603 
604 	*s = '\0';
605 
606 	/*
607 	 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
608 	 * then it needs to have the closing parenthesis.
609 	 */
610 	if (strchr(ops->raw, '(')) {
611 		*s = ',';
612 		s = strchr(ops->raw, ')');
613 		if (s == NULL || s[1] != ',')
614 			return -1;
615 		*++s = '\0';
616 	}
617 
618 	ops->source.raw = strdup(ops->raw);
619 	*s = ',';
620 
621 	if (ops->source.raw == NULL)
622 		return -1;
623 
624 	ops->source.multi_regs = check_multi_regs(arch, ops->source.raw);
625 
626 	target = skip_spaces(++s);
627 	comment = strchr(s, arch->objdump.comment_char);
628 
629 	if (comment != NULL)
630 		s = comment - 1;
631 	else
632 		s = strchr(s, '\0') - 1;
633 
634 	while (s > target && isspace(s[0]))
635 		--s;
636 	s++;
637 	prev = *s;
638 	*s = '\0';
639 
640 	ops->target.raw = strdup(target);
641 	*s = prev;
642 
643 	if (ops->target.raw == NULL)
644 		goto out_free_source;
645 
646 	ops->target.multi_regs = check_multi_regs(arch, ops->target.raw);
647 
648 	if (comment == NULL)
649 		return 0;
650 
651 	comment = skip_spaces(comment);
652 	comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
653 	comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
654 
655 	return 0;
656 
657 out_free_source:
658 	zfree(&ops->source.raw);
659 	return -1;
660 }
661 
662 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
663 			   struct ins_operands *ops, int max_ins_name)
664 {
665 	return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
666 			 ops->source.name ?: ops->source.raw,
667 			 ops->target.name ?: ops->target.raw);
668 }
669 
670 static struct ins_ops mov_ops = {
671 	.parse	   = mov__parse,
672 	.scnprintf = mov__scnprintf,
673 };
674 
675 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
676 {
677 	char *target, *comment, *s, prev;
678 
679 	target = s = ops->raw;
680 
681 	while (s[0] != '\0' && !isspace(s[0]))
682 		++s;
683 	prev = *s;
684 	*s = '\0';
685 
686 	ops->target.raw = strdup(target);
687 	*s = prev;
688 
689 	if (ops->target.raw == NULL)
690 		return -1;
691 
692 	comment = strchr(s, arch->objdump.comment_char);
693 	if (comment == NULL)
694 		return 0;
695 
696 	comment = skip_spaces(comment);
697 	comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
698 
699 	return 0;
700 }
701 
702 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
703 			   struct ins_operands *ops, int max_ins_name)
704 {
705 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
706 			 ops->target.name ?: ops->target.raw);
707 }
708 
709 static struct ins_ops dec_ops = {
710 	.parse	   = dec__parse,
711 	.scnprintf = dec__scnprintf,
712 };
713 
714 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
715 			  struct ins_operands *ops __maybe_unused, int max_ins_name)
716 {
717 	return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
718 }
719 
720 static struct ins_ops nop_ops = {
721 	.scnprintf = nop__scnprintf,
722 };
723 
724 static struct ins_ops ret_ops = {
725 	.scnprintf = ins__raw_scnprintf,
726 };
727 
728 bool ins__is_nop(const struct ins *ins)
729 {
730 	return ins->ops == &nop_ops;
731 }
732 
733 bool ins__is_ret(const struct ins *ins)
734 {
735 	return ins->ops == &ret_ops;
736 }
737 
738 bool ins__is_lock(const struct ins *ins)
739 {
740 	return ins->ops == &lock_ops;
741 }
742 
743 static int ins__key_cmp(const void *name, const void *insp)
744 {
745 	const struct ins *ins = insp;
746 
747 	return strcmp(name, ins->name);
748 }
749 
750 static int ins__cmp(const void *a, const void *b)
751 {
752 	const struct ins *ia = a;
753 	const struct ins *ib = b;
754 
755 	return strcmp(ia->name, ib->name);
756 }
757 
758 static void ins__sort(struct arch *arch)
759 {
760 	const int nmemb = arch->nr_instructions;
761 
762 	qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
763 }
764 
765 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
766 {
767 	struct ins *ins;
768 	const int nmemb = arch->nr_instructions;
769 
770 	if (!arch->sorted_instructions) {
771 		ins__sort(arch);
772 		arch->sorted_instructions = true;
773 	}
774 
775 	ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
776 	if (ins)
777 		return ins->ops;
778 
779 	if (arch->insn_suffix) {
780 		char tmp[32];
781 		char suffix;
782 		size_t len = strlen(name);
783 
784 		if (len == 0 || len >= sizeof(tmp))
785 			return NULL;
786 
787 		suffix = name[len - 1];
788 		if (strchr(arch->insn_suffix, suffix) == NULL)
789 			return NULL;
790 
791 		strcpy(tmp, name);
792 		tmp[len - 1] = '\0'; /* remove the suffix and check again */
793 
794 		ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
795 	}
796 	return ins ? ins->ops : NULL;
797 }
798 
799 struct ins_ops *ins__find(struct arch *arch, const char *name)
800 {
801 	struct ins_ops *ops = __ins__find(arch, name);
802 
803 	if (!ops && arch->associate_instruction_ops)
804 		ops = arch->associate_instruction_ops(arch, name);
805 
806 	return ops;
807 }
808 
809 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
810 {
811 	dl->ins.ops = ins__find(arch, dl->ins.name);
812 
813 	if (!dl->ins.ops)
814 		return;
815 
816 	if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
817 		dl->ins.ops = NULL;
818 }
819 
820 static int disasm_line__parse(char *line, const char **namep, char **rawp)
821 {
822 	char tmp, *name = skip_spaces(line);
823 
824 	if (name[0] == '\0')
825 		return -1;
826 
827 	*rawp = name + 1;
828 
829 	while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
830 		++*rawp;
831 
832 	tmp = (*rawp)[0];
833 	(*rawp)[0] = '\0';
834 	*namep = strdup(name);
835 
836 	if (*namep == NULL)
837 		goto out;
838 
839 	(*rawp)[0] = tmp;
840 	*rawp = strim(*rawp);
841 
842 	return 0;
843 
844 out:
845 	return -1;
846 }
847 
848 static void annotation_line__init(struct annotation_line *al,
849 				  struct annotate_args *args,
850 				  int nr)
851 {
852 	al->offset = args->offset;
853 	al->line = strdup(args->line);
854 	al->line_nr = args->line_nr;
855 	al->fileloc = args->fileloc;
856 	al->data_nr = nr;
857 }
858 
859 static void annotation_line__exit(struct annotation_line *al)
860 {
861 	zfree_srcline(&al->path);
862 	zfree(&al->line);
863 	zfree(&al->cycles);
864 }
865 
866 static size_t disasm_line_size(int nr)
867 {
868 	struct annotation_line *al;
869 
870 	return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
871 }
872 
873 /*
874  * Allocating the disasm annotation line data with
875  * following structure:
876  *
877  *    -------------------------------------------
878  *    struct disasm_line | struct annotation_line
879  *    -------------------------------------------
880  *
881  * We have 'struct annotation_line' member as last member
882  * of 'struct disasm_line' to have an easy access.
883  */
884 struct disasm_line *disasm_line__new(struct annotate_args *args)
885 {
886 	struct disasm_line *dl = NULL;
887 	int nr = 1;
888 
889 	if (evsel__is_group_event(args->evsel))
890 		nr = args->evsel->core.nr_members;
891 
892 	dl = zalloc(disasm_line_size(nr));
893 	if (!dl)
894 		return NULL;
895 
896 	annotation_line__init(&dl->al, args, nr);
897 	if (dl->al.line == NULL)
898 		goto out_delete;
899 
900 	if (args->offset != -1) {
901 		if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
902 			goto out_free_line;
903 
904 		disasm_line__init_ins(dl, args->arch, &args->ms);
905 	}
906 
907 	return dl;
908 
909 out_free_line:
910 	zfree(&dl->al.line);
911 out_delete:
912 	free(dl);
913 	return NULL;
914 }
915 
916 void disasm_line__free(struct disasm_line *dl)
917 {
918 	if (dl->ins.ops && dl->ins.ops->free)
919 		dl->ins.ops->free(&dl->ops);
920 	else
921 		ins_ops__delete(&dl->ops);
922 	zfree(&dl->ins.name);
923 	annotation_line__exit(&dl->al);
924 	free(dl);
925 }
926 
927 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
928 {
929 	if (raw || !dl->ins.ops)
930 		return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
931 
932 	return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
933 }
934 
935 /*
936  * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
937  * which looks like following
938  *
939  *  0000000000415500 <_init>:
940  *    415500:       sub    $0x8,%rsp
941  *    415504:       mov    0x2f5ad5(%rip),%rax        # 70afe0 <_DYNAMIC+0x2f8>
942  *    41550b:       test   %rax,%rax
943  *    41550e:       je     415515 <_init+0x15>
944  *    415510:       callq  416e70 <__gmon_start__@plt>
945  *    415515:       add    $0x8,%rsp
946  *    415519:       retq
947  *
948  * it will be parsed and saved into struct disasm_line as
949  *  <offset>       <name>  <ops.raw>
950  *
951  * The offset will be a relative offset from the start of the symbol and -1
952  * means that it's not a disassembly line so should be treated differently.
953  * The ops.raw part will be parsed further according to type of the instruction.
954  */
955 static int symbol__parse_objdump_line(struct symbol *sym,
956 				      struct annotate_args *args,
957 				      char *parsed_line, int *line_nr, char **fileloc)
958 {
959 	struct map *map = args->ms.map;
960 	struct annotation *notes = symbol__annotation(sym);
961 	struct disasm_line *dl;
962 	char *tmp;
963 	s64 line_ip, offset = -1;
964 	regmatch_t match[2];
965 
966 	/* /filename:linenr ? Save line number and ignore. */
967 	if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
968 		*line_nr = atoi(parsed_line + match[1].rm_so);
969 		free(*fileloc);
970 		*fileloc = strdup(parsed_line);
971 		return 0;
972 	}
973 
974 	/* Process hex address followed by ':'. */
975 	line_ip = strtoull(parsed_line, &tmp, 16);
976 	if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') {
977 		u64 start = map__rip_2objdump(map, sym->start),
978 		    end = map__rip_2objdump(map, sym->end);
979 
980 		offset = line_ip - start;
981 		if ((u64)line_ip < start || (u64)line_ip >= end)
982 			offset = -1;
983 		else
984 			parsed_line = tmp + 1;
985 	}
986 
987 	args->offset  = offset;
988 	args->line    = parsed_line;
989 	args->line_nr = *line_nr;
990 	args->fileloc = *fileloc;
991 	args->ms.sym  = sym;
992 
993 	dl = disasm_line__new(args);
994 	(*line_nr)++;
995 
996 	if (dl == NULL)
997 		return -1;
998 
999 	if (!disasm_line__has_local_offset(dl)) {
1000 		dl->ops.target.offset = dl->ops.target.addr -
1001 					map__rip_2objdump(map, sym->start);
1002 		dl->ops.target.offset_avail = true;
1003 	}
1004 
1005 	/* kcore has no symbols, so add the call target symbol */
1006 	if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
1007 		struct addr_map_symbol target = {
1008 			.addr = dl->ops.target.addr,
1009 			.ms = { .map = map, },
1010 		};
1011 
1012 		if (!maps__find_ams(args->ms.maps, &target) &&
1013 		    target.ms.sym->start == target.al_addr)
1014 			dl->ops.target.sym = target.ms.sym;
1015 	}
1016 
1017 	annotation_line__add(&dl->al, &notes->src->source);
1018 	return 0;
1019 }
1020 
1021 static void delete_last_nop(struct symbol *sym)
1022 {
1023 	struct annotation *notes = symbol__annotation(sym);
1024 	struct list_head *list = &notes->src->source;
1025 	struct disasm_line *dl;
1026 
1027 	while (!list_empty(list)) {
1028 		dl = list_entry(list->prev, struct disasm_line, al.node);
1029 
1030 		if (dl->ins.ops) {
1031 			if (!ins__is_nop(&dl->ins))
1032 				return;
1033 		} else {
1034 			if (!strstr(dl->al.line, " nop ") &&
1035 			    !strstr(dl->al.line, " nopl ") &&
1036 			    !strstr(dl->al.line, " nopw "))
1037 				return;
1038 		}
1039 
1040 		list_del_init(&dl->al.node);
1041 		disasm_line__free(dl);
1042 	}
1043 }
1044 
1045 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
1046 {
1047 	struct dso *dso = map__dso(ms->map);
1048 
1049 	BUG_ON(buflen == 0);
1050 
1051 	if (errnum >= 0) {
1052 		str_error_r(errnum, buf, buflen);
1053 		return 0;
1054 	}
1055 
1056 	switch (errnum) {
1057 	case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1058 		char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1059 		char *build_id_msg = NULL;
1060 
1061 		if (dso__has_build_id(dso)) {
1062 			build_id__sprintf(dso__bid(dso), bf + 15);
1063 			build_id_msg = bf;
1064 		}
1065 		scnprintf(buf, buflen,
1066 			  "No vmlinux file%s\nwas found in the path.\n\n"
1067 			  "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1068 			  "Please use:\n\n"
1069 			  "  perf buildid-cache -vu vmlinux\n\n"
1070 			  "or:\n\n"
1071 			  "  --vmlinux vmlinux\n", build_id_msg ?: "");
1072 	}
1073 		break;
1074 	case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1075 		scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1076 		break;
1077 	case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
1078 		scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
1079 		break;
1080 	case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
1081 		scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
1082 		break;
1083 	case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
1084 		scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso));
1085 		break;
1086 	case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF:
1087 		scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.",
1088 			  dso__long_name(dso));
1089 		break;
1090 	default:
1091 		scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1092 		break;
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1099 {
1100 	char linkname[PATH_MAX];
1101 	char *build_id_filename;
1102 	char *build_id_path = NULL;
1103 	char *pos;
1104 	int len;
1105 
1106 	if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
1107 	    !dso__is_kcore(dso))
1108 		return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1109 
1110 	build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1111 	if (build_id_filename) {
1112 		__symbol__join_symfs(filename, filename_size, build_id_filename);
1113 		free(build_id_filename);
1114 	} else {
1115 		if (dso__has_build_id(dso))
1116 			return ENOMEM;
1117 		goto fallback;
1118 	}
1119 
1120 	build_id_path = strdup(filename);
1121 	if (!build_id_path)
1122 		return ENOMEM;
1123 
1124 	/*
1125 	 * old style build-id cache has name of XX/XXXXXXX.. while
1126 	 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1127 	 * extract the build-id part of dirname in the new style only.
1128 	 */
1129 	pos = strrchr(build_id_path, '/');
1130 	if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1131 		dirname(build_id_path);
1132 
1133 	if (dso__is_kcore(dso))
1134 		goto fallback;
1135 
1136 	len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1137 	if (len < 0)
1138 		goto fallback;
1139 
1140 	linkname[len] = '\0';
1141 	if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1142 		access(filename, R_OK)) {
1143 fallback:
1144 		/*
1145 		 * If we don't have build-ids or the build-id file isn't in the
1146 		 * cache, or is just a kallsyms file, well, lets hope that this
1147 		 * DSO is the same as when 'perf record' ran.
1148 		 */
1149 		if (dso__kernel(dso) && dso__long_name(dso)[0] == '/')
1150 			snprintf(filename, filename_size, "%s", dso__long_name(dso));
1151 		else
1152 			__symbol__join_symfs(filename, filename_size, dso__long_name(dso));
1153 
1154 		mutex_lock(dso__lock(dso));
1155 		if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) {
1156 			char *new_name = dso__filename_with_chroot(dso, filename);
1157 			if (new_name) {
1158 				strlcpy(filename, new_name, filename_size);
1159 				free(new_name);
1160 			}
1161 		}
1162 		mutex_unlock(dso__lock(dso));
1163 	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
1164 		dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE);
1165 	}
1166 
1167 	free(build_id_path);
1168 	return 0;
1169 }
1170 
1171 #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1172 #define PACKAGE "perf"
1173 #include <bfd.h>
1174 #include <dis-asm.h>
1175 #include <bpf/bpf.h>
1176 #include <bpf/btf.h>
1177 #include <bpf/libbpf.h>
1178 #include <linux/btf.h>
1179 #include <tools/dis-asm-compat.h>
1180 
1181 #include "bpf-event.h"
1182 #include "bpf-utils.h"
1183 
1184 static int symbol__disassemble_bpf(struct symbol *sym,
1185 				   struct annotate_args *args)
1186 {
1187 	struct annotation *notes = symbol__annotation(sym);
1188 	struct bpf_prog_linfo *prog_linfo = NULL;
1189 	struct bpf_prog_info_node *info_node;
1190 	int len = sym->end - sym->start;
1191 	disassembler_ftype disassemble;
1192 	struct map *map = args->ms.map;
1193 	struct perf_bpil *info_linear;
1194 	struct disassemble_info info;
1195 	struct dso *dso = map__dso(map);
1196 	int pc = 0, count, sub_id;
1197 	struct btf *btf = NULL;
1198 	char tpath[PATH_MAX];
1199 	size_t buf_size;
1200 	int nr_skip = 0;
1201 	char *buf;
1202 	bfd *bfdf;
1203 	int ret;
1204 	FILE *s;
1205 
1206 	if (dso__binary_type(dso) != DSO_BINARY_TYPE__BPF_PROG_INFO)
1207 		return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
1208 
1209 	pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
1210 		  sym->name, sym->start, sym->end - sym->start);
1211 
1212 	memset(tpath, 0, sizeof(tpath));
1213 	perf_exe(tpath, sizeof(tpath));
1214 
1215 	bfdf = bfd_openr(tpath, NULL);
1216 	if (bfdf == NULL)
1217 		abort();
1218 
1219 	if (!bfd_check_format(bfdf, bfd_object))
1220 		abort();
1221 
1222 	s = open_memstream(&buf, &buf_size);
1223 	if (!s) {
1224 		ret = errno;
1225 		goto out;
1226 	}
1227 	init_disassemble_info_compat(&info, s,
1228 				     (fprintf_ftype) fprintf,
1229 				     fprintf_styled);
1230 	info.arch = bfd_get_arch(bfdf);
1231 	info.mach = bfd_get_mach(bfdf);
1232 
1233 	info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env,
1234 						 dso__bpf_prog(dso)->id);
1235 	if (!info_node) {
1236 		ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
1237 		goto out;
1238 	}
1239 	info_linear = info_node->info_linear;
1240 	sub_id = dso__bpf_prog(dso)->sub_id;
1241 
1242 	info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
1243 	info.buffer_length = info_linear->info.jited_prog_len;
1244 
1245 	if (info_linear->info.nr_line_info)
1246 		prog_linfo = bpf_prog_linfo__new(&info_linear->info);
1247 
1248 	if (info_linear->info.btf_id) {
1249 		struct btf_node *node;
1250 
1251 		node = perf_env__find_btf(dso__bpf_prog(dso)->env,
1252 					  info_linear->info.btf_id);
1253 		if (node)
1254 			btf = btf__new((__u8 *)(node->data),
1255 				       node->data_size);
1256 	}
1257 
1258 	disassemble_init_for_target(&info);
1259 
1260 #ifdef DISASM_FOUR_ARGS_SIGNATURE
1261 	disassemble = disassembler(info.arch,
1262 				   bfd_big_endian(bfdf),
1263 				   info.mach,
1264 				   bfdf);
1265 #else
1266 	disassemble = disassembler(bfdf);
1267 #endif
1268 	if (disassemble == NULL)
1269 		abort();
1270 
1271 	fflush(s);
1272 	do {
1273 		const struct bpf_line_info *linfo = NULL;
1274 		struct disasm_line *dl;
1275 		size_t prev_buf_size;
1276 		const char *srcline;
1277 		u64 addr;
1278 
1279 		addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
1280 		count = disassemble(pc, &info);
1281 
1282 		if (prog_linfo)
1283 			linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
1284 								addr, sub_id,
1285 								nr_skip);
1286 
1287 		if (linfo && btf) {
1288 			srcline = btf__name_by_offset(btf, linfo->line_off);
1289 			nr_skip++;
1290 		} else
1291 			srcline = NULL;
1292 
1293 		fprintf(s, "\n");
1294 		prev_buf_size = buf_size;
1295 		fflush(s);
1296 
1297 		if (!annotate_opts.hide_src_code && srcline) {
1298 			args->offset = -1;
1299 			args->line = strdup(srcline);
1300 			args->line_nr = 0;
1301 			args->fileloc = NULL;
1302 			args->ms.sym  = sym;
1303 			dl = disasm_line__new(args);
1304 			if (dl) {
1305 				annotation_line__add(&dl->al,
1306 						     &notes->src->source);
1307 			}
1308 		}
1309 
1310 		args->offset = pc;
1311 		args->line = buf + prev_buf_size;
1312 		args->line_nr = 0;
1313 		args->fileloc = NULL;
1314 		args->ms.sym  = sym;
1315 		dl = disasm_line__new(args);
1316 		if (dl)
1317 			annotation_line__add(&dl->al, &notes->src->source);
1318 
1319 		pc += count;
1320 	} while (count > 0 && pc < len);
1321 
1322 	ret = 0;
1323 out:
1324 	free(prog_linfo);
1325 	btf__free(btf);
1326 	fclose(s);
1327 	bfd_close(bfdf);
1328 	return ret;
1329 }
1330 #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1331 static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused,
1332 				   struct annotate_args *args __maybe_unused)
1333 {
1334 	return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
1335 }
1336 #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1337 
1338 static int
1339 symbol__disassemble_bpf_image(struct symbol *sym,
1340 			      struct annotate_args *args)
1341 {
1342 	struct annotation *notes = symbol__annotation(sym);
1343 	struct disasm_line *dl;
1344 
1345 	args->offset = -1;
1346 	args->line = strdup("to be implemented");
1347 	args->line_nr = 0;
1348 	args->fileloc = NULL;
1349 	dl = disasm_line__new(args);
1350 	if (dl)
1351 		annotation_line__add(&dl->al, &notes->src->source);
1352 
1353 	zfree(&args->line);
1354 	return 0;
1355 }
1356 
1357 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1358 #include <capstone/capstone.h>
1359 
1360 static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
1361 				csh *handle)
1362 {
1363 	struct annotation_options *opt = args->options;
1364 	cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
1365 
1366 	/* TODO: support more architectures */
1367 	if (!arch__is(args->arch, "x86"))
1368 		return -1;
1369 
1370 	if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
1371 		return -1;
1372 
1373 	if (!opt->disassembler_style ||
1374 	    !strcmp(opt->disassembler_style, "att"))
1375 		cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
1376 
1377 	/*
1378 	 * Resolving address operands to symbols is implemented
1379 	 * on x86 by investigating instruction details.
1380 	 */
1381 	cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
1382 
1383 	return 0;
1384 }
1385 
1386 struct find_file_offset_data {
1387 	u64 ip;
1388 	u64 offset;
1389 };
1390 
1391 /* This will be called for each PHDR in an ELF binary */
1392 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
1393 {
1394 	struct find_file_offset_data *data = arg;
1395 
1396 	if (start <= data->ip && data->ip < start + len) {
1397 		data->offset = pgoff + data->ip - start;
1398 		return 1;
1399 	}
1400 	return 0;
1401 }
1402 
1403 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
1404 				  struct annotate_args *args, u64 addr)
1405 {
1406 	int i;
1407 	struct map *map = args->ms.map;
1408 	struct symbol *sym;
1409 
1410 	/* TODO: support more architectures */
1411 	if (!arch__is(args->arch, "x86"))
1412 		return;
1413 
1414 	if (insn->detail == NULL)
1415 		return;
1416 
1417 	for (i = 0; i < insn->detail->x86.op_count; i++) {
1418 		cs_x86_op *op = &insn->detail->x86.operands[i];
1419 		u64 orig_addr;
1420 
1421 		if (op->type != X86_OP_MEM)
1422 			continue;
1423 
1424 		/* only print RIP-based global symbols for now */
1425 		if (op->mem.base != X86_REG_RIP)
1426 			continue;
1427 
1428 		/* get the target address */
1429 		orig_addr = addr + insn->size + op->mem.disp;
1430 		addr = map__objdump_2mem(map, orig_addr);
1431 
1432 		if (dso__kernel(map__dso(map))) {
1433 			/*
1434 			 * The kernel maps can be splitted into sections,
1435 			 * let's find the map first and the search the symbol.
1436 			 */
1437 			map = maps__find(map__kmaps(map), addr);
1438 			if (map == NULL)
1439 				continue;
1440 		}
1441 
1442 		/* convert it to map-relative address for search */
1443 		addr = map__map_ip(map, addr);
1444 
1445 		sym = map__find_symbol(map, addr);
1446 		if (sym == NULL)
1447 			continue;
1448 
1449 		if (addr == sym->start) {
1450 			scnprintf(buf, len, "\t# %"PRIx64" <%s>",
1451 				  orig_addr, sym->name);
1452 		} else {
1453 			scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">",
1454 				  orig_addr, sym->name, addr - sym->start);
1455 		}
1456 		break;
1457 	}
1458 }
1459 
1460 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1461 					struct annotate_args *args)
1462 {
1463 	struct annotation *notes = symbol__annotation(sym);
1464 	struct map *map = args->ms.map;
1465 	struct dso *dso = map__dso(map);
1466 	struct nscookie nsc;
1467 	u64 start = map__rip_2objdump(map, sym->start);
1468 	u64 end = map__rip_2objdump(map, sym->end);
1469 	u64 len = end - start;
1470 	u64 offset;
1471 	int i, fd, count;
1472 	bool is_64bit = false;
1473 	bool needs_cs_close = false;
1474 	u8 *buf = NULL;
1475 	struct find_file_offset_data data = {
1476 		.ip = start,
1477 	};
1478 	csh handle;
1479 	cs_insn *insn;
1480 	char disasm_buf[512];
1481 	struct disasm_line *dl;
1482 
1483 	if (args->options->objdump_path)
1484 		return -1;
1485 
1486 	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1487 	fd = open(filename, O_RDONLY);
1488 	nsinfo__mountns_exit(&nsc);
1489 	if (fd < 0)
1490 		return -1;
1491 
1492 	if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1493 			    &is_64bit) == 0)
1494 		goto err;
1495 
1496 	if (open_capstone_handle(args, is_64bit, &handle) < 0)
1497 		goto err;
1498 
1499 	needs_cs_close = true;
1500 
1501 	buf = malloc(len);
1502 	if (buf == NULL)
1503 		goto err;
1504 
1505 	count = pread(fd, buf, len, data.offset);
1506 	close(fd);
1507 	fd = -1;
1508 
1509 	if ((u64)count != len)
1510 		goto err;
1511 
1512 	/* add the function address and name */
1513 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1514 		  start, sym->name);
1515 
1516 	args->offset = -1;
1517 	args->line = disasm_buf;
1518 	args->line_nr = 0;
1519 	args->fileloc = NULL;
1520 	args->ms.sym = sym;
1521 
1522 	dl = disasm_line__new(args);
1523 	if (dl == NULL)
1524 		goto err;
1525 
1526 	annotation_line__add(&dl->al, &notes->src->source);
1527 
1528 	count = cs_disasm(handle, buf, len, start, len, &insn);
1529 	for (i = 0, offset = 0; i < count; i++) {
1530 		int printed;
1531 
1532 		printed = scnprintf(disasm_buf, sizeof(disasm_buf),
1533 				    "       %-7s %s",
1534 				    insn[i].mnemonic, insn[i].op_str);
1535 		print_capstone_detail(&insn[i], disasm_buf + printed,
1536 				      sizeof(disasm_buf) - printed, args,
1537 				      start + offset);
1538 
1539 		args->offset = offset;
1540 		args->line = disasm_buf;
1541 
1542 		dl = disasm_line__new(args);
1543 		if (dl == NULL)
1544 			goto err;
1545 
1546 		annotation_line__add(&dl->al, &notes->src->source);
1547 
1548 		offset += insn[i].size;
1549 	}
1550 
1551 	/* It failed in the middle: probably due to unknown instructions */
1552 	if (offset != len) {
1553 		struct list_head *list = &notes->src->source;
1554 
1555 		/* Discard all lines and fallback to objdump */
1556 		while (!list_empty(list)) {
1557 			dl = list_first_entry(list, struct disasm_line, al.node);
1558 
1559 			list_del_init(&dl->al.node);
1560 			disasm_line__free(dl);
1561 		}
1562 		count = -1;
1563 	}
1564 
1565 out:
1566 	if (needs_cs_close)
1567 		cs_close(&handle);
1568 	free(buf);
1569 	return count < 0 ? count : 0;
1570 
1571 err:
1572 	if (fd >= 0)
1573 		close(fd);
1574 	if (needs_cs_close) {
1575 		struct disasm_line *tmp;
1576 
1577 		/*
1578 		 * It probably failed in the middle of the above loop.
1579 		 * Release any resources it might add.
1580 		 */
1581 		list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
1582 			list_del(&dl->al.node);
1583 			free(dl);
1584 		}
1585 	}
1586 	count = -1;
1587 	goto out;
1588 }
1589 #endif
1590 
1591 /*
1592  * Possibly create a new version of line with tabs expanded. Returns the
1593  * existing or new line, storage is updated if a new line is allocated. If
1594  * allocation fails then NULL is returned.
1595  */
1596 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
1597 {
1598 	size_t i, src, dst, len, new_storage_len, num_tabs;
1599 	char *new_line;
1600 	size_t line_len = strlen(line);
1601 
1602 	for (num_tabs = 0, i = 0; i < line_len; i++)
1603 		if (line[i] == '\t')
1604 			num_tabs++;
1605 
1606 	if (num_tabs == 0)
1607 		return line;
1608 
1609 	/*
1610 	 * Space for the line and '\0', less the leading and trailing
1611 	 * spaces. Each tab may introduce 7 additional spaces.
1612 	 */
1613 	new_storage_len = line_len + 1 + (num_tabs * 7);
1614 
1615 	new_line = malloc(new_storage_len);
1616 	if (new_line == NULL) {
1617 		pr_err("Failure allocating memory for tab expansion\n");
1618 		return NULL;
1619 	}
1620 
1621 	/*
1622 	 * Copy regions starting at src and expand tabs. If there are two
1623 	 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
1624 	 * are inserted.
1625 	 */
1626 	for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
1627 		if (line[i] == '\t') {
1628 			len = i - src;
1629 			memcpy(&new_line[dst], &line[src], len);
1630 			dst += len;
1631 			new_line[dst++] = ' ';
1632 			while (dst % 8 != 0)
1633 				new_line[dst++] = ' ';
1634 			src = i + 1;
1635 			num_tabs--;
1636 		}
1637 	}
1638 
1639 	/* Expand the last region. */
1640 	len = line_len - src;
1641 	memcpy(&new_line[dst], &line[src], len);
1642 	dst += len;
1643 	new_line[dst] = '\0';
1644 
1645 	free(*storage);
1646 	*storage = new_line;
1647 	*storage_len = new_storage_len;
1648 	return new_line;
1649 }
1650 
1651 int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
1652 {
1653 	struct annotation_options *opts = &annotate_opts;
1654 	struct map *map = args->ms.map;
1655 	struct dso *dso = map__dso(map);
1656 	char *command;
1657 	FILE *file;
1658 	char symfs_filename[PATH_MAX];
1659 	struct kcore_extract kce;
1660 	bool delete_extract = false;
1661 	bool decomp = false;
1662 	int lineno = 0;
1663 	char *fileloc = NULL;
1664 	int nline;
1665 	char *line;
1666 	size_t line_len;
1667 	const char *objdump_argv[] = {
1668 		"/bin/sh",
1669 		"-c",
1670 		NULL, /* Will be the objdump command to run. */
1671 		"--",
1672 		NULL, /* Will be the symfs path. */
1673 		NULL,
1674 	};
1675 	struct child_process objdump_process;
1676 	int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1677 
1678 	if (err)
1679 		return err;
1680 
1681 	pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1682 		 symfs_filename, sym->name, map__unmap_ip(map, sym->start),
1683 		 map__unmap_ip(map, sym->end));
1684 
1685 	pr_debug("annotating [%p] %30s : [%p] %30s\n",
1686 		 dso, dso__long_name(dso), sym, sym->name);
1687 
1688 	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
1689 		return symbol__disassemble_bpf(sym, args);
1690 	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
1691 		return symbol__disassemble_bpf_image(sym, args);
1692 	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
1693 		return -1;
1694 	} else if (dso__is_kcore(dso)) {
1695 		kce.kcore_filename = symfs_filename;
1696 		kce.addr = map__rip_2objdump(map, sym->start);
1697 		kce.offs = sym->start;
1698 		kce.len = sym->end - sym->start;
1699 		if (!kcore_extract__create(&kce)) {
1700 			delete_extract = true;
1701 			strlcpy(symfs_filename, kce.extract_filename,
1702 				sizeof(symfs_filename));
1703 		}
1704 	} else if (dso__needs_decompress(dso)) {
1705 		char tmp[KMOD_DECOMP_LEN];
1706 
1707 		if (dso__decompress_kmodule_path(dso, symfs_filename,
1708 						 tmp, sizeof(tmp)) < 0)
1709 			return -1;
1710 
1711 		decomp = true;
1712 		strcpy(symfs_filename, tmp);
1713 	}
1714 
1715 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1716 	err = symbol__disassemble_capstone(symfs_filename, sym, args);
1717 	if (err == 0)
1718 		goto out_remove_tmp;
1719 #endif
1720 
1721 	err = asprintf(&command,
1722 		 "%s %s%s --start-address=0x%016" PRIx64
1723 		 " --stop-address=0x%016" PRIx64
1724 		 " %s -d %s %s %s %c%s%c %s%s -C \"$1\"",
1725 		 opts->objdump_path ?: "objdump",
1726 		 opts->disassembler_style ? "-M " : "",
1727 		 opts->disassembler_style ?: "",
1728 		 map__rip_2objdump(map, sym->start),
1729 		 map__rip_2objdump(map, sym->end),
1730 		 opts->show_linenr ? "-l" : "",
1731 		 opts->show_asm_raw ? "" : "--no-show-raw-insn",
1732 		 opts->annotate_src ? "-S" : "",
1733 		 opts->prefix ? "--prefix " : "",
1734 		 opts->prefix ? '"' : ' ',
1735 		 opts->prefix ?: "",
1736 		 opts->prefix ? '"' : ' ',
1737 		 opts->prefix_strip ? "--prefix-strip=" : "",
1738 		 opts->prefix_strip ?: "");
1739 
1740 	if (err < 0) {
1741 		pr_err("Failure allocating memory for the command to run\n");
1742 		goto out_remove_tmp;
1743 	}
1744 
1745 	pr_debug("Executing: %s\n", command);
1746 
1747 	objdump_argv[2] = command;
1748 	objdump_argv[4] = symfs_filename;
1749 
1750 	/* Create a pipe to read from for stdout */
1751 	memset(&objdump_process, 0, sizeof(objdump_process));
1752 	objdump_process.argv = objdump_argv;
1753 	objdump_process.out = -1;
1754 	objdump_process.err = -1;
1755 	objdump_process.no_stderr = 1;
1756 	if (start_command(&objdump_process)) {
1757 		pr_err("Failure starting to run %s\n", command);
1758 		err = -1;
1759 		goto out_free_command;
1760 	}
1761 
1762 	file = fdopen(objdump_process.out, "r");
1763 	if (!file) {
1764 		pr_err("Failure creating FILE stream for %s\n", command);
1765 		/*
1766 		 * If we were using debug info should retry with
1767 		 * original binary.
1768 		 */
1769 		err = -1;
1770 		goto out_close_stdout;
1771 	}
1772 
1773 	/* Storage for getline. */
1774 	line = NULL;
1775 	line_len = 0;
1776 
1777 	nline = 0;
1778 	while (!feof(file)) {
1779 		const char *match;
1780 		char *expanded_line;
1781 
1782 		if (getline(&line, &line_len, file) < 0 || !line)
1783 			break;
1784 
1785 		/* Skip lines containing "filename:" */
1786 		match = strstr(line, symfs_filename);
1787 		if (match && match[strlen(symfs_filename)] == ':')
1788 			continue;
1789 
1790 		expanded_line = strim(line);
1791 		expanded_line = expand_tabs(expanded_line, &line, &line_len);
1792 		if (!expanded_line)
1793 			break;
1794 
1795 		/*
1796 		 * The source code line number (lineno) needs to be kept in
1797 		 * across calls to symbol__parse_objdump_line(), so that it
1798 		 * can associate it with the instructions till the next one.
1799 		 * See disasm_line__new() and struct disasm_line::line_nr.
1800 		 */
1801 		if (symbol__parse_objdump_line(sym, args, expanded_line,
1802 					       &lineno, &fileloc) < 0)
1803 			break;
1804 		nline++;
1805 	}
1806 	free(line);
1807 	free(fileloc);
1808 
1809 	err = finish_command(&objdump_process);
1810 	if (err)
1811 		pr_err("Error running %s\n", command);
1812 
1813 	if (nline == 0) {
1814 		err = -1;
1815 		pr_err("No output from %s\n", command);
1816 	}
1817 
1818 	/*
1819 	 * kallsyms does not have symbol sizes so there may a nop at the end.
1820 	 * Remove it.
1821 	 */
1822 	if (dso__is_kcore(dso))
1823 		delete_last_nop(sym);
1824 
1825 	fclose(file);
1826 
1827 out_close_stdout:
1828 	close(objdump_process.out);
1829 
1830 out_free_command:
1831 	free(command);
1832 
1833 out_remove_tmp:
1834 	if (decomp)
1835 		unlink(symfs_filename);
1836 
1837 	if (delete_extract)
1838 		kcore_extract__delete(&kce);
1839 
1840 	return err;
1841 }
1842