Linux Audio

Check our new training course

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