check.c 54 KB

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