check.c 45 KB

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