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