builtin-check.c 30 KB

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