check.c 53 KB

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