check.c 54 KB

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