builtin-check.c 30 KB

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