disas.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
  4. */
  5. #define _GNU_SOURCE
  6. #include <fnmatch.h>
  7. #include <objtool/arch.h>
  8. #include <objtool/check.h>
  9. #include <objtool/disas.h>
  10. #include <objtool/special.h>
  11. #include <objtool/warn.h>
  12. #include <bfd.h>
  13. #include <linux/string.h>
  14. #include <tools/dis-asm-compat.h>
  15. /*
  16. * Size of the buffer for storing the result of disassembling
  17. * a single instruction.
  18. */
  19. #define DISAS_RESULT_SIZE 1024
  20. struct disas_context {
  21. struct objtool_file *file;
  22. struct instruction *insn;
  23. bool alt_applied;
  24. char result[DISAS_RESULT_SIZE];
  25. disassembler_ftype disassembler;
  26. struct disassemble_info info;
  27. };
  28. /*
  29. * Maximum number of alternatives
  30. */
  31. #define DISAS_ALT_MAX 5
  32. /*
  33. * Maximum number of instructions per alternative
  34. */
  35. #define DISAS_ALT_INSN_MAX 50
  36. /*
  37. * Information to disassemble an alternative
  38. */
  39. struct disas_alt {
  40. struct instruction *orig_insn; /* original instruction */
  41. struct alternative *alt; /* alternative or NULL if default code */
  42. char *name; /* name for this alternative */
  43. int width; /* formatting width */
  44. struct {
  45. char *str; /* instruction string */
  46. int offset; /* instruction offset */
  47. int nops; /* number of nops */
  48. } insn[DISAS_ALT_INSN_MAX]; /* alternative instructions */
  49. int insn_idx; /* index of the next instruction to print */
  50. };
  51. #define DALT_DEFAULT(dalt) (!(dalt)->alt)
  52. #define DALT_INSN(dalt) (DALT_DEFAULT(dalt) ? (dalt)->orig_insn : (dalt)->alt->insn)
  53. #define DALT_GROUP(dalt) (DALT_INSN(dalt)->alt_group)
  54. #define DALT_ALTID(dalt) ((dalt)->orig_insn->offset)
  55. #define ALT_FLAGS_SHIFT 16
  56. #define ALT_FLAG_NOT (1 << 0)
  57. #define ALT_FLAG_DIRECT_CALL (1 << 1)
  58. #define ALT_FEATURE_MASK ((1 << ALT_FLAGS_SHIFT) - 1)
  59. static int alt_feature(unsigned int ft_flags)
  60. {
  61. return (ft_flags & ALT_FEATURE_MASK);
  62. }
  63. static int alt_flags(unsigned int ft_flags)
  64. {
  65. return (ft_flags >> ALT_FLAGS_SHIFT);
  66. }
  67. /*
  68. * Wrapper around asprintf() to allocate and format a string.
  69. * Return the allocated string or NULL on error.
  70. */
  71. static char *strfmt(const char *fmt, ...)
  72. {
  73. va_list ap;
  74. char *str;
  75. int rv;
  76. va_start(ap, fmt);
  77. rv = vasprintf(&str, fmt, ap);
  78. va_end(ap);
  79. return rv == -1 ? NULL : str;
  80. }
  81. static int sprint_name(char *str, const char *name, unsigned long offset)
  82. {
  83. int len;
  84. if (offset)
  85. len = sprintf(str, "%s+0x%lx", name, offset);
  86. else
  87. len = sprintf(str, "%s", name);
  88. return len;
  89. }
  90. #define DINFO_FPRINTF(dinfo, ...) \
  91. ((*(dinfo)->fprintf_func)((dinfo)->stream, __VA_ARGS__))
  92. #define bfd_vma_fmt \
  93. __builtin_choose_expr(sizeof(bfd_vma) == sizeof(unsigned long), "%#lx <%s>", "%#llx <%s>")
  94. static int disas_result_fprintf(struct disas_context *dctx,
  95. const char *fmt, va_list ap)
  96. {
  97. char *buf = dctx->result;
  98. int avail, len;
  99. len = strlen(buf);
  100. if (len >= DISAS_RESULT_SIZE - 1) {
  101. WARN_FUNC(dctx->insn->sec, dctx->insn->offset,
  102. "disassembly buffer is full");
  103. return -1;
  104. }
  105. avail = DISAS_RESULT_SIZE - len;
  106. len = vsnprintf(buf + len, avail, fmt, ap);
  107. if (len < 0 || len >= avail) {
  108. WARN_FUNC(dctx->insn->sec, dctx->insn->offset,
  109. "disassembly buffer is truncated");
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. static int disas_fprintf(void *stream, const char *fmt, ...)
  115. {
  116. va_list arg;
  117. int rv;
  118. va_start(arg, fmt);
  119. rv = disas_result_fprintf(stream, fmt, arg);
  120. va_end(arg);
  121. return rv;
  122. }
  123. /*
  124. * For init_disassemble_info_compat().
  125. */
  126. static int disas_fprintf_styled(void *stream,
  127. enum disassembler_style style,
  128. const char *fmt, ...)
  129. {
  130. va_list arg;
  131. int rv;
  132. va_start(arg, fmt);
  133. rv = disas_result_fprintf(stream, fmt, arg);
  134. va_end(arg);
  135. return rv;
  136. }
  137. static void disas_print_addr_sym(struct section *sec, struct symbol *sym,
  138. bfd_vma addr, struct disassemble_info *dinfo)
  139. {
  140. char symstr[1024];
  141. char *str;
  142. if (sym) {
  143. sprint_name(symstr, sym->name, addr - sym->offset);
  144. DINFO_FPRINTF(dinfo, bfd_vma_fmt, addr, symstr);
  145. } else {
  146. str = offstr(sec, addr);
  147. DINFO_FPRINTF(dinfo, bfd_vma_fmt, addr, str);
  148. free(str);
  149. }
  150. }
  151. static bool disas_print_addr_alt(bfd_vma addr, struct disassemble_info *dinfo)
  152. {
  153. struct disas_context *dctx = dinfo->application_data;
  154. struct instruction *orig_first_insn;
  155. struct alt_group *alt_group;
  156. unsigned long offset;
  157. struct symbol *sym;
  158. /*
  159. * Check if we are processing an alternative at the original
  160. * instruction address (i.e. if alt_applied is true) and if
  161. * we are referencing an address inside the alternative.
  162. *
  163. * For example, this happens if there is a branch inside an
  164. * alternative. In that case, the address should be updated
  165. * to a reference inside the original instruction flow.
  166. */
  167. if (!dctx->alt_applied)
  168. return false;
  169. alt_group = dctx->insn->alt_group;
  170. if (!alt_group || !alt_group->orig_group ||
  171. addr < alt_group->first_insn->offset ||
  172. addr > alt_group->last_insn->offset)
  173. return false;
  174. orig_first_insn = alt_group->orig_group->first_insn;
  175. offset = addr - alt_group->first_insn->offset;
  176. addr = orig_first_insn->offset + offset;
  177. sym = orig_first_insn->sym;
  178. disas_print_addr_sym(orig_first_insn->sec, sym, addr, dinfo);
  179. return true;
  180. }
  181. static void disas_print_addr_noreloc(bfd_vma addr,
  182. struct disassemble_info *dinfo)
  183. {
  184. struct disas_context *dctx = dinfo->application_data;
  185. struct instruction *insn = dctx->insn;
  186. struct symbol *sym = NULL;
  187. if (disas_print_addr_alt(addr, dinfo))
  188. return;
  189. if (insn->sym && addr >= insn->sym->offset &&
  190. addr < insn->sym->offset + insn->sym->len) {
  191. sym = insn->sym;
  192. }
  193. disas_print_addr_sym(insn->sec, sym, addr, dinfo);
  194. }
  195. static void disas_print_addr_reloc(bfd_vma addr, struct disassemble_info *dinfo)
  196. {
  197. struct disas_context *dctx = dinfo->application_data;
  198. struct instruction *insn = dctx->insn;
  199. unsigned long offset;
  200. struct reloc *reloc;
  201. char symstr[1024];
  202. char *str;
  203. reloc = find_reloc_by_dest_range(dctx->file->elf, insn->sec,
  204. insn->offset, insn->len);
  205. if (!reloc) {
  206. /*
  207. * There is no relocation for this instruction although
  208. * the address to resolve points to the next instruction.
  209. * So this is an effective reference to the next IP, for
  210. * example: "lea 0x0(%rip),%rdi". The kernel can reference
  211. * the next IP with _THIS_IP_ macro.
  212. */
  213. DINFO_FPRINTF(dinfo, bfd_vma_fmt, addr, "_THIS_IP_");
  214. return;
  215. }
  216. offset = arch_insn_adjusted_addend(insn, reloc);
  217. /*
  218. * If the relocation symbol is a section name (for example ".bss")
  219. * then we try to further resolve the name.
  220. */
  221. if (reloc->sym->type == STT_SECTION) {
  222. str = offstr(reloc->sym->sec, reloc->sym->offset + offset);
  223. DINFO_FPRINTF(dinfo, bfd_vma_fmt, addr, str);
  224. free(str);
  225. } else {
  226. sprint_name(symstr, reloc->sym->name, offset);
  227. DINFO_FPRINTF(dinfo, bfd_vma_fmt, addr, symstr);
  228. }
  229. }
  230. /*
  231. * Resolve an address into a "<symbol>+<offset>" string.
  232. */
  233. static void disas_print_address(bfd_vma addr, struct disassemble_info *dinfo)
  234. {
  235. struct disas_context *dctx = dinfo->application_data;
  236. struct instruction *insn = dctx->insn;
  237. struct instruction *jump_dest;
  238. struct symbol *sym;
  239. bool is_reloc;
  240. /*
  241. * If the instruction is a call/jump and it references a
  242. * destination then this is likely the address we are looking
  243. * up. So check it first.
  244. */
  245. jump_dest = insn->jump_dest;
  246. if (jump_dest && jump_dest->sym && jump_dest->offset == addr) {
  247. if (!disas_print_addr_alt(addr, dinfo))
  248. disas_print_addr_sym(jump_dest->sec, jump_dest->sym,
  249. addr, dinfo);
  250. return;
  251. }
  252. /*
  253. * If the address points to the next instruction then there is
  254. * probably a relocation. It can be a false positive when the
  255. * current instruction is referencing the address of the next
  256. * instruction. This particular case will be handled in
  257. * disas_print_addr_reloc().
  258. */
  259. is_reloc = (addr == insn->offset + insn->len);
  260. /*
  261. * The call destination offset can be the address we are looking
  262. * up, or 0 if there is a relocation.
  263. */
  264. sym = insn_call_dest(insn);
  265. if (sym && (sym->offset == addr || (sym->offset == 0 && is_reloc))) {
  266. DINFO_FPRINTF(dinfo, bfd_vma_fmt, addr, sym->name);
  267. return;
  268. }
  269. if (!is_reloc)
  270. disas_print_addr_noreloc(addr, dinfo);
  271. else
  272. disas_print_addr_reloc(addr, dinfo);
  273. }
  274. /*
  275. * Initialize disassemble info arch, mach (32 or 64-bit) and options.
  276. */
  277. int disas_info_init(struct disassemble_info *dinfo,
  278. int arch, int mach32, int mach64,
  279. const char *options)
  280. {
  281. struct disas_context *dctx = dinfo->application_data;
  282. struct objtool_file *file = dctx->file;
  283. dinfo->arch = arch;
  284. switch (file->elf->ehdr.e_ident[EI_CLASS]) {
  285. case ELFCLASS32:
  286. dinfo->mach = mach32;
  287. break;
  288. case ELFCLASS64:
  289. dinfo->mach = mach64;
  290. break;
  291. default:
  292. return -1;
  293. }
  294. dinfo->disassembler_options = options;
  295. return 0;
  296. }
  297. struct disas_context *disas_context_create(struct objtool_file *file)
  298. {
  299. struct disas_context *dctx;
  300. struct disassemble_info *dinfo;
  301. int err;
  302. dctx = malloc(sizeof(*dctx));
  303. if (!dctx) {
  304. WARN("failed to allocate disassembly context");
  305. return NULL;
  306. }
  307. dctx->file = file;
  308. dinfo = &dctx->info;
  309. init_disassemble_info_compat(dinfo, dctx,
  310. disas_fprintf, disas_fprintf_styled);
  311. dinfo->read_memory_func = buffer_read_memory;
  312. dinfo->print_address_func = disas_print_address;
  313. dinfo->application_data = dctx;
  314. /*
  315. * bfd_openr() is not used to avoid doing ELF data processing
  316. * and caching that has already being done. Here, we just need
  317. * to identify the target file so we call an arch specific
  318. * function to fill some disassemble info (arch, mach).
  319. */
  320. dinfo->arch = bfd_arch_unknown;
  321. dinfo->mach = 0;
  322. err = arch_disas_info_init(dinfo);
  323. if (err || dinfo->arch == bfd_arch_unknown || dinfo->mach == 0) {
  324. WARN("failed to init disassembly arch");
  325. goto error;
  326. }
  327. dinfo->endian = (file->elf->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) ?
  328. BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
  329. disassemble_init_for_target(dinfo);
  330. dctx->disassembler = disassembler(dinfo->arch,
  331. dinfo->endian == BFD_ENDIAN_BIG,
  332. dinfo->mach, NULL);
  333. if (!dctx->disassembler) {
  334. WARN("failed to create disassembler function");
  335. goto error;
  336. }
  337. return dctx;
  338. error:
  339. free(dctx);
  340. return NULL;
  341. }
  342. void disas_context_destroy(struct disas_context *dctx)
  343. {
  344. free(dctx);
  345. }
  346. char *disas_result(struct disas_context *dctx)
  347. {
  348. return dctx->result;
  349. }
  350. #define DISAS_INSN_OFFSET_SPACE 10
  351. #define DISAS_INSN_SPACE 60
  352. #define DISAS_PRINSN(dctx, insn, depth) \
  353. disas_print_insn(stdout, dctx, insn, depth, "\n")
  354. /*
  355. * Print a message in the instruction flow. If sec is not NULL then the
  356. * address at the section offset is printed in addition of the message,
  357. * otherwise only the message is printed.
  358. */
  359. static int disas_vprint(FILE *stream, struct section *sec, unsigned long offset,
  360. int depth, const char *format, va_list ap)
  361. {
  362. const char *addr_str;
  363. int i, n;
  364. int len;
  365. len = sym_name_max_len + DISAS_INSN_OFFSET_SPACE;
  366. if (depth < 0) {
  367. len += depth;
  368. depth = 0;
  369. }
  370. n = 0;
  371. if (sec) {
  372. addr_str = offstr(sec, offset);
  373. n += fprintf(stream, "%6lx: %-*s ", offset, len, addr_str);
  374. free((char *)addr_str);
  375. } else {
  376. len += DISAS_INSN_OFFSET_SPACE + 1;
  377. n += fprintf(stream, "%-*s", len, "");
  378. }
  379. /* print vertical bars to show the code flow */
  380. for (i = 0; i < depth; i++)
  381. n += fprintf(stream, "| ");
  382. if (format)
  383. n += vfprintf(stream, format, ap);
  384. return n;
  385. }
  386. static int disas_print(FILE *stream, struct section *sec, unsigned long offset,
  387. int depth, const char *format, ...)
  388. {
  389. va_list args;
  390. int len;
  391. va_start(args, format);
  392. len = disas_vprint(stream, sec, offset, depth, format, args);
  393. va_end(args);
  394. return len;
  395. }
  396. /*
  397. * Print a message in the instruction flow. If insn is not NULL then
  398. * the instruction address is printed in addition of the message,
  399. * otherwise only the message is printed. In all cases, the instruction
  400. * itself is not printed.
  401. */
  402. void disas_print_info(FILE *stream, struct instruction *insn, int depth,
  403. const char *format, ...)
  404. {
  405. struct section *sec;
  406. unsigned long off;
  407. va_list args;
  408. if (insn) {
  409. sec = insn->sec;
  410. off = insn->offset;
  411. } else {
  412. sec = NULL;
  413. off = 0;
  414. }
  415. va_start(args, format);
  416. disas_vprint(stream, sec, off, depth, format, args);
  417. va_end(args);
  418. }
  419. /*
  420. * Print an instruction address (offset and function), the instruction itself
  421. * and an optional message.
  422. */
  423. void disas_print_insn(FILE *stream, struct disas_context *dctx,
  424. struct instruction *insn, int depth,
  425. const char *format, ...)
  426. {
  427. char fake_nop_insn[32];
  428. const char *insn_str;
  429. bool fake_nop;
  430. va_list args;
  431. int len;
  432. /*
  433. * Alternative can insert a fake nop, sometimes with no
  434. * associated section so nothing to disassemble.
  435. */
  436. fake_nop = (!insn->sec && insn->type == INSN_NOP);
  437. if (fake_nop) {
  438. snprintf(fake_nop_insn, 32, "<fake nop> (%d bytes)", insn->len);
  439. insn_str = fake_nop_insn;
  440. } else {
  441. disas_insn(dctx, insn);
  442. insn_str = disas_result(dctx);
  443. }
  444. /* print the instruction */
  445. len = (depth + 1) * 2 < DISAS_INSN_SPACE ? DISAS_INSN_SPACE - (depth+1) * 2 : 1;
  446. disas_print_info(stream, insn, depth, "%-*s", len, insn_str);
  447. /* print message if any */
  448. if (!format)
  449. return;
  450. if (strcmp(format, "\n") == 0) {
  451. fprintf(stream, "\n");
  452. return;
  453. }
  454. fprintf(stream, " - ");
  455. va_start(args, format);
  456. vfprintf(stream, format, args);
  457. va_end(args);
  458. }
  459. /*
  460. * Disassemble a single instruction. Return the size of the instruction.
  461. *
  462. * If alt_applied is true then insn should be an instruction from of an
  463. * alternative (i.e. insn->alt_group != NULL), and it is disassembled
  464. * at the location of the original code it is replacing. When the
  465. * instruction references any address inside the alternative then
  466. * these references will be re-adjusted to replace the original code.
  467. */
  468. static size_t disas_insn_common(struct disas_context *dctx,
  469. struct instruction *insn,
  470. bool alt_applied)
  471. {
  472. disassembler_ftype disasm = dctx->disassembler;
  473. struct disassemble_info *dinfo = &dctx->info;
  474. dctx->insn = insn;
  475. dctx->alt_applied = alt_applied;
  476. dctx->result[0] = '\0';
  477. if (insn->type == INSN_NOP) {
  478. DINFO_FPRINTF(dinfo, "nop%d", insn->len);
  479. return insn->len;
  480. }
  481. /*
  482. * Set the disassembler buffer to read data from the section
  483. * containing the instruction to disassemble.
  484. */
  485. dinfo->buffer = insn->sec->data->d_buf;
  486. dinfo->buffer_vma = 0;
  487. dinfo->buffer_length = insn->sec->sh.sh_size;
  488. return disasm(insn->offset, &dctx->info);
  489. }
  490. size_t disas_insn(struct disas_context *dctx, struct instruction *insn)
  491. {
  492. return disas_insn_common(dctx, insn, false);
  493. }
  494. static size_t disas_insn_alt(struct disas_context *dctx,
  495. struct instruction *insn)
  496. {
  497. return disas_insn_common(dctx, insn, true);
  498. }
  499. static struct instruction *next_insn_same_alt(struct objtool_file *file,
  500. struct alt_group *alt_grp,
  501. struct instruction *insn)
  502. {
  503. if (alt_grp->last_insn == insn || alt_grp->nop == insn)
  504. return NULL;
  505. return next_insn_same_sec(file, insn);
  506. }
  507. #define alt_for_each_insn(file, alt_grp, insn) \
  508. for (insn = alt_grp->first_insn; \
  509. insn; \
  510. insn = next_insn_same_alt(file, alt_grp, insn))
  511. /*
  512. * Provide a name for the type of alternatives present at the
  513. * specified instruction.
  514. *
  515. * An instruction can have alternatives with different types, for
  516. * example alternative instructions and an exception table. In that
  517. * case the name for the alternative instructions type is used.
  518. *
  519. * Return NULL if the instruction as no alternative.
  520. */
  521. const char *disas_alt_type_name(struct instruction *insn)
  522. {
  523. struct alternative *alt;
  524. const char *name;
  525. name = NULL;
  526. for (alt = insn->alts; alt; alt = alt->next) {
  527. if (alt->type == ALT_TYPE_INSTRUCTIONS) {
  528. name = "alternative";
  529. break;
  530. }
  531. switch (alt->type) {
  532. case ALT_TYPE_EX_TABLE:
  533. name = "ex_table";
  534. break;
  535. case ALT_TYPE_JUMP_TABLE:
  536. name = "jump_table";
  537. break;
  538. default:
  539. name = "unknown";
  540. break;
  541. }
  542. }
  543. return name;
  544. }
  545. /*
  546. * Provide a name for an alternative.
  547. */
  548. char *disas_alt_name(struct alternative *alt)
  549. {
  550. char pfx[4] = { 0 };
  551. char *str = NULL;
  552. const char *name;
  553. int feature;
  554. int flags;
  555. int num;
  556. switch (alt->type) {
  557. case ALT_TYPE_EX_TABLE:
  558. str = strdup("EXCEPTION");
  559. break;
  560. case ALT_TYPE_JUMP_TABLE:
  561. str = strdup("JUMP");
  562. break;
  563. case ALT_TYPE_INSTRUCTIONS:
  564. /*
  565. * This is a non-default group alternative. Create a name
  566. * based on the feature and flags associated with this
  567. * alternative. Use either the feature name (it is available)
  568. * or the feature number. And add a prefix to show the flags
  569. * used.
  570. *
  571. * Prefix flags characters:
  572. *
  573. * '!' alternative used when feature not enabled
  574. * '+' direct call alternative
  575. * '?' unknown flag
  576. */
  577. if (!alt->insn->alt_group)
  578. return NULL;
  579. feature = alt->insn->alt_group->feature;
  580. num = alt_feature(feature);
  581. flags = alt_flags(feature);
  582. str = pfx;
  583. if (flags & ~(ALT_FLAG_NOT | ALT_FLAG_DIRECT_CALL))
  584. *str++ = '?';
  585. if (flags & ALT_FLAG_DIRECT_CALL)
  586. *str++ = '+';
  587. if (flags & ALT_FLAG_NOT)
  588. *str++ = '!';
  589. name = arch_cpu_feature_name(num);
  590. if (!name)
  591. str = strfmt("%sFEATURE 0x%X", pfx, num);
  592. else
  593. str = strfmt("%s%s", pfx, name);
  594. break;
  595. }
  596. return str;
  597. }
  598. /*
  599. * Initialize an alternative. The default alternative should be initialized
  600. * with alt=NULL.
  601. */
  602. static int disas_alt_init(struct disas_alt *dalt,
  603. struct instruction *orig_insn,
  604. struct alternative *alt)
  605. {
  606. dalt->orig_insn = orig_insn;
  607. dalt->alt = alt;
  608. dalt->insn_idx = 0;
  609. dalt->name = alt ? disas_alt_name(alt) : strdup("DEFAULT");
  610. if (!dalt->name)
  611. return -1;
  612. dalt->width = strlen(dalt->name);
  613. return 0;
  614. }
  615. static int disas_alt_add_insn(struct disas_alt *dalt, int index, char *insn_str,
  616. int offset, int nops)
  617. {
  618. int len;
  619. if (index >= DISAS_ALT_INSN_MAX) {
  620. WARN("Alternative %lx.%s has more instructions than supported",
  621. DALT_ALTID(dalt), dalt->name);
  622. return -1;
  623. }
  624. len = strlen(insn_str);
  625. dalt->insn[index].str = insn_str;
  626. dalt->insn[index].offset = offset;
  627. dalt->insn[index].nops = nops;
  628. if (len > dalt->width)
  629. dalt->width = len;
  630. return 0;
  631. }
  632. static int disas_alt_jump(struct disas_alt *dalt)
  633. {
  634. struct instruction *orig_insn;
  635. struct instruction *dest_insn;
  636. char suffix[2] = { 0 };
  637. char *str;
  638. int nops;
  639. orig_insn = dalt->orig_insn;
  640. dest_insn = dalt->alt->insn;
  641. if (orig_insn->type == INSN_NOP) {
  642. if (orig_insn->len == 5)
  643. suffix[0] = 'q';
  644. str = strfmt("jmp%-3s %lx <%s+0x%lx>", suffix,
  645. dest_insn->offset, dest_insn->sym->name,
  646. dest_insn->offset - dest_insn->sym->offset);
  647. nops = 0;
  648. } else {
  649. str = strfmt("nop%d", orig_insn->len);
  650. nops = orig_insn->len;
  651. }
  652. if (!str)
  653. return -1;
  654. disas_alt_add_insn(dalt, 0, str, 0, nops);
  655. return 1;
  656. }
  657. /*
  658. * Disassemble an exception table alternative.
  659. */
  660. static int disas_alt_extable(struct disas_alt *dalt)
  661. {
  662. struct instruction *alt_insn;
  663. char *str;
  664. alt_insn = dalt->alt->insn;
  665. str = strfmt("resume at 0x%lx <%s+0x%lx>",
  666. alt_insn->offset, alt_insn->sym->name,
  667. alt_insn->offset - alt_insn->sym->offset);
  668. if (!str)
  669. return -1;
  670. disas_alt_add_insn(dalt, 0, str, 0, 0);
  671. return 1;
  672. }
  673. /*
  674. * Disassemble an alternative and store instructions in the disas_alt
  675. * structure. Return the number of instructions in the alternative.
  676. */
  677. static int disas_alt_group(struct disas_context *dctx, struct disas_alt *dalt)
  678. {
  679. struct objtool_file *file;
  680. struct instruction *insn;
  681. int offset;
  682. char *str;
  683. int count;
  684. int nops;
  685. int err;
  686. file = dctx->file;
  687. count = 0;
  688. offset = 0;
  689. nops = 0;
  690. alt_for_each_insn(file, DALT_GROUP(dalt), insn) {
  691. disas_insn_alt(dctx, insn);
  692. str = strdup(disas_result(dctx));
  693. if (!str)
  694. return -1;
  695. nops = insn->type == INSN_NOP ? insn->len : 0;
  696. err = disas_alt_add_insn(dalt, count, str, offset, nops);
  697. if (err)
  698. break;
  699. offset += insn->len;
  700. count++;
  701. }
  702. return count;
  703. }
  704. /*
  705. * Disassemble the default alternative.
  706. */
  707. static int disas_alt_default(struct disas_context *dctx, struct disas_alt *dalt)
  708. {
  709. char *str;
  710. int nops;
  711. int err;
  712. if (DALT_GROUP(dalt))
  713. return disas_alt_group(dctx, dalt);
  714. /*
  715. * Default alternative with no alt_group: this is the default
  716. * code associated with either a jump table or an exception
  717. * table and no other instruction alternatives. In that case
  718. * the default alternative is made of a single instruction.
  719. */
  720. disas_insn(dctx, dalt->orig_insn);
  721. str = strdup(disas_result(dctx));
  722. if (!str)
  723. return -1;
  724. nops = dalt->orig_insn->type == INSN_NOP ? dalt->orig_insn->len : 0;
  725. err = disas_alt_add_insn(dalt, 0, str, 0, nops);
  726. if (err)
  727. return -1;
  728. return 1;
  729. }
  730. /*
  731. * For each alternative, if there is an instruction at the specified
  732. * offset then print this instruction, otherwise print a blank entry.
  733. * The offset is an offset from the start of the alternative.
  734. *
  735. * Return the offset for the next instructions to print, or -1 if all
  736. * instructions have been printed.
  737. */
  738. static int disas_alt_print_insn(struct disas_alt *dalts, int alt_count,
  739. int insn_count, int offset)
  740. {
  741. struct disas_alt *dalt;
  742. int offset_next;
  743. char *str;
  744. int i, j;
  745. offset_next = -1;
  746. for (i = 0; i < alt_count; i++) {
  747. dalt = &dalts[i];
  748. j = dalt->insn_idx;
  749. if (j == -1) {
  750. printf("| %-*s ", dalt->width, "");
  751. continue;
  752. }
  753. if (dalt->insn[j].offset == offset) {
  754. str = dalt->insn[j].str;
  755. printf("| %-*s ", dalt->width, str ?: "");
  756. if (++j < insn_count) {
  757. dalt->insn_idx = j;
  758. } else {
  759. dalt->insn_idx = -1;
  760. continue;
  761. }
  762. } else {
  763. printf("| %-*s ", dalt->width, "");
  764. }
  765. if (dalt->insn[j].offset > 0 &&
  766. (offset_next == -1 ||
  767. (dalt->insn[j].offset < offset_next)))
  768. offset_next = dalt->insn[j].offset;
  769. }
  770. printf("\n");
  771. return offset_next;
  772. }
  773. /*
  774. * Print all alternatives side-by-side.
  775. */
  776. static void disas_alt_print_wide(char *alt_name, struct disas_alt *dalts, int alt_count,
  777. int insn_count)
  778. {
  779. struct instruction *orig_insn;
  780. int offset_next;
  781. int offset;
  782. int i;
  783. orig_insn = dalts[0].orig_insn;
  784. /*
  785. * Print an header with the name of each alternative.
  786. */
  787. disas_print_info(stdout, orig_insn, -2, NULL);
  788. if (strlen(alt_name) > dalts[0].width)
  789. dalts[0].width = strlen(alt_name);
  790. printf("| %-*s ", dalts[0].width, alt_name);
  791. for (i = 1; i < alt_count; i++)
  792. printf("| %-*s ", dalts[i].width, dalts[i].name);
  793. printf("\n");
  794. /*
  795. * Print instructions for each alternative.
  796. */
  797. offset_next = 0;
  798. do {
  799. offset = offset_next;
  800. disas_print(stdout, orig_insn->sec, orig_insn->offset + offset,
  801. -2, NULL);
  802. offset_next = disas_alt_print_insn(dalts, alt_count, insn_count,
  803. offset);
  804. } while (offset_next > offset);
  805. }
  806. /*
  807. * Print all alternatives one above the other.
  808. */
  809. static void disas_alt_print_compact(char *alt_name, struct disas_alt *dalts,
  810. int alt_count, int insn_count)
  811. {
  812. struct instruction *orig_insn;
  813. int width;
  814. int i, j;
  815. int len;
  816. orig_insn = dalts[0].orig_insn;
  817. len = disas_print(stdout, orig_insn->sec, orig_insn->offset, 0, NULL);
  818. printf("%s\n", alt_name);
  819. /*
  820. * If all alternatives have a single instruction then print each
  821. * alternative on a single line. Otherwise, print alternatives
  822. * one above the other with a clear separation.
  823. */
  824. if (insn_count == 1) {
  825. width = 0;
  826. for (i = 0; i < alt_count; i++) {
  827. if (dalts[i].width > width)
  828. width = dalts[i].width;
  829. }
  830. for (i = 0; i < alt_count; i++) {
  831. printf("%*s= %-*s (if %s)\n", len, "", width,
  832. dalts[i].insn[0].str, dalts[i].name);
  833. }
  834. return;
  835. }
  836. for (i = 0; i < alt_count; i++) {
  837. printf("%*s= %s\n", len, "", dalts[i].name);
  838. for (j = 0; j < insn_count; j++) {
  839. if (!dalts[i].insn[j].str)
  840. break;
  841. disas_print(stdout, orig_insn->sec,
  842. orig_insn->offset + dalts[i].insn[j].offset, 0,
  843. "| %s\n", dalts[i].insn[j].str);
  844. }
  845. printf("%*s|\n", len, "");
  846. }
  847. }
  848. /*
  849. * Trim NOPs in alternatives. This replaces trailing NOPs in alternatives
  850. * with a single indication of the number of bytes covered with NOPs.
  851. *
  852. * Return the maximum numbers of instructions in all alternatives after
  853. * trailing NOPs have been trimmed.
  854. */
  855. static int disas_alt_trim_nops(struct disas_alt *dalts, int alt_count,
  856. int insn_count)
  857. {
  858. struct disas_alt *dalt;
  859. int nops_count;
  860. const char *s;
  861. int offset;
  862. int count;
  863. int nops;
  864. int i, j;
  865. count = 0;
  866. for (i = 0; i < alt_count; i++) {
  867. offset = 0;
  868. nops = 0;
  869. nops_count = 0;
  870. dalt = &dalts[i];
  871. for (j = insn_count - 1; j >= 0; j--) {
  872. if (!dalt->insn[j].str || !dalt->insn[j].nops)
  873. break;
  874. offset = dalt->insn[j].offset;
  875. free(dalt->insn[j].str);
  876. dalt->insn[j].offset = 0;
  877. dalt->insn[j].str = NULL;
  878. nops += dalt->insn[j].nops;
  879. nops_count++;
  880. }
  881. /*
  882. * All trailing NOPs have been removed. If there was a single
  883. * NOP instruction then re-add it. If there was a block of
  884. * NOPs then indicate the number of bytes than the block
  885. * covers (nop*<number-of-bytes>).
  886. */
  887. if (nops_count) {
  888. s = nops_count == 1 ? "" : "*";
  889. dalt->insn[j + 1].str = strfmt("nop%s%d", s, nops);
  890. dalt->insn[j + 1].offset = offset;
  891. dalt->insn[j + 1].nops = nops;
  892. j++;
  893. }
  894. if (j > count)
  895. count = j;
  896. }
  897. return count + 1;
  898. }
  899. /*
  900. * Disassemble an alternative.
  901. *
  902. * Return the last instruction in the default alternative so that
  903. * disassembly can continue with the next instruction. Return NULL
  904. * on error.
  905. */
  906. static void *disas_alt(struct disas_context *dctx,
  907. struct instruction *orig_insn)
  908. {
  909. struct disas_alt dalts[DISAS_ALT_MAX] = { 0 };
  910. struct instruction *last_insn = NULL;
  911. struct alternative *alt;
  912. struct disas_alt *dalt;
  913. int insn_count = 0;
  914. int alt_count = 0;
  915. char *alt_name;
  916. int count;
  917. int i, j;
  918. int err;
  919. alt_name = strfmt("<%s.%lx>", disas_alt_type_name(orig_insn),
  920. orig_insn->offset);
  921. if (!alt_name) {
  922. WARN("Failed to define name for alternative at instruction 0x%lx",
  923. orig_insn->offset);
  924. goto done;
  925. }
  926. /*
  927. * Initialize and disassemble the default alternative.
  928. */
  929. err = disas_alt_init(&dalts[0], orig_insn, NULL);
  930. if (err) {
  931. WARN("%s: failed to initialize default alternative", alt_name);
  932. goto done;
  933. }
  934. insn_count = disas_alt_default(dctx, &dalts[0]);
  935. if (insn_count < 0) {
  936. WARN("%s: failed to disassemble default alternative", alt_name);
  937. goto done;
  938. }
  939. /*
  940. * Initialize and disassemble all other alternatives.
  941. */
  942. i = 1;
  943. for (alt = orig_insn->alts; alt; alt = alt->next) {
  944. if (i >= DISAS_ALT_MAX) {
  945. WARN("%s has more alternatives than supported", alt_name);
  946. break;
  947. }
  948. dalt = &dalts[i];
  949. err = disas_alt_init(dalt, orig_insn, alt);
  950. if (err) {
  951. WARN("%s: failed to disassemble alternative", alt_name);
  952. goto done;
  953. }
  954. count = -1;
  955. switch (dalt->alt->type) {
  956. case ALT_TYPE_INSTRUCTIONS:
  957. count = disas_alt_group(dctx, dalt);
  958. break;
  959. case ALT_TYPE_EX_TABLE:
  960. count = disas_alt_extable(dalt);
  961. break;
  962. case ALT_TYPE_JUMP_TABLE:
  963. count = disas_alt_jump(dalt);
  964. break;
  965. }
  966. if (count < 0) {
  967. WARN("%s: failed to disassemble alternative %s",
  968. alt_name, dalt->name);
  969. goto done;
  970. }
  971. insn_count = count > insn_count ? count : insn_count;
  972. i++;
  973. }
  974. alt_count = i;
  975. /*
  976. * Print default and non-default alternatives.
  977. */
  978. insn_count = disas_alt_trim_nops(dalts, alt_count, insn_count);
  979. if (opts.wide)
  980. disas_alt_print_wide(alt_name, dalts, alt_count, insn_count);
  981. else
  982. disas_alt_print_compact(alt_name, dalts, alt_count, insn_count);
  983. last_insn = orig_insn->alt_group ? orig_insn->alt_group->last_insn :
  984. orig_insn;
  985. done:
  986. for (i = 0; i < alt_count; i++) {
  987. free(dalts[i].name);
  988. for (j = 0; j < insn_count; j++)
  989. free(dalts[i].insn[j].str);
  990. }
  991. free(alt_name);
  992. return last_insn;
  993. }
  994. /*
  995. * Disassemble a function.
  996. */
  997. static void disas_func(struct disas_context *dctx, struct symbol *func)
  998. {
  999. struct instruction *insn_start;
  1000. struct instruction *insn;
  1001. printf("%s:\n", func->name);
  1002. sym_for_each_insn(dctx->file, func, insn) {
  1003. if (insn->alts) {
  1004. insn_start = insn;
  1005. insn = disas_alt(dctx, insn);
  1006. if (insn)
  1007. continue;
  1008. /*
  1009. * There was an error with disassembling
  1010. * the alternative. Resume disassembling
  1011. * at the current instruction, this will
  1012. * disassemble the default alternative
  1013. * only and continue with the code after
  1014. * the alternative.
  1015. */
  1016. insn = insn_start;
  1017. }
  1018. DISAS_PRINSN(dctx, insn, 0);
  1019. }
  1020. printf("\n");
  1021. }
  1022. /*
  1023. * Disassemble all warned functions.
  1024. */
  1025. void disas_warned_funcs(struct disas_context *dctx)
  1026. {
  1027. struct symbol *sym;
  1028. if (!dctx)
  1029. return;
  1030. for_each_sym(dctx->file->elf, sym) {
  1031. if (sym->warned)
  1032. disas_func(dctx, sym);
  1033. }
  1034. }
  1035. void disas_funcs(struct disas_context *dctx)
  1036. {
  1037. bool disas_all = !strcmp(opts.disas, "*");
  1038. struct section *sec;
  1039. struct symbol *sym;
  1040. for_each_sec(dctx->file->elf, sec) {
  1041. if (!(sec->sh.sh_flags & SHF_EXECINSTR))
  1042. continue;
  1043. sec_for_each_sym(sec, sym) {
  1044. /*
  1045. * If the function had a warning and the verbose
  1046. * option is used then the function was already
  1047. * disassemble.
  1048. */
  1049. if (opts.verbose && sym->warned)
  1050. continue;
  1051. if (disas_all || fnmatch(opts.disas, sym->name, 0) == 0)
  1052. disas_func(dctx, sym);
  1053. }
  1054. }
  1055. }