check.c 52 KB

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