check.c 53 KB

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