check.c 53 KB

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