check.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /*
  2. * Copyright (C) 2015-2017 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. #include <string.h>
  18. #include <stdlib.h>
  19. #include "check.h"
  20. #include "elf.h"
  21. #include "special.h"
  22. #include "arch.h"
  23. #include "warn.h"
  24. #include <linux/hashtable.h>
  25. #include <linux/kernel.h>
  26. struct alternative {
  27. struct list_head list;
  28. struct instruction *insn;
  29. };
  30. const char *objname;
  31. static bool nofp;
  32. struct cfi_state initial_func_cfi;
  33. static struct instruction *find_insn(struct objtool_file *file,
  34. struct section *sec, unsigned long offset)
  35. {
  36. struct instruction *insn;
  37. hash_for_each_possible(file->insn_hash, insn, hash, offset)
  38. if (insn->sec == sec && insn->offset == offset)
  39. return insn;
  40. return NULL;
  41. }
  42. static struct instruction *next_insn_same_sec(struct objtool_file *file,
  43. struct instruction *insn)
  44. {
  45. struct instruction *next = list_next_entry(insn, list);
  46. if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
  47. return NULL;
  48. return next;
  49. }
  50. static bool gcov_enabled(struct objtool_file *file)
  51. {
  52. struct section *sec;
  53. struct symbol *sym;
  54. for_each_sec(file, sec)
  55. list_for_each_entry(sym, &sec->symbol_list, list)
  56. if (!strncmp(sym->name, "__gcov_.", 8))
  57. return true;
  58. return false;
  59. }
  60. #define func_for_each_insn(file, func, insn) \
  61. for (insn = find_insn(file, func->sec, func->offset); \
  62. insn && &insn->list != &file->insn_list && \
  63. insn->sec == func->sec && \
  64. insn->offset < func->offset + func->len; \
  65. insn = list_next_entry(insn, list))
  66. #define func_for_each_insn_continue_reverse(file, func, insn) \
  67. for (insn = list_prev_entry(insn, list); \
  68. &insn->list != &file->insn_list && \
  69. insn->sec == func->sec && insn->offset >= func->offset; \
  70. insn = list_prev_entry(insn, list))
  71. #define sec_for_each_insn_from(file, insn) \
  72. for (; insn; insn = next_insn_same_sec(file, insn))
  73. #define sec_for_each_insn_continue(file, insn) \
  74. for (insn = next_insn_same_sec(file, insn); insn; \
  75. insn = next_insn_same_sec(file, insn))
  76. /*
  77. * Check if the function has been manually whitelisted with the
  78. * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
  79. * due to its use of a context switching instruction.
  80. */
  81. static bool ignore_func(struct objtool_file *file, struct symbol *func)
  82. {
  83. struct rela *rela;
  84. struct instruction *insn;
  85. /* check for STACK_FRAME_NON_STANDARD */
  86. if (file->whitelist && file->whitelist->rela)
  87. list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
  88. if (rela->sym->type == STT_SECTION &&
  89. rela->sym->sec == func->sec &&
  90. rela->addend == func->offset)
  91. return true;
  92. if (rela->sym->type == STT_FUNC && rela->sym == func)
  93. return true;
  94. }
  95. /* check if it has a context switching instruction */
  96. func_for_each_insn(file, func, insn)
  97. if (insn->type == INSN_CONTEXT_SWITCH)
  98. return true;
  99. return false;
  100. }
  101. /*
  102. * This checks to see if the given function is a "noreturn" function.
  103. *
  104. * For global functions which are outside the scope of this object file, we
  105. * have to keep a manual list of them.
  106. *
  107. * For local functions, we have to detect them manually by simply looking for
  108. * the lack of a return instruction.
  109. *
  110. * Returns:
  111. * -1: error
  112. * 0: no dead end
  113. * 1: dead end
  114. */
  115. static int __dead_end_function(struct objtool_file *file, struct symbol *func,
  116. int recursion)
  117. {
  118. int i;
  119. struct instruction *insn;
  120. bool empty = true;
  121. /*
  122. * Unfortunately these have to be hard coded because the noreturn
  123. * attribute isn't provided in ELF data.
  124. */
  125. static const char * const global_noreturns[] = {
  126. "__stack_chk_fail",
  127. "panic",
  128. "do_exit",
  129. "do_task_dead",
  130. "__module_put_and_exit",
  131. "complete_and_exit",
  132. "kvm_spurious_fault",
  133. "__reiserfs_panic",
  134. "lbug_with_loc",
  135. "fortify_panic",
  136. };
  137. if (func->bind == STB_WEAK)
  138. return 0;
  139. if (func->bind == STB_GLOBAL)
  140. for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
  141. if (!strcmp(func->name, global_noreturns[i]))
  142. return 1;
  143. if (!func->sec)
  144. return 0;
  145. func_for_each_insn(file, func, insn) {
  146. empty = false;
  147. if (insn->type == INSN_RETURN)
  148. return 0;
  149. }
  150. if (empty)
  151. return 0;
  152. /*
  153. * A function can have a sibling call instead of a return. In that
  154. * case, the function's dead-end status depends on whether the target
  155. * of the sibling call returns.
  156. */
  157. func_for_each_insn(file, func, insn) {
  158. if (insn->sec != func->sec ||
  159. insn->offset >= func->offset + func->len)
  160. break;
  161. if (insn->type == INSN_JUMP_UNCONDITIONAL) {
  162. struct instruction *dest = insn->jump_dest;
  163. struct symbol *dest_func;
  164. if (!dest)
  165. /* sibling call to another file */
  166. return 0;
  167. if (dest->sec != func->sec ||
  168. dest->offset < func->offset ||
  169. dest->offset >= func->offset + func->len) {
  170. /* local sibling call */
  171. dest_func = find_symbol_by_offset(dest->sec,
  172. dest->offset);
  173. if (!dest_func)
  174. continue;
  175. if (recursion == 5) {
  176. WARN_FUNC("infinite recursion (objtool bug!)",
  177. dest->sec, dest->offset);
  178. return -1;
  179. }
  180. return __dead_end_function(file, dest_func,
  181. recursion + 1);
  182. }
  183. }
  184. if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
  185. /* sibling call */
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. static int dead_end_function(struct objtool_file *file, struct symbol *func)
  191. {
  192. return __dead_end_function(file, func, 0);
  193. }
  194. static void clear_insn_state(struct insn_state *state)
  195. {
  196. int i;
  197. memset(state, 0, sizeof(*state));
  198. state->cfa.base = CFI_UNDEFINED;
  199. for (i = 0; i < CFI_NUM_REGS; i++)
  200. state->regs[i].base = CFI_UNDEFINED;
  201. state->drap_reg = CFI_UNDEFINED;
  202. }
  203. /*
  204. * Call the arch-specific instruction decoder for all the instructions and add
  205. * them to the global instruction list.
  206. */
  207. static int decode_instructions(struct objtool_file *file)
  208. {
  209. struct section *sec;
  210. struct symbol *func;
  211. unsigned long offset;
  212. struct instruction *insn;
  213. int ret;
  214. for_each_sec(file, sec) {
  215. if (!(sec->sh.sh_flags & SHF_EXECINSTR))
  216. continue;
  217. for (offset = 0; offset < sec->len; offset += insn->len) {
  218. insn = malloc(sizeof(*insn));
  219. if (!insn) {
  220. WARN("malloc failed");
  221. return -1;
  222. }
  223. memset(insn, 0, sizeof(*insn));
  224. INIT_LIST_HEAD(&insn->alts);
  225. clear_insn_state(&insn->state);
  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. &insn->stack_op);
  233. if (ret)
  234. return ret;
  235. if (!insn->type || insn->type > INSN_LAST) {
  236. WARN_FUNC("invalid instruction type %d",
  237. insn->sec, insn->offset, insn->type);
  238. return -1;
  239. }
  240. hash_add(file->insn_hash, &insn->hash, insn->offset);
  241. list_add_tail(&insn->list, &file->insn_list);
  242. }
  243. list_for_each_entry(func, &sec->symbol_list, list) {
  244. if (func->type != STT_FUNC)
  245. continue;
  246. if (!find_insn(file, sec, func->offset)) {
  247. WARN("%s(): can't find starting instruction",
  248. func->name);
  249. return -1;
  250. }
  251. func_for_each_insn(file, func, insn)
  252. if (!insn->func)
  253. insn->func = func;
  254. }
  255. }
  256. return 0;
  257. }
  258. /*
  259. * Find all uses of the unreachable() macro, which are code path dead ends.
  260. */
  261. static int add_dead_ends(struct objtool_file *file)
  262. {
  263. struct section *sec;
  264. struct rela *rela;
  265. struct instruction *insn;
  266. bool found;
  267. sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
  268. if (!sec)
  269. return 0;
  270. list_for_each_entry(rela, &sec->rela_list, list) {
  271. if (rela->sym->type != STT_SECTION) {
  272. WARN("unexpected relocation symbol type in %s", sec->name);
  273. return -1;
  274. }
  275. insn = find_insn(file, rela->sym->sec, rela->addend);
  276. if (insn)
  277. insn = list_prev_entry(insn, list);
  278. else if (rela->addend == rela->sym->sec->len) {
  279. found = false;
  280. list_for_each_entry_reverse(insn, &file->insn_list, list) {
  281. if (insn->sec == rela->sym->sec) {
  282. found = true;
  283. break;
  284. }
  285. }
  286. if (!found) {
  287. WARN("can't find unreachable insn at %s+0x%x",
  288. rela->sym->sec->name, rela->addend);
  289. return -1;
  290. }
  291. } else {
  292. WARN("can't find unreachable insn at %s+0x%x",
  293. rela->sym->sec->name, rela->addend);
  294. return -1;
  295. }
  296. insn->dead_end = true;
  297. }
  298. return 0;
  299. }
  300. /*
  301. * Warnings shouldn't be reported for ignored functions.
  302. */
  303. static void add_ignores(struct objtool_file *file)
  304. {
  305. struct instruction *insn;
  306. struct section *sec;
  307. struct symbol *func;
  308. for_each_sec(file, sec) {
  309. list_for_each_entry(func, &sec->symbol_list, list) {
  310. if (func->type != STT_FUNC)
  311. continue;
  312. if (!ignore_func(file, func))
  313. continue;
  314. func_for_each_insn(file, func, insn)
  315. insn->ignore = true;
  316. }
  317. }
  318. }
  319. /*
  320. * Find the destination instructions for all jumps.
  321. */
  322. static int add_jump_destinations(struct objtool_file *file)
  323. {
  324. struct instruction *insn;
  325. struct rela *rela;
  326. struct section *dest_sec;
  327. unsigned long dest_off;
  328. for_each_insn(file, insn) {
  329. if (insn->type != INSN_JUMP_CONDITIONAL &&
  330. insn->type != INSN_JUMP_UNCONDITIONAL)
  331. continue;
  332. if (insn->ignore)
  333. continue;
  334. rela = find_rela_by_dest_range(insn->sec, insn->offset,
  335. insn->len);
  336. if (!rela) {
  337. dest_sec = insn->sec;
  338. dest_off = insn->offset + insn->len + insn->immediate;
  339. } else if (rela->sym->type == STT_SECTION) {
  340. dest_sec = rela->sym->sec;
  341. dest_off = rela->addend + 4;
  342. } else if (rela->sym->sec->idx) {
  343. dest_sec = rela->sym->sec;
  344. dest_off = rela->sym->sym.st_value + rela->addend + 4;
  345. } else {
  346. /* sibling call */
  347. insn->jump_dest = 0;
  348. continue;
  349. }
  350. insn->jump_dest = find_insn(file, dest_sec, dest_off);
  351. if (!insn->jump_dest) {
  352. /*
  353. * This is a special case where an alt instruction
  354. * jumps past the end of the section. These are
  355. * handled later in handle_group_alt().
  356. */
  357. if (!strcmp(insn->sec->name, ".altinstr_replacement"))
  358. continue;
  359. WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
  360. insn->sec, insn->offset, dest_sec->name,
  361. dest_off);
  362. return -1;
  363. }
  364. }
  365. return 0;
  366. }
  367. /*
  368. * Find the destination instructions for all calls.
  369. */
  370. static int add_call_destinations(struct objtool_file *file)
  371. {
  372. struct instruction *insn;
  373. unsigned long dest_off;
  374. struct rela *rela;
  375. for_each_insn(file, insn) {
  376. if (insn->type != INSN_CALL)
  377. continue;
  378. rela = find_rela_by_dest_range(insn->sec, insn->offset,
  379. insn->len);
  380. if (!rela) {
  381. dest_off = insn->offset + insn->len + insn->immediate;
  382. insn->call_dest = find_symbol_by_offset(insn->sec,
  383. dest_off);
  384. if (!insn->call_dest) {
  385. WARN_FUNC("can't find call dest symbol at offset 0x%lx",
  386. insn->sec, insn->offset, dest_off);
  387. return -1;
  388. }
  389. } else if (rela->sym->type == STT_SECTION) {
  390. insn->call_dest = find_symbol_by_offset(rela->sym->sec,
  391. rela->addend+4);
  392. if (!insn->call_dest ||
  393. insn->call_dest->type != STT_FUNC) {
  394. WARN_FUNC("can't find call dest symbol at %s+0x%x",
  395. insn->sec, insn->offset,
  396. rela->sym->sec->name,
  397. rela->addend + 4);
  398. return -1;
  399. }
  400. } else
  401. insn->call_dest = rela->sym;
  402. }
  403. return 0;
  404. }
  405. /*
  406. * The .alternatives section requires some extra special care, over and above
  407. * what other special sections require:
  408. *
  409. * 1. Because alternatives are patched in-place, we need to insert a fake jump
  410. * instruction at the end so that validate_branch() skips all the original
  411. * replaced instructions when validating the new instruction path.
  412. *
  413. * 2. An added wrinkle is that the new instruction length might be zero. In
  414. * that case the old instructions are replaced with noops. We simulate that
  415. * by creating a fake jump as the only new instruction.
  416. *
  417. * 3. In some cases, the alternative section includes an instruction which
  418. * conditionally jumps to the _end_ of the entry. We have to modify these
  419. * jumps' destinations to point back to .text rather than the end of the
  420. * entry in .altinstr_replacement.
  421. *
  422. * 4. It has been requested that we don't validate the !POPCNT feature path
  423. * which is a "very very small percentage of machines".
  424. */
  425. static int handle_group_alt(struct objtool_file *file,
  426. struct special_alt *special_alt,
  427. struct instruction *orig_insn,
  428. struct instruction **new_insn)
  429. {
  430. struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump;
  431. unsigned long dest_off;
  432. last_orig_insn = NULL;
  433. insn = orig_insn;
  434. sec_for_each_insn_from(file, insn) {
  435. if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
  436. break;
  437. if (special_alt->skip_orig)
  438. insn->type = INSN_NOP;
  439. insn->alt_group = true;
  440. last_orig_insn = insn;
  441. }
  442. if (!next_insn_same_sec(file, last_orig_insn)) {
  443. WARN("%s: don't know how to handle alternatives at end of section",
  444. special_alt->orig_sec->name);
  445. return -1;
  446. }
  447. fake_jump = malloc(sizeof(*fake_jump));
  448. if (!fake_jump) {
  449. WARN("malloc failed");
  450. return -1;
  451. }
  452. memset(fake_jump, 0, sizeof(*fake_jump));
  453. INIT_LIST_HEAD(&fake_jump->alts);
  454. clear_insn_state(&fake_jump->state);
  455. fake_jump->sec = special_alt->new_sec;
  456. fake_jump->offset = -1;
  457. fake_jump->type = INSN_JUMP_UNCONDITIONAL;
  458. fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
  459. fake_jump->ignore = true;
  460. if (!special_alt->new_len) {
  461. *new_insn = fake_jump;
  462. return 0;
  463. }
  464. last_new_insn = NULL;
  465. insn = *new_insn;
  466. sec_for_each_insn_from(file, insn) {
  467. if (insn->offset >= special_alt->new_off + special_alt->new_len)
  468. break;
  469. last_new_insn = insn;
  470. if (insn->type != INSN_JUMP_CONDITIONAL &&
  471. insn->type != INSN_JUMP_UNCONDITIONAL)
  472. continue;
  473. if (!insn->immediate)
  474. continue;
  475. dest_off = insn->offset + insn->len + insn->immediate;
  476. if (dest_off == special_alt->new_off + special_alt->new_len)
  477. insn->jump_dest = fake_jump;
  478. if (!insn->jump_dest) {
  479. WARN_FUNC("can't find alternative jump destination",
  480. insn->sec, insn->offset);
  481. return -1;
  482. }
  483. }
  484. if (!last_new_insn) {
  485. WARN_FUNC("can't find last new alternative instruction",
  486. special_alt->new_sec, special_alt->new_off);
  487. return -1;
  488. }
  489. list_add(&fake_jump->list, &last_new_insn->list);
  490. return 0;
  491. }
  492. /*
  493. * A jump table entry can either convert a nop to a jump or a jump to a nop.
  494. * If the original instruction is a jump, make the alt entry an effective nop
  495. * by just skipping the original instruction.
  496. */
  497. static int handle_jump_alt(struct objtool_file *file,
  498. struct special_alt *special_alt,
  499. struct instruction *orig_insn,
  500. struct instruction **new_insn)
  501. {
  502. if (orig_insn->type == INSN_NOP)
  503. return 0;
  504. if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
  505. WARN_FUNC("unsupported instruction at jump label",
  506. orig_insn->sec, orig_insn->offset);
  507. return -1;
  508. }
  509. *new_insn = list_next_entry(orig_insn, list);
  510. return 0;
  511. }
  512. /*
  513. * Read all the special sections which have alternate instructions which can be
  514. * patched in or redirected to at runtime. Each instruction having alternate
  515. * instruction(s) has them added to its insn->alts list, which will be
  516. * traversed in validate_branch().
  517. */
  518. static int add_special_section_alts(struct objtool_file *file)
  519. {
  520. struct list_head special_alts;
  521. struct instruction *orig_insn, *new_insn;
  522. struct special_alt *special_alt, *tmp;
  523. struct alternative *alt;
  524. int ret;
  525. ret = special_get_alts(file->elf, &special_alts);
  526. if (ret)
  527. return ret;
  528. list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
  529. alt = malloc(sizeof(*alt));
  530. if (!alt) {
  531. WARN("malloc failed");
  532. ret = -1;
  533. goto out;
  534. }
  535. orig_insn = find_insn(file, special_alt->orig_sec,
  536. special_alt->orig_off);
  537. if (!orig_insn) {
  538. WARN_FUNC("special: can't find orig instruction",
  539. special_alt->orig_sec, special_alt->orig_off);
  540. ret = -1;
  541. goto out;
  542. }
  543. new_insn = NULL;
  544. if (!special_alt->group || special_alt->new_len) {
  545. new_insn = find_insn(file, special_alt->new_sec,
  546. special_alt->new_off);
  547. if (!new_insn) {
  548. WARN_FUNC("special: can't find new instruction",
  549. special_alt->new_sec,
  550. special_alt->new_off);
  551. ret = -1;
  552. goto out;
  553. }
  554. }
  555. if (special_alt->group) {
  556. ret = handle_group_alt(file, special_alt, orig_insn,
  557. &new_insn);
  558. if (ret)
  559. goto out;
  560. } else if (special_alt->jump_or_nop) {
  561. ret = handle_jump_alt(file, special_alt, orig_insn,
  562. &new_insn);
  563. if (ret)
  564. goto out;
  565. }
  566. alt->insn = new_insn;
  567. list_add_tail(&alt->list, &orig_insn->alts);
  568. list_del(&special_alt->list);
  569. free(special_alt);
  570. }
  571. out:
  572. return ret;
  573. }
  574. static int add_switch_table(struct objtool_file *file, struct symbol *func,
  575. struct instruction *insn, struct rela *table,
  576. struct rela *next_table)
  577. {
  578. struct rela *rela = table;
  579. struct instruction *alt_insn;
  580. struct alternative *alt;
  581. list_for_each_entry_from(rela, &file->rodata->rela->rela_list, list) {
  582. if (rela == next_table)
  583. break;
  584. if (rela->sym->sec != insn->sec ||
  585. rela->addend <= func->offset ||
  586. rela->addend >= func->offset + func->len)
  587. break;
  588. alt_insn = find_insn(file, insn->sec, rela->addend);
  589. if (!alt_insn) {
  590. WARN("%s: can't find instruction at %s+0x%x",
  591. file->rodata->rela->name, insn->sec->name,
  592. rela->addend);
  593. return -1;
  594. }
  595. alt = malloc(sizeof(*alt));
  596. if (!alt) {
  597. WARN("malloc failed");
  598. return -1;
  599. }
  600. alt->insn = alt_insn;
  601. list_add_tail(&alt->list, &insn->alts);
  602. }
  603. return 0;
  604. }
  605. /*
  606. * find_switch_table() - Given a dynamic jump, find the switch jump table in
  607. * .rodata associated with it.
  608. *
  609. * There are 3 basic patterns:
  610. *
  611. * 1. jmpq *[rodata addr](,%reg,8)
  612. *
  613. * This is the most common case by far. It jumps to an address in a simple
  614. * jump table which is stored in .rodata.
  615. *
  616. * 2. jmpq *[rodata addr](%rip)
  617. *
  618. * This is caused by a rare GCC quirk, currently only seen in three driver
  619. * functions in the kernel, only with certain obscure non-distro configs.
  620. *
  621. * As part of an optimization, GCC makes a copy of an existing switch jump
  622. * table, modifies it, and then hard-codes the jump (albeit with an indirect
  623. * jump) to use a single entry in the table. The rest of the jump table and
  624. * some of its jump targets remain as dead code.
  625. *
  626. * In such a case we can just crudely ignore all unreachable instruction
  627. * warnings for the entire object file. Ideally we would just ignore them
  628. * for the function, but that would require redesigning the code quite a
  629. * bit. And honestly that's just not worth doing: unreachable instruction
  630. * warnings are of questionable value anyway, and this is such a rare issue.
  631. *
  632. * 3. mov [rodata addr],%reg1
  633. * ... some instructions ...
  634. * jmpq *(%reg1,%reg2,8)
  635. *
  636. * This is a fairly uncommon pattern which is new for GCC 6. As of this
  637. * writing, there are 11 occurrences of it in the allmodconfig kernel.
  638. *
  639. * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
  640. * ensure the same register is used in the mov and jump instructions.
  641. */
  642. static struct rela *find_switch_table(struct objtool_file *file,
  643. struct symbol *func,
  644. struct instruction *insn)
  645. {
  646. struct rela *text_rela, *rodata_rela;
  647. struct instruction *orig_insn = insn;
  648. text_rela = find_rela_by_dest_range(insn->sec, insn->offset, insn->len);
  649. if (text_rela && text_rela->sym == file->rodata->sym) {
  650. /* case 1 */
  651. rodata_rela = find_rela_by_dest(file->rodata,
  652. text_rela->addend);
  653. if (rodata_rela)
  654. return rodata_rela;
  655. /* case 2 */
  656. rodata_rela = find_rela_by_dest(file->rodata,
  657. text_rela->addend + 4);
  658. if (!rodata_rela)
  659. return NULL;
  660. file->ignore_unreachables = true;
  661. return rodata_rela;
  662. }
  663. /* case 3 */
  664. func_for_each_insn_continue_reverse(file, func, insn) {
  665. if (insn->type == INSN_JUMP_DYNAMIC)
  666. break;
  667. /* allow small jumps within the range */
  668. if (insn->type == INSN_JUMP_UNCONDITIONAL &&
  669. insn->jump_dest &&
  670. (insn->jump_dest->offset <= insn->offset ||
  671. insn->jump_dest->offset > orig_insn->offset))
  672. break;
  673. /* look for a relocation which references .rodata */
  674. text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
  675. insn->len);
  676. if (!text_rela || text_rela->sym != file->rodata->sym)
  677. continue;
  678. /*
  679. * Make sure the .rodata address isn't associated with a
  680. * symbol. gcc jump tables are anonymous data.
  681. */
  682. if (find_symbol_containing(file->rodata, text_rela->addend))
  683. continue;
  684. return find_rela_by_dest(file->rodata, text_rela->addend);
  685. }
  686. return NULL;
  687. }
  688. static int add_func_switch_tables(struct objtool_file *file,
  689. struct symbol *func)
  690. {
  691. struct instruction *insn, *prev_jump = NULL;
  692. struct rela *rela, *prev_rela = NULL;
  693. int ret;
  694. func_for_each_insn(file, func, insn) {
  695. if (insn->type != INSN_JUMP_DYNAMIC)
  696. continue;
  697. rela = find_switch_table(file, func, insn);
  698. if (!rela)
  699. continue;
  700. /*
  701. * We found a switch table, but we don't know yet how big it
  702. * is. Don't add it until we reach the end of the function or
  703. * the beginning of another switch table in the same function.
  704. */
  705. if (prev_jump) {
  706. ret = add_switch_table(file, func, prev_jump, prev_rela,
  707. rela);
  708. if (ret)
  709. return ret;
  710. }
  711. prev_jump = insn;
  712. prev_rela = rela;
  713. }
  714. if (prev_jump) {
  715. ret = add_switch_table(file, func, prev_jump, prev_rela, NULL);
  716. if (ret)
  717. return ret;
  718. }
  719. return 0;
  720. }
  721. /*
  722. * For some switch statements, gcc generates a jump table in the .rodata
  723. * section which contains a list of addresses within the function to jump to.
  724. * This finds these jump tables and adds them to the insn->alts lists.
  725. */
  726. static int add_switch_table_alts(struct objtool_file *file)
  727. {
  728. struct section *sec;
  729. struct symbol *func;
  730. int ret;
  731. if (!file->rodata || !file->rodata->rela)
  732. return 0;
  733. for_each_sec(file, sec) {
  734. list_for_each_entry(func, &sec->symbol_list, list) {
  735. if (func->type != STT_FUNC)
  736. continue;
  737. ret = add_func_switch_tables(file, func);
  738. if (ret)
  739. return ret;
  740. }
  741. }
  742. return 0;
  743. }
  744. static int decode_sections(struct objtool_file *file)
  745. {
  746. int ret;
  747. ret = decode_instructions(file);
  748. if (ret)
  749. return ret;
  750. ret = add_dead_ends(file);
  751. if (ret)
  752. return ret;
  753. add_ignores(file);
  754. ret = add_jump_destinations(file);
  755. if (ret)
  756. return ret;
  757. ret = add_call_destinations(file);
  758. if (ret)
  759. return ret;
  760. ret = add_special_section_alts(file);
  761. if (ret)
  762. return ret;
  763. ret = add_switch_table_alts(file);
  764. if (ret)
  765. return ret;
  766. return 0;
  767. }
  768. static bool is_fentry_call(struct instruction *insn)
  769. {
  770. if (insn->type == INSN_CALL &&
  771. insn->call_dest->type == STT_NOTYPE &&
  772. !strcmp(insn->call_dest->name, "__fentry__"))
  773. return true;
  774. return false;
  775. }
  776. static bool has_modified_stack_frame(struct insn_state *state)
  777. {
  778. int i;
  779. if (state->cfa.base != initial_func_cfi.cfa.base ||
  780. state->cfa.offset != initial_func_cfi.cfa.offset ||
  781. state->stack_size != initial_func_cfi.cfa.offset ||
  782. state->drap)
  783. return true;
  784. for (i = 0; i < CFI_NUM_REGS; i++)
  785. if (state->regs[i].base != initial_func_cfi.regs[i].base ||
  786. state->regs[i].offset != initial_func_cfi.regs[i].offset)
  787. return true;
  788. return false;
  789. }
  790. static bool has_valid_stack_frame(struct insn_state *state)
  791. {
  792. if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
  793. state->regs[CFI_BP].offset == -16)
  794. return true;
  795. if (state->drap && state->regs[CFI_BP].base == CFI_BP)
  796. return true;
  797. return false;
  798. }
  799. static void save_reg(struct insn_state *state, unsigned char reg, int base,
  800. int offset)
  801. {
  802. if ((arch_callee_saved_reg(reg) ||
  803. (state->drap && reg == state->drap_reg)) &&
  804. state->regs[reg].base == CFI_UNDEFINED) {
  805. state->regs[reg].base = base;
  806. state->regs[reg].offset = offset;
  807. }
  808. }
  809. static void restore_reg(struct insn_state *state, unsigned char reg)
  810. {
  811. state->regs[reg].base = CFI_UNDEFINED;
  812. state->regs[reg].offset = 0;
  813. }
  814. /*
  815. * A note about DRAP stack alignment:
  816. *
  817. * GCC has the concept of a DRAP register, which is used to help keep track of
  818. * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
  819. * register. The typical DRAP pattern is:
  820. *
  821. * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
  822. * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
  823. * 41 ff 72 f8 pushq -0x8(%r10)
  824. * 55 push %rbp
  825. * 48 89 e5 mov %rsp,%rbp
  826. * (more pushes)
  827. * 41 52 push %r10
  828. * ...
  829. * 41 5a pop %r10
  830. * (more pops)
  831. * 5d pop %rbp
  832. * 49 8d 62 f8 lea -0x8(%r10),%rsp
  833. * c3 retq
  834. *
  835. * There are some variations in the epilogues, like:
  836. *
  837. * 5b pop %rbx
  838. * 41 5a pop %r10
  839. * 41 5c pop %r12
  840. * 41 5d pop %r13
  841. * 41 5e pop %r14
  842. * c9 leaveq
  843. * 49 8d 62 f8 lea -0x8(%r10),%rsp
  844. * c3 retq
  845. *
  846. * and:
  847. *
  848. * 4c 8b 55 e8 mov -0x18(%rbp),%r10
  849. * 48 8b 5d e0 mov -0x20(%rbp),%rbx
  850. * 4c 8b 65 f0 mov -0x10(%rbp),%r12
  851. * 4c 8b 6d f8 mov -0x8(%rbp),%r13
  852. * c9 leaveq
  853. * 49 8d 62 f8 lea -0x8(%r10),%rsp
  854. * c3 retq
  855. *
  856. * Sometimes r13 is used as the DRAP register, in which case it's saved and
  857. * restored beforehand:
  858. *
  859. * 41 55 push %r13
  860. * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
  861. * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
  862. * ...
  863. * 49 8d 65 f0 lea -0x10(%r13),%rsp
  864. * 41 5d pop %r13
  865. * c3 retq
  866. */
  867. static int update_insn_state(struct instruction *insn, struct insn_state *state)
  868. {
  869. struct stack_op *op = &insn->stack_op;
  870. struct cfi_reg *cfa = &state->cfa;
  871. struct cfi_reg *regs = state->regs;
  872. /* stack operations don't make sense with an undefined CFA */
  873. if (cfa->base == CFI_UNDEFINED) {
  874. if (insn->func) {
  875. WARN_FUNC("undefined stack state", insn->sec, insn->offset);
  876. return -1;
  877. }
  878. return 0;
  879. }
  880. switch (op->dest.type) {
  881. case OP_DEST_REG:
  882. switch (op->src.type) {
  883. case OP_SRC_REG:
  884. if (cfa->base == op->src.reg && cfa->base == CFI_SP &&
  885. op->dest.reg == CFI_BP && regs[CFI_BP].base == CFI_CFA &&
  886. regs[CFI_BP].offset == -cfa->offset) {
  887. /* mov %rsp, %rbp */
  888. cfa->base = op->dest.reg;
  889. state->bp_scratch = false;
  890. } else if (state->drap) {
  891. /* drap: mov %rsp, %rbp */
  892. regs[CFI_BP].base = CFI_BP;
  893. regs[CFI_BP].offset = -state->stack_size;
  894. state->bp_scratch = false;
  895. } else if (!nofp) {
  896. WARN_FUNC("unknown stack-related register move",
  897. insn->sec, insn->offset);
  898. return -1;
  899. }
  900. break;
  901. case OP_SRC_ADD:
  902. if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
  903. /* add imm, %rsp */
  904. state->stack_size -= op->src.offset;
  905. if (cfa->base == CFI_SP)
  906. cfa->offset -= op->src.offset;
  907. break;
  908. }
  909. if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
  910. /* lea disp(%rbp), %rsp */
  911. state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
  912. break;
  913. }
  914. if (op->dest.reg != CFI_BP && op->src.reg == CFI_SP &&
  915. cfa->base == CFI_SP) {
  916. /* drap: lea disp(%rsp), %drap */
  917. state->drap_reg = op->dest.reg;
  918. break;
  919. }
  920. if (state->drap && op->dest.reg == CFI_SP &&
  921. op->src.reg == state->drap_reg) {
  922. /* drap: lea disp(%drap), %rsp */
  923. cfa->base = CFI_SP;
  924. cfa->offset = state->stack_size = -op->src.offset;
  925. state->drap_reg = CFI_UNDEFINED;
  926. state->drap = false;
  927. break;
  928. }
  929. if (op->dest.reg == state->cfa.base) {
  930. WARN_FUNC("unsupported stack register modification",
  931. insn->sec, insn->offset);
  932. return -1;
  933. }
  934. break;
  935. case OP_SRC_AND:
  936. if (op->dest.reg != CFI_SP ||
  937. (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
  938. (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
  939. WARN_FUNC("unsupported stack pointer realignment",
  940. insn->sec, insn->offset);
  941. return -1;
  942. }
  943. if (state->drap_reg != CFI_UNDEFINED) {
  944. /* drap: and imm, %rsp */
  945. cfa->base = state->drap_reg;
  946. cfa->offset = state->stack_size = 0;
  947. state->drap = true;
  948. }
  949. /*
  950. * Older versions of GCC (4.8ish) realign the stack
  951. * without DRAP, with a frame pointer.
  952. */
  953. break;
  954. case OP_SRC_POP:
  955. if (!state->drap && op->dest.type == OP_DEST_REG &&
  956. op->dest.reg == cfa->base) {
  957. /* pop %rbp */
  958. cfa->base = CFI_SP;
  959. }
  960. if (regs[op->dest.reg].offset == -state->stack_size) {
  961. if (state->drap && cfa->base == CFI_BP_INDIRECT &&
  962. op->dest.type == OP_DEST_REG &&
  963. op->dest.reg == state->drap_reg) {
  964. /* drap: pop %drap */
  965. cfa->base = state->drap_reg;
  966. cfa->offset = 0;
  967. }
  968. restore_reg(state, op->dest.reg);
  969. }
  970. state->stack_size -= 8;
  971. if (cfa->base == CFI_SP)
  972. cfa->offset -= 8;
  973. break;
  974. case OP_SRC_REG_INDIRECT:
  975. if (state->drap && op->src.reg == CFI_BP &&
  976. op->src.offset == regs[op->dest.reg].offset) {
  977. /* drap: mov disp(%rbp), %reg */
  978. if (op->dest.reg == state->drap_reg) {
  979. cfa->base = state->drap_reg;
  980. cfa->offset = 0;
  981. }
  982. restore_reg(state, op->dest.reg);
  983. } else if (op->src.reg == cfa->base &&
  984. op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
  985. /* mov disp(%rbp), %reg */
  986. /* mov disp(%rsp), %reg */
  987. restore_reg(state, op->dest.reg);
  988. }
  989. break;
  990. default:
  991. WARN_FUNC("unknown stack-related instruction",
  992. insn->sec, insn->offset);
  993. return -1;
  994. }
  995. break;
  996. case OP_DEST_PUSH:
  997. state->stack_size += 8;
  998. if (cfa->base == CFI_SP)
  999. cfa->offset += 8;
  1000. if (op->src.type != OP_SRC_REG)
  1001. break;
  1002. if (state->drap) {
  1003. if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
  1004. /* drap: push %drap */
  1005. cfa->base = CFI_BP_INDIRECT;
  1006. cfa->offset = -state->stack_size;
  1007. /* save drap so we know when to undefine it */
  1008. save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
  1009. } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
  1010. /* drap: push %rbp */
  1011. state->stack_size = 0;
  1012. } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
  1013. /* drap: push %reg */
  1014. save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
  1015. }
  1016. } else {
  1017. /* push %reg */
  1018. save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
  1019. }
  1020. /* detect when asm code uses rbp as a scratch register */
  1021. if (!nofp && insn->func && op->src.reg == CFI_BP &&
  1022. cfa->base != CFI_BP)
  1023. state->bp_scratch = true;
  1024. break;
  1025. case OP_DEST_REG_INDIRECT:
  1026. if (state->drap) {
  1027. if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
  1028. /* drap: mov %drap, disp(%rbp) */
  1029. cfa->base = CFI_BP_INDIRECT;
  1030. cfa->offset = op->dest.offset;
  1031. /* save drap so we know when to undefine it */
  1032. save_reg(state, op->src.reg, CFI_CFA, op->dest.offset);
  1033. }
  1034. else if (regs[op->src.reg].base == CFI_UNDEFINED) {
  1035. /* drap: mov reg, disp(%rbp) */
  1036. save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
  1037. }
  1038. } else if (op->dest.reg == cfa->base) {
  1039. /* mov reg, disp(%rbp) */
  1040. /* mov reg, disp(%rsp) */
  1041. save_reg(state, op->src.reg, CFI_CFA,
  1042. op->dest.offset - state->cfa.offset);
  1043. }
  1044. break;
  1045. case OP_DEST_LEAVE:
  1046. if ((!state->drap && cfa->base != CFI_BP) ||
  1047. (state->drap && cfa->base != state->drap_reg)) {
  1048. WARN_FUNC("leave instruction with modified stack frame",
  1049. insn->sec, insn->offset);
  1050. return -1;
  1051. }
  1052. /* leave (mov %rbp, %rsp; pop %rbp) */
  1053. state->stack_size = -state->regs[CFI_BP].offset - 8;
  1054. restore_reg(state, CFI_BP);
  1055. if (!state->drap) {
  1056. cfa->base = CFI_SP;
  1057. cfa->offset -= 8;
  1058. }
  1059. break;
  1060. case OP_DEST_MEM:
  1061. if (op->src.type != OP_SRC_POP) {
  1062. WARN_FUNC("unknown stack-related memory operation",
  1063. insn->sec, insn->offset);
  1064. return -1;
  1065. }
  1066. /* pop mem */
  1067. state->stack_size -= 8;
  1068. if (cfa->base == CFI_SP)
  1069. cfa->offset -= 8;
  1070. break;
  1071. default:
  1072. WARN_FUNC("unknown stack-related instruction",
  1073. insn->sec, insn->offset);
  1074. return -1;
  1075. }
  1076. return 0;
  1077. }
  1078. static bool insn_state_match(struct instruction *insn, struct insn_state *state)
  1079. {
  1080. struct insn_state *state1 = &insn->state, *state2 = state;
  1081. int i;
  1082. if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
  1083. WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
  1084. insn->sec, insn->offset,
  1085. state1->cfa.base, state1->cfa.offset,
  1086. state2->cfa.base, state2->cfa.offset);
  1087. } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
  1088. for (i = 0; i < CFI_NUM_REGS; i++) {
  1089. if (!memcmp(&state1->regs[i], &state2->regs[i],
  1090. sizeof(struct cfi_reg)))
  1091. continue;
  1092. WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
  1093. insn->sec, insn->offset,
  1094. i, state1->regs[i].base, state1->regs[i].offset,
  1095. i, state2->regs[i].base, state2->regs[i].offset);
  1096. break;
  1097. }
  1098. } else if (state1->drap != state2->drap ||
  1099. (state1->drap && state1->drap_reg != state2->drap_reg)) {
  1100. WARN_FUNC("stack state mismatch: drap1=%d(%d) drap2=%d(%d)",
  1101. insn->sec, insn->offset,
  1102. state1->drap, state1->drap_reg,
  1103. state2->drap, state2->drap_reg);
  1104. } else
  1105. return true;
  1106. return false;
  1107. }
  1108. /*
  1109. * Follow the branch starting at the given instruction, and recursively follow
  1110. * any other branches (jumps). Meanwhile, track the frame pointer state at
  1111. * each instruction and validate all the rules described in
  1112. * tools/objtool/Documentation/stack-validation.txt.
  1113. */
  1114. static int validate_branch(struct objtool_file *file, struct instruction *first,
  1115. struct insn_state state)
  1116. {
  1117. struct alternative *alt;
  1118. struct instruction *insn;
  1119. struct section *sec;
  1120. struct symbol *func = NULL;
  1121. int ret;
  1122. insn = first;
  1123. sec = insn->sec;
  1124. if (insn->alt_group && list_empty(&insn->alts)) {
  1125. WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
  1126. sec, insn->offset);
  1127. return -1;
  1128. }
  1129. while (1) {
  1130. if (file->c_file && insn->func) {
  1131. if (func && func != insn->func) {
  1132. WARN("%s() falls through to next function %s()",
  1133. func->name, insn->func->name);
  1134. return 1;
  1135. }
  1136. }
  1137. func = insn->func;
  1138. if (func && insn->ignore) {
  1139. WARN_FUNC("BUG: why am I validating an ignored function?",
  1140. sec, insn->offset);
  1141. return -1;
  1142. }
  1143. if (insn->visited) {
  1144. if (!!insn_state_match(insn, &state))
  1145. return 1;
  1146. return 0;
  1147. }
  1148. insn->state = state;
  1149. insn->visited = true;
  1150. list_for_each_entry(alt, &insn->alts, list) {
  1151. ret = validate_branch(file, alt->insn, state);
  1152. if (ret)
  1153. return 1;
  1154. }
  1155. switch (insn->type) {
  1156. case INSN_RETURN:
  1157. if (func && has_modified_stack_frame(&state)) {
  1158. WARN_FUNC("return with modified stack frame",
  1159. sec, insn->offset);
  1160. return 1;
  1161. }
  1162. if (state.bp_scratch) {
  1163. WARN("%s uses BP as a scratch register",
  1164. insn->func->name);
  1165. return 1;
  1166. }
  1167. return 0;
  1168. case INSN_CALL:
  1169. if (is_fentry_call(insn))
  1170. break;
  1171. ret = dead_end_function(file, insn->call_dest);
  1172. if (ret == 1)
  1173. return 0;
  1174. if (ret == -1)
  1175. return 1;
  1176. /* fallthrough */
  1177. case INSN_CALL_DYNAMIC:
  1178. if (!nofp && func && !has_valid_stack_frame(&state)) {
  1179. WARN_FUNC("call without frame pointer save/setup",
  1180. sec, insn->offset);
  1181. return 1;
  1182. }
  1183. break;
  1184. case INSN_JUMP_CONDITIONAL:
  1185. case INSN_JUMP_UNCONDITIONAL:
  1186. if (insn->jump_dest &&
  1187. (!func || !insn->jump_dest->func ||
  1188. func == insn->jump_dest->func)) {
  1189. ret = validate_branch(file, insn->jump_dest,
  1190. state);
  1191. if (ret)
  1192. return 1;
  1193. } else if (func && has_modified_stack_frame(&state)) {
  1194. WARN_FUNC("sibling call from callable instruction with modified stack frame",
  1195. sec, insn->offset);
  1196. return 1;
  1197. }
  1198. if (insn->type == INSN_JUMP_UNCONDITIONAL)
  1199. return 0;
  1200. break;
  1201. case INSN_JUMP_DYNAMIC:
  1202. if (func && list_empty(&insn->alts) &&
  1203. has_modified_stack_frame(&state)) {
  1204. WARN_FUNC("sibling call from callable instruction with modified stack frame",
  1205. sec, insn->offset);
  1206. return 1;
  1207. }
  1208. return 0;
  1209. case INSN_STACK:
  1210. if (update_insn_state(insn, &state))
  1211. return -1;
  1212. break;
  1213. default:
  1214. break;
  1215. }
  1216. if (insn->dead_end)
  1217. return 0;
  1218. insn = next_insn_same_sec(file, insn);
  1219. if (!insn) {
  1220. WARN("%s: unexpected end of section", sec->name);
  1221. return 1;
  1222. }
  1223. }
  1224. return 0;
  1225. }
  1226. static bool is_kasan_insn(struct instruction *insn)
  1227. {
  1228. return (insn->type == INSN_CALL &&
  1229. !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
  1230. }
  1231. static bool is_ubsan_insn(struct instruction *insn)
  1232. {
  1233. return (insn->type == INSN_CALL &&
  1234. !strcmp(insn->call_dest->name,
  1235. "__ubsan_handle_builtin_unreachable"));
  1236. }
  1237. static bool ignore_unreachable_insn(struct instruction *insn)
  1238. {
  1239. int i;
  1240. if (insn->ignore || insn->type == INSN_NOP)
  1241. return true;
  1242. /*
  1243. * Ignore any unused exceptions. This can happen when a whitelisted
  1244. * function has an exception table entry.
  1245. */
  1246. if (!strcmp(insn->sec->name, ".fixup"))
  1247. return true;
  1248. /*
  1249. * Check if this (or a subsequent) instruction is related to
  1250. * CONFIG_UBSAN or CONFIG_KASAN.
  1251. *
  1252. * End the search at 5 instructions to avoid going into the weeds.
  1253. */
  1254. if (!insn->func)
  1255. return false;
  1256. for (i = 0; i < 5; i++) {
  1257. if (is_kasan_insn(insn) || is_ubsan_insn(insn))
  1258. return true;
  1259. if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest) {
  1260. insn = insn->jump_dest;
  1261. continue;
  1262. }
  1263. if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
  1264. break;
  1265. insn = list_next_entry(insn, list);
  1266. }
  1267. return false;
  1268. }
  1269. static int validate_functions(struct objtool_file *file)
  1270. {
  1271. struct section *sec;
  1272. struct symbol *func;
  1273. struct instruction *insn;
  1274. struct insn_state state;
  1275. int ret, warnings = 0;
  1276. clear_insn_state(&state);
  1277. state.cfa = initial_func_cfi.cfa;
  1278. memcpy(&state.regs, &initial_func_cfi.regs,
  1279. CFI_NUM_REGS * sizeof(struct cfi_reg));
  1280. state.stack_size = initial_func_cfi.cfa.offset;
  1281. for_each_sec(file, sec) {
  1282. list_for_each_entry(func, &sec->symbol_list, list) {
  1283. if (func->type != STT_FUNC)
  1284. continue;
  1285. insn = find_insn(file, sec, func->offset);
  1286. if (!insn || insn->ignore)
  1287. continue;
  1288. ret = validate_branch(file, insn, state);
  1289. warnings += ret;
  1290. }
  1291. }
  1292. return warnings;
  1293. }
  1294. static int validate_reachable_instructions(struct objtool_file *file)
  1295. {
  1296. struct instruction *insn;
  1297. if (file->ignore_unreachables)
  1298. return 0;
  1299. for_each_insn(file, insn) {
  1300. if (insn->visited || ignore_unreachable_insn(insn))
  1301. continue;
  1302. /*
  1303. * gcov produces a lot of unreachable instructions. If we get
  1304. * an unreachable warning and the file has gcov enabled, just
  1305. * ignore it, and all other such warnings for the file. Do
  1306. * this here because this is an expensive function.
  1307. */
  1308. if (gcov_enabled(file))
  1309. return 0;
  1310. WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
  1311. return 1;
  1312. }
  1313. return 0;
  1314. }
  1315. static void cleanup(struct objtool_file *file)
  1316. {
  1317. struct instruction *insn, *tmpinsn;
  1318. struct alternative *alt, *tmpalt;
  1319. list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
  1320. list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
  1321. list_del(&alt->list);
  1322. free(alt);
  1323. }
  1324. list_del(&insn->list);
  1325. hash_del(&insn->hash);
  1326. free(insn);
  1327. }
  1328. elf_close(file->elf);
  1329. }
  1330. int check(const char *_objname, bool _nofp)
  1331. {
  1332. struct objtool_file file;
  1333. int ret, warnings = 0;
  1334. objname = _objname;
  1335. nofp = _nofp;
  1336. file.elf = elf_open(objname);
  1337. if (!file.elf)
  1338. return 1;
  1339. INIT_LIST_HEAD(&file.insn_list);
  1340. hash_init(file.insn_hash);
  1341. file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
  1342. file.rodata = find_section_by_name(file.elf, ".rodata");
  1343. file.ignore_unreachables = false;
  1344. file.c_file = find_section_by_name(file.elf, ".comment");
  1345. arch_initial_func_cfi_state(&initial_func_cfi);
  1346. ret = decode_sections(&file);
  1347. if (ret < 0)
  1348. goto out;
  1349. warnings += ret;
  1350. if (list_empty(&file.insn_list))
  1351. goto out;
  1352. ret = validate_functions(&file);
  1353. if (ret < 0)
  1354. goto out;
  1355. warnings += ret;
  1356. if (!warnings) {
  1357. ret = validate_reachable_instructions(&file);
  1358. if (ret < 0)
  1359. goto out;
  1360. warnings += ret;
  1361. }
  1362. out:
  1363. cleanup(&file);
  1364. /* ignore warnings for now until we get all the code cleaned up */
  1365. if (ret || warnings)
  1366. return 0;
  1367. return 0;
  1368. }