builtin-check.c 28 KB

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