check.c 53 KB

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