check.c 51 KB

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