check.c 52 KB

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