builtin-check.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * objtool check:
  19. *
  20. * This command analyzes every .o file and ensures the validity of its stack
  21. * trace metadata. It enforces a set of rules on asm code and C inline
  22. * assembly code so that stack traces can be reliable.
  23. *
  24. * For more information, see tools/objtool/Documentation/stack-validation.txt.
  25. */
  26. #include <string.h>
  27. #include <subcmd/parse-options.h>
  28. #include "builtin.h"
  29. #include "elf.h"
  30. #include "special.h"
  31. #include "arch.h"
  32. #include "warn.h"
  33. #include <linux/hashtable.h>
  34. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  35. #define STATE_FP_SAVED 0x1
  36. #define STATE_FP_SETUP 0x2
  37. #define STATE_FENTRY 0x4
  38. struct instruction {
  39. struct list_head list;
  40. struct hlist_node hash;
  41. struct section *sec;
  42. unsigned long offset;
  43. unsigned int len, state;
  44. unsigned char type;
  45. unsigned long immediate;
  46. bool alt_group, visited;
  47. struct symbol *call_dest;
  48. struct instruction *jump_dest;
  49. struct list_head alts;
  50. };
  51. struct alternative {
  52. struct list_head list;
  53. struct instruction *insn;
  54. };
  55. struct objtool_file {
  56. struct elf *elf;
  57. struct list_head insn_list;
  58. DECLARE_HASHTABLE(insn_hash, 16);
  59. struct section *rodata, *whitelist;
  60. };
  61. const char *objname;
  62. static bool nofp;
  63. static struct instruction *find_insn(struct objtool_file *file,
  64. struct section *sec, unsigned long offset)
  65. {
  66. struct instruction *insn;
  67. hash_for_each_possible(file->insn_hash, insn, hash, offset)
  68. if (insn->sec == sec && insn->offset == offset)
  69. return insn;
  70. return NULL;
  71. }
  72. static struct instruction *next_insn_same_sec(struct objtool_file *file,
  73. struct instruction *insn)
  74. {
  75. struct instruction *next = list_next_entry(insn, list);
  76. if (&next->list == &file->insn_list || next->sec != insn->sec)
  77. return NULL;
  78. return next;
  79. }
  80. #define for_each_insn(file, insn) \
  81. list_for_each_entry(insn, &file->insn_list, list)
  82. #define func_for_each_insn(file, func, insn) \
  83. for (insn = find_insn(file, func->sec, func->offset); \
  84. insn && &insn->list != &file->insn_list && \
  85. insn->sec == func->sec && \
  86. insn->offset < func->offset + func->len; \
  87. insn = list_next_entry(insn, list))
  88. #define sec_for_each_insn_from(file, insn) \
  89. for (; insn; insn = next_insn_same_sec(file, insn))
  90. /*
  91. * Check if the function has been manually whitelisted with the
  92. * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
  93. * due to its use of a context switching instruction.
  94. */
  95. static bool ignore_func(struct objtool_file *file, struct symbol *func)
  96. {
  97. struct rela *rela;
  98. struct instruction *insn;
  99. /* check for STACK_FRAME_NON_STANDARD */
  100. if (file->whitelist && file->whitelist->rela)
  101. list_for_each_entry(rela, &file->whitelist->rela->rela_list, list)
  102. if (rela->sym->sec == func->sec &&
  103. rela->addend == func->offset)
  104. return true;
  105. /* check if it has a context switching instruction */
  106. func_for_each_insn(file, func, insn)
  107. if (insn->type == INSN_CONTEXT_SWITCH)
  108. return true;
  109. return false;
  110. }
  111. /*
  112. * This checks to see if the given function is a "noreturn" function.
  113. *
  114. * For global functions which are outside the scope of this object file, we
  115. * have to keep a manual list of them.
  116. *
  117. * For local functions, we have to detect them manually by simply looking for
  118. * the lack of a return instruction.
  119. *
  120. * Returns:
  121. * -1: error
  122. * 0: no dead end
  123. * 1: dead end
  124. */
  125. static int __dead_end_function(struct objtool_file *file, struct symbol *func,
  126. int recursion)
  127. {
  128. int i;
  129. struct instruction *insn;
  130. bool empty = true;
  131. /*
  132. * Unfortunately these have to be hard coded because the noreturn
  133. * attribute isn't provided in ELF data.
  134. */
  135. static const char * const global_noreturns[] = {
  136. "__stack_chk_fail",
  137. "panic",
  138. "do_exit",
  139. "__module_put_and_exit",
  140. "complete_and_exit",
  141. "kvm_spurious_fault",
  142. "__reiserfs_panic",
  143. "lbug_with_loc"
  144. };
  145. if (func->bind == STB_WEAK)
  146. return 0;
  147. if (func->bind == STB_GLOBAL)
  148. for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
  149. if (!strcmp(func->name, global_noreturns[i]))
  150. return 1;
  151. if (!func->sec)
  152. return 0;
  153. func_for_each_insn(file, func, insn) {
  154. empty = false;
  155. if (insn->type == INSN_RETURN)
  156. return 0;
  157. }
  158. if (empty)
  159. return 0;
  160. /*
  161. * A function can have a sibling call instead of a return. In that
  162. * case, the function's dead-end status depends on whether the target
  163. * of the sibling call returns.
  164. */
  165. func_for_each_insn(file, func, insn) {
  166. if (insn->sec != func->sec ||
  167. insn->offset >= func->offset + func->len)
  168. break;
  169. if (insn->type == INSN_JUMP_UNCONDITIONAL) {
  170. struct instruction *dest = insn->jump_dest;
  171. struct symbol *dest_func;
  172. if (!dest)
  173. /* sibling call to another file */
  174. return 0;
  175. if (dest->sec != func->sec ||
  176. dest->offset < func->offset ||
  177. dest->offset >= func->offset + func->len) {
  178. /* local sibling call */
  179. dest_func = find_symbol_by_offset(dest->sec,
  180. dest->offset);
  181. if (!dest_func)
  182. continue;
  183. if (recursion == 5) {
  184. WARN_FUNC("infinite recursion (objtool bug!)",
  185. dest->sec, dest->offset);
  186. return -1;
  187. }
  188. return __dead_end_function(file, dest_func,
  189. recursion + 1);
  190. }
  191. }
  192. if (insn->type == INSN_JUMP_DYNAMIC)
  193. /* sibling call */
  194. return 0;
  195. }
  196. return 1;
  197. }
  198. static int dead_end_function(struct objtool_file *file, struct symbol *func)
  199. {
  200. return __dead_end_function(file, func, 0);
  201. }
  202. /*
  203. * Call the arch-specific instruction decoder for all the instructions and add
  204. * them to the global instruction list.
  205. */
  206. static int decode_instructions(struct objtool_file *file)
  207. {
  208. struct section *sec;
  209. unsigned long offset;
  210. struct instruction *insn;
  211. int ret;
  212. list_for_each_entry(sec, &file->elf->sections, list) {
  213. if (!(sec->sh.sh_flags & SHF_EXECINSTR))
  214. continue;
  215. for (offset = 0; offset < sec->len; offset += insn->len) {
  216. insn = malloc(sizeof(*insn));
  217. memset(insn, 0, sizeof(*insn));
  218. INIT_LIST_HEAD(&insn->alts);
  219. insn->sec = sec;
  220. insn->offset = offset;
  221. ret = arch_decode_instruction(file->elf, sec, offset,
  222. sec->len - offset,
  223. &insn->len, &insn->type,
  224. &insn->immediate);
  225. if (ret)
  226. return ret;
  227. if (!insn->type || insn->type > INSN_LAST) {
  228. WARN_FUNC("invalid instruction type %d",
  229. insn->sec, insn->offset, insn->type);
  230. return -1;
  231. }
  232. hash_add(file->insn_hash, &insn->hash, insn->offset);
  233. list_add_tail(&insn->list, &file->insn_list);
  234. }
  235. }
  236. return 0;
  237. }
  238. /*
  239. * Warnings shouldn't be reported for ignored functions.
  240. */
  241. static void add_ignores(struct objtool_file *file)
  242. {
  243. struct instruction *insn;
  244. struct section *sec;
  245. struct symbol *func;
  246. list_for_each_entry(sec, &file->elf->sections, list) {
  247. list_for_each_entry(func, &sec->symbol_list, list) {
  248. if (func->type != STT_FUNC)
  249. continue;
  250. if (!ignore_func(file, func))
  251. continue;
  252. func_for_each_insn(file, func, insn)
  253. insn->visited = true;
  254. }
  255. }
  256. }
  257. /*
  258. * Find the destination instructions for all jumps.
  259. */
  260. static int add_jump_destinations(struct objtool_file *file)
  261. {
  262. struct instruction *insn;
  263. struct rela *rela;
  264. struct section *dest_sec;
  265. unsigned long dest_off;
  266. for_each_insn(file, insn) {
  267. if (insn->type != INSN_JUMP_CONDITIONAL &&
  268. insn->type != INSN_JUMP_UNCONDITIONAL)
  269. continue;
  270. /* skip ignores */
  271. if (insn->visited)
  272. continue;
  273. rela = find_rela_by_dest_range(insn->sec, insn->offset,
  274. insn->len);
  275. if (!rela) {
  276. dest_sec = insn->sec;
  277. dest_off = insn->offset + insn->len + insn->immediate;
  278. } else if (rela->sym->type == STT_SECTION) {
  279. dest_sec = rela->sym->sec;
  280. dest_off = rela->addend + 4;
  281. } else if (rela->sym->sec->idx) {
  282. dest_sec = rela->sym->sec;
  283. dest_off = rela->sym->sym.st_value + rela->addend + 4;
  284. } else {
  285. /* sibling call */
  286. insn->jump_dest = 0;
  287. continue;
  288. }
  289. insn->jump_dest = find_insn(file, dest_sec, dest_off);
  290. if (!insn->jump_dest) {
  291. /*
  292. * This is a special case where an alt instruction
  293. * jumps past the end of the section. These are
  294. * handled later in handle_group_alt().
  295. */
  296. if (!strcmp(insn->sec->name, ".altinstr_replacement"))
  297. continue;
  298. WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
  299. insn->sec, insn->offset, dest_sec->name,
  300. dest_off);
  301. return -1;
  302. }
  303. }
  304. return 0;
  305. }
  306. /*
  307. * Find the destination instructions for all calls.
  308. */
  309. static int add_call_destinations(struct objtool_file *file)
  310. {
  311. struct instruction *insn;
  312. unsigned long dest_off;
  313. struct rela *rela;
  314. for_each_insn(file, insn) {
  315. if (insn->type != INSN_CALL)
  316. continue;
  317. rela = find_rela_by_dest_range(insn->sec, insn->offset,
  318. insn->len);
  319. if (!rela) {
  320. dest_off = insn->offset + insn->len + insn->immediate;
  321. insn->call_dest = find_symbol_by_offset(insn->sec,
  322. dest_off);
  323. if (!insn->call_dest) {
  324. WARN_FUNC("can't find call dest symbol at offset 0x%lx",
  325. insn->sec, insn->offset, dest_off);
  326. return -1;
  327. }
  328. } else if (rela->sym->type == STT_SECTION) {
  329. insn->call_dest = find_symbol_by_offset(rela->sym->sec,
  330. rela->addend+4);
  331. if (!insn->call_dest ||
  332. insn->call_dest->type != STT_FUNC) {
  333. WARN_FUNC("can't find call dest symbol at %s+0x%x",
  334. insn->sec, insn->offset,
  335. rela->sym->sec->name,
  336. rela->addend + 4);
  337. return -1;
  338. }
  339. } else
  340. insn->call_dest = rela->sym;
  341. }
  342. return 0;
  343. }
  344. /*
  345. * The .alternatives section requires some extra special care, over and above
  346. * what other special sections require:
  347. *
  348. * 1. Because alternatives are patched in-place, we need to insert a fake jump
  349. * instruction at the end so that validate_branch() skips all the original
  350. * replaced instructions when validating the new instruction path.
  351. *
  352. * 2. An added wrinkle is that the new instruction length might be zero. In
  353. * that case the old instructions are replaced with noops. We simulate that
  354. * by creating a fake jump as the only new instruction.
  355. *
  356. * 3. In some cases, the alternative section includes an instruction which
  357. * conditionally jumps to the _end_ of the entry. We have to modify these
  358. * jumps' destinations to point back to .text rather than the end of the
  359. * entry in .altinstr_replacement.
  360. *
  361. * 4. It has been requested that we don't validate the !POPCNT feature path
  362. * which is a "very very small percentage of machines".
  363. */
  364. static int handle_group_alt(struct objtool_file *file,
  365. struct special_alt *special_alt,
  366. struct instruction *orig_insn,
  367. struct instruction **new_insn)
  368. {
  369. struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
  370. unsigned long dest_off;
  371. last_orig_insn = NULL;
  372. insn = orig_insn;
  373. sec_for_each_insn_from(file, insn) {
  374. if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
  375. break;
  376. if (special_alt->skip_orig)
  377. insn->type = INSN_NOP;
  378. insn->alt_group = true;
  379. last_orig_insn = insn;
  380. }
  381. if (!next_insn_same_sec(file, last_orig_insn)) {
  382. WARN("%s: don't know how to handle alternatives at end of section",
  383. special_alt->orig_sec->name);
  384. return -1;
  385. }
  386. fake_jump = malloc(sizeof(*fake_jump));
  387. if (!fake_jump) {
  388. WARN("malloc failed");
  389. return -1;
  390. }
  391. memset(fake_jump, 0, sizeof(*fake_jump));
  392. INIT_LIST_HEAD(&fake_jump->alts);
  393. fake_jump->sec = special_alt->new_sec;
  394. fake_jump->offset = -1;
  395. fake_jump->type = INSN_JUMP_UNCONDITIONAL;
  396. fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
  397. if (!special_alt->new_len) {
  398. *new_insn = fake_jump;
  399. return 0;
  400. }
  401. last_new_insn = NULL;
  402. insn = *new_insn;
  403. sec_for_each_insn_from(file, insn) {
  404. if (insn->offset >= special_alt->new_off + special_alt->new_len)
  405. break;
  406. last_new_insn = insn;
  407. if (insn->type != INSN_JUMP_CONDITIONAL &&
  408. insn->type != INSN_JUMP_UNCONDITIONAL)
  409. continue;
  410. if (!insn->immediate)
  411. continue;
  412. dest_off = insn->offset + insn->len + insn->immediate;
  413. if (dest_off == special_alt->new_off + special_alt->new_len)
  414. insn->jump_dest = fake_jump;
  415. if (!insn->jump_dest) {
  416. WARN_FUNC("can't find alternative jump destination",
  417. insn->sec, insn->offset);
  418. return -1;
  419. }
  420. }
  421. if (!last_new_insn) {
  422. WARN_FUNC("can't find last new alternative instruction",
  423. special_alt->new_sec, special_alt->new_off);
  424. return -1;
  425. }
  426. list_add(&fake_jump->list, &last_new_insn->list);
  427. return 0;
  428. }
  429. /*
  430. * A jump table entry can either convert a nop to a jump or a jump to a nop.
  431. * If the original instruction is a jump, make the alt entry an effective nop
  432. * by just skipping the original instruction.
  433. */
  434. static int handle_jump_alt(struct objtool_file *file,
  435. struct special_alt *special_alt,
  436. struct instruction *orig_insn,
  437. struct instruction **new_insn)
  438. {
  439. if (orig_insn->type == INSN_NOP)
  440. return 0;
  441. if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
  442. WARN_FUNC("unsupported instruction at jump label",
  443. orig_insn->sec, orig_insn->offset);
  444. return -1;
  445. }
  446. *new_insn = list_next_entry(orig_insn, list);
  447. return 0;
  448. }
  449. /*
  450. * Read all the special sections which have alternate instructions which can be
  451. * patched in or redirected to at runtime. Each instruction having alternate
  452. * instruction(s) has them added to its insn->alts list, which will be
  453. * traversed in validate_branch().
  454. */
  455. static int add_special_section_alts(struct objtool_file *file)
  456. {
  457. struct list_head special_alts;
  458. struct instruction *orig_insn, *new_insn;
  459. struct special_alt *special_alt, *tmp;
  460. struct alternative *alt;
  461. int ret;
  462. ret = special_get_alts(file->elf, &special_alts);
  463. if (ret)
  464. return ret;
  465. list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
  466. alt = malloc(sizeof(*alt));
  467. if (!alt) {
  468. WARN("malloc failed");
  469. ret = -1;
  470. goto out;
  471. }
  472. orig_insn = find_insn(file, special_alt->orig_sec,
  473. special_alt->orig_off);
  474. if (!orig_insn) {
  475. WARN_FUNC("special: can't find orig instruction",
  476. special_alt->orig_sec, special_alt->orig_off);
  477. ret = -1;
  478. goto out;
  479. }
  480. new_insn = NULL;
  481. if (!special_alt->group || special_alt->new_len) {
  482. new_insn = find_insn(file, special_alt->new_sec,
  483. special_alt->new_off);
  484. if (!new_insn) {
  485. WARN_FUNC("special: can't find new instruction",
  486. special_alt->new_sec,
  487. special_alt->new_off);
  488. ret = -1;
  489. goto out;
  490. }
  491. }
  492. if (special_alt->group) {
  493. ret = handle_group_alt(file, special_alt, orig_insn,
  494. &new_insn);
  495. if (ret)
  496. goto out;
  497. } else if (special_alt->jump_or_nop) {
  498. ret = handle_jump_alt(file, special_alt, orig_insn,
  499. &new_insn);
  500. if (ret)
  501. goto out;
  502. }
  503. alt->insn = new_insn;
  504. list_add_tail(&alt->list, &orig_insn->alts);
  505. list_del(&special_alt->list);
  506. free(special_alt);
  507. }
  508. out:
  509. return ret;
  510. }
  511. static int add_switch_table(struct objtool_file *file, struct symbol *func,
  512. struct instruction *insn, struct rela *table,
  513. struct rela *next_table)
  514. {
  515. struct rela *rela = table;
  516. struct instruction *alt_insn;
  517. struct alternative *alt;
  518. list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
  519. if (rela == next_table)
  520. break;
  521. if (rela->sym->sec != insn->sec ||
  522. rela->addend <= func->offset ||
  523. rela->addend >= func->offset + func->len)
  524. break;
  525. alt_insn = find_insn(file, insn->sec, rela->addend);
  526. if (!alt_insn) {
  527. WARN("%s: can't find instruction at %s+0x%x",
  528. file->rodata->rela->name, insn->sec->name,
  529. rela->addend);
  530. return -1;
  531. }
  532. alt = malloc(sizeof(*alt));
  533. if (!alt) {
  534. WARN("malloc failed");
  535. return -1;
  536. }
  537. alt->insn = alt_insn;
  538. list_add_tail(&alt->list, &insn->alts);
  539. }
  540. return 0;
  541. }
  542. static int add_func_switch_tables(struct objtool_file *file,
  543. struct symbol *func)
  544. {
  545. struct instruction *insn, *prev_jump;
  546. struct rela *text_rela, *rodata_rela, *prev_rela;
  547. int ret;
  548. prev_jump = NULL;
  549. func_for_each_insn(file, func, insn) {
  550. if (insn->type != INSN_JUMP_DYNAMIC)
  551. continue;
  552. text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
  553. insn->len);
  554. if (!text_rela || text_rela->sym != file->rodata->sym)
  555. continue;
  556. /* common case: jmpq *[addr](,%rax,8) */
  557. rodata_rela = find_rela_by_dest(file->rodata,
  558. text_rela->addend);
  559. /*
  560. * TODO: Document where this is needed, or get rid of it.
  561. *
  562. * rare case: jmpq *[addr](%rip)
  563. */
  564. if (!rodata_rela)
  565. rodata_rela = find_rela_by_dest(file->rodata,
  566. text_rela->addend + 4);
  567. if (!rodata_rela)
  568. continue;
  569. /*
  570. * We found a switch table, but we don't know yet how big it
  571. * is. Don't add it until we reach the end of the function or
  572. * the beginning of another switch table in the same function.
  573. */
  574. if (prev_jump) {
  575. ret = add_switch_table(file, func, prev_jump, prev_rela,
  576. rodata_rela);
  577. if (ret)
  578. return ret;
  579. }
  580. prev_jump = insn;
  581. prev_rela = rodata_rela;
  582. }
  583. if (prev_jump) {
  584. ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
  585. if (ret)
  586. return ret;
  587. }
  588. return 0;
  589. }
  590. /*
  591. * For some switch statements, gcc generates a jump table in the .rodata
  592. * section which contains a list of addresses within the function to jump to.
  593. * This finds these jump tables and adds them to the insn->alts lists.
  594. */
  595. static int add_switch_table_alts(struct objtool_file *file)
  596. {
  597. struct section *sec;
  598. struct symbol *func;
  599. int ret;
  600. if (!file->rodata || !file->rodata->rela)
  601. return 0;
  602. list_for_each_entry(sec, &file->elf->sections, list) {
  603. list_for_each_entry(func, &sec->symbol_list, list) {
  604. if (func->type != STT_FUNC)
  605. continue;
  606. ret = add_func_switch_tables(file, func);
  607. if (ret)
  608. return ret;
  609. }
  610. }
  611. return 0;
  612. }
  613. static int decode_sections(struct objtool_file *file)
  614. {
  615. int ret;
  616. file->whitelist = find_section_by_name(file->elf, "__func_stack_frame_non_standard");
  617. file->rodata = find_section_by_name(file->elf, ".rodata");
  618. ret = decode_instructions(file);
  619. if (ret)
  620. return ret;
  621. add_ignores(file);
  622. ret = add_jump_destinations(file);
  623. if (ret)
  624. return ret;
  625. ret = add_call_destinations(file);
  626. if (ret)
  627. return ret;
  628. ret = add_special_section_alts(file);
  629. if (ret)
  630. return ret;
  631. ret = add_switch_table_alts(file);
  632. if (ret)
  633. return ret;
  634. return 0;
  635. }
  636. static bool is_fentry_call(struct instruction *insn)
  637. {
  638. if (insn->type == INSN_CALL &&
  639. insn->call_dest->type == STT_NOTYPE &&
  640. !strcmp(insn->call_dest->name, "__fentry__"))
  641. return true;
  642. return false;
  643. }
  644. static bool has_modified_stack_frame(struct instruction *insn)
  645. {
  646. return (insn->state & STATE_FP_SAVED) ||
  647. (insn->state & STATE_FP_SETUP);
  648. }
  649. static bool has_valid_stack_frame(struct instruction *insn)
  650. {
  651. return (insn->state & STATE_FP_SAVED) &&
  652. (insn->state & STATE_FP_SETUP);
  653. }
  654. static unsigned int frame_state(unsigned long state)
  655. {
  656. return (state & (STATE_FP_SAVED | STATE_FP_SETUP));
  657. }
  658. /*
  659. * Follow the branch starting at the given instruction, and recursively follow
  660. * any other branches (jumps). Meanwhile, track the frame pointer state at
  661. * each instruction and validate all the rules described in
  662. * tools/objtool/Documentation/stack-validation.txt.
  663. */
  664. static int validate_branch(struct objtool_file *file,
  665. struct instruction *first, unsigned char first_state)
  666. {
  667. struct alternative *alt;
  668. struct instruction *insn;
  669. struct section *sec;
  670. unsigned char state;
  671. int ret;
  672. insn = first;
  673. sec = insn->sec;
  674. state = first_state;
  675. if (insn->alt_group && list_empty(&insn->alts)) {
  676. WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
  677. sec, insn->offset);
  678. return 1;
  679. }
  680. while (1) {
  681. if (insn->visited) {
  682. if (frame_state(insn->state) != frame_state(state)) {
  683. WARN_FUNC("frame pointer state mismatch",
  684. sec, insn->offset);
  685. return 1;
  686. }
  687. return 0;
  688. }
  689. /*
  690. * Catch a rare case where a noreturn function falls through to
  691. * the next function.
  692. */
  693. if (is_fentry_call(insn) && (state & STATE_FENTRY))
  694. return 0;
  695. insn->visited = true;
  696. insn->state = state;
  697. list_for_each_entry(alt, &insn->alts, list) {
  698. ret = validate_branch(file, alt->insn, state);
  699. if (ret)
  700. return 1;
  701. }
  702. switch (insn->type) {
  703. case INSN_FP_SAVE:
  704. if (!nofp) {
  705. if (state & STATE_FP_SAVED) {
  706. WARN_FUNC("duplicate frame pointer save",
  707. sec, insn->offset);
  708. return 1;
  709. }
  710. state |= STATE_FP_SAVED;
  711. }
  712. break;
  713. case INSN_FP_SETUP:
  714. if (!nofp) {
  715. if (state & STATE_FP_SETUP) {
  716. WARN_FUNC("duplicate frame pointer setup",
  717. sec, insn->offset);
  718. return 1;
  719. }
  720. state |= STATE_FP_SETUP;
  721. }
  722. break;
  723. case INSN_FP_RESTORE:
  724. if (!nofp) {
  725. if (has_valid_stack_frame(insn))
  726. state &= ~STATE_FP_SETUP;
  727. state &= ~STATE_FP_SAVED;
  728. }
  729. break;
  730. case INSN_RETURN:
  731. if (!nofp && has_modified_stack_frame(insn)) {
  732. WARN_FUNC("return without frame pointer restore",
  733. sec, insn->offset);
  734. return 1;
  735. }
  736. return 0;
  737. case INSN_CALL:
  738. if (is_fentry_call(insn)) {
  739. state |= STATE_FENTRY;
  740. break;
  741. }
  742. ret = dead_end_function(file, insn->call_dest);
  743. if (ret == 1)
  744. return 0;
  745. if (ret == -1)
  746. return 1;
  747. /* fallthrough */
  748. case INSN_CALL_DYNAMIC:
  749. if (!nofp && !has_valid_stack_frame(insn)) {
  750. WARN_FUNC("call without frame pointer save/setup",
  751. sec, insn->offset);
  752. return 1;
  753. }
  754. break;
  755. case INSN_JUMP_CONDITIONAL:
  756. case INSN_JUMP_UNCONDITIONAL:
  757. if (insn->jump_dest) {
  758. ret = validate_branch(file, insn->jump_dest,
  759. state);
  760. if (ret)
  761. return 1;
  762. } else if (has_modified_stack_frame(insn)) {
  763. WARN_FUNC("sibling call from callable instruction with changed frame pointer",
  764. sec, insn->offset);
  765. return 1;
  766. } /* else it's a sibling call */
  767. if (insn->type == INSN_JUMP_UNCONDITIONAL)
  768. return 0;
  769. break;
  770. case INSN_JUMP_DYNAMIC:
  771. if (list_empty(&insn->alts) &&
  772. has_modified_stack_frame(insn)) {
  773. WARN_FUNC("sibling call from callable instruction with changed frame pointer",
  774. sec, insn->offset);
  775. return 1;
  776. }
  777. return 0;
  778. case INSN_BUG:
  779. return 0;
  780. default:
  781. break;
  782. }
  783. insn = next_insn_same_sec(file, insn);
  784. if (!insn) {
  785. WARN("%s: unexpected end of section", sec->name);
  786. return 1;
  787. }
  788. }
  789. return 0;
  790. }
  791. static bool is_gcov_insn(struct instruction *insn)
  792. {
  793. struct rela *rela;
  794. struct section *sec;
  795. struct symbol *sym;
  796. unsigned long offset;
  797. rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
  798. if (!rela)
  799. return false;
  800. if (rela->sym->type != STT_SECTION)
  801. return false;
  802. sec = rela->sym->sec;
  803. offset = rela->addend + insn->offset + insn->len - rela->offset;
  804. list_for_each_entry(sym, &sec->symbol_list, list) {
  805. if (sym->type != STT_OBJECT)
  806. continue;
  807. if (offset >= sym->offset && offset < sym->offset + sym->len)
  808. return (!memcmp(sym->name, "__gcov0.", 8));
  809. }
  810. return false;
  811. }
  812. static bool is_kasan_insn(struct instruction *insn)
  813. {
  814. return (insn->type == INSN_CALL &&
  815. !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
  816. }
  817. static bool is_ubsan_insn(struct instruction *insn)
  818. {
  819. return (insn->type == INSN_CALL &&
  820. !strcmp(insn->call_dest->name,
  821. "__ubsan_handle_builtin_unreachable"));
  822. }
  823. static bool ignore_unreachable_insn(struct symbol *func,
  824. struct instruction *insn)
  825. {
  826. int i;
  827. if (insn->type == INSN_NOP)
  828. return true;
  829. if (is_gcov_insn(insn))
  830. return true;
  831. /*
  832. * Check if this (or a subsequent) instruction is related to
  833. * CONFIG_UBSAN or CONFIG_KASAN.
  834. *
  835. * End the search at 5 instructions to avoid going into the weeds.
  836. */
  837. for (i = 0; i < 5; i++) {
  838. if (is_kasan_insn(insn) || is_ubsan_insn(insn))
  839. return true;
  840. if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
  841. insn = insn->jump_dest;
  842. continue;
  843. }
  844. if (insn->offset + insn->len >= func->offset + func->len)
  845. break;
  846. insn = list_next_entry(insn, list);
  847. }
  848. return false;
  849. }
  850. static int validate_functions(struct objtool_file *file)
  851. {
  852. struct section *sec;
  853. struct symbol *func;
  854. struct instruction *insn;
  855. int ret, warnings = 0;
  856. list_for_each_entry(sec, &file->elf->sections, list) {
  857. list_for_each_entry(func, &sec->symbol_list, list) {
  858. if (func->type != STT_FUNC)
  859. continue;
  860. insn = find_insn(file, sec, func->offset);
  861. if (!insn) {
  862. WARN("%s(): can't find starting instruction",
  863. func->name);
  864. warnings++;
  865. continue;
  866. }
  867. ret = validate_branch(file, insn, 0);
  868. warnings += ret;
  869. }
  870. }
  871. list_for_each_entry(sec, &file->elf->sections, list) {
  872. list_for_each_entry(func, &sec->symbol_list, list) {
  873. if (func->type != STT_FUNC)
  874. continue;
  875. func_for_each_insn(file, func, insn) {
  876. if (insn->visited)
  877. continue;
  878. if (!ignore_unreachable_insn(func, insn) &&
  879. !warnings) {
  880. WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
  881. warnings++;
  882. }
  883. insn->visited = true;
  884. }
  885. }
  886. }
  887. return warnings;
  888. }
  889. static int validate_uncallable_instructions(struct objtool_file *file)
  890. {
  891. struct instruction *insn;
  892. int warnings = 0;
  893. for_each_insn(file, insn) {
  894. if (!insn->visited && insn->type == INSN_RETURN) {
  895. WARN_FUNC("return instruction outside of a callable function",
  896. insn->sec, insn->offset);
  897. warnings++;
  898. }
  899. }
  900. return warnings;
  901. }
  902. static void cleanup(struct objtool_file *file)
  903. {
  904. struct instruction *insn, *tmpinsn;
  905. struct alternative *alt, *tmpalt;
  906. list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
  907. list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
  908. list_del(&alt->list);
  909. free(alt);
  910. }
  911. list_del(&insn->list);
  912. hash_del(&insn->hash);
  913. free(insn);
  914. }
  915. elf_close(file->elf);
  916. }
  917. const char * const check_usage[] = {
  918. "objtool check [<options>] file.o",
  919. NULL,
  920. };
  921. int cmd_check(int argc, const char **argv)
  922. {
  923. struct objtool_file file;
  924. int ret, warnings = 0;
  925. const struct option options[] = {
  926. OPT_BOOLEAN('f', "no-fp", &nofp, "Skip frame pointer validation"),
  927. OPT_END(),
  928. };
  929. argc = parse_options(argc, argv, options, check_usage, 0);
  930. if (argc != 1)
  931. usage_with_options(check_usage, options);
  932. objname = argv[0];
  933. file.elf = elf_open(objname);
  934. if (!file.elf) {
  935. fprintf(stderr, "error reading elf file %s\n", objname);
  936. return 1;
  937. }
  938. INIT_LIST_HEAD(&file.insn_list);
  939. hash_init(file.insn_hash);
  940. ret = decode_sections(&file);
  941. if (ret < 0)
  942. goto out;
  943. warnings += ret;
  944. ret = validate_functions(&file);
  945. if (ret < 0)
  946. goto out;
  947. warnings += ret;
  948. ret = validate_uncallable_instructions(&file);
  949. if (ret < 0)
  950. goto out;
  951. warnings += ret;
  952. out:
  953. cleanup(&file);
  954. /* ignore warnings for now until we get all the code cleaned up */
  955. if (ret || warnings)
  956. return 0;
  957. return 0;
  958. }