xref: /linux/tools/perf/util/disasm.c (revision e3b2949e3fa2fd8c19cd5fbb0424d38f70a70e9c)
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 "disasm_bpf.h"
20 #include "dso.h"
21 #include "env.h"
22 #include "evsel.h"
23 #include "map.h"
24 #include "maps.h"
25 #include "namespaces.h"
26 #include "srcline.h"
27 #include "symbol.h"
28 #include "util.h"
29 
30 static regex_t	 file_lineno;
31 
32 /* These can be referred from the arch-dependent code */
33 static struct ins_ops call_ops;
34 static struct ins_ops dec_ops;
35 static struct ins_ops jump_ops;
36 static struct ins_ops mov_ops;
37 static struct ins_ops nop_ops;
38 static struct ins_ops lock_ops;
39 static struct ins_ops ret_ops;
40 static struct ins_ops load_store_ops;
41 static struct ins_ops arithmetic_ops;
42 
43 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
44 			   struct ins_operands *ops, int max_ins_name);
45 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
46 			   struct ins_operands *ops, int max_ins_name);
47 
48 static void ins__sort(struct arch *arch);
49 static int disasm_line__parse(char *line, const char **namep, char **rawp);
50 static int disasm_line__parse_powerpc(struct disasm_line *dl);
51 static char *expand_tabs(char *line, char **storage, size_t *storage_len);
52 
53 static __attribute__((constructor)) void symbol__init_regexpr(void)
54 {
55 	regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
56 }
57 
58 static int arch__grow_instructions(struct arch *arch)
59 {
60 	struct ins *new_instructions;
61 	size_t new_nr_allocated;
62 
63 	if (arch->nr_instructions_allocated == 0 && arch->instructions)
64 		goto grow_from_non_allocated_table;
65 
66 	new_nr_allocated = arch->nr_instructions_allocated + 128;
67 	new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
68 	if (new_instructions == NULL)
69 		return -1;
70 
71 out_update_instructions:
72 	arch->instructions = new_instructions;
73 	arch->nr_instructions_allocated = new_nr_allocated;
74 	return 0;
75 
76 grow_from_non_allocated_table:
77 	new_nr_allocated = arch->nr_instructions + 128;
78 	new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
79 	if (new_instructions == NULL)
80 		return -1;
81 
82 	memcpy(new_instructions, arch->instructions, arch->nr_instructions);
83 	goto out_update_instructions;
84 }
85 
86 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
87 {
88 	struct ins *ins;
89 
90 	if (arch->nr_instructions == arch->nr_instructions_allocated &&
91 	    arch__grow_instructions(arch))
92 		return -1;
93 
94 	ins = &arch->instructions[arch->nr_instructions];
95 	ins->name = strdup(name);
96 	if (!ins->name)
97 		return -1;
98 
99 	ins->ops  = ops;
100 	arch->nr_instructions++;
101 
102 	ins__sort(arch);
103 	return 0;
104 }
105 
106 #include "arch/arc/annotate/instructions.c"
107 #include "arch/arm/annotate/instructions.c"
108 #include "arch/arm64/annotate/instructions.c"
109 #include "arch/csky/annotate/instructions.c"
110 #include "arch/loongarch/annotate/instructions.c"
111 #include "arch/mips/annotate/instructions.c"
112 #include "arch/x86/annotate/instructions.c"
113 #include "arch/powerpc/annotate/instructions.c"
114 #include "arch/riscv64/annotate/instructions.c"
115 #include "arch/s390/annotate/instructions.c"
116 #include "arch/sparc/annotate/instructions.c"
117 
118 static struct arch architectures[] = {
119 	{
120 		.name = "arc",
121 		.init = arc__annotate_init,
122 	},
123 	{
124 		.name = "arm",
125 		.init = arm__annotate_init,
126 	},
127 	{
128 		.name = "arm64",
129 		.init = arm64__annotate_init,
130 	},
131 	{
132 		.name = "csky",
133 		.init = csky__annotate_init,
134 	},
135 	{
136 		.name = "mips",
137 		.init = mips__annotate_init,
138 		.objdump = {
139 			.comment_char = '#',
140 		},
141 	},
142 	{
143 		.name = "x86",
144 		.init = x86__annotate_init,
145 		.instructions = x86__instructions,
146 		.nr_instructions = ARRAY_SIZE(x86__instructions),
147 		.insn_suffix = "bwlq",
148 		.objdump =  {
149 			.comment_char = '#',
150 			.register_char = '%',
151 			.memory_ref_char = '(',
152 			.imm_char = '$',
153 		},
154 #ifdef HAVE_LIBDW_SUPPORT
155 		.update_insn_state = update_insn_state_x86,
156 #endif
157 	},
158 	{
159 		.name = "powerpc",
160 		.init = powerpc__annotate_init,
161 #ifdef HAVE_LIBDW_SUPPORT
162 		.update_insn_state = update_insn_state_powerpc,
163 #endif
164 	},
165 	{
166 		.name = "riscv64",
167 		.init = riscv64__annotate_init,
168 	},
169 	{
170 		.name = "s390",
171 		.init = s390__annotate_init,
172 		.objdump =  {
173 			.comment_char = '#',
174 		},
175 	},
176 	{
177 		.name = "sparc",
178 		.init = sparc__annotate_init,
179 		.objdump = {
180 			.comment_char = '#',
181 		},
182 	},
183 	{
184 		.name = "loongarch",
185 		.init = loongarch__annotate_init,
186 		.objdump = {
187 			.comment_char = '#',
188 		},
189 	},
190 };
191 
192 static int arch__key_cmp(const void *name, const void *archp)
193 {
194 	const struct arch *arch = archp;
195 
196 	return strcmp(name, arch->name);
197 }
198 
199 static int arch__cmp(const void *a, const void *b)
200 {
201 	const struct arch *aa = a;
202 	const struct arch *ab = b;
203 
204 	return strcmp(aa->name, ab->name);
205 }
206 
207 static void arch__sort(void)
208 {
209 	const int nmemb = ARRAY_SIZE(architectures);
210 
211 	qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
212 }
213 
214 struct arch *arch__find(const char *name)
215 {
216 	const int nmemb = ARRAY_SIZE(architectures);
217 	static bool sorted;
218 
219 	if (!sorted) {
220 		arch__sort();
221 		sorted = true;
222 	}
223 
224 	return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
225 }
226 
227 bool arch__is(struct arch *arch, const char *name)
228 {
229 	return !strcmp(arch->name, name);
230 }
231 
232 static void ins_ops__delete(struct ins_operands *ops)
233 {
234 	if (ops == NULL)
235 		return;
236 	zfree(&ops->source.raw);
237 	zfree(&ops->source.name);
238 	zfree(&ops->target.raw);
239 	zfree(&ops->target.name);
240 }
241 
242 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
243 			      struct ins_operands *ops, int max_ins_name)
244 {
245 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
246 }
247 
248 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
249 		   struct ins_operands *ops, int max_ins_name)
250 {
251 	if (ins->ops->scnprintf)
252 		return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
253 
254 	return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
255 }
256 
257 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
258 {
259 	if (!arch || !arch->ins_is_fused)
260 		return false;
261 
262 	return arch->ins_is_fused(arch, ins1, ins2);
263 }
264 
265 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
266 		struct disasm_line *dl __maybe_unused)
267 {
268 	char *endptr, *tok, *name;
269 	struct map *map = ms->map;
270 	struct addr_map_symbol target = {
271 		.ms = { .map = map, },
272 	};
273 
274 	ops->target.addr = strtoull(ops->raw, &endptr, 16);
275 
276 	name = strchr(endptr, '<');
277 	if (name == NULL)
278 		goto indirect_call;
279 
280 	name++;
281 
282 	if (arch->objdump.skip_functions_char &&
283 	    strchr(name, arch->objdump.skip_functions_char))
284 		return -1;
285 
286 	tok = strchr(name, '>');
287 	if (tok == NULL)
288 		return -1;
289 
290 	*tok = '\0';
291 	ops->target.name = strdup(name);
292 	*tok = '>';
293 
294 	if (ops->target.name == NULL)
295 		return -1;
296 find_target:
297 	target.addr = map__objdump_2mem(map, ops->target.addr);
298 
299 	if (maps__find_ams(ms->maps, &target) == 0 &&
300 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
301 		ops->target.sym = target.ms.sym;
302 
303 	return 0;
304 
305 indirect_call:
306 	tok = strchr(endptr, '*');
307 	if (tok != NULL) {
308 		endptr++;
309 
310 		/* Indirect call can use a non-rip register and offset: callq  *0x8(%rbx).
311 		 * Do not parse such instruction.  */
312 		if (strstr(endptr, "(%r") == NULL)
313 			ops->target.addr = strtoull(endptr, NULL, 16);
314 	}
315 	goto find_target;
316 }
317 
318 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
319 			   struct ins_operands *ops, int max_ins_name)
320 {
321 	if (ops->target.sym)
322 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
323 
324 	if (ops->target.addr == 0)
325 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
326 
327 	if (ops->target.name)
328 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
329 
330 	return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
331 }
332 
333 static struct ins_ops call_ops = {
334 	.parse	   = call__parse,
335 	.scnprintf = call__scnprintf,
336 };
337 
338 bool ins__is_call(const struct ins *ins)
339 {
340 	return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops;
341 }
342 
343 /*
344  * Prevents from matching commas in the comment section, e.g.:
345  * ffff200008446e70:       b.cs    ffff2000084470f4 <generic_exec_single+0x314>  // b.hs, b.nlast
346  *
347  * and skip comma as part of function arguments, e.g.:
348  * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
349  */
350 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
351 {
352 	if (ops->jump.raw_comment && c > ops->jump.raw_comment)
353 		return NULL;
354 
355 	if (ops->jump.raw_func_start && c > ops->jump.raw_func_start)
356 		return NULL;
357 
358 	return c;
359 }
360 
361 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
362 		struct disasm_line *dl __maybe_unused)
363 {
364 	struct map *map = ms->map;
365 	struct symbol *sym = ms->sym;
366 	struct addr_map_symbol target = {
367 		.ms = { .map = map, },
368 	};
369 	const char *c = strchr(ops->raw, ',');
370 	u64 start, end;
371 
372 	ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char);
373 	ops->jump.raw_func_start = strchr(ops->raw, '<');
374 
375 	c = validate_comma(c, ops);
376 
377 	/*
378 	 * Examples of lines to parse for the _cpp_lex_token@@Base
379 	 * function:
380 	 *
381 	 * 1159e6c: jne    115aa32 <_cpp_lex_token@@Base+0xf92>
382 	 * 1159e8b: jne    c469be <cpp_named_operator2name@@Base+0xa72>
383 	 *
384 	 * The first is a jump to an offset inside the same function,
385 	 * the second is to another function, i.e. that 0xa72 is an
386 	 * offset in the cpp_named_operator2name@@base function.
387 	 */
388 	/*
389 	 * skip over possible up to 2 operands to get to address, e.g.:
390 	 * tbnz	 w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
391 	 */
392 	if (c++ != NULL) {
393 		ops->target.addr = strtoull(c, NULL, 16);
394 		if (!ops->target.addr) {
395 			c = strchr(c, ',');
396 			c = validate_comma(c, ops);
397 			if (c++ != NULL)
398 				ops->target.addr = strtoull(c, NULL, 16);
399 		}
400 	} else {
401 		ops->target.addr = strtoull(ops->raw, NULL, 16);
402 	}
403 
404 	target.addr = map__objdump_2mem(map, ops->target.addr);
405 	start = map__unmap_ip(map, sym->start);
406 	end = map__unmap_ip(map, sym->end);
407 
408 	ops->target.outside = target.addr < start || target.addr > end;
409 
410 	/*
411 	 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
412 
413 		cpp_named_operator2name@@Base+0xa72
414 
415 	 * Point to a place that is after the cpp_named_operator2name
416 	 * boundaries, i.e.  in the ELF symbol table for cc1
417 	 * cpp_named_operator2name is marked as being 32-bytes long, but it in
418 	 * fact is much larger than that, so we seem to need a symbols__find()
419 	 * routine that looks for >= current->start and  < next_symbol->start,
420 	 * possibly just for C++ objects?
421 	 *
422 	 * For now lets just make some progress by marking jumps to outside the
423 	 * current function as call like.
424 	 *
425 	 * Actual navigation will come next, with further understanding of how
426 	 * the symbol searching and disassembly should be done.
427 	 */
428 	if (maps__find_ams(ms->maps, &target) == 0 &&
429 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
430 		ops->target.sym = target.ms.sym;
431 
432 	if (!ops->target.outside) {
433 		ops->target.offset = target.addr - start;
434 		ops->target.offset_avail = true;
435 	} else {
436 		ops->target.offset_avail = false;
437 	}
438 
439 	return 0;
440 }
441 
442 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
443 			   struct ins_operands *ops, int max_ins_name)
444 {
445 	const char *c;
446 
447 	if (!ops->target.addr || ops->target.offset < 0)
448 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
449 
450 	if (ops->target.outside && ops->target.sym != NULL)
451 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
452 
453 	c = strchr(ops->raw, ',');
454 	c = validate_comma(c, ops);
455 
456 	if (c != NULL) {
457 		const char *c2 = strchr(c + 1, ',');
458 
459 		c2 = validate_comma(c2, ops);
460 		/* check for 3-op insn */
461 		if (c2 != NULL)
462 			c = c2;
463 		c++;
464 
465 		/* mirror arch objdump's space-after-comma style */
466 		if (*c == ' ')
467 			c++;
468 	}
469 
470 	return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
471 			 ins->name, c ? c - ops->raw : 0, ops->raw,
472 			 ops->target.offset);
473 }
474 
475 static void jump__delete(struct ins_operands *ops __maybe_unused)
476 {
477 	/*
478 	 * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the
479 	 * raw string, don't free them.
480 	 */
481 }
482 
483 static struct ins_ops jump_ops = {
484 	.free	   = jump__delete,
485 	.parse	   = jump__parse,
486 	.scnprintf = jump__scnprintf,
487 };
488 
489 bool ins__is_jump(const struct ins *ins)
490 {
491 	return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops;
492 }
493 
494 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
495 {
496 	char *endptr, *name, *t;
497 
498 	if (strstr(raw, "(%rip)") == NULL)
499 		return 0;
500 
501 	*addrp = strtoull(comment, &endptr, 16);
502 	if (endptr == comment)
503 		return 0;
504 	name = strchr(endptr, '<');
505 	if (name == NULL)
506 		return -1;
507 
508 	name++;
509 
510 	t = strchr(name, '>');
511 	if (t == NULL)
512 		return 0;
513 
514 	*t = '\0';
515 	*namep = strdup(name);
516 	*t = '>';
517 
518 	return 0;
519 }
520 
521 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
522 		struct disasm_line *dl __maybe_unused)
523 {
524 	ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
525 	if (ops->locked.ops == NULL)
526 		return 0;
527 
528 	if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
529 		goto out_free_ops;
530 
531 	ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name, 0);
532 
533 	if (ops->locked.ins.ops == NULL)
534 		goto out_free_ops;
535 
536 	if (ops->locked.ins.ops->parse &&
537 	    ops->locked.ins.ops->parse(arch, ops->locked.ops, ms, NULL) < 0)
538 		goto out_free_ops;
539 
540 	return 0;
541 
542 out_free_ops:
543 	zfree(&ops->locked.ops);
544 	return 0;
545 }
546 
547 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
548 			   struct ins_operands *ops, int max_ins_name)
549 {
550 	int printed;
551 
552 	if (ops->locked.ins.ops == NULL)
553 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
554 
555 	printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
556 	return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
557 					size - printed, ops->locked.ops, max_ins_name);
558 }
559 
560 static void lock__delete(struct ins_operands *ops)
561 {
562 	struct ins *ins = &ops->locked.ins;
563 
564 	if (ins->ops && ins->ops->free)
565 		ins->ops->free(ops->locked.ops);
566 	else
567 		ins_ops__delete(ops->locked.ops);
568 
569 	zfree(&ops->locked.ops);
570 	zfree(&ops->locked.ins.name);
571 	zfree(&ops->target.raw);
572 	zfree(&ops->target.name);
573 }
574 
575 static struct ins_ops lock_ops = {
576 	.free	   = lock__delete,
577 	.parse	   = lock__parse,
578 	.scnprintf = lock__scnprintf,
579 };
580 
581 /*
582  * Check if the operand has more than one registers like x86 SIB addressing:
583  *   0x1234(%rax, %rbx, 8)
584  *
585  * But it doesn't care segment selectors like %gs:0x5678(%rcx), so just check
586  * the input string after 'memory_ref_char' if exists.
587  */
588 static bool check_multi_regs(struct arch *arch, const char *op)
589 {
590 	int count = 0;
591 
592 	if (arch->objdump.register_char == 0)
593 		return false;
594 
595 	if (arch->objdump.memory_ref_char) {
596 		op = strchr(op, arch->objdump.memory_ref_char);
597 		if (op == NULL)
598 			return false;
599 	}
600 
601 	while ((op = strchr(op, arch->objdump.register_char)) != NULL) {
602 		count++;
603 		op++;
604 	}
605 
606 	return count > 1;
607 }
608 
609 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused,
610 		struct disasm_line *dl __maybe_unused)
611 {
612 	char *s = strchr(ops->raw, ','), *target, *comment, prev;
613 
614 	if (s == NULL)
615 		return -1;
616 
617 	*s = '\0';
618 
619 	/*
620 	 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
621 	 * then it needs to have the closing parenthesis.
622 	 */
623 	if (strchr(ops->raw, '(')) {
624 		*s = ',';
625 		s = strchr(ops->raw, ')');
626 		if (s == NULL || s[1] != ',')
627 			return -1;
628 		*++s = '\0';
629 	}
630 
631 	ops->source.raw = strdup(ops->raw);
632 	*s = ',';
633 
634 	if (ops->source.raw == NULL)
635 		return -1;
636 
637 	ops->source.multi_regs = check_multi_regs(arch, ops->source.raw);
638 
639 	target = skip_spaces(++s);
640 	comment = strchr(s, arch->objdump.comment_char);
641 
642 	if (comment != NULL)
643 		s = comment - 1;
644 	else
645 		s = strchr(s, '\0') - 1;
646 
647 	while (s > target && isspace(s[0]))
648 		--s;
649 	s++;
650 	prev = *s;
651 	*s = '\0';
652 
653 	ops->target.raw = strdup(target);
654 	*s = prev;
655 
656 	if (ops->target.raw == NULL)
657 		goto out_free_source;
658 
659 	ops->target.multi_regs = check_multi_regs(arch, ops->target.raw);
660 
661 	if (comment == NULL)
662 		return 0;
663 
664 	comment = skip_spaces(comment);
665 	comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
666 	comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
667 
668 	return 0;
669 
670 out_free_source:
671 	zfree(&ops->source.raw);
672 	return -1;
673 }
674 
675 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
676 			   struct ins_operands *ops, int max_ins_name)
677 {
678 	return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
679 			 ops->source.name ?: ops->source.raw,
680 			 ops->target.name ?: ops->target.raw);
681 }
682 
683 static struct ins_ops mov_ops = {
684 	.parse	   = mov__parse,
685 	.scnprintf = mov__scnprintf,
686 };
687 
688 #define PPC_22_30(R)    (((R) >> 1) & 0x1ff)
689 #define MINUS_EXT_XO_FORM	234
690 #define SUB_EXT_XO_FORM		232
691 #define	ADD_ZERO_EXT_XO_FORM	202
692 #define	SUB_ZERO_EXT_XO_FORM	200
693 
694 static int arithmetic__scnprintf(struct ins *ins, char *bf, size_t size,
695 		struct ins_operands *ops, int max_ins_name)
696 {
697 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
698 			ops->raw);
699 }
700 
701 /*
702  * Sets the fields: multi_regs and "mem_ref".
703  * "mem_ref" is set for ops->source which is later used to
704  * fill the objdump->memory_ref-char field. This ops is currently
705  * used by powerpc and since binary instruction code is used to
706  * extract opcode, regs and offset, no other parsing is needed here.
707  *
708  * Dont set multi regs for 4 cases since it has only one operand
709  * for source:
710  * - Add to Minus One Extended XO-form ( Ex: addme, addmeo )
711  * - Subtract From Minus One Extended XO-form ( Ex: subfme )
712  * - Add to Zero Extended XO-form ( Ex: addze, addzeo )
713  * - Subtract From Zero Extended XO-form ( Ex: subfze )
714  */
715 static int arithmetic__parse(struct arch *arch __maybe_unused, struct ins_operands *ops,
716 		struct map_symbol *ms __maybe_unused, struct disasm_line *dl)
717 {
718 	int opcode = PPC_OP(dl->raw.raw_insn);
719 
720 	ops->source.mem_ref = false;
721 	if (opcode == 31) {
722 		if ((opcode != MINUS_EXT_XO_FORM) && (opcode != SUB_EXT_XO_FORM) \
723 				&& (opcode != ADD_ZERO_EXT_XO_FORM) && (opcode != SUB_ZERO_EXT_XO_FORM))
724 			ops->source.multi_regs = true;
725 	}
726 
727 	ops->target.mem_ref = false;
728 	ops->target.multi_regs = false;
729 
730 	return 0;
731 }
732 
733 static struct ins_ops arithmetic_ops = {
734 	.parse     = arithmetic__parse,
735 	.scnprintf = arithmetic__scnprintf,
736 };
737 
738 static int load_store__scnprintf(struct ins *ins, char *bf, size_t size,
739 		struct ins_operands *ops, int max_ins_name)
740 {
741 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
742 			ops->raw);
743 }
744 
745 /*
746  * Sets the fields: multi_regs and "mem_ref".
747  * "mem_ref" is set for ops->source which is later used to
748  * fill the objdump->memory_ref-char field. This ops is currently
749  * used by powerpc and since binary instruction code is used to
750  * extract opcode, regs and offset, no other parsing is needed here
751  */
752 static int load_store__parse(struct arch *arch __maybe_unused, struct ins_operands *ops,
753 		struct map_symbol *ms __maybe_unused, struct disasm_line *dl __maybe_unused)
754 {
755 	ops->source.mem_ref = true;
756 	ops->source.multi_regs = false;
757 	/* opcode 31 is of X form */
758 	if (PPC_OP(dl->raw.raw_insn) == 31)
759 		ops->source.multi_regs = true;
760 
761 	ops->target.mem_ref = false;
762 	ops->target.multi_regs = false;
763 
764 	return 0;
765 }
766 
767 static struct ins_ops load_store_ops = {
768 	.parse     = load_store__parse,
769 	.scnprintf = load_store__scnprintf,
770 };
771 
772 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused,
773 		struct disasm_line *dl __maybe_unused)
774 {
775 	char *target, *comment, *s, prev;
776 
777 	target = s = ops->raw;
778 
779 	while (s[0] != '\0' && !isspace(s[0]))
780 		++s;
781 	prev = *s;
782 	*s = '\0';
783 
784 	ops->target.raw = strdup(target);
785 	*s = prev;
786 
787 	if (ops->target.raw == NULL)
788 		return -1;
789 
790 	comment = strchr(s, arch->objdump.comment_char);
791 	if (comment == NULL)
792 		return 0;
793 
794 	comment = skip_spaces(comment);
795 	comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
796 
797 	return 0;
798 }
799 
800 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
801 			   struct ins_operands *ops, int max_ins_name)
802 {
803 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
804 			 ops->target.name ?: ops->target.raw);
805 }
806 
807 static struct ins_ops dec_ops = {
808 	.parse	   = dec__parse,
809 	.scnprintf = dec__scnprintf,
810 };
811 
812 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
813 			  struct ins_operands *ops __maybe_unused, int max_ins_name)
814 {
815 	return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
816 }
817 
818 static struct ins_ops nop_ops = {
819 	.scnprintf = nop__scnprintf,
820 };
821 
822 static struct ins_ops ret_ops = {
823 	.scnprintf = ins__raw_scnprintf,
824 };
825 
826 bool ins__is_nop(const struct ins *ins)
827 {
828 	return ins->ops == &nop_ops;
829 }
830 
831 bool ins__is_ret(const struct ins *ins)
832 {
833 	return ins->ops == &ret_ops;
834 }
835 
836 bool ins__is_lock(const struct ins *ins)
837 {
838 	return ins->ops == &lock_ops;
839 }
840 
841 static int ins__key_cmp(const void *name, const void *insp)
842 {
843 	const struct ins *ins = insp;
844 
845 	return strcmp(name, ins->name);
846 }
847 
848 static int ins__cmp(const void *a, const void *b)
849 {
850 	const struct ins *ia = a;
851 	const struct ins *ib = b;
852 
853 	return strcmp(ia->name, ib->name);
854 }
855 
856 static void ins__sort(struct arch *arch)
857 {
858 	const int nmemb = arch->nr_instructions;
859 
860 	qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
861 }
862 
863 static struct ins_ops *__ins__find(struct arch *arch, const char *name, struct disasm_line *dl)
864 {
865 	struct ins *ins;
866 	const int nmemb = arch->nr_instructions;
867 
868 	if (arch__is(arch, "powerpc")) {
869 		/*
870 		 * For powerpc, identify the instruction ops
871 		 * from the opcode using raw_insn.
872 		 */
873 		struct ins_ops *ops;
874 
875 		ops = check_ppc_insn(dl);
876 		if (ops)
877 			return ops;
878 	}
879 
880 	if (!arch->sorted_instructions) {
881 		ins__sort(arch);
882 		arch->sorted_instructions = true;
883 	}
884 
885 	ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
886 	if (ins)
887 		return ins->ops;
888 
889 	if (arch->insn_suffix) {
890 		char tmp[32];
891 		char suffix;
892 		size_t len = strlen(name);
893 
894 		if (len == 0 || len >= sizeof(tmp))
895 			return NULL;
896 
897 		suffix = name[len - 1];
898 		if (strchr(arch->insn_suffix, suffix) == NULL)
899 			return NULL;
900 
901 		strcpy(tmp, name);
902 		tmp[len - 1] = '\0'; /* remove the suffix and check again */
903 
904 		ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
905 	}
906 	return ins ? ins->ops : NULL;
907 }
908 
909 struct ins_ops *ins__find(struct arch *arch, const char *name, struct disasm_line *dl)
910 {
911 	struct ins_ops *ops = __ins__find(arch, name, dl);
912 
913 	if (!ops && arch->associate_instruction_ops)
914 		ops = arch->associate_instruction_ops(arch, name);
915 
916 	return ops;
917 }
918 
919 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
920 {
921 	dl->ins.ops = ins__find(arch, dl->ins.name, dl);
922 
923 	if (!dl->ins.ops)
924 		return;
925 
926 	if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms, dl) < 0)
927 		dl->ins.ops = NULL;
928 }
929 
930 static int disasm_line__parse(char *line, const char **namep, char **rawp)
931 {
932 	char tmp, *name = skip_spaces(line);
933 
934 	if (name[0] == '\0')
935 		return -1;
936 
937 	*rawp = name + 1;
938 
939 	while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
940 		++*rawp;
941 
942 	tmp = (*rawp)[0];
943 	(*rawp)[0] = '\0';
944 	*namep = strdup(name);
945 
946 	if (*namep == NULL)
947 		goto out;
948 
949 	(*rawp)[0] = tmp;
950 	*rawp = strim(*rawp);
951 
952 	return 0;
953 
954 out:
955 	return -1;
956 }
957 
958 /*
959  * Parses the result captured from symbol__disassemble_*
960  * Example, line read from DSO file in powerpc:
961  * line:    38 01 81 e8
962  * opcode: fetched from arch specific get_opcode_insn
963  * rawp_insn: e8810138
964  *
965  * rawp_insn is used later to extract the reg/offset fields
966  */
967 #define	PPC_OP(op)	(((op) >> 26) & 0x3F)
968 #define	RAW_BYTES	11
969 
970 static int disasm_line__parse_powerpc(struct disasm_line *dl)
971 {
972 	char *line = dl->al.line;
973 	const char **namep = &dl->ins.name;
974 	char **rawp = &dl->ops.raw;
975 	char *tmp_raw_insn, *name_raw_insn = skip_spaces(line);
976 	char *name = skip_spaces(name_raw_insn + RAW_BYTES);
977 	int objdump = 0;
978 
979 	if (strlen(line) > RAW_BYTES)
980 		objdump = 1;
981 
982 	if (name_raw_insn[0] == '\0')
983 		return -1;
984 
985 	if (objdump) {
986 		disasm_line__parse(name, namep, rawp);
987 	} else
988 		*namep = "";
989 
990 	tmp_raw_insn = strndup(name_raw_insn, 11);
991 	if (tmp_raw_insn == NULL)
992 		return -1;
993 
994 	remove_spaces(tmp_raw_insn);
995 
996 	sscanf(tmp_raw_insn, "%x", &dl->raw.raw_insn);
997 	if (objdump)
998 		dl->raw.raw_insn = be32_to_cpu(dl->raw.raw_insn);
999 
1000 	return 0;
1001 }
1002 
1003 static void annotation_line__init(struct annotation_line *al,
1004 				  struct annotate_args *args,
1005 				  int nr)
1006 {
1007 	al->offset = args->offset;
1008 	al->line = strdup(args->line);
1009 	al->line_nr = args->line_nr;
1010 	al->fileloc = args->fileloc;
1011 	al->data_nr = nr;
1012 }
1013 
1014 static void annotation_line__exit(struct annotation_line *al)
1015 {
1016 	zfree_srcline(&al->path);
1017 	zfree(&al->line);
1018 	zfree(&al->cycles);
1019 	zfree(&al->br_cntr);
1020 }
1021 
1022 static size_t disasm_line_size(int nr)
1023 {
1024 	struct annotation_line *al;
1025 
1026 	return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
1027 }
1028 
1029 /*
1030  * Allocating the disasm annotation line data with
1031  * following structure:
1032  *
1033  *    -------------------------------------------
1034  *    struct disasm_line | struct annotation_line
1035  *    -------------------------------------------
1036  *
1037  * We have 'struct annotation_line' member as last member
1038  * of 'struct disasm_line' to have an easy access.
1039  */
1040 struct disasm_line *disasm_line__new(struct annotate_args *args)
1041 {
1042 	struct disasm_line *dl = NULL;
1043 	struct annotation *notes = symbol__annotation(args->ms.sym);
1044 	int nr = notes->src->nr_events;
1045 
1046 	dl = zalloc(disasm_line_size(nr));
1047 	if (!dl)
1048 		return NULL;
1049 
1050 	annotation_line__init(&dl->al, args, nr);
1051 	if (dl->al.line == NULL)
1052 		goto out_delete;
1053 
1054 	if (args->offset != -1) {
1055 		if (arch__is(args->arch, "powerpc")) {
1056 			if (disasm_line__parse_powerpc(dl) < 0)
1057 				goto out_free_line;
1058 		} else if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
1059 			goto out_free_line;
1060 
1061 		disasm_line__init_ins(dl, args->arch, &args->ms);
1062 	}
1063 
1064 	return dl;
1065 
1066 out_free_line:
1067 	zfree(&dl->al.line);
1068 out_delete:
1069 	free(dl);
1070 	return NULL;
1071 }
1072 
1073 void disasm_line__free(struct disasm_line *dl)
1074 {
1075 	if (dl->ins.ops && dl->ins.ops->free)
1076 		dl->ins.ops->free(&dl->ops);
1077 	else
1078 		ins_ops__delete(&dl->ops);
1079 	zfree(&dl->ins.name);
1080 	annotation_line__exit(&dl->al);
1081 	free(dl);
1082 }
1083 
1084 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
1085 {
1086 	if (raw || !dl->ins.ops)
1087 		return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
1088 
1089 	return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
1090 }
1091 
1092 /*
1093  * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1094  * which looks like following
1095  *
1096  *  0000000000415500 <_init>:
1097  *    415500:       sub    $0x8,%rsp
1098  *    415504:       mov    0x2f5ad5(%rip),%rax        # 70afe0 <_DYNAMIC+0x2f8>
1099  *    41550b:       test   %rax,%rax
1100  *    41550e:       je     415515 <_init+0x15>
1101  *    415510:       callq  416e70 <__gmon_start__@plt>
1102  *    415515:       add    $0x8,%rsp
1103  *    415519:       retq
1104  *
1105  * it will be parsed and saved into struct disasm_line as
1106  *  <offset>       <name>  <ops.raw>
1107  *
1108  * The offset will be a relative offset from the start of the symbol and -1
1109  * means that it's not a disassembly line so should be treated differently.
1110  * The ops.raw part will be parsed further according to type of the instruction.
1111  */
1112 static int symbol__parse_objdump_line(struct symbol *sym,
1113 				      struct annotate_args *args,
1114 				      char *parsed_line, int *line_nr, char **fileloc)
1115 {
1116 	struct map *map = args->ms.map;
1117 	struct annotation *notes = symbol__annotation(sym);
1118 	struct disasm_line *dl;
1119 	char *tmp;
1120 	s64 line_ip, offset = -1;
1121 	regmatch_t match[2];
1122 
1123 	/* /filename:linenr ? Save line number and ignore. */
1124 	if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1125 		*line_nr = atoi(parsed_line + match[1].rm_so);
1126 		free(*fileloc);
1127 		*fileloc = strdup(parsed_line);
1128 		return 0;
1129 	}
1130 
1131 	/* Process hex address followed by ':'. */
1132 	line_ip = strtoull(parsed_line, &tmp, 16);
1133 	if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') {
1134 		u64 start = map__rip_2objdump(map, sym->start),
1135 		    end = map__rip_2objdump(map, sym->end);
1136 
1137 		offset = line_ip - start;
1138 		if ((u64)line_ip < start || (u64)line_ip >= end)
1139 			offset = -1;
1140 		else
1141 			parsed_line = tmp + 1;
1142 	}
1143 
1144 	args->offset  = offset;
1145 	args->line    = parsed_line;
1146 	args->line_nr = *line_nr;
1147 	args->fileloc = *fileloc;
1148 	args->ms.sym  = sym;
1149 
1150 	dl = disasm_line__new(args);
1151 	(*line_nr)++;
1152 
1153 	if (dl == NULL)
1154 		return -1;
1155 
1156 	if (!disasm_line__has_local_offset(dl)) {
1157 		dl->ops.target.offset = dl->ops.target.addr -
1158 					map__rip_2objdump(map, sym->start);
1159 		dl->ops.target.offset_avail = true;
1160 	}
1161 
1162 	/* kcore has no symbols, so add the call target symbol */
1163 	if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
1164 		struct addr_map_symbol target = {
1165 			.addr = dl->ops.target.addr,
1166 			.ms = { .map = map, },
1167 		};
1168 
1169 		if (!maps__find_ams(args->ms.maps, &target) &&
1170 		    target.ms.sym->start == target.al_addr)
1171 			dl->ops.target.sym = target.ms.sym;
1172 	}
1173 
1174 	annotation_line__add(&dl->al, &notes->src->source);
1175 	return 0;
1176 }
1177 
1178 static void delete_last_nop(struct symbol *sym)
1179 {
1180 	struct annotation *notes = symbol__annotation(sym);
1181 	struct list_head *list = &notes->src->source;
1182 	struct disasm_line *dl;
1183 
1184 	while (!list_empty(list)) {
1185 		dl = list_entry(list->prev, struct disasm_line, al.node);
1186 
1187 		if (dl->ins.ops) {
1188 			if (!ins__is_nop(&dl->ins))
1189 				return;
1190 		} else {
1191 			if (!strstr(dl->al.line, " nop ") &&
1192 			    !strstr(dl->al.line, " nopl ") &&
1193 			    !strstr(dl->al.line, " nopw "))
1194 				return;
1195 		}
1196 
1197 		list_del_init(&dl->al.node);
1198 		disasm_line__free(dl);
1199 	}
1200 }
1201 
1202 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
1203 {
1204 	struct dso *dso = map__dso(ms->map);
1205 
1206 	BUG_ON(buflen == 0);
1207 
1208 	if (errnum >= 0) {
1209 		str_error_r(errnum, buf, buflen);
1210 		return 0;
1211 	}
1212 
1213 	switch (errnum) {
1214 	case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1215 		char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1216 		char *build_id_msg = NULL;
1217 
1218 		if (dso__has_build_id(dso)) {
1219 			build_id__sprintf(dso__bid(dso), bf + 15);
1220 			build_id_msg = bf;
1221 		}
1222 		scnprintf(buf, buflen,
1223 			  "No vmlinux file%s\nwas found in the path.\n\n"
1224 			  "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1225 			  "Please use:\n\n"
1226 			  "  perf buildid-cache -vu vmlinux\n\n"
1227 			  "or:\n\n"
1228 			  "  --vmlinux vmlinux\n", build_id_msg ?: "");
1229 	}
1230 		break;
1231 	case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1232 		scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1233 		break;
1234 	case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
1235 		scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
1236 		break;
1237 	case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
1238 		scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
1239 		break;
1240 	case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
1241 		scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso));
1242 		break;
1243 	case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF:
1244 		scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.",
1245 			  dso__long_name(dso));
1246 		break;
1247 	default:
1248 		scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1249 		break;
1250 	}
1251 
1252 	return 0;
1253 }
1254 
1255 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1256 {
1257 	char linkname[PATH_MAX];
1258 	char *build_id_filename;
1259 	char *build_id_path = NULL;
1260 	char *pos;
1261 	int len;
1262 
1263 	if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
1264 	    !dso__is_kcore(dso))
1265 		return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1266 
1267 	build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1268 	if (build_id_filename) {
1269 		__symbol__join_symfs(filename, filename_size, build_id_filename);
1270 		free(build_id_filename);
1271 	} else {
1272 		if (dso__has_build_id(dso))
1273 			return ENOMEM;
1274 		goto fallback;
1275 	}
1276 
1277 	build_id_path = strdup(filename);
1278 	if (!build_id_path)
1279 		return ENOMEM;
1280 
1281 	/*
1282 	 * old style build-id cache has name of XX/XXXXXXX.. while
1283 	 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1284 	 * extract the build-id part of dirname in the new style only.
1285 	 */
1286 	pos = strrchr(build_id_path, '/');
1287 	if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1288 		dirname(build_id_path);
1289 
1290 	if (dso__is_kcore(dso))
1291 		goto fallback;
1292 
1293 	len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1294 	if (len < 0)
1295 		goto fallback;
1296 
1297 	linkname[len] = '\0';
1298 	if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1299 		access(filename, R_OK)) {
1300 fallback:
1301 		/*
1302 		 * If we don't have build-ids or the build-id file isn't in the
1303 		 * cache, or is just a kallsyms file, well, lets hope that this
1304 		 * DSO is the same as when 'perf record' ran.
1305 		 */
1306 		if (dso__kernel(dso) && dso__long_name(dso)[0] == '/')
1307 			snprintf(filename, filename_size, "%s", dso__long_name(dso));
1308 		else
1309 			__symbol__join_symfs(filename, filename_size, dso__long_name(dso));
1310 
1311 		mutex_lock(dso__lock(dso));
1312 		if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) {
1313 			char *new_name = dso__filename_with_chroot(dso, filename);
1314 			if (new_name) {
1315 				strlcpy(filename, new_name, filename_size);
1316 				free(new_name);
1317 			}
1318 		}
1319 		mutex_unlock(dso__lock(dso));
1320 	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
1321 		dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE);
1322 	}
1323 
1324 	free(build_id_path);
1325 	return 0;
1326 }
1327 
1328 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1329 #include <capstone/capstone.h>
1330 
1331 int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool disassembler_style);
1332 
1333 static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
1334 				csh *handle)
1335 {
1336 	struct annotation_options *opt = args->options;
1337 	cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
1338 
1339 	/* TODO: support more architectures */
1340 	if (!arch__is(args->arch, "x86"))
1341 		return -1;
1342 
1343 	if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
1344 		return -1;
1345 
1346 	if (!opt->disassembler_style ||
1347 	    !strcmp(opt->disassembler_style, "att"))
1348 		cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
1349 
1350 	/*
1351 	 * Resolving address operands to symbols is implemented
1352 	 * on x86 by investigating instruction details.
1353 	 */
1354 	cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
1355 
1356 	return 0;
1357 }
1358 #endif
1359 
1360 #if defined(HAVE_LIBCAPSTONE_SUPPORT) || defined(HAVE_LIBLLVM_SUPPORT)
1361 struct find_file_offset_data {
1362 	u64 ip;
1363 	u64 offset;
1364 };
1365 
1366 /* This will be called for each PHDR in an ELF binary */
1367 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
1368 {
1369 	struct find_file_offset_data *data = arg;
1370 
1371 	if (start <= data->ip && data->ip < start + len) {
1372 		data->offset = pgoff + data->ip - start;
1373 		return 1;
1374 	}
1375 	return 0;
1376 }
1377 
1378 static u8 *
1379 read_symbol(const char *filename, struct map *map, struct symbol *sym,
1380 	    u64 *len, bool *is_64bit)
1381 {
1382 	struct dso *dso = map__dso(map);
1383 	struct nscookie nsc;
1384 	u64 start = map__rip_2objdump(map, sym->start);
1385 	u64 end = map__rip_2objdump(map, sym->end);
1386 	int fd, count;
1387 	u8 *buf = NULL;
1388 	struct find_file_offset_data data = {
1389 		.ip = start,
1390 	};
1391 
1392 	*is_64bit = false;
1393 
1394 	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1395 	fd = open(filename, O_RDONLY);
1396 	nsinfo__mountns_exit(&nsc);
1397 	if (fd < 0)
1398 		return NULL;
1399 
1400 	if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1401 			    is_64bit) == 0)
1402 		goto err;
1403 
1404 	*len = end - start;
1405 	buf = malloc(*len);
1406 	if (buf == NULL)
1407 		goto err;
1408 
1409 	count = pread(fd, buf, *len, data.offset);
1410 	close(fd);
1411 	fd = -1;
1412 
1413 	if ((u64)count != *len)
1414 		goto err;
1415 
1416 	return buf;
1417 
1418 err:
1419 	if (fd >= 0)
1420 		close(fd);
1421 	free(buf);
1422 	return NULL;
1423 }
1424 #endif
1425 
1426 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1427 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
1428 				  struct annotate_args *args, u64 addr)
1429 {
1430 	int i;
1431 	struct map *map = args->ms.map;
1432 	struct symbol *sym;
1433 
1434 	/* TODO: support more architectures */
1435 	if (!arch__is(args->arch, "x86"))
1436 		return;
1437 
1438 	if (insn->detail == NULL)
1439 		return;
1440 
1441 	for (i = 0; i < insn->detail->x86.op_count; i++) {
1442 		cs_x86_op *op = &insn->detail->x86.operands[i];
1443 		u64 orig_addr;
1444 
1445 		if (op->type != X86_OP_MEM)
1446 			continue;
1447 
1448 		/* only print RIP-based global symbols for now */
1449 		if (op->mem.base != X86_REG_RIP)
1450 			continue;
1451 
1452 		/* get the target address */
1453 		orig_addr = addr + insn->size + op->mem.disp;
1454 		addr = map__objdump_2mem(map, orig_addr);
1455 
1456 		if (dso__kernel(map__dso(map))) {
1457 			/*
1458 			 * The kernel maps can be splitted into sections,
1459 			 * let's find the map first and the search the symbol.
1460 			 */
1461 			map = maps__find(map__kmaps(map), addr);
1462 			if (map == NULL)
1463 				continue;
1464 		}
1465 
1466 		/* convert it to map-relative address for search */
1467 		addr = map__map_ip(map, addr);
1468 
1469 		sym = map__find_symbol(map, addr);
1470 		if (sym == NULL)
1471 			continue;
1472 
1473 		if (addr == sym->start) {
1474 			scnprintf(buf, len, "\t# %"PRIx64" <%s>",
1475 				  orig_addr, sym->name);
1476 		} else {
1477 			scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">",
1478 				  orig_addr, sym->name, addr - sym->start);
1479 		}
1480 		break;
1481 	}
1482 }
1483 
1484 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym,
1485 					struct annotate_args *args)
1486 {
1487 	struct annotation *notes = symbol__annotation(sym);
1488 	struct map *map = args->ms.map;
1489 	struct dso *dso = map__dso(map);
1490 	struct nscookie nsc;
1491 	u64 start = map__rip_2objdump(map, sym->start);
1492 	u64 end = map__rip_2objdump(map, sym->end);
1493 	u64 len = end - start;
1494 	u64 offset;
1495 	int i, fd, count;
1496 	bool is_64bit = false;
1497 	bool needs_cs_close = false;
1498 	u8 *buf = NULL;
1499 	struct find_file_offset_data data = {
1500 		.ip = start,
1501 	};
1502 	csh handle;
1503 	char disasm_buf[512];
1504 	struct disasm_line *dl;
1505 	u32 *line;
1506 	bool disassembler_style = false;
1507 
1508 	if (args->options->objdump_path)
1509 		return -1;
1510 
1511 	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1512 	fd = open(filename, O_RDONLY);
1513 	nsinfo__mountns_exit(&nsc);
1514 	if (fd < 0)
1515 		return -1;
1516 
1517 	if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1518 			    &is_64bit) == 0)
1519 		goto err;
1520 
1521 	if (!args->options->disassembler_style ||
1522 			!strcmp(args->options->disassembler_style, "att"))
1523 		disassembler_style = true;
1524 
1525 	if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0)
1526 		goto err;
1527 
1528 	needs_cs_close = true;
1529 
1530 	buf = malloc(len);
1531 	if (buf == NULL)
1532 		goto err;
1533 
1534 	count = pread(fd, buf, len, data.offset);
1535 	close(fd);
1536 	fd = -1;
1537 
1538 	if ((u64)count != len)
1539 		goto err;
1540 
1541 	line = (u32 *)buf;
1542 
1543 	/* add the function address and name */
1544 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1545 		  start, sym->name);
1546 
1547 	args->offset = -1;
1548 	args->line = disasm_buf;
1549 	args->line_nr = 0;
1550 	args->fileloc = NULL;
1551 	args->ms.sym = sym;
1552 
1553 	dl = disasm_line__new(args);
1554 	if (dl == NULL)
1555 		goto err;
1556 
1557 	annotation_line__add(&dl->al, &notes->src->source);
1558 
1559 	/*
1560 	 * TODO: enable disassm for powerpc
1561 	 * count = cs_disasm(handle, buf, len, start, len, &insn);
1562 	 *
1563 	 * For now, only binary code is saved in disassembled line
1564 	 * to be used in "type" and "typeoff" sort keys. Each raw code
1565 	 * is 32 bit instruction. So use "len/4" to get the number of
1566 	 * entries.
1567 	 */
1568 	count = len/4;
1569 
1570 	for (i = 0, offset = 0; i < count; i++) {
1571 		args->offset = offset;
1572 		sprintf(args->line, "%x", line[i]);
1573 
1574 		dl = disasm_line__new(args);
1575 		if (dl == NULL)
1576 			break;
1577 
1578 		annotation_line__add(&dl->al, &notes->src->source);
1579 
1580 		offset += 4;
1581 	}
1582 
1583 	/* It failed in the middle */
1584 	if (offset != len) {
1585 		struct list_head *list = &notes->src->source;
1586 
1587 		/* Discard all lines and fallback to objdump */
1588 		while (!list_empty(list)) {
1589 			dl = list_first_entry(list, struct disasm_line, al.node);
1590 
1591 			list_del_init(&dl->al.node);
1592 			disasm_line__free(dl);
1593 		}
1594 		count = -1;
1595 	}
1596 
1597 out:
1598 	if (needs_cs_close)
1599 		cs_close(&handle);
1600 	free(buf);
1601 	return count < 0 ? count : 0;
1602 
1603 err:
1604 	if (fd >= 0)
1605 		close(fd);
1606 	count = -1;
1607 	goto out;
1608 }
1609 
1610 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1611 					struct annotate_args *args)
1612 {
1613 	struct annotation *notes = symbol__annotation(sym);
1614 	struct map *map = args->ms.map;
1615 	u64 start = map__rip_2objdump(map, sym->start);
1616 	u64 len;
1617 	u64 offset;
1618 	int i, count, free_count;
1619 	bool is_64bit = false;
1620 	bool needs_cs_close = false;
1621 	u8 *buf = NULL;
1622 	csh handle;
1623 	cs_insn *insn = NULL;
1624 	char disasm_buf[512];
1625 	struct disasm_line *dl;
1626 
1627 	if (args->options->objdump_path)
1628 		return -1;
1629 
1630 	buf = read_symbol(filename, map, sym, &len, &is_64bit);
1631 	if (buf == NULL)
1632 		return -1;
1633 
1634 	/* add the function address and name */
1635 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1636 		  start, sym->name);
1637 
1638 	args->offset = -1;
1639 	args->line = disasm_buf;
1640 	args->line_nr = 0;
1641 	args->fileloc = NULL;
1642 	args->ms.sym = sym;
1643 
1644 	dl = disasm_line__new(args);
1645 	if (dl == NULL)
1646 		goto err;
1647 
1648 	annotation_line__add(&dl->al, &notes->src->source);
1649 
1650 	if (open_capstone_handle(args, is_64bit, &handle) < 0)
1651 		goto err;
1652 
1653 	needs_cs_close = true;
1654 
1655 	free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
1656 	for (i = 0, offset = 0; i < count; i++) {
1657 		int printed;
1658 
1659 		printed = scnprintf(disasm_buf, sizeof(disasm_buf),
1660 				    "       %-7s %s",
1661 				    insn[i].mnemonic, insn[i].op_str);
1662 		print_capstone_detail(&insn[i], disasm_buf + printed,
1663 				      sizeof(disasm_buf) - printed, args,
1664 				      start + offset);
1665 
1666 		args->offset = offset;
1667 		args->line = disasm_buf;
1668 
1669 		dl = disasm_line__new(args);
1670 		if (dl == NULL)
1671 			goto err;
1672 
1673 		annotation_line__add(&dl->al, &notes->src->source);
1674 
1675 		offset += insn[i].size;
1676 	}
1677 
1678 	/* It failed in the middle: probably due to unknown instructions */
1679 	if (offset != len) {
1680 		struct list_head *list = &notes->src->source;
1681 
1682 		/* Discard all lines and fallback to objdump */
1683 		while (!list_empty(list)) {
1684 			dl = list_first_entry(list, struct disasm_line, al.node);
1685 
1686 			list_del_init(&dl->al.node);
1687 			disasm_line__free(dl);
1688 		}
1689 		count = -1;
1690 	}
1691 
1692 out:
1693 	if (needs_cs_close) {
1694 		cs_close(&handle);
1695 		if (free_count > 0)
1696 			cs_free(insn, free_count);
1697 	}
1698 	free(buf);
1699 	return count < 0 ? count : 0;
1700 
1701 err:
1702 	if (needs_cs_close) {
1703 		struct disasm_line *tmp;
1704 
1705 		/*
1706 		 * It probably failed in the middle of the above loop.
1707 		 * Release any resources it might add.
1708 		 */
1709 		list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
1710 			list_del(&dl->al.node);
1711 			disasm_line__free(dl);
1712 		}
1713 	}
1714 	count = -1;
1715 	goto out;
1716 }
1717 #endif
1718 
1719 static int symbol__disassemble_raw(char *filename, struct symbol *sym,
1720 					struct annotate_args *args)
1721 {
1722 	struct annotation *notes = symbol__annotation(sym);
1723 	struct map *map = args->ms.map;
1724 	struct dso *dso = map__dso(map);
1725 	u64 start = map__rip_2objdump(map, sym->start);
1726 	u64 end = map__rip_2objdump(map, sym->end);
1727 	u64 len = end - start;
1728 	u64 offset;
1729 	int i, count;
1730 	u8 *buf = NULL;
1731 	char disasm_buf[512];
1732 	struct disasm_line *dl;
1733 	u32 *line;
1734 
1735 	/* Return if objdump is specified explicitly */
1736 	if (args->options->objdump_path)
1737 		return -1;
1738 
1739 	pr_debug("Reading raw instruction from : %s using dso__data_read_offset\n", filename);
1740 
1741 	buf = malloc(len);
1742 	if (buf == NULL)
1743 		goto err;
1744 
1745 	count = dso__data_read_offset(dso, NULL, sym->start, buf, len);
1746 
1747 	line = (u32 *)buf;
1748 
1749 	if ((u64)count != len)
1750 		goto err;
1751 
1752 	/* add the function address and name */
1753 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1754 		  start, sym->name);
1755 
1756 	args->offset = -1;
1757 	args->line = disasm_buf;
1758 	args->line_nr = 0;
1759 	args->fileloc = NULL;
1760 	args->ms.sym = sym;
1761 
1762 	dl = disasm_line__new(args);
1763 	if (dl == NULL)
1764 		goto err;
1765 
1766 	annotation_line__add(&dl->al, &notes->src->source);
1767 
1768 	/* Each raw instruction is 4 byte */
1769 	count = len/4;
1770 
1771 	for (i = 0, offset = 0; i < count; i++) {
1772 		args->offset = offset;
1773 		sprintf(args->line, "%x", line[i]);
1774 		dl = disasm_line__new(args);
1775 		if (dl == NULL)
1776 			break;
1777 
1778 		annotation_line__add(&dl->al, &notes->src->source);
1779 		offset += 4;
1780 	}
1781 
1782 	/* It failed in the middle */
1783 	if (offset != len) {
1784 		struct list_head *list = &notes->src->source;
1785 
1786 		/* Discard all lines and fallback to objdump */
1787 		while (!list_empty(list)) {
1788 			dl = list_first_entry(list, struct disasm_line, al.node);
1789 
1790 			list_del_init(&dl->al.node);
1791 			disasm_line__free(dl);
1792 		}
1793 		count = -1;
1794 	}
1795 
1796 out:
1797 	free(buf);
1798 	return count < 0 ? count : 0;
1799 
1800 err:
1801 	count = -1;
1802 	goto out;
1803 }
1804 
1805 #ifdef HAVE_LIBLLVM_SUPPORT
1806 #include <llvm-c/Disassembler.h>
1807 #include <llvm-c/Target.h>
1808 #include "util/llvm-c-helpers.h"
1809 
1810 struct symbol_lookup_storage {
1811 	u64 branch_addr;
1812 	u64 pcrel_load_addr;
1813 };
1814 
1815 /*
1816  * Whenever LLVM wants to resolve an address into a symbol, it calls this
1817  * callback. We don't ever actually _return_ anything (in particular, because
1818  * it puts quotation marks around what we return), but we use this as a hint
1819  * that there is a branch or PC-relative address in the expression that we
1820  * should add some textual annotation for after the instruction. The caller
1821  * will use this information to add the actual annotation.
1822  */
1823 static const char *
1824 symbol_lookup_callback(void *disinfo, uint64_t value,
1825 		       uint64_t *ref_type,
1826 		       uint64_t address __maybe_unused,
1827 		       const char **ref __maybe_unused)
1828 {
1829 	struct symbol_lookup_storage *storage = disinfo;
1830 
1831 	if (*ref_type == LLVMDisassembler_ReferenceType_In_Branch)
1832 		storage->branch_addr = value;
1833 	else if (*ref_type == LLVMDisassembler_ReferenceType_In_PCrel_Load)
1834 		storage->pcrel_load_addr = value;
1835 	*ref_type = LLVMDisassembler_ReferenceType_InOut_None;
1836 	return NULL;
1837 }
1838 
1839 static int symbol__disassemble_llvm(char *filename, struct symbol *sym,
1840 				    struct annotate_args *args)
1841 {
1842 	struct annotation *notes = symbol__annotation(sym);
1843 	struct map *map = args->ms.map;
1844 	struct dso *dso = map__dso(map);
1845 	u64 start = map__rip_2objdump(map, sym->start);
1846 	u8 *buf;
1847 	u64 len;
1848 	u64 pc;
1849 	bool is_64bit;
1850 	char triplet[64];
1851 	char disasm_buf[2048];
1852 	size_t disasm_len;
1853 	struct disasm_line *dl;
1854 	LLVMDisasmContextRef disasm = NULL;
1855 	struct symbol_lookup_storage storage;
1856 	char *line_storage = NULL;
1857 	size_t line_storage_len = 0;
1858 	int ret = -1;
1859 
1860 	if (args->options->objdump_path)
1861 		return -1;
1862 
1863 	LLVMInitializeAllTargetInfos();
1864 	LLVMInitializeAllTargetMCs();
1865 	LLVMInitializeAllDisassemblers();
1866 
1867 	buf = read_symbol(filename, map, sym, &len, &is_64bit);
1868 	if (buf == NULL)
1869 		return -1;
1870 
1871 	if (arch__is(args->arch, "x86")) {
1872 		if (is_64bit)
1873 			scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux");
1874 		else
1875 			scnprintf(triplet, sizeof(triplet), "i686-pc-linux");
1876 	} else {
1877 		scnprintf(triplet, sizeof(triplet), "%s-linux-gnu",
1878 			  args->arch->name);
1879 	}
1880 
1881 	disasm = LLVMCreateDisasm(triplet, &storage, 0, NULL,
1882 				  symbol_lookup_callback);
1883 	if (disasm == NULL)
1884 		goto err;
1885 
1886 	if (args->options->disassembler_style &&
1887 	    !strcmp(args->options->disassembler_style, "intel"))
1888 		LLVMSetDisasmOptions(disasm,
1889 				     LLVMDisassembler_Option_AsmPrinterVariant);
1890 
1891 	/*
1892 	 * This needs to be set after AsmPrinterVariant, due to a bug in LLVM;
1893 	 * setting AsmPrinterVariant makes a new instruction printer, making it
1894 	 * forget about the PrintImmHex flag (which is applied before if both
1895 	 * are given to the same call).
1896 	 */
1897 	LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex);
1898 
1899 	/* add the function address and name */
1900 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1901 		  start, sym->name);
1902 
1903 	args->offset = -1;
1904 	args->line = disasm_buf;
1905 	args->line_nr = 0;
1906 	args->fileloc = NULL;
1907 	args->ms.sym = sym;
1908 
1909 	dl = disasm_line__new(args);
1910 	if (dl == NULL)
1911 		goto err;
1912 
1913 	annotation_line__add(&dl->al, &notes->src->source);
1914 
1915 	pc = start;
1916 	for (u64 offset = 0; offset < len; ) {
1917 		unsigned int ins_len;
1918 
1919 		storage.branch_addr = 0;
1920 		storage.pcrel_load_addr = 0;
1921 
1922 		ins_len = LLVMDisasmInstruction(disasm, buf + offset,
1923 						len - offset, pc,
1924 						disasm_buf, sizeof(disasm_buf));
1925 		if (ins_len == 0)
1926 			goto err;
1927 		disasm_len = strlen(disasm_buf);
1928 
1929 		if (storage.branch_addr != 0) {
1930 			char *name = llvm_name_for_code(dso, filename,
1931 							storage.branch_addr);
1932 			if (name != NULL) {
1933 				disasm_len += scnprintf(disasm_buf + disasm_len,
1934 							sizeof(disasm_buf) -
1935 								disasm_len,
1936 							" <%s>", name);
1937 				free(name);
1938 			}
1939 		}
1940 		if (storage.pcrel_load_addr != 0) {
1941 			char *name = llvm_name_for_data(dso, filename,
1942 							storage.pcrel_load_addr);
1943 			disasm_len += scnprintf(disasm_buf + disasm_len,
1944 						sizeof(disasm_buf) - disasm_len,
1945 						"  # %#"PRIx64,
1946 						storage.pcrel_load_addr);
1947 			if (name) {
1948 				disasm_len += scnprintf(disasm_buf + disasm_len,
1949 							sizeof(disasm_buf) -
1950 							disasm_len,
1951 							" <%s>", name);
1952 				free(name);
1953 			}
1954 		}
1955 
1956 		args->offset = offset;
1957 		args->line = expand_tabs(disasm_buf, &line_storage,
1958 					 &line_storage_len);
1959 		args->line_nr = 0;
1960 		args->fileloc = NULL;
1961 		args->ms.sym = sym;
1962 
1963 		llvm_addr2line(filename, pc, &args->fileloc,
1964 			       (unsigned int *)&args->line_nr, false, NULL);
1965 
1966 		dl = disasm_line__new(args);
1967 		if (dl == NULL)
1968 			goto err;
1969 
1970 		annotation_line__add(&dl->al, &notes->src->source);
1971 
1972 		free(args->fileloc);
1973 		pc += ins_len;
1974 		offset += ins_len;
1975 	}
1976 
1977 	ret = 0;
1978 
1979 err:
1980 	LLVMDisasmDispose(disasm);
1981 	free(buf);
1982 	free(line_storage);
1983 	return ret;
1984 }
1985 #endif
1986 
1987 /*
1988  * Possibly create a new version of line with tabs expanded. Returns the
1989  * existing or new line, storage is updated if a new line is allocated. If
1990  * allocation fails then NULL is returned.
1991  */
1992 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
1993 {
1994 	size_t i, src, dst, len, new_storage_len, num_tabs;
1995 	char *new_line;
1996 	size_t line_len = strlen(line);
1997 
1998 	for (num_tabs = 0, i = 0; i < line_len; i++)
1999 		if (line[i] == '\t')
2000 			num_tabs++;
2001 
2002 	if (num_tabs == 0)
2003 		return line;
2004 
2005 	/*
2006 	 * Space for the line and '\0', less the leading and trailing
2007 	 * spaces. Each tab may introduce 7 additional spaces.
2008 	 */
2009 	new_storage_len = line_len + 1 + (num_tabs * 7);
2010 
2011 	new_line = malloc(new_storage_len);
2012 	if (new_line == NULL) {
2013 		pr_err("Failure allocating memory for tab expansion\n");
2014 		return NULL;
2015 	}
2016 
2017 	/*
2018 	 * Copy regions starting at src and expand tabs. If there are two
2019 	 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
2020 	 * are inserted.
2021 	 */
2022 	for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
2023 		if (line[i] == '\t') {
2024 			len = i - src;
2025 			memcpy(&new_line[dst], &line[src], len);
2026 			dst += len;
2027 			new_line[dst++] = ' ';
2028 			while (dst % 8 != 0)
2029 				new_line[dst++] = ' ';
2030 			src = i + 1;
2031 			num_tabs--;
2032 		}
2033 	}
2034 
2035 	/* Expand the last region. */
2036 	len = line_len - src;
2037 	memcpy(&new_line[dst], &line[src], len);
2038 	dst += len;
2039 	new_line[dst] = '\0';
2040 
2041 	free(*storage);
2042 	*storage = new_line;
2043 	*storage_len = new_storage_len;
2044 	return new_line;
2045 }
2046 
2047 int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
2048 {
2049 	struct annotation_options *opts = &annotate_opts;
2050 	struct map *map = args->ms.map;
2051 	struct dso *dso = map__dso(map);
2052 	char *command;
2053 	FILE *file;
2054 	char symfs_filename[PATH_MAX];
2055 	struct kcore_extract kce;
2056 	bool delete_extract = false;
2057 	bool decomp = false;
2058 	int lineno = 0;
2059 	char *fileloc = NULL;
2060 	int nline;
2061 	char *line;
2062 	size_t line_len;
2063 	const char *objdump_argv[] = {
2064 		"/bin/sh",
2065 		"-c",
2066 		NULL, /* Will be the objdump command to run. */
2067 		"--",
2068 		NULL, /* Will be the symfs path. */
2069 		NULL,
2070 	};
2071 	struct child_process objdump_process;
2072 	int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
2073 
2074 	if (err)
2075 		return err;
2076 
2077 	pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
2078 		 symfs_filename, sym->name, map__unmap_ip(map, sym->start),
2079 		 map__unmap_ip(map, sym->end));
2080 
2081 	pr_debug("annotating [%p] %30s : [%p] %30s\n",
2082 		 dso, dso__long_name(dso), sym, sym->name);
2083 
2084 	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
2085 		return symbol__disassemble_bpf(sym, args);
2086 	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
2087 		return symbol__disassemble_bpf_image(sym, args);
2088 	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
2089 		return -1;
2090 	} else if (dso__is_kcore(dso)) {
2091 		kce.kcore_filename = symfs_filename;
2092 		kce.addr = map__rip_2objdump(map, sym->start);
2093 		kce.offs = sym->start;
2094 		kce.len = sym->end - sym->start;
2095 		if (!kcore_extract__create(&kce)) {
2096 			delete_extract = true;
2097 			strlcpy(symfs_filename, kce.extract_filename,
2098 				sizeof(symfs_filename));
2099 		}
2100 	} else if (dso__needs_decompress(dso)) {
2101 		char tmp[KMOD_DECOMP_LEN];
2102 
2103 		if (dso__decompress_kmodule_path(dso, symfs_filename,
2104 						 tmp, sizeof(tmp)) < 0)
2105 			return -1;
2106 
2107 		decomp = true;
2108 		strcpy(symfs_filename, tmp);
2109 	}
2110 
2111 	/*
2112 	 * For powerpc data type profiling, use the dso__data_read_offset
2113 	 * to read raw instruction directly and interpret the binary code
2114 	 * to understand instructions and register fields. For sort keys as
2115 	 * type and typeoff, disassemble to mnemonic notation is
2116 	 * not required in case of powerpc.
2117 	 */
2118 	if (arch__is(args->arch, "powerpc")) {
2119 		extern const char *sort_order;
2120 
2121 		if (sort_order && !strstr(sort_order, "sym")) {
2122 			err = symbol__disassemble_raw(symfs_filename, sym, args);
2123 			if (err == 0)
2124 				goto out_remove_tmp;
2125 #ifdef HAVE_LIBCAPSTONE_SUPPORT
2126 			err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args);
2127 			if (err == 0)
2128 				goto out_remove_tmp;
2129 #endif
2130 		}
2131 	}
2132 
2133 #ifdef HAVE_LIBLLVM_SUPPORT
2134 	err = symbol__disassemble_llvm(symfs_filename, sym, args);
2135 	if (err == 0)
2136 		goto out_remove_tmp;
2137 #endif
2138 #ifdef HAVE_LIBCAPSTONE_SUPPORT
2139 	err = symbol__disassemble_capstone(symfs_filename, sym, args);
2140 	if (err == 0)
2141 		goto out_remove_tmp;
2142 #endif
2143 
2144 	err = asprintf(&command,
2145 		 "%s %s%s --start-address=0x%016" PRIx64
2146 		 " --stop-address=0x%016" PRIx64
2147 		 " %s -d %s %s %s %c%s%c %s%s -C \"$1\"",
2148 		 opts->objdump_path ?: "objdump",
2149 		 opts->disassembler_style ? "-M " : "",
2150 		 opts->disassembler_style ?: "",
2151 		 map__rip_2objdump(map, sym->start),
2152 		 map__rip_2objdump(map, sym->end),
2153 		 opts->show_linenr ? "-l" : "",
2154 		 opts->show_asm_raw ? "" : "--no-show-raw-insn",
2155 		 opts->annotate_src ? "-S" : "",
2156 		 opts->prefix ? "--prefix " : "",
2157 		 opts->prefix ? '"' : ' ',
2158 		 opts->prefix ?: "",
2159 		 opts->prefix ? '"' : ' ',
2160 		 opts->prefix_strip ? "--prefix-strip=" : "",
2161 		 opts->prefix_strip ?: "");
2162 
2163 	if (err < 0) {
2164 		pr_err("Failure allocating memory for the command to run\n");
2165 		goto out_remove_tmp;
2166 	}
2167 
2168 	pr_debug("Executing: %s\n", command);
2169 
2170 	objdump_argv[2] = command;
2171 	objdump_argv[4] = symfs_filename;
2172 
2173 	/* Create a pipe to read from for stdout */
2174 	memset(&objdump_process, 0, sizeof(objdump_process));
2175 	objdump_process.argv = objdump_argv;
2176 	objdump_process.out = -1;
2177 	objdump_process.err = -1;
2178 	objdump_process.no_stderr = 1;
2179 	if (start_command(&objdump_process)) {
2180 		pr_err("Failure starting to run %s\n", command);
2181 		err = -1;
2182 		goto out_free_command;
2183 	}
2184 
2185 	file = fdopen(objdump_process.out, "r");
2186 	if (!file) {
2187 		pr_err("Failure creating FILE stream for %s\n", command);
2188 		/*
2189 		 * If we were using debug info should retry with
2190 		 * original binary.
2191 		 */
2192 		err = -1;
2193 		goto out_close_stdout;
2194 	}
2195 
2196 	/* Storage for getline. */
2197 	line = NULL;
2198 	line_len = 0;
2199 
2200 	nline = 0;
2201 	while (!feof(file)) {
2202 		const char *match;
2203 		char *expanded_line;
2204 
2205 		if (getline(&line, &line_len, file) < 0 || !line)
2206 			break;
2207 
2208 		/* Skip lines containing "filename:" */
2209 		match = strstr(line, symfs_filename);
2210 		if (match && match[strlen(symfs_filename)] == ':')
2211 			continue;
2212 
2213 		expanded_line = strim(line);
2214 		expanded_line = expand_tabs(expanded_line, &line, &line_len);
2215 		if (!expanded_line)
2216 			break;
2217 
2218 		/*
2219 		 * The source code line number (lineno) needs to be kept in
2220 		 * across calls to symbol__parse_objdump_line(), so that it
2221 		 * can associate it with the instructions till the next one.
2222 		 * See disasm_line__new() and struct disasm_line::line_nr.
2223 		 */
2224 		if (symbol__parse_objdump_line(sym, args, expanded_line,
2225 					       &lineno, &fileloc) < 0)
2226 			break;
2227 		nline++;
2228 	}
2229 	free(line);
2230 	free(fileloc);
2231 
2232 	err = finish_command(&objdump_process);
2233 	if (err)
2234 		pr_err("Error running %s\n", command);
2235 
2236 	if (nline == 0) {
2237 		err = -1;
2238 		pr_err("No output from %s\n", command);
2239 	}
2240 
2241 	/*
2242 	 * kallsyms does not have symbol sizes so there may a nop at the end.
2243 	 * Remove it.
2244 	 */
2245 	if (dso__is_kcore(dso))
2246 		delete_last_nop(sym);
2247 
2248 	fclose(file);
2249 
2250 out_close_stdout:
2251 	close(objdump_process.out);
2252 
2253 out_free_command:
2254 	free(command);
2255 
2256 out_remove_tmp:
2257 	if (decomp)
2258 		unlink(symfs_filename);
2259 
2260 	if (delete_extract)
2261 		kcore_extract__delete(&kce);
2262 
2263 	return err;
2264 }
2265